github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/common/iso_config_test.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func testISOConfig() ISOConfig {
    11  	return ISOConfig{
    12  		ISOChecksum:     "foo",
    13  		ISOChecksumURL:  "",
    14  		ISOChecksumType: "md5",
    15  		RawSingleISOUrl: "http://www.packer.io/the-OS.iso",
    16  	}
    17  }
    18  
    19  var cs_bsd_style = `
    20  MD5 (other.iso) = bAr
    21  MD5 (the-OS.iso) = baZ
    22  `
    23  
    24  var cs_gnu_style = `
    25  bAr0 *the-OS.iso
    26  baZ0  other.iso
    27  `
    28  
    29  func TestISOConfigPrepare_ISOChecksum(t *testing.T) {
    30  	i := testISOConfig()
    31  
    32  	// Test bad
    33  	i.ISOChecksum = ""
    34  	warns, err := i.Prepare(nil)
    35  	if len(warns) > 0 {
    36  		t.Fatalf("bad: %#v", warns)
    37  	}
    38  	if err == nil {
    39  		t.Fatal("should have error")
    40  	}
    41  
    42  	// Test good
    43  	i = testISOConfig()
    44  	i.ISOChecksum = "FOo"
    45  	warns, err = i.Prepare(nil)
    46  	if len(warns) > 0 {
    47  		t.Fatalf("bad: %#v", warns)
    48  	}
    49  	if err != nil {
    50  		t.Fatalf("should not have error: %s", err)
    51  	}
    52  
    53  	if i.ISOChecksum != "foo" {
    54  		t.Fatalf("should've lowercased: %s", i.ISOChecksum)
    55  	}
    56  }
    57  
    58  func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
    59  	i := testISOConfig()
    60  	i.ISOChecksumURL = "file:///not_read"
    61  
    62  	// Test ISOChecksum overrides url
    63  	warns, err := i.Prepare(nil)
    64  	if len(warns) > 0 && len(err) > 0 {
    65  		t.Fatalf("bad: %#v, %#v", warns, err)
    66  	}
    67  
    68  	// Test good - ISOChecksumURL BSD style
    69  	i = testISOConfig()
    70  	i.ISOChecksum = ""
    71  	cs_file, _ := ioutil.TempFile("", "packer-test-")
    72  	ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666)
    73  	i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
    74  	warns, err = i.Prepare(nil)
    75  	if len(warns) > 0 {
    76  		t.Fatalf("bad: %#v", warns)
    77  	}
    78  	if err != nil {
    79  		t.Fatalf("should not have error: %s", err)
    80  	}
    81  
    82  	if i.ISOChecksum != "baz" {
    83  		t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum)
    84  	}
    85  
    86  	// Test good - ISOChecksumURL GNU style
    87  	i = testISOConfig()
    88  	i.ISOChecksum = ""
    89  	cs_file, _ = ioutil.TempFile("", "packer-test-")
    90  	ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666)
    91  	i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name())
    92  	warns, err = i.Prepare(nil)
    93  	if len(warns) > 0 {
    94  		t.Fatalf("bad: %#v", warns)
    95  	}
    96  	if err != nil {
    97  		t.Fatalf("should not have error: %s", err)
    98  	}
    99  
   100  	if i.ISOChecksum != "bar0" {
   101  		t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
   102  	}
   103  }
   104  
   105  func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {
   106  	i := testISOConfig()
   107  
   108  	// Test bad
   109  	i.ISOChecksumType = ""
   110  	warns, err := i.Prepare(nil)
   111  	if len(warns) > 0 {
   112  		t.Fatalf("bad: %#v", warns)
   113  	}
   114  	if err == nil {
   115  		t.Fatal("should have error")
   116  	}
   117  
   118  	// Test good
   119  	i = testISOConfig()
   120  	i.ISOChecksumType = "mD5"
   121  	warns, err = i.Prepare(nil)
   122  	if len(warns) > 0 {
   123  		t.Fatalf("bad: %#v", warns)
   124  	}
   125  	if err != nil {
   126  		t.Fatalf("should not have error: %s", err)
   127  	}
   128  
   129  	if i.ISOChecksumType != "md5" {
   130  		t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
   131  	}
   132  
   133  	// Test unknown
   134  	i = testISOConfig()
   135  	i.ISOChecksumType = "fake"
   136  	warns, err = i.Prepare(nil)
   137  	if len(warns) > 0 {
   138  		t.Fatalf("bad: %#v", warns)
   139  	}
   140  	if err == nil {
   141  		t.Fatal("should have error")
   142  	}
   143  
   144  	// Test none
   145  	i = testISOConfig()
   146  	i.ISOChecksumType = "none"
   147  	warns, err = i.Prepare(nil)
   148  	if len(warns) == 0 {
   149  		t.Fatalf("bad: %#v", warns)
   150  	}
   151  	if err != nil {
   152  		t.Fatalf("should not have error: %s", err)
   153  	}
   154  
   155  	if i.ISOChecksumType != "none" {
   156  		t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
   157  	}
   158  }
   159  
   160  func TestISOConfigPrepare_ISOUrl(t *testing.T) {
   161  	i := testISOConfig()
   162  
   163  	// Test both empty
   164  	i.RawSingleISOUrl = ""
   165  	i.ISOUrls = []string{}
   166  	warns, err := i.Prepare(nil)
   167  	if len(warns) > 0 {
   168  		t.Fatalf("bad: %#v", warns)
   169  	}
   170  	if err == nil {
   171  		t.Fatal("should have error")
   172  	}
   173  
   174  	// Test iso_url set
   175  	i = testISOConfig()
   176  	i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
   177  	warns, err = i.Prepare(nil)
   178  	if len(warns) > 0 {
   179  		t.Fatalf("bad: %#v", warns)
   180  	}
   181  	if err != nil {
   182  		t.Errorf("should not have error: %s", err)
   183  	}
   184  
   185  	expected := []string{"http://www.packer.io/the-OS.iso"}
   186  	if !reflect.DeepEqual(i.ISOUrls, expected) {
   187  		t.Fatalf("bad: %#v", i.ISOUrls)
   188  	}
   189  
   190  	// Test both set
   191  	i = testISOConfig()
   192  	i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
   193  	i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"}
   194  	warns, err = i.Prepare(nil)
   195  	if len(warns) > 0 {
   196  		t.Fatalf("bad: %#v", warns)
   197  	}
   198  	if err == nil {
   199  		t.Fatal("should have error")
   200  	}
   201  
   202  	// Test just iso_urls set
   203  	i = testISOConfig()
   204  	i.RawSingleISOUrl = ""
   205  	i.ISOUrls = []string{
   206  		"http://www.packer.io/the-OS.iso",
   207  		"http://www.hashicorp.com/the-OS.iso",
   208  	}
   209  
   210  	warns, err = i.Prepare(nil)
   211  	if len(warns) > 0 {
   212  		t.Fatalf("bad: %#v", warns)
   213  	}
   214  	if err != nil {
   215  		t.Errorf("should not have error: %s", err)
   216  	}
   217  
   218  	expected = []string{
   219  		"http://www.packer.io/the-OS.iso",
   220  		"http://www.hashicorp.com/the-OS.iso",
   221  	}
   222  	if !reflect.DeepEqual(i.ISOUrls, expected) {
   223  		t.Fatalf("bad: %#v", i.ISOUrls)
   224  	}
   225  }