github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/iso/builder_test.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/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.google.com/",
    16  		"shutdown_command":       "yes",
    17  		"ssh_username":           "foo",
    18  		"parallels_tools_flavor": "lin",
    19  
    20  		packer.BuildNameConfigKey: "foo",
    21  	}
    22  }
    23  
    24  func TestBuilder_ImplementsBuilder(t *testing.T) {
    25  	var raw interface{}
    26  	raw = &Builder{}
    27  	if _, ok := raw.(packer.Builder); !ok {
    28  		t.Error("Builder must implement builder.")
    29  	}
    30  }
    31  
    32  func TestBuilderPrepare_Defaults(t *testing.T) {
    33  	var b Builder
    34  	config := testConfig()
    35  	warns, err := b.Prepare(config)
    36  	if len(warns) > 0 {
    37  		t.Fatalf("bad: %#v", warns)
    38  	}
    39  	if err != nil {
    40  		t.Fatalf("should not have error: %s", err)
    41  	}
    42  
    43  	if b.config.GuestOSType != "other" {
    44  		t.Errorf("bad guest OS type: %s", b.config.GuestOSType)
    45  	}
    46  
    47  	if b.config.VMName != "packer-foo" {
    48  		t.Errorf("bad vm name: %s", b.config.VMName)
    49  	}
    50  }
    51  
    52  func TestBuilderPrepare_FloppyFiles(t *testing.T) {
    53  	var b Builder
    54  	config := testConfig()
    55  
    56  	delete(config, "floppy_files")
    57  	warns, err := b.Prepare(config)
    58  	if len(warns) > 0 {
    59  		t.Fatalf("bad: %#v", warns)
    60  	}
    61  	if err != nil {
    62  		t.Fatalf("bad err: %s", err)
    63  	}
    64  
    65  	if len(b.config.FloppyFiles) != 0 {
    66  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
    67  	}
    68  
    69  	floppies_path := "../../../common/test-fixtures/floppies"
    70  	config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
    71  	b = Builder{}
    72  	warns, err = b.Prepare(config)
    73  	if len(warns) > 0 {
    74  		t.Fatalf("bad: %#v", warns)
    75  	}
    76  	if err != nil {
    77  		t.Fatalf("should not have error: %s", err)
    78  	}
    79  
    80  	expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
    81  	if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
    82  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
    83  	}
    84  }
    85  
    86  func TestBuilderPrepare_InvalidFloppies(t *testing.T) {
    87  	var b Builder
    88  	config := testConfig()
    89  	config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"}
    90  	b = Builder{}
    91  	_, errs := b.Prepare(config)
    92  	if errs == nil {
    93  		t.Fatalf("Non existant floppies should trigger multierror")
    94  	}
    95  
    96  	if len(errs.(*packer.MultiError).Errors) != 2 {
    97  		t.Fatalf("Multierror should work and report 2 errors")
    98  	}
    99  }
   100  
   101  func TestBuilderPrepare_DiskSize(t *testing.T) {
   102  	var b Builder
   103  	config := testConfig()
   104  
   105  	delete(config, "disk_size")
   106  	warns, err := b.Prepare(config)
   107  	if len(warns) > 0 {
   108  		t.Fatalf("bad: %#v", warns)
   109  	}
   110  	if err != nil {
   111  		t.Fatalf("bad err: %s", err)
   112  	}
   113  
   114  	if b.config.DiskSize != 40000 {
   115  		t.Fatalf("bad size: %d", b.config.DiskSize)
   116  	}
   117  
   118  	config["disk_size"] = 60000
   119  	b = Builder{}
   120  	warns, err = b.Prepare(config)
   121  	if len(warns) > 0 {
   122  		t.Fatalf("bad: %#v", warns)
   123  	}
   124  	if err != nil {
   125  		t.Fatalf("should not have error: %s", err)
   126  	}
   127  
   128  	if b.config.DiskSize != 60000 {
   129  		t.Fatalf("bad size: %d", b.config.DiskSize)
   130  	}
   131  }
   132  
   133  func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
   134  	var b Builder
   135  	config := testConfig()
   136  
   137  	// Test a default boot_wait
   138  	delete(config, "hard_drive_interface")
   139  	warns, err := b.Prepare(config)
   140  	if len(warns) > 0 {
   141  		t.Fatalf("bad: %#v", warns)
   142  	}
   143  	if err != nil {
   144  		t.Fatalf("err: %s", err)
   145  	}
   146  
   147  	if b.config.HardDriveInterface != "sata" {
   148  		t.Fatalf("bad: %s", b.config.HardDriveInterface)
   149  	}
   150  
   151  	// Test with a bad
   152  	config["hard_drive_interface"] = "fake"
   153  	b = Builder{}
   154  	warns, err = b.Prepare(config)
   155  	if len(warns) > 0 {
   156  		t.Fatalf("bad: %#v", warns)
   157  	}
   158  	if err == nil {
   159  		t.Fatal("should have error")
   160  	}
   161  
   162  	// Test with a good
   163  	config["hard_drive_interface"] = "scsi"
   164  	b = Builder{}
   165  	warns, err = b.Prepare(config)
   166  	if len(warns) > 0 {
   167  		t.Fatalf("bad: %#v", warns)
   168  	}
   169  	if err != nil {
   170  		t.Fatalf("should not have error: %s", err)
   171  	}
   172  }
   173  
   174  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   175  	var b Builder
   176  	config := testConfig()
   177  
   178  	// Add a random key
   179  	config["i_should_not_be_valid"] = true
   180  	warns, err := b.Prepare(config)
   181  	if len(warns) > 0 {
   182  		t.Fatalf("bad: %#v", warns)
   183  	}
   184  	if err == nil {
   185  		t.Fatal("should have error")
   186  	}
   187  }