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