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