github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/hyperv/iso/builder_test.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/packer/packer"
     9  )
    10  
    11  func testConfig() map[string]interface{} {
    12  	return map[string]interface{}{
    13  		"iso_checksum":            "foo",
    14  		"iso_checksum_type":       "md5",
    15  		"iso_url":                 "http://www.packer.io",
    16  		"shutdown_command":        "yes",
    17  		"ssh_username":            "foo",
    18  		"ram_size":                64,
    19  		"disk_size":               256,
    20  		"guest_additions_mode":    "none",
    21  		packer.BuildNameConfigKey: "foo",
    22  	}
    23  }
    24  
    25  func TestBuilder_ImplementsBuilder(t *testing.T) {
    26  	var raw interface{}
    27  	raw = &Builder{}
    28  	if _, ok := raw.(packer.Builder); !ok {
    29  		t.Error("Builder must implement builder.")
    30  	}
    31  }
    32  
    33  func TestBuilderPrepare_Defaults(t *testing.T) {
    34  	var b Builder
    35  	config := testConfig()
    36  
    37  	warns, err := b.Prepare(config)
    38  	if len(warns) > 0 {
    39  		t.Fatalf("bad: %#v", warns)
    40  	}
    41  	if err != nil {
    42  		t.Fatalf("should not have error: %s", err)
    43  	}
    44  
    45  	if b.config.VMName != "packer-foo" {
    46  		t.Errorf("bad vm name: %s", b.config.VMName)
    47  	}
    48  }
    49  
    50  func TestBuilderPrepare_DiskSize(t *testing.T) {
    51  	var b Builder
    52  	config := testConfig()
    53  
    54  	delete(config, "disk_size")
    55  	warns, err := b.Prepare(config)
    56  	if len(warns) > 0 {
    57  		t.Fatalf("bad: %#v", warns)
    58  	}
    59  	if err != nil {
    60  		t.Fatalf("bad err: %s", err)
    61  	}
    62  
    63  	if b.config.DiskSize != 40*1024 {
    64  		t.Fatalf("bad size: %d", b.config.DiskSize)
    65  	}
    66  
    67  	config["disk_size"] = 256
    68  	b = Builder{}
    69  	warns, err = b.Prepare(config)
    70  	if len(warns) > 0 {
    71  		t.Fatalf("bad: %#v", warns)
    72  	}
    73  	if err != nil {
    74  		t.Fatalf("should not have error: %s", err)
    75  	}
    76  
    77  	if b.config.DiskSize != 256 {
    78  		t.Fatalf("bad size: %d", b.config.DiskSize)
    79  	}
    80  }
    81  
    82  func TestBuilderPrepare_FloppyFiles(t *testing.T) {
    83  	var b Builder
    84  	config := testConfig()
    85  
    86  	delete(config, "floppy_files")
    87  	warns, err := b.Prepare(config)
    88  	if len(warns) > 0 {
    89  		t.Fatalf("bad: %#v", warns)
    90  	}
    91  	if err != nil {
    92  		t.Fatalf("bad err: %s", err)
    93  	}
    94  
    95  	if len(b.config.FloppyFiles) != 0 {
    96  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
    97  	}
    98  
    99  	floppiesPath := "../../../common/test-fixtures/floppies"
   100  	config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppiesPath), fmt.Sprintf("%s/foo.ps1", floppiesPath)}
   101  	b = Builder{}
   102  	warns, err = b.Prepare(config)
   103  	if len(warns) > 0 {
   104  		t.Fatalf("bad: %#v", warns)
   105  	}
   106  	if err != nil {
   107  		t.Fatalf("should not have error: %s", err)
   108  	}
   109  
   110  	expected := []string{fmt.Sprintf("%s/bar.bat", floppiesPath), fmt.Sprintf("%s/foo.ps1", floppiesPath)}
   111  	if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
   112  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
   113  	}
   114  }
   115  
   116  func TestBuilderPrepare_InvalidFloppies(t *testing.T) {
   117  	var b Builder
   118  	config := testConfig()
   119  	config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
   120  	b = Builder{}
   121  	_, errs := b.Prepare(config)
   122  	if errs == nil {
   123  		t.Fatalf("Nonexistent floppies should trigger multierror")
   124  	}
   125  
   126  	if len(errs.(*packer.MultiError).Errors) != 2 {
   127  		t.Fatalf("Multierror should work and report 2 errors")
   128  	}
   129  }
   130  
   131  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   132  	var b Builder
   133  	config := testConfig()
   134  
   135  	// Add a random key
   136  	config["i_should_not_be_valid"] = true
   137  	warns, err := b.Prepare(config)
   138  	if len(warns) > 0 {
   139  		t.Fatalf("bad: %#v", warns)
   140  	}
   141  	if err == nil {
   142  		t.Fatal("should have error")
   143  	}
   144  }
   145  
   146  func TestBuilderPrepare_ISOChecksum(t *testing.T) {
   147  	var b Builder
   148  	config := testConfig()
   149  
   150  	// Test bad
   151  	config["iso_checksum"] = ""
   152  	warns, err := b.Prepare(config)
   153  	if len(warns) > 0 {
   154  		t.Fatalf("bad: %#v", warns)
   155  	}
   156  	if err == nil {
   157  		t.Fatal("should have error")
   158  	}
   159  
   160  	// Test good
   161  	config["iso_checksum"] = "FOo"
   162  	b = Builder{}
   163  	warns, err = b.Prepare(config)
   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 b.config.ISOChecksum != "foo" {
   172  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
   173  	}
   174  }
   175  
   176  func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
   177  	var b Builder
   178  	config := testConfig()
   179  
   180  	// Test bad
   181  	config["iso_checksum_type"] = ""
   182  	warns, err := b.Prepare(config)
   183  	if len(warns) > 0 {
   184  		t.Fatalf("bad: %#v", warns)
   185  	}
   186  	if err == nil {
   187  		t.Fatal("should have error")
   188  	}
   189  
   190  	// Test good
   191  	config["iso_checksum_type"] = "mD5"
   192  	b = Builder{}
   193  	warns, err = b.Prepare(config)
   194  	if len(warns) > 0 {
   195  		t.Fatalf("bad: %#v", warns)
   196  	}
   197  	if err != nil {
   198  		t.Fatalf("should not have error: %s", err)
   199  	}
   200  
   201  	if b.config.ISOChecksumType != "md5" {
   202  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   203  	}
   204  
   205  	// Test unknown
   206  	config["iso_checksum_type"] = "fake"
   207  	b = Builder{}
   208  	warns, err = b.Prepare(config)
   209  	if len(warns) > 0 {
   210  		t.Fatalf("bad: %#v", warns)
   211  	}
   212  	if err == nil {
   213  		t.Fatal("should have error")
   214  	}
   215  
   216  	// Test none
   217  	config["iso_checksum_type"] = "none"
   218  	b = Builder{}
   219  	warns, err = b.Prepare(config)
   220  	if len(warns) == 0 {
   221  		t.Fatalf("bad: %#v", warns)
   222  	}
   223  	if err != nil {
   224  		t.Fatalf("should not have error: %s", err)
   225  	}
   226  
   227  	if b.config.ISOChecksumType != "none" {
   228  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   229  	}
   230  }
   231  
   232  func TestBuilderPrepare_ISOUrl(t *testing.T) {
   233  	var b Builder
   234  	config := testConfig()
   235  	delete(config, "iso_url")
   236  	delete(config, "iso_urls")
   237  
   238  	// Test both epty
   239  	config["iso_url"] = ""
   240  	b = Builder{}
   241  	warns, err := b.Prepare(config)
   242  	if len(warns) > 0 {
   243  		t.Fatalf("bad: %#v", warns)
   244  	}
   245  	if err == nil {
   246  		t.Fatal("should have error")
   247  	}
   248  
   249  	// Test iso_url set
   250  	config["iso_url"] = "http://www.packer.io"
   251  	b = Builder{}
   252  	warns, err = b.Prepare(config)
   253  	if len(warns) > 0 {
   254  		t.Fatalf("bad: %#v", warns)
   255  	}
   256  	if err != nil {
   257  		t.Errorf("should not have error: %s", err)
   258  	}
   259  
   260  	expected := []string{"http://www.packer.io"}
   261  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   262  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   263  	}
   264  
   265  	// Test both set
   266  	config["iso_url"] = "http://www.packer.io"
   267  	config["iso_urls"] = []string{"http://www.packer.io"}
   268  	b = Builder{}
   269  	warns, err = b.Prepare(config)
   270  	if len(warns) > 0 {
   271  		t.Fatalf("bad: %#v", warns)
   272  	}
   273  	if err == nil {
   274  		t.Fatal("should have error")
   275  	}
   276  
   277  	// Test just iso_urls set
   278  	delete(config, "iso_url")
   279  	config["iso_urls"] = []string{
   280  		"http://www.packer.io",
   281  		"http://www.hashicorp.com",
   282  	}
   283  
   284  	b = Builder{}
   285  	warns, err = b.Prepare(config)
   286  	if len(warns) > 0 {
   287  		t.Fatalf("bad: %#v", warns)
   288  	}
   289  	if err != nil {
   290  		t.Errorf("should not have error: %s", err)
   291  	}
   292  
   293  	expected = []string{
   294  		"http://www.packer.io",
   295  		"http://www.hashicorp.com",
   296  	}
   297  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   298  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   299  	}
   300  }