Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions tests/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package urunce2etesting

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -250,21 +251,53 @@ func checkExpectedOut(expected string, output string, e error) error {
}

func findValOfKey(searchArea string, key string) (string, error) {
keystr := "\"" + key + "\":[^,;\\]}]*"
r, err := regexp.Compile(keystr)
if err != nil {
return "", err
var data interface{}
if err := json.Unmarshal([]byte(searchArea), &data); err != nil {
return "", fmt.Errorf("key %s not found in search area", key)
}
match := r.FindString(searchArea)
if match == "" {
val, found := searchJSONKey(data, key)
if !found {
return "", fmt.Errorf("key %s not found in search area", key)
}
return val, nil
}

keyValMatch := strings.Split(match, ":")
if len(keyValMatch) < 2 {
return "", fmt.Errorf("invalid format for key %s: %s", key, match)
func searchJSONKey(data interface{}, key string) (string, bool) {
switch v := data.(type) {
case map[string]interface{}:
if val, ok := v[key]; ok {
return jsonValToString(val), true
}
for _, child := range v {
if result, found := searchJSONKey(child, key); found {
return result, true
}
}
case []interface{}:
for _, item := range v {
if result, found := searchJSONKey(item, key); found {
return result, true
}
}
}
return "", false
}

val := strings.ReplaceAll(keyValMatch[1], "\"", "")
return strings.TrimSpace(val), nil
func jsonValToString(v interface{}) string {
switch val := v.(type) {
case string:
return val
case float64:
if val == float64(int64(val)) {
return strconv.FormatInt(int64(val), 10)
}
return strconv.FormatFloat(val, 'f', -1, 64)
case bool:
return strconv.FormatBool(val)
case nil:
return ""
default:
b, _ := json.Marshal(val)
return string(b)
}
}
73 changes: 73 additions & 0 deletions tests/e2e/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package urunce2etesting

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFindValOfKey(t *testing.T) {
tests := []struct {
name string
searchArea string
key string
wantVal string
wantErr bool
}{
{
name: "scalar string value",
searchArea: `[{"IPAddress":"172.17.0.2","Name":"test"}]`,
key: "IPAddress",
wantVal: "172.17.0.2",
},
{
name: "scalar integer value",
searchArea: `[{"Pid":12345,"Name":"test"}]`,
key: "Pid",
wantVal: "12345",
},
{
name: "value containing comma is truncated",
searchArea: `[{"Name":"test","Label":"foo,bar"}]`,
key: "Label",
wantVal: "foo,bar",
},
{
name: "nested object value",
searchArea: `[{"NetworkSettings":{"IPAddress":"172.17.0.2"}}]`,
key: "NetworkSettings",
wantVal: `{"IPAddress":"172.17.0.2"}`,
},
{
name: "key not found",
searchArea: `[{"Name":"test"}]`,
key: "Missing",
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
val, err := findValOfKey(tc.searchArea, tc.key)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.wantVal, val)
})
}
}