github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/common/iso_config_test.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "reflect" 10 "runtime" 11 "testing" 12 ) 13 14 func testISOConfig() ISOConfig { 15 return ISOConfig{ 16 ISOChecksum: "foo", 17 ISOChecksumURL: "", 18 ISOChecksumType: "md5", 19 RawSingleISOUrl: "http://www.packer.io/the-OS.iso", 20 } 21 } 22 23 var cs_bsd_style = ` 24 MD5 (other.iso) = bAr 25 MD5 (the-OS.iso) = baZ 26 ` 27 28 var cs_bsd_style_subdir = ` 29 MD5 (other.iso) = bAr 30 MD5 (./subdir/the-OS.iso) = baZ 31 ` 32 33 var cs_gnu_style = ` 34 bAr0 *the-OS.iso 35 baZ0 other.iso 36 ` 37 38 var cs_gnu_style_subdir = ` 39 bAr0 *./subdir/the-OS.iso 40 baZ0 other.iso 41 ` 42 43 var cs_bsd_style_no_newline = ` 44 MD5 (other.iso) = bAr 45 MD5 (the-OS.iso) = baZ` 46 47 var cs_gnu_style_no_newline = ` 48 bAr0 *the-OS.iso 49 baZ0 other.iso` 50 51 func TestISOConfigPrepare_ISOChecksum(t *testing.T) { 52 i := testISOConfig() 53 54 // Test bad 55 i.ISOChecksum = "" 56 warns, err := i.Prepare(nil) 57 if len(warns) > 0 { 58 t.Fatalf("bad: %#v", warns) 59 } 60 if err == nil { 61 t.Fatal("should have error") 62 } 63 64 // Test good 65 i = testISOConfig() 66 i.ISOChecksum = "FOo" 67 warns, err = i.Prepare(nil) 68 if len(warns) > 0 { 69 t.Fatalf("bad: %#v", warns) 70 } 71 if err != nil { 72 t.Fatalf("should not have error: %s", err) 73 } 74 75 if i.ISOChecksum != "foo" { 76 t.Fatalf("should've lowercased: %s", i.ISOChecksum) 77 } 78 } 79 80 func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { 81 i := testISOConfig() 82 i.ISOChecksumURL = "file:///not_read" 83 84 // Test ISOChecksum overrides url 85 warns, err := i.Prepare(nil) 86 if len(warns) > 0 && len(err) > 0 { 87 t.Fatalf("bad: %#v, %#v", warns, err) 88 } 89 90 // Test good - ISOChecksumURL BSD style 91 i = testISOConfig() 92 i.ISOChecksum = "" 93 cs_file, _ := ioutil.TempFile("", "packer-test-") 94 defer os.Remove(cs_file.Name()) 95 defer cs_file.Close() 96 ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666) 97 filePrefix := "file://" 98 if runtime.GOOS == "windows" { 99 filePrefix += "/" 100 } 101 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 102 warns, err = i.Prepare(nil) 103 if len(warns) > 0 { 104 t.Fatalf("bad: %#v", warns) 105 } 106 if err != nil { 107 t.Fatalf("should not have error: %s", err) 108 } 109 110 if i.ISOChecksum != "baz" { 111 t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) 112 } 113 114 // Test good - ISOChecksumURL GNU style 115 i = testISOConfig() 116 i.ISOChecksum = "" 117 cs_file, _ = ioutil.TempFile("", "packer-test-") 118 defer os.Remove(cs_file.Name()) 119 defer cs_file.Close() 120 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) 121 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 122 warns, err = i.Prepare(nil) 123 if len(warns) > 0 { 124 t.Fatalf("bad: %#v", warns) 125 } 126 if err != nil { 127 t.Fatalf("should not have error: %s", err) 128 } 129 130 if i.ISOChecksum != "bar0" { 131 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 132 } 133 134 // Test good - ISOChecksumURL BSD style no newline 135 i = testISOConfig() 136 i.ISOChecksum = "" 137 cs_file, _ = ioutil.TempFile("", "packer-test-") 138 defer os.Remove(cs_file.Name()) 139 defer cs_file.Close() 140 ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666) 141 i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) 142 warns, err = i.Prepare(nil) 143 if len(warns) > 0 { 144 t.Fatalf("bad: %#v", warns) 145 } 146 if err != nil { 147 t.Fatalf("should not have error: %s", err) 148 } 149 150 if i.ISOChecksum != "baz" { 151 t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) 152 } 153 154 // Test good - ISOChecksumURL BSD style with relative path 155 i = testISOConfig() 156 i.ISOChecksum = "" 157 158 cs_dir, _ := ioutil.TempDir("", "packer-testdir-") 159 cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-") 160 defer os.RemoveAll(cs_dir) // Removes the file as well 161 defer cs_file.Close() 162 ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0666) 163 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 164 i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") 165 warns, err = i.Prepare(nil) 166 if len(warns) > 0 { 167 t.Fatalf("bad: %#v", warns) 168 } 169 if err != nil { 170 t.Fatalf("should not have error: %s", err) 171 } 172 173 if i.ISOChecksum != "baz" { 174 t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) 175 } 176 177 // Test good - ISOChecksumURL GNU style no newline 178 i = testISOConfig() 179 i.ISOChecksum = "" 180 cs_file, _ = ioutil.TempFile("", "packer-test-") 181 defer os.Remove(cs_file.Name()) 182 defer cs_file.Close() 183 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666) 184 i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) 185 warns, err = i.Prepare(nil) 186 if len(warns) > 0 { 187 t.Fatalf("bad: %#v", warns) 188 } 189 if err != nil { 190 t.Fatalf("should not have error: %s", err) 191 } 192 193 if i.ISOChecksum != "bar0" { 194 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 195 } 196 197 // Test good - ISOChecksumURL GNU style with query parameters 198 i = testISOConfig() 199 i.ISOChecksum = "" 200 i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo" 201 202 cs_file, _ = ioutil.TempFile("", "packer-test-") 203 defer os.Remove(cs_file.Name()) 204 defer cs_file.Close() 205 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) 206 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 207 warns, err = i.Prepare(nil) 208 if len(warns) > 0 { 209 t.Fatalf("bad: %#v", warns) 210 } 211 if err != nil { 212 t.Fatalf("should not have error: %s", err) 213 } 214 215 if i.ISOChecksum != "bar0" { 216 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 217 } 218 219 // Test good - ISOChecksumURL GNU style with relative path 220 i = testISOConfig() 221 i.ISOChecksum = "" 222 223 cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-") 224 defer os.Remove(cs_file.Name()) 225 defer cs_file.Close() 226 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0666) 227 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 228 i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso") 229 warns, err = i.Prepare(nil) 230 if len(warns) > 0 { 231 t.Fatalf("bad: %#v", warns) 232 } 233 if err != nil { 234 t.Fatalf("should not have error: %s", err) 235 } 236 237 if i.ISOChecksum != "bar0" { 238 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 239 } 240 } 241 242 func TestISOConfigPrepare_ISOChecksumType(t *testing.T) { 243 i := testISOConfig() 244 245 // Test bad 246 i.ISOChecksumType = "" 247 warns, err := i.Prepare(nil) 248 if len(warns) > 0 { 249 t.Fatalf("bad: %#v", warns) 250 } 251 if err == nil { 252 t.Fatal("should have error") 253 } 254 255 // Test good 256 i = testISOConfig() 257 i.ISOChecksumType = "mD5" 258 warns, err = i.Prepare(nil) 259 if len(warns) > 0 { 260 t.Fatalf("bad: %#v", warns) 261 } 262 if err != nil { 263 t.Fatalf("should not have error: %s", err) 264 } 265 266 if i.ISOChecksumType != "md5" { 267 t.Fatalf("should've lowercased: %s", i.ISOChecksumType) 268 } 269 270 // Test unknown 271 i = testISOConfig() 272 i.ISOChecksumType = "fake" 273 warns, err = i.Prepare(nil) 274 if len(warns) > 0 { 275 t.Fatalf("bad: %#v", warns) 276 } 277 if err == nil { 278 t.Fatal("should have error") 279 } 280 281 // Test none 282 i = testISOConfig() 283 i.ISOChecksumType = "none" 284 warns, err = i.Prepare(nil) 285 if len(warns) == 0 { 286 t.Fatalf("bad: %#v", warns) 287 } 288 if err != nil { 289 t.Fatalf("should not have error: %s", err) 290 } 291 292 if i.ISOChecksumType != "none" { 293 t.Fatalf("should've lowercased: %s", i.ISOChecksumType) 294 } 295 } 296 297 func TestISOConfigPrepare_ISOUrl(t *testing.T) { 298 i := testISOConfig() 299 300 // Test both empty 301 i.RawSingleISOUrl = "" 302 i.ISOUrls = []string{} 303 warns, err := i.Prepare(nil) 304 if len(warns) > 0 { 305 t.Fatalf("bad: %#v", warns) 306 } 307 if err == nil { 308 t.Fatal("should have error") 309 } 310 311 // Test iso_url not set but checksum url is 312 ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root"))) 313 defer ts.Close() 314 i = testISOConfig() 315 i.RawSingleISOUrl = "" 316 i.ISOChecksum = "" 317 i.ISOChecksumURL = ts.URL + "/basic.txt" 318 warns, err = i.Prepare(nil) 319 320 // Test iso_url set 321 i = testISOConfig() 322 i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso" 323 warns, err = i.Prepare(nil) 324 if len(warns) > 0 { 325 t.Fatalf("bad: %#v", warns) 326 } 327 if err != nil { 328 t.Errorf("should not have error: %s", err) 329 } 330 331 expected := []string{"http://www.packer.io/the-OS.iso"} 332 if !reflect.DeepEqual(i.ISOUrls, expected) { 333 t.Fatalf("bad: %#v", i.ISOUrls) 334 } 335 336 // Test both set 337 i = testISOConfig() 338 i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso" 339 i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"} 340 warns, err = i.Prepare(nil) 341 if len(warns) > 0 { 342 t.Fatalf("bad: %#v", warns) 343 } 344 if err == nil { 345 t.Fatal("should have error") 346 } 347 348 // Test just iso_urls set 349 i = testISOConfig() 350 i.RawSingleISOUrl = "" 351 i.ISOUrls = []string{ 352 "http://www.packer.io/the-OS.iso", 353 "http://www.hashicorp.com/the-OS.iso", 354 } 355 356 warns, err = i.Prepare(nil) 357 if len(warns) > 0 { 358 t.Fatalf("bad: %#v", warns) 359 } 360 if err != nil { 361 t.Errorf("should not have error: %s", err) 362 } 363 364 expected = []string{ 365 "http://www.packer.io/the-OS.iso", 366 "http://www.hashicorp.com/the-OS.iso", 367 } 368 if !reflect.DeepEqual(i.ISOUrls, expected) { 369 t.Fatalf("bad: %#v", i.ISOUrls) 370 } 371 } 372 373 func TestISOConfigPrepare_TargetExtension(t *testing.T) { 374 i := testISOConfig() 375 376 // Test the default value 377 warns, err := i.Prepare(nil) 378 if len(warns) > 0 { 379 t.Fatalf("bad: %#v", warns) 380 } 381 if err != nil { 382 t.Fatalf("should not have error: %s", err) 383 } 384 385 if i.TargetExtension != "iso" { 386 t.Fatalf("should've found \"iso\" got: %s", i.TargetExtension) 387 } 388 389 // Test the lowercased value 390 i = testISOConfig() 391 i.TargetExtension = "DMG" 392 warns, err = i.Prepare(nil) 393 if len(warns) > 0 { 394 t.Fatalf("bad: %#v", warns) 395 } 396 if err != nil { 397 t.Fatalf("should not have error: %s", err) 398 } 399 400 if i.TargetExtension != "dmg" { 401 t.Fatalf("should've lowercased: %s", i.TargetExtension) 402 } 403 }