github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/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.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{"nonexistent.bat", "nonexistent.ps1"}
    90  	b = Builder{}
    91  	_, errs := b.Prepare(config)
    92  	if errs == nil {
    93  		t.Fatalf("Nonexistent 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_DiskType(t *testing.T) {
   134  	var b Builder
   135  	config := testConfig()
   136  
   137  	// Test a default disk_type
   138  	delete(config, "disk_type")
   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.DiskType != "expand" {
   148  		t.Fatalf("bad: %s", b.config.DiskType)
   149  	}
   150  
   151  	// Test with a bad
   152  	config["disk_type"] = "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 plain disk with wrong setting for compaction
   163  	config["disk_type"] = "plain"
   164  	config["skip_compaction"] = false
   165  	b = Builder{}
   166  	warns, err = b.Prepare(config)
   167  	if len(warns) == 0 {
   168  		t.Fatalf("should have warning")
   169  	}
   170  	if err != nil {
   171  		t.Fatalf("should not have error: %s", err)
   172  	}
   173  
   174  	// Test with plain disk with correct setting for compaction
   175  	config["disk_type"] = "plain"
   176  	config["skip_compaction"] = true
   177  	b = Builder{}
   178  	warns, err = b.Prepare(config)
   179  	if len(warns) > 0 {
   180  		t.Fatalf("bad: %#v", warns)
   181  	}
   182  	if err != nil {
   183  		t.Fatalf("should not have error: %s", err)
   184  	}
   185  
   186  }
   187  
   188  func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
   189  	var b Builder
   190  	config := testConfig()
   191  
   192  	// Test a default boot_wait
   193  	delete(config, "hard_drive_interface")
   194  	warns, err := b.Prepare(config)
   195  	if len(warns) > 0 {
   196  		t.Fatalf("bad: %#v", warns)
   197  	}
   198  	if err != nil {
   199  		t.Fatalf("err: %s", err)
   200  	}
   201  
   202  	if b.config.HardDriveInterface != "sata" {
   203  		t.Fatalf("bad: %s", b.config.HardDriveInterface)
   204  	}
   205  
   206  	// Test with a bad
   207  	config["hard_drive_interface"] = "fake"
   208  	b = Builder{}
   209  	warns, err = b.Prepare(config)
   210  	if len(warns) > 0 {
   211  		t.Fatalf("bad: %#v", warns)
   212  	}
   213  	if err == nil {
   214  		t.Fatal("should have error")
   215  	}
   216  
   217  	// Test with a good
   218  	config["hard_drive_interface"] = "scsi"
   219  	b = Builder{}
   220  	warns, err = b.Prepare(config)
   221  	if len(warns) > 0 {
   222  		t.Fatalf("bad: %#v", warns)
   223  	}
   224  	if err != nil {
   225  		t.Fatalf("should not have error: %s", err)
   226  	}
   227  }
   228  
   229  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   230  	var b Builder
   231  	config := testConfig()
   232  
   233  	// Add a random key
   234  	config["i_should_not_be_valid"] = true
   235  	warns, err := b.Prepare(config)
   236  	if len(warns) > 0 {
   237  		t.Fatalf("bad: %#v", warns)
   238  	}
   239  	if err == nil {
   240  		t.Fatal("should have error")
   241  	}
   242  }