github.com/aclaygray/packer@v1.3.2/template/parse_test.go (about) 1 // +build !windows 2 3 package template 4 5 import ( 6 "path/filepath" 7 "reflect" 8 "strings" 9 "testing" 10 "time" 11 ) 12 13 func TestParse(t *testing.T) { 14 cases := []struct { 15 File string 16 Result *Template 17 Err bool 18 }{ 19 /* 20 * Builders 21 */ 22 { 23 "parse-basic.json", 24 &Template{ 25 Builders: map[string]*Builder{ 26 "something": { 27 Name: "something", 28 Type: "something", 29 }, 30 }, 31 }, 32 false, 33 }, 34 { 35 "parse-builder-no-type.json", 36 nil, 37 true, 38 }, 39 { 40 "parse-builder-repeat.json", 41 nil, 42 true, 43 }, 44 45 /* 46 * Provisioners 47 */ 48 { 49 "parse-provisioner-basic.json", 50 &Template{ 51 Provisioners: []*Provisioner{ 52 { 53 Type: "something", 54 }, 55 }, 56 }, 57 false, 58 }, 59 60 { 61 "parse-provisioner-pause-before.json", 62 &Template{ 63 Provisioners: []*Provisioner{ 64 { 65 Type: "something", 66 PauseBefore: 1 * time.Second, 67 }, 68 }, 69 }, 70 false, 71 }, 72 73 { 74 "parse-provisioner-only.json", 75 &Template{ 76 Provisioners: []*Provisioner{ 77 { 78 Type: "something", 79 OnlyExcept: OnlyExcept{ 80 Only: []string{"foo"}, 81 }, 82 }, 83 }, 84 }, 85 false, 86 }, 87 88 { 89 "parse-provisioner-except.json", 90 &Template{ 91 Provisioners: []*Provisioner{ 92 { 93 Type: "something", 94 OnlyExcept: OnlyExcept{ 95 Except: []string{"foo"}, 96 }, 97 }, 98 }, 99 }, 100 false, 101 }, 102 103 { 104 "parse-provisioner-override.json", 105 &Template{ 106 Provisioners: []*Provisioner{ 107 { 108 Type: "something", 109 Override: map[string]interface{}{ 110 "foo": map[string]interface{}{}, 111 }, 112 }, 113 }, 114 }, 115 false, 116 }, 117 118 { 119 "parse-provisioner-no-type.json", 120 nil, 121 true, 122 }, 123 124 { 125 "parse-variable-default.json", 126 &Template{ 127 Variables: map[string]*Variable{ 128 "foo": { 129 Default: "foo", 130 }, 131 }, 132 }, 133 false, 134 }, 135 136 { 137 "parse-variable-required.json", 138 &Template{ 139 Variables: map[string]*Variable{ 140 "foo": { 141 Required: true, 142 }, 143 }, 144 }, 145 false, 146 }, 147 148 { 149 "parse-pp-basic.json", 150 &Template{ 151 PostProcessors: [][]*PostProcessor{ 152 { 153 { 154 Type: "foo", 155 Config: map[string]interface{}{ 156 "foo": "bar", 157 }, 158 }, 159 }, 160 }, 161 }, 162 false, 163 }, 164 165 { 166 "parse-pp-keep.json", 167 &Template{ 168 PostProcessors: [][]*PostProcessor{ 169 { 170 { 171 Type: "foo", 172 KeepInputArtifact: true, 173 }, 174 }, 175 }, 176 }, 177 false, 178 }, 179 180 { 181 "parse-pp-only.json", 182 &Template{ 183 PostProcessors: [][]*PostProcessor{ 184 { 185 { 186 Type: "foo", 187 OnlyExcept: OnlyExcept{ 188 Only: []string{"bar"}, 189 }, 190 }, 191 }, 192 }, 193 }, 194 false, 195 }, 196 197 { 198 "parse-pp-except.json", 199 &Template{ 200 PostProcessors: [][]*PostProcessor{ 201 { 202 { 203 Type: "foo", 204 OnlyExcept: OnlyExcept{ 205 Except: []string{"bar"}, 206 }, 207 }, 208 }, 209 }, 210 }, 211 false, 212 }, 213 214 { 215 "parse-pp-string.json", 216 &Template{ 217 PostProcessors: [][]*PostProcessor{ 218 { 219 { 220 Type: "foo", 221 }, 222 }, 223 }, 224 }, 225 false, 226 }, 227 228 { 229 "parse-pp-map.json", 230 &Template{ 231 PostProcessors: [][]*PostProcessor{ 232 { 233 { 234 Type: "foo", 235 }, 236 }, 237 }, 238 }, 239 false, 240 }, 241 242 { 243 "parse-pp-slice.json", 244 &Template{ 245 PostProcessors: [][]*PostProcessor{ 246 { 247 { 248 Type: "foo", 249 }, 250 }, 251 { 252 { 253 Type: "bar", 254 }, 255 }, 256 }, 257 }, 258 false, 259 }, 260 261 { 262 "parse-pp-multi.json", 263 &Template{ 264 PostProcessors: [][]*PostProcessor{ 265 { 266 { 267 Type: "foo", 268 }, 269 { 270 Type: "bar", 271 }, 272 }, 273 }, 274 }, 275 false, 276 }, 277 278 { 279 "parse-pp-no-type.json", 280 nil, 281 true, 282 }, 283 284 { 285 "parse-description.json", 286 &Template{ 287 Description: "foo", 288 }, 289 false, 290 }, 291 292 { 293 "parse-min-version.json", 294 &Template{ 295 MinVersion: "1.2", 296 }, 297 false, 298 }, 299 300 { 301 "parse-push.json", 302 &Template{ 303 Push: Push{ 304 Name: "foo", 305 }, 306 }, 307 false, 308 }, 309 310 { 311 "parse-comment.json", 312 &Template{ 313 Builders: map[string]*Builder{ 314 "something": { 315 Name: "something", 316 Type: "something", 317 }, 318 }, 319 }, 320 false, 321 }, 322 } 323 324 for _, tc := range cases { 325 path, _ := filepath.Abs(fixtureDir(tc.File)) 326 tpl, err := ParseFile(fixtureDir(tc.File)) 327 if (err != nil) != tc.Err { 328 t.Fatalf("err: %s", err) 329 } 330 331 if tc.Result != nil { 332 tc.Result.Path = path 333 } 334 if tpl != nil { 335 tpl.RawContents = nil 336 } 337 if !reflect.DeepEqual(tpl, tc.Result) { 338 t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result) 339 } 340 } 341 } 342 343 func TestParse_contents(t *testing.T) { 344 tpl, err := ParseFile(fixtureDir("parse-contents.json")) 345 if err != nil { 346 t.Fatalf("err: %s", err) 347 } 348 349 actual := strings.TrimSpace(string(tpl.RawContents)) 350 expected := `{"builders":[{"type":"test"}]}` 351 if actual != expected { 352 t.Fatalf("bad: %s\n\n%s", actual, expected) 353 } 354 } 355 356 func TestParse_bad(t *testing.T) { 357 cases := []struct { 358 File string 359 Expected string 360 }{ 361 {"error-beginning.json", "line 1, column 1 (offset 1)"}, 362 {"error-middle.json", "line 5, column 6 (offset 50)"}, 363 {"error-end.json", "line 1, column 30 (offset 30)"}, 364 {"malformed.json", "line 16, column 3 (offset 433)"}, 365 } 366 for _, tc := range cases { 367 _, err := ParseFile(fixtureDir(tc.File)) 368 if err == nil { 369 t.Fatalf("expected error") 370 } 371 if !strings.Contains(err.Error(), tc.Expected) { 372 t.Fatalf("file: %s\nExpected: %s\n%s\n", tc.File, tc.Expected, err.Error()) 373 } 374 } 375 }