github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/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  	// Test good - ISOChecksumURL GNU style with query parameters
   156  	i = testISOConfig()
   157  	i.ISOChecksum = ""
   158  	i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso?stuff=boo"
   159  
   160  	cs_file, _ = ioutil.TempFile("", "packer-test-")
   161  	ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
   162  	i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
   163  	warns, err = i.Prepare(nil)
   164  	if len(warns) > 0 {
   165  		t.Fatalf("bad: %#v", warns)
   166  	}
   167  	if err != nil {
   168  		t.Fatalf("should not have error: %s", err)
   169  	}
   170  
   171  	if i.ISOChecksum != "bar0" {
   172  		t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
   173  	}
   174  }
   175  
   176  func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {
   177  	i := testISOConfig()
   178  
   179  	// Test bad
   180  	i.ISOChecksumType = ""
   181  	warns, err := i.Prepare(nil)
   182  	if len(warns) > 0 {
   183  		t.Fatalf("bad: %#v", warns)
   184  	}
   185  	if err == nil {
   186  		t.Fatal("should have error")
   187  	}
   188  
   189  	// Test good
   190  	i = testISOConfig()
   191  	i.ISOChecksumType = "mD5"
   192  	warns, err = i.Prepare(nil)
   193  	if len(warns) > 0 {
   194  		t.Fatalf("bad: %#v", warns)
   195  	}
   196  	if err != nil {
   197  		t.Fatalf("should not have error: %s", err)
   198  	}
   199  
   200  	if i.ISOChecksumType != "md5" {
   201  		t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
   202  	}
   203  
   204  	// Test unknown
   205  	i = testISOConfig()
   206  	i.ISOChecksumType = "fake"
   207  	warns, err = i.Prepare(nil)
   208  	if len(warns) > 0 {
   209  		t.Fatalf("bad: %#v", warns)
   210  	}
   211  	if err == nil {
   212  		t.Fatal("should have error")
   213  	}
   214  
   215  	// Test none
   216  	i = testISOConfig()
   217  	i.ISOChecksumType = "none"
   218  	warns, err = i.Prepare(nil)
   219  	if len(warns) == 0 {
   220  		t.Fatalf("bad: %#v", warns)
   221  	}
   222  	if err != nil {
   223  		t.Fatalf("should not have error: %s", err)
   224  	}
   225  
   226  	if i.ISOChecksumType != "none" {
   227  		t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
   228  	}
   229  }
   230  
   231  func TestISOConfigPrepare_ISOUrl(t *testing.T) {
   232  	i := testISOConfig()
   233  
   234  	// Test both empty
   235  	i.RawSingleISOUrl = ""
   236  	i.ISOUrls = []string{}
   237  	warns, err := i.Prepare(nil)
   238  	if len(warns) > 0 {
   239  		t.Fatalf("bad: %#v", warns)
   240  	}
   241  	if err == nil {
   242  		t.Fatal("should have error")
   243  	}
   244  
   245  	// Test iso_url not set but checksum url is
   246  	ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
   247  	defer ts.Close()
   248  	i = testISOConfig()
   249  	i.RawSingleISOUrl = ""
   250  	i.ISOChecksum = ""
   251  	i.ISOChecksumURL = ts.URL + "/basic.txt"
   252  	warns, err = i.Prepare(nil)
   253  
   254  	// Test iso_url set
   255  	i = testISOConfig()
   256  	i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
   257  	warns, err = i.Prepare(nil)
   258  	if len(warns) > 0 {
   259  		t.Fatalf("bad: %#v", warns)
   260  	}
   261  	if err != nil {
   262  		t.Errorf("should not have error: %s", err)
   263  	}
   264  
   265  	expected := []string{"http://www.packer.io/the-OS.iso"}
   266  	if !reflect.DeepEqual(i.ISOUrls, expected) {
   267  		t.Fatalf("bad: %#v", i.ISOUrls)
   268  	}
   269  
   270  	// Test both set
   271  	i = testISOConfig()
   272  	i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
   273  	i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"}
   274  	warns, err = i.Prepare(nil)
   275  	if len(warns) > 0 {
   276  		t.Fatalf("bad: %#v", warns)
   277  	}
   278  	if err == nil {
   279  		t.Fatal("should have error")
   280  	}
   281  
   282  	// Test just iso_urls set
   283  	i = testISOConfig()
   284  	i.RawSingleISOUrl = ""
   285  	i.ISOUrls = []string{
   286  		"http://www.packer.io/the-OS.iso",
   287  		"http://www.hashicorp.com/the-OS.iso",
   288  	}
   289  
   290  	warns, err = i.Prepare(nil)
   291  	if len(warns) > 0 {
   292  		t.Fatalf("bad: %#v", warns)
   293  	}
   294  	if err != nil {
   295  		t.Errorf("should not have error: %s", err)
   296  	}
   297  
   298  	expected = []string{
   299  		"http://www.packer.io/the-OS.iso",
   300  		"http://www.hashicorp.com/the-OS.iso",
   301  	}
   302  	if !reflect.DeepEqual(i.ISOUrls, expected) {
   303  		t.Fatalf("bad: %#v", i.ISOUrls)
   304  	}
   305  }
   306  
   307  func TestISOConfigPrepare_TargetExtension(t *testing.T) {
   308  	i := testISOConfig()
   309  
   310  	// Test the default value
   311  	warns, err := i.Prepare(nil)
   312  	if len(warns) > 0 {
   313  		t.Fatalf("bad: %#v", warns)
   314  	}
   315  	if err != nil {
   316  		t.Fatalf("should not have error: %s", err)
   317  	}
   318  
   319  	if i.TargetExtension != "iso" {
   320  		t.Fatalf("should've found \"iso\" got: %s", i.TargetExtension)
   321  	}
   322  
   323  	// Test the lowercased value
   324  	i = testISOConfig()
   325  	i.TargetExtension = "DMG"
   326  	warns, err = i.Prepare(nil)
   327  	if len(warns) > 0 {
   328  		t.Fatalf("bad: %#v", warns)
   329  	}
   330  	if err != nil {
   331  		t.Fatalf("should not have error: %s", err)
   332  	}
   333  
   334  	if i.TargetExtension != "dmg" {
   335  		t.Fatalf("should've lowercased: %s", i.TargetExtension)
   336  	}
   337  }