2018-10-16 12:41:27 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2016-02-08 09:51:43 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-10-14 03:56:05 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-04-25 07:25:45 -04:00
|
|
|
"os"
|
2015-10-20 19:27:47 -04:00
|
|
|
"reflect"
|
2016-04-01 05:55:02 -04:00
|
|
|
"runtime"
|
2015-10-20 19:27:47 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testISOConfig() ISOConfig {
|
|
|
|
return ISOConfig{
|
|
|
|
ISOChecksum: "foo",
|
2016-02-08 09:51:43 -05:00
|
|
|
ISOChecksumURL: "",
|
2015-10-20 19:27:47 -04:00
|
|
|
ISOChecksumType: "md5",
|
2016-02-08 09:51:43 -05:00
|
|
|
RawSingleISOUrl: "http://www.packer.io/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
var cs_bsd_style = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (the-OS.iso) = baZ
|
|
|
|
`
|
|
|
|
|
2017-11-05 18:32:39 -05:00
|
|
|
var cs_bsd_style_subdir = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (./subdir/the-OS.iso) = baZ
|
|
|
|
`
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
var cs_gnu_style = `
|
|
|
|
bAr0 *the-OS.iso
|
|
|
|
baZ0 other.iso
|
|
|
|
`
|
|
|
|
|
2017-11-05 18:32:39 -05:00
|
|
|
var cs_gnu_style_subdir = `
|
|
|
|
bAr0 *./subdir/the-OS.iso
|
|
|
|
baZ0 other.iso
|
|
|
|
`
|
|
|
|
|
2016-03-31 03:05:00 -04:00
|
|
|
var cs_bsd_style_no_newline = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (the-OS.iso) = baZ`
|
|
|
|
|
|
|
|
var cs_gnu_style_no_newline = `
|
|
|
|
bAr0 *the-OS.iso
|
|
|
|
baZ0 other.iso`
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
func TestISOConfigPrepare_ISOChecksum(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = "FOo"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "foo" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.ISOChecksum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
i.ISOChecksumURL = "file:///not_read"
|
|
|
|
|
|
|
|
// Test ISOChecksum overrides url
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 && len(err) > 0 {
|
|
|
|
t.Fatalf("bad: %#v, %#v", warns, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good - ISOChecksumURL BSD style
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
cs_file, _ := ioutil.TempFile("", "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2016-02-08 09:51:43 -05:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666)
|
2016-04-01 05:55:02 -04:00
|
|
|
filePrefix := "file://"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
filePrefix += "/"
|
|
|
|
}
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
|
2016-02-08 09:51:43 -05:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "baz" {
|
|
|
|
t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good - ISOChecksumURL GNU style
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
cs_file, _ = ioutil.TempFile("", "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2016-02-08 09:51:43 -05:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
|
2016-04-01 05:55:02 -04:00
|
|
|
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
|
2016-02-08 09:51:43 -05:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "bar0" {
|
|
|
|
t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
2016-03-31 03:05:00 -04:00
|
|
|
|
|
|
|
// Test good - ISOChecksumURL BSD style no newline
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
cs_file, _ = ioutil.TempFile("", "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2016-03-31 03:05:00 -04:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666)
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "baz" {
|
|
|
|
t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
|
|
|
|
2017-11-05 18:32:39 -05:00
|
|
|
// Test good - ISOChecksumURL BSD style with relative path
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
|
|
|
|
cs_dir, _ := ioutil.TempDir("", "packer-testdir-")
|
|
|
|
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.RemoveAll(cs_dir) // Removes the file as well
|
|
|
|
defer cs_file.Close()
|
2017-11-05 18:32:39 -05:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0666)
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
|
|
|
|
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "baz" {
|
|
|
|
t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
|
|
|
|
2016-03-31 03:05:00 -04:00
|
|
|
// Test good - ISOChecksumURL GNU style no newline
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
cs_file, _ = ioutil.TempFile("", "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2016-03-31 03:05:00 -04:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666)
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "bar0" {
|
|
|
|
t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:37:00 -04:00
|
|
|
// Test good - ISOChecksumURL GNU style with query parameters
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo"
|
|
|
|
|
|
|
|
cs_file, _ = ioutil.TempFile("", "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2017-07-26 15:37:00 -04:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "bar0" {
|
|
|
|
t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
2017-11-05 18:32:39 -05:00
|
|
|
|
|
|
|
// Test good - ISOChecksumURL GNU style with relative path
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
|
|
|
|
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
|
2018-04-25 07:25:45 -04:00
|
|
|
defer os.Remove(cs_file.Name())
|
|
|
|
defer cs_file.Close()
|
2017-11-05 18:32:39 -05:00
|
|
|
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0666)
|
|
|
|
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
|
|
|
|
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksum != "bar0" {
|
|
|
|
t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
|
|
|
|
}
|
2019-02-13 19:21:55 -05:00
|
|
|
|
|
|
|
// Test that we won't try to read an iso into memory because of a user
|
|
|
|
// error
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumURL = "file:///not_read.iso"
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error because iso is bad filetype: %s", err)
|
|
|
|
}
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
i.ISOChecksumType = ""
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumType = "mD5"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksumType != "md5" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test unknown
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumType = "fake"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test none
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumType = "none"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksumType != "none" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestISOConfigPrepare_ISOUrl(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test both empty
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOUrls = []string{}
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:56:05 -04:00
|
|
|
// Test iso_url not set but checksum url is
|
|
|
|
ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
|
|
|
|
defer ts.Close()
|
|
|
|
i = testISOConfig()
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
i.ISOChecksumURL = ts.URL + "/basic.txt"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
// Test iso_url set
|
|
|
|
i = testISOConfig()
|
2016-02-08 09:51:43 -05:00
|
|
|
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
|
2015-10-20 19:27:47 -04:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
expected := []string{"http://www.packer.io/the-OS.iso"}
|
2015-10-20 19:27:47 -04:00
|
|
|
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", i.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both set
|
|
|
|
i = testISOConfig()
|
2016-02-08 09:51:43 -05:00
|
|
|
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
|
|
|
|
i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"}
|
2015-10-20 19:27:47 -04:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set
|
|
|
|
i = testISOConfig()
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOUrls = []string{
|
2016-02-08 09:51:43 -05:00
|
|
|
"http://www.packer.io/the-OS.iso",
|
|
|
|
"http://www.hashicorp.com/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
2016-02-08 09:51:43 -05:00
|
|
|
"http://www.packer.io/the-OS.iso",
|
|
|
|
"http://www.hashicorp.com/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", i.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|
2016-12-17 05:49:54 -05:00
|
|
|
|
|
|
|
func TestISOConfigPrepare_TargetExtension(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test the default value
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.TargetExtension != "iso" {
|
|
|
|
t.Fatalf("should've found \"iso\" got: %s", i.TargetExtension)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the lowercased value
|
|
|
|
i = testISOConfig()
|
|
|
|
i.TargetExtension = "DMG"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.TargetExtension != "dmg" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.TargetExtension)
|
|
|
|
}
|
|
|
|
}
|