github.com/replicatedcom/ship@v0.50.0/integration/lib_test.go (about) 1 package integration 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func Test_replaceInJSON(t *testing.T) { 11 tests := []struct { 12 name string 13 path string 14 obj map[string]interface{} 15 want map[string]interface{} 16 }{ 17 { 18 name: "noop", 19 path: "", 20 obj: map[string]interface{}{ 21 "a": "b", 22 "abc": map[string]interface{}{ 23 "bcd": "efg", 24 }, 25 }, 26 want: map[string]interface{}{ 27 "a": "b", 28 "abc": map[string]interface{}{ 29 "bcd": "efg", 30 }, 31 }, 32 }, 33 { 34 name: "remove top level", 35 path: "a", 36 obj: map[string]interface{}{ 37 "a": "b", 38 "abc": map[string]interface{}{ 39 "bcd": "efg", 40 }, 41 }, 42 want: map[string]interface{}{ 43 "abc": map[string]interface{}{ 44 "bcd": "efg", 45 }, 46 }, 47 }, 48 { 49 name: "remove nonexistent", 50 path: "b", 51 obj: map[string]interface{}{ 52 "a": "b", 53 "abc": map[string]interface{}{ 54 "bcd": "efg", 55 }, 56 }, 57 want: map[string]interface{}{ 58 "a": "b", 59 "abc": map[string]interface{}{ 60 "bcd": "efg", 61 }, 62 }, 63 }, 64 { 65 name: "remove nested nonexistent", 66 path: "b.c", 67 obj: map[string]interface{}{ 68 "a": "b", 69 "abc": map[string]interface{}{ 70 "bcd": "efg", 71 }, 72 }, 73 want: map[string]interface{}{ 74 "a": "b", 75 "abc": map[string]interface{}{ 76 "bcd": "efg", 77 }, 78 }, 79 }, 80 { 81 name: "remove second level obj", 82 path: "abc.bcd", 83 obj: map[string]interface{}{ 84 "a": "b", 85 "abc": map[string]interface{}{ 86 "bcd": "efg", 87 "hij": "klm", 88 }, 89 }, 90 want: map[string]interface{}{ 91 "a": "b", 92 "abc": map[string]interface{}{ 93 "hij": "klm", 94 }, 95 }, 96 }, 97 { 98 name: "remove last element of obj", 99 path: "abc.bcd", 100 obj: map[string]interface{}{ 101 "a": "b", 102 "abc": map[string]interface{}{ 103 "bcd": "efg", 104 }, 105 }, 106 want: map[string]interface{}{ 107 "a": "b", 108 }, 109 }, 110 { 111 name: "remove non map element", 112 path: "abc.bcd", 113 obj: map[string]interface{}{ 114 "a": "b", 115 "abc": map[string]interface{}{ 116 "bcd": 57, 117 }, 118 }, 119 want: map[string]interface{}{ 120 "a": "b", 121 }, 122 }, 123 { 124 name: "remove child of non map element", 125 path: "abc.bcd.def", 126 obj: map[string]interface{}{ 127 "a": "b", 128 "abc": map[string]interface{}{ 129 "bcd": 57, 130 "hij": "klm", 131 }, 132 }, 133 want: map[string]interface{}{ 134 "a": "b", 135 "abc": map[string]interface{}{ 136 "hij": "klm", 137 }, 138 }, 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 req := require.New(t) 144 145 got := replaceInJSON(tt.obj, tt.path) 146 147 req.Equal(tt.want, got) 148 }) 149 } 150 } 151 152 func Test_prettyAndCleanJSON(t *testing.T) { 153 tests := []struct { 154 name string 155 data interface{} 156 keysToIgnore []string 157 want interface{} 158 }{ 159 { 160 name: "basic", 161 data: map[string]interface{}{ 162 "a": "b", 163 "abc": map[string]interface{}{ 164 "hij": "klm", 165 }, 166 }, 167 keysToIgnore: []string{}, 168 want: map[string]interface{}{ 169 "a": "b", 170 "abc": map[string]interface{}{ 171 "hij": "klm", 172 }, 173 }, 174 }, 175 { 176 name: "remove two keys", 177 data: map[string]interface{}{ 178 "a": "b", 179 "c": "d", 180 "abc": map[string]interface{}{ 181 "hij": "klm", 182 }, 183 }, 184 keysToIgnore: []string{"c", "abc.d"}, 185 want: map[string]interface{}{ 186 "a": "b", 187 "abc": map[string]interface{}{ 188 "hij": "klm", 189 }, 190 }, 191 }, 192 } 193 for _, tt := range tests { 194 t.Run(tt.name, func(t *testing.T) { 195 req := require.New(t) 196 dataBytes, err := json.Marshal(tt.data) 197 req.NoError(err) 198 199 got, err := prettyAndCleanJSON(dataBytes, tt.keysToIgnore) 200 req.NoError(err) 201 202 var outData interface{} 203 err = json.Unmarshal(got, &outData) 204 req.NoError(err) 205 206 req.Equal(tt.want, outData) 207 }) 208 } 209 }