github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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_gnu_style = ` 28 bAr0 *the-OS.iso 29 baZ0 other.iso 30 ` 31 32 var cs_bsd_style_no_newline = ` 33 MD5 (other.iso) = bAr 34 MD5 (the-OS.iso) = baZ` 35 36 var cs_gnu_style_no_newline = ` 37 bAr0 *the-OS.iso 38 baZ0 other.iso` 39 40 func TestISOConfigPrepare_ISOChecksum(t *testing.T) { 41 i := testISOConfig() 42 43 // Test bad 44 i.ISOChecksum = "" 45 warns, err := i.Prepare(nil) 46 if len(warns) > 0 { 47 t.Fatalf("bad: %#v", warns) 48 } 49 if err == nil { 50 t.Fatal("should have error") 51 } 52 53 // Test good 54 i = testISOConfig() 55 i.ISOChecksum = "FOo" 56 warns, err = i.Prepare(nil) 57 if len(warns) > 0 { 58 t.Fatalf("bad: %#v", warns) 59 } 60 if err != nil { 61 t.Fatalf("should not have error: %s", err) 62 } 63 64 if i.ISOChecksum != "foo" { 65 t.Fatalf("should've lowercased: %s", i.ISOChecksum) 66 } 67 } 68 69 func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { 70 i := testISOConfig() 71 i.ISOChecksumURL = "file:///not_read" 72 73 // Test ISOChecksum overrides url 74 warns, err := i.Prepare(nil) 75 if len(warns) > 0 && len(err) > 0 { 76 t.Fatalf("bad: %#v, %#v", warns, err) 77 } 78 79 // Test good - ISOChecksumURL BSD style 80 i = testISOConfig() 81 i.ISOChecksum = "" 82 cs_file, _ := ioutil.TempFile("", "packer-test-") 83 ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666) 84 filePrefix := "file://" 85 if runtime.GOOS == "windows" { 86 filePrefix += "/" 87 } 88 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 89 warns, err = i.Prepare(nil) 90 if len(warns) > 0 { 91 t.Fatalf("bad: %#v", warns) 92 } 93 if err != nil { 94 t.Fatalf("should not have error: %s", err) 95 } 96 97 if i.ISOChecksum != "baz" { 98 t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) 99 } 100 101 // Test good - ISOChecksumURL GNU style 102 i = testISOConfig() 103 i.ISOChecksum = "" 104 cs_file, _ = ioutil.TempFile("", "packer-test-") 105 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) 106 i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) 107 warns, err = i.Prepare(nil) 108 if len(warns) > 0 { 109 t.Fatalf("bad: %#v", warns) 110 } 111 if err != nil { 112 t.Fatalf("should not have error: %s", err) 113 } 114 115 if i.ISOChecksum != "bar0" { 116 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 117 } 118 119 // Test good - ISOChecksumURL BSD style no newline 120 i = testISOConfig() 121 i.ISOChecksum = "" 122 cs_file, _ = ioutil.TempFile("", "packer-test-") 123 ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666) 124 i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) 125 warns, err = i.Prepare(nil) 126 if len(warns) > 0 { 127 t.Fatalf("bad: %#v", warns) 128 } 129 if err != nil { 130 t.Fatalf("should not have error: %s", err) 131 } 132 133 if i.ISOChecksum != "baz" { 134 t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) 135 } 136 137 // Test good - ISOChecksumURL GNU style no newline 138 i = testISOConfig() 139 i.ISOChecksum = "" 140 cs_file, _ = ioutil.TempFile("", "packer-test-") 141 ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666) 142 i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) 143 warns, err = i.Prepare(nil) 144 if len(warns) > 0 { 145 t.Fatalf("bad: %#v", warns) 146 } 147 if err != nil { 148 t.Fatalf("should not have error: %s", err) 149 } 150 151 if i.ISOChecksum != "bar0" { 152 t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) 153 } 154 155 } 156 157 func TestISOConfigPrepare_ISOChecksumType(t *testing.T) { 158 i := testISOConfig() 159 160 // Test bad 161 i.ISOChecksumType = "" 162 warns, err := i.Prepare(nil) 163 if len(warns) > 0 { 164 t.Fatalf("bad: %#v", warns) 165 } 166 if err == nil { 167 t.Fatal("should have error") 168 } 169 170 // Test good 171 i = testISOConfig() 172 i.ISOChecksumType = "mD5" 173 warns, err = i.Prepare(nil) 174 if len(warns) > 0 { 175 t.Fatalf("bad: %#v", warns) 176 } 177 if err != nil { 178 t.Fatalf("should not have error: %s", err) 179 } 180 181 if i.ISOChecksumType != "md5" { 182 t.Fatalf("should've lowercased: %s", i.ISOChecksumType) 183 } 184 185 // Test unknown 186 i = testISOConfig() 187 i.ISOChecksumType = "fake" 188 warns, err = i.Prepare(nil) 189 if len(warns) > 0 { 190 t.Fatalf("bad: %#v", warns) 191 } 192 if err == nil { 193 t.Fatal("should have error") 194 } 195 196 // Test none 197 i = testISOConfig() 198 i.ISOChecksumType = "none" 199 warns, err = i.Prepare(nil) 200 if len(warns) == 0 { 201 t.Fatalf("bad: %#v", warns) 202 } 203 if err != nil { 204 t.Fatalf("should not have error: %s", err) 205 } 206 207 if i.ISOChecksumType != "none" { 208 t.Fatalf("should've lowercased: %s", i.ISOChecksumType) 209 } 210 } 211 212 func TestISOConfigPrepare_ISOUrl(t *testing.T) { 213 i := testISOConfig() 214 215 // Test both empty 216 i.RawSingleISOUrl = "" 217 i.ISOUrls = []string{} 218 warns, err := i.Prepare(nil) 219 if len(warns) > 0 { 220 t.Fatalf("bad: %#v", warns) 221 } 222 if err == nil { 223 t.Fatal("should have error") 224 } 225 226 // Test iso_url not set but checksum url is 227 ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root"))) 228 defer ts.Close() 229 i = testISOConfig() 230 i.RawSingleISOUrl = "" 231 i.ISOChecksum = "" 232 i.ISOChecksumURL = ts.URL + "/basic.txt" 233 warns, err = i.Prepare(nil) 234 235 // Test iso_url set 236 i = testISOConfig() 237 i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso" 238 warns, err = i.Prepare(nil) 239 if len(warns) > 0 { 240 t.Fatalf("bad: %#v", warns) 241 } 242 if err != nil { 243 t.Errorf("should not have error: %s", err) 244 } 245 246 expected := []string{"http://www.packer.io/the-OS.iso"} 247 if !reflect.DeepEqual(i.ISOUrls, expected) { 248 t.Fatalf("bad: %#v", i.ISOUrls) 249 } 250 251 // Test both set 252 i = testISOConfig() 253 i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso" 254 i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"} 255 warns, err = i.Prepare(nil) 256 if len(warns) > 0 { 257 t.Fatalf("bad: %#v", warns) 258 } 259 if err == nil { 260 t.Fatal("should have error") 261 } 262 263 // Test just iso_urls set 264 i = testISOConfig() 265 i.RawSingleISOUrl = "" 266 i.ISOUrls = []string{ 267 "http://www.packer.io/the-OS.iso", 268 "http://www.hashicorp.com/the-OS.iso", 269 } 270 271 warns, err = i.Prepare(nil) 272 if len(warns) > 0 { 273 t.Fatalf("bad: %#v", warns) 274 } 275 if err != nil { 276 t.Errorf("should not have error: %s", err) 277 } 278 279 expected = []string{ 280 "http://www.packer.io/the-OS.iso", 281 "http://www.hashicorp.com/the-OS.iso", 282 } 283 if !reflect.DeepEqual(i.ISOUrls, expected) { 284 t.Fatalf("bad: %#v", i.ISOUrls) 285 } 286 }