github.com/someshkoli/terratest@v0.41.1/modules/k8s/jsonpath_test.go (about) 1 package k8s 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestUnmarshalJSONPath(t *testing.T) { 12 t.Parallel() 13 14 testCases := []struct { 15 name string 16 jsonBlob string 17 jsonPath string 18 expectedOut interface{} 19 }{ 20 { 21 "boolField", 22 `{"key": true}`, 23 "{ .key }", 24 []bool{true}, 25 }, 26 { 27 "nestedObject", 28 `{"key": {"data": [1,2,3]}}`, 29 "{ .key }", 30 []map[string][]int{ 31 map[string][]int{ 32 "data": []int{1, 2, 3}, 33 }, 34 }, 35 }, 36 { 37 "nestedArray", 38 `{"key": {"data": [1,2,3]}}`, 39 "{ .key.data[*] }", 40 []int{1, 2, 3}, 41 }, 42 } 43 44 for _, testCase := range testCases { 45 // capture range variable so that it doesn't update when the subtest goroutine swaps. 46 testCase := testCase 47 t.Run(testCase.name, func(t *testing.T) { 48 t.Parallel() 49 var output interface{} 50 UnmarshalJSONPath(t, []byte(testCase.jsonBlob), testCase.jsonPath, &output) 51 // NOTE: we have to do equality check on the marshalled json data to allow equality checks over dynamic 52 // types in this table driven test. 53 expectedOutJSON, err := json.Marshal(testCase.expectedOut) 54 require.NoError(t, err) 55 actualOutJSON, err := json.Marshal(output) 56 require.NoError(t, err) 57 assert.Equal(t, string(expectedOutJSON), string(actualOutJSON)) 58 }) 59 } 60 }