github.com/Equinix-Metal/virtlet@v1.5.2-0.20210807010419-342346535dc5/tests/gm/data_test.go (about) 1 /* 2 Copyright 2017 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gm 18 19 import ( 20 "io/ioutil" 21 "os" 22 "strings" 23 "testing" 24 ) 25 26 func TestFileNaming(t *testing.T) { 27 wd, err := os.Getwd() 28 if err != nil { 29 t.Fatalf("os.Getwd(): %v", err) 30 } 31 for _, tc := range []struct { 32 testName string 33 filename string 34 data interface{} 35 }{ 36 { 37 "TestSomething", 38 "TestSomething.out.json", 39 []string{"foobar"}, 40 }, 41 { 42 "TestSomething/qqq", 43 "TestSomething__qqq.out.json", 44 []string{"foobar"}, 45 }, 46 { 47 "TestSomething/it's_foobar", 48 "TestSomething__it_s_foobar.out.json", 49 []string{"foobar"}, 50 }, 51 { 52 "TestSomething", 53 "TestSomething.out.txt", 54 "foobar", 55 }, 56 { 57 "TestSomething", 58 "TestSomething.out.yaml", 59 NewYamlVerifier([]string{"foobar"}), 60 }, 61 } { 62 filename, err := GetFilenameForTest(tc.testName, tc.data) 63 if err != nil { 64 t.Errorf("GetFilenameForTest failed for %q: %v", tc.testName, err) 65 } 66 if !strings.HasPrefix(filename, wd+"/") { 67 t.Errorf("%q: the filename doesn't have workdir prefix: %q (workdir: %q)", tc.testName, filename, wd) 68 continue 69 } 70 filename = filename[len(wd)+1:] 71 if filename != tc.filename { 72 t.Errorf("bad filename: %q instead of %q", tc.testName, tc.filename) 73 } 74 } 75 } 76 77 func TestData(t *testing.T) { 78 for _, tc := range []struct { 79 name string 80 toWrite interface{} 81 toWriteRaw string 82 compareWith interface{} 83 diff bool 84 }{ 85 { 86 name: "non-existent file", 87 compareWith: map[string]interface{}{"x": 42}, 88 diff: true, 89 }, 90 { 91 name: "unchanged", 92 toWrite: map[string]interface{}{"x": 42}, 93 compareWith: map[string]interface{}{"x": 42}, 94 diff: false, 95 }, 96 { 97 name: "changed", 98 toWrite: map[string]interface{}{"x": 42}, 99 compareWith: map[string]interface{}{"x": 43}, 100 diff: true, 101 }, 102 { 103 name: "unchanged with different formatting", 104 toWriteRaw: `{ "x": 42} `, 105 compareWith: map[string]interface{}{"x": 42}, 106 diff: false, 107 }, 108 { 109 name: "malformed golden master data file", 110 toWriteRaw: `{ `, 111 compareWith: map[string]interface{}{"x": 42}, 112 diff: true, 113 }, 114 { 115 name: "text content", 116 toWrite: "abcdef", 117 compareWith: "abcdef", 118 diff: false, 119 }, 120 { 121 name: "text content with changes", 122 toWrite: "abcdef", 123 compareWith: "abcdefgh", 124 diff: true, 125 }, 126 { 127 name: "text content (raw)", 128 toWriteRaw: "abcdef", 129 compareWith: "abcdef", 130 diff: false, 131 }, 132 { 133 name: "yaml content", 134 toWrite: NewYamlVerifier(map[string]interface{}{"x": 42}), 135 compareWith: NewYamlVerifier(map[string]interface{}{"x": 42}), 136 diff: false, 137 }, 138 { 139 name: "yaml content (subst)", 140 toWrite: NewSubstVerifier( 141 NewYamlVerifier(map[string]interface{}{"ZZZ": 42}), 142 []Replacement{ 143 { 144 Old: "ZZZ", 145 New: "x", 146 }, 147 }), 148 compareWith: NewYamlVerifier(map[string]interface{}{"x": 42}), 149 diff: false, 150 }, 151 { 152 name: "yaml content with changes", 153 toWrite: NewYamlVerifier(map[string]interface{}{"x": 42}), 154 compareWith: NewYamlVerifier(map[string]interface{}{"x": 43}), 155 diff: true, 156 }, 157 { 158 name: "yaml content with changes (subst)", 159 toWrite: NewSubstVerifier( 160 NewYamlVerifier(map[string]interface{}{"ZZZ": 42}), 161 []Replacement{ 162 { 163 Old: "ZZZ", 164 New: "x", 165 }, 166 }), 167 compareWith: NewYamlVerifier(map[string]interface{}{"x": 43}), 168 diff: true, 169 }, 170 { 171 name: "yaml content (raw)", 172 toWriteRaw: "x: 42", 173 compareWith: NewYamlVerifier(map[string]interface{}{"x": 42}), 174 diff: false, 175 }, 176 { 177 name: "yaml content (raw comparison, string)", 178 toWriteRaw: "x: 42", 179 compareWith: NewYamlVerifier("x: 42"), 180 diff: false, 181 }, 182 { 183 name: "yaml content (raw comparison, string, multiple docs)", 184 toWriteRaw: "x: 42\n---\ny: 4242", 185 compareWith: NewYamlVerifier("x: 43\n---\ny: 4242"), 186 diff: true, 187 }, 188 { 189 name: "yaml content with changes (raw comparison, string)", 190 toWriteRaw: "x: 42", 191 compareWith: NewYamlVerifier("x: 43"), 192 diff: true, 193 }, 194 { 195 name: "yaml content with changes (raw comparison, string, multiple docs)", 196 toWriteRaw: "x: 42\n---\ny: 4242", 197 compareWith: NewYamlVerifier("x: 42\n---\ny: 4243"), 198 diff: true, 199 }, 200 { 201 name: "yaml content (raw comparison, []byte)", 202 toWriteRaw: "x: 42", 203 compareWith: NewYamlVerifier([]byte("x: 42")), 204 diff: false, 205 }, 206 { 207 name: "yaml content with changes (raw comparison, []byte)", 208 toWriteRaw: "x: 42", 209 compareWith: NewYamlVerifier([]byte("x: 43")), 210 diff: true, 211 }, 212 } { 213 t.Run(tc.name, func(t *testing.T) { 214 f, err := ioutil.TempFile("", "gm-test-") 215 if err != nil { 216 t.Fatalf("ioutil.TempFile(): %v", err) 217 } 218 tmpFile := f.Name() 219 defer os.Remove(tmpFile) 220 if err := f.Close(); err != nil { 221 t.Fatalf("Close(): %v", err) 222 } 223 224 if tc.toWrite != nil { 225 if err := WriteDataFile(tmpFile, tc.toWrite); err != nil { 226 t.Fatalf("WriteDataFile(): %v", err) 227 } 228 } else if tc.toWriteRaw != "" { 229 if err := ioutil.WriteFile(tmpFile, []byte(tc.toWriteRaw), 0777); err != nil { 230 t.Fatalf("error writing %q: %v", tmpFile, err) 231 } 232 } 233 234 hasDiff, err := DataFileDiffers(tmpFile, tc.compareWith) 235 switch { 236 case err != nil: 237 t.Errorf("DataFileDiffers failed: %v", err) 238 case tc.diff && !hasDiff: 239 t.Errorf("diff expected but got no diff") 240 case !tc.diff && hasDiff: 241 t.Errorf("no diff expected but got diff") 242 } 243 }) 244 245 } 246 }