github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/builder/parallels/iso/builder_test.go (about)

     1  package iso
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func testConfig() map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"iso_checksum":           "foo",
    12  		"iso_checksum_type":      "md5",
    13  		"iso_url":                "http://www.google.com/",
    14  		"shutdown_command":       "yes",
    15  		"ssh_username":           "foo",
    16  		"parallels_tools_flavor": "lin",
    17  
    18  		packer.BuildNameConfigKey: "foo",
    19  	}
    20  }
    21  
    22  func TestBuilder_ImplementsBuilder(t *testing.T) {
    23  	var raw interface{}
    24  	raw = &Builder{}
    25  	if _, ok := raw.(packer.Builder); !ok {
    26  		t.Error("Builder must implement builder.")
    27  	}
    28  }
    29  
    30  func TestBuilderPrepare_Defaults(t *testing.T) {
    31  	var b Builder
    32  	config := testConfig()
    33  	warns, err := b.Prepare(config)
    34  	if len(warns) > 0 {
    35  		t.Fatalf("bad: %#v", warns)
    36  	}
    37  	if err != nil {
    38  		t.Fatalf("should not have error: %s", err)
    39  	}
    40  
    41  	if b.config.GuestOSType != "other" {
    42  		t.Errorf("bad guest OS type: %s", b.config.GuestOSType)
    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 != 40000 {
    64  		t.Fatalf("bad size: %d", b.config.DiskSize)
    65  	}
    66  
    67  	config["disk_size"] = 60000
    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 != 60000 {
    78  		t.Fatalf("bad size: %d", b.config.DiskSize)
    79  	}
    80  }
    81  
    82  func TestBuilderPrepare_GuestOSType(t *testing.T) {
    83  	var b Builder
    84  	config := testConfig()
    85  	delete(config, "guest_os_distribution")
    86  
    87  	// Test deprecated parameter
    88  	config["guest_os_distribution"] = "bolgenos"
    89  	warns, err := b.Prepare(config)
    90  	if len(warns) == 0 {
    91  		t.Fatalf("should have warning")
    92  	}
    93  	if err != nil {
    94  		t.Fatalf("should not have error: %s", err)
    95  	}
    96  	if b.config.GuestOSType != "bolgenos" {
    97  		t.Fatalf("bad: %s", b.config.GuestOSType)
    98  	}
    99  }
   100  
   101  func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
   102  	var b Builder
   103  	config := testConfig()
   104  
   105  	// Test a default boot_wait
   106  	delete(config, "hard_drive_interface")
   107  	warns, err := b.Prepare(config)
   108  	if len(warns) > 0 {
   109  		t.Fatalf("bad: %#v", warns)
   110  	}
   111  	if err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	if b.config.HardDriveInterface != "sata" {
   116  		t.Fatalf("bad: %s", b.config.HardDriveInterface)
   117  	}
   118  
   119  	// Test with a bad
   120  	config["hard_drive_interface"] = "fake"
   121  	b = Builder{}
   122  	warns, err = b.Prepare(config)
   123  	if len(warns) > 0 {
   124  		t.Fatalf("bad: %#v", warns)
   125  	}
   126  	if err == nil {
   127  		t.Fatal("should have error")
   128  	}
   129  
   130  	// Test with a good
   131  	config["hard_drive_interface"] = "scsi"
   132  	b = Builder{}
   133  	warns, err = b.Prepare(config)
   134  	if len(warns) > 0 {
   135  		t.Fatalf("bad: %#v", warns)
   136  	}
   137  	if err != nil {
   138  		t.Fatalf("should not have error: %s", err)
   139  	}
   140  }
   141  
   142  func TestBuilderPrepare_HTTPPort(t *testing.T) {
   143  	var b Builder
   144  	config := testConfig()
   145  
   146  	// Bad
   147  	config["http_port_min"] = 1000
   148  	config["http_port_max"] = 500
   149  	warns, err := b.Prepare(config)
   150  	if len(warns) > 0 {
   151  		t.Fatalf("bad: %#v", warns)
   152  	}
   153  	if err == nil {
   154  		t.Fatal("should have error")
   155  	}
   156  
   157  	// Bad
   158  	config["http_port_min"] = -500
   159  	b = Builder{}
   160  	warns, err = b.Prepare(config)
   161  	if len(warns) > 0 {
   162  		t.Fatalf("bad: %#v", warns)
   163  	}
   164  	if err == nil {
   165  		t.Fatal("should have error")
   166  	}
   167  
   168  	// Good
   169  	config["http_port_min"] = 500
   170  	config["http_port_max"] = 1000
   171  	b = Builder{}
   172  	warns, err = b.Prepare(config)
   173  	if len(warns) > 0 {
   174  		t.Fatalf("bad: %#v", warns)
   175  	}
   176  	if err != nil {
   177  		t.Fatalf("should not have error: %s", err)
   178  	}
   179  }
   180  
   181  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   182  	var b Builder
   183  	config := testConfig()
   184  
   185  	// Add a random key
   186  	config["i_should_not_be_valid"] = true
   187  	warns, err := b.Prepare(config)
   188  	if len(warns) > 0 {
   189  		t.Fatalf("bad: %#v", warns)
   190  	}
   191  	if err == nil {
   192  		t.Fatal("should have error")
   193  	}
   194  }
   195  
   196  func TestBuilderPrepare_ISOChecksum(t *testing.T) {
   197  	var b Builder
   198  	config := testConfig()
   199  
   200  	// Test bad
   201  	config["iso_checksum"] = ""
   202  	warns, err := b.Prepare(config)
   203  	if len(warns) > 0 {
   204  		t.Fatalf("bad: %#v", warns)
   205  	}
   206  	if err == nil {
   207  		t.Fatal("should have error")
   208  	}
   209  
   210  	// Test good
   211  	config["iso_checksum"] = "FOo"
   212  	b = Builder{}
   213  	warns, err = b.Prepare(config)
   214  	if len(warns) > 0 {
   215  		t.Fatalf("bad: %#v", warns)
   216  	}
   217  	if err != nil {
   218  		t.Fatalf("should not have error: %s", err)
   219  	}
   220  
   221  	if b.config.ISOChecksum != "foo" {
   222  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
   223  	}
   224  }
   225  
   226  func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
   227  	var b Builder
   228  	config := testConfig()
   229  
   230  	// Test bad
   231  	config["iso_checksum_type"] = ""
   232  	warns, err := b.Prepare(config)
   233  	if len(warns) > 0 {
   234  		t.Fatalf("bad: %#v", warns)
   235  	}
   236  	if err == nil {
   237  		t.Fatal("should have error")
   238  	}
   239  
   240  	// Test good
   241  	config["iso_checksum_type"] = "mD5"
   242  	b = Builder{}
   243  	warns, err = b.Prepare(config)
   244  	if len(warns) > 0 {
   245  		t.Fatalf("bad: %#v", warns)
   246  	}
   247  	if err != nil {
   248  		t.Fatalf("should not have error: %s", err)
   249  	}
   250  
   251  	if b.config.ISOChecksumType != "md5" {
   252  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   253  	}
   254  
   255  	// Test unknown
   256  	config["iso_checksum_type"] = "fake"
   257  	b = Builder{}
   258  	warns, err = b.Prepare(config)
   259  	if len(warns) > 0 {
   260  		t.Fatalf("bad: %#v", warns)
   261  	}
   262  	if err == nil {
   263  		t.Fatal("should have error")
   264  	}
   265  
   266  	// Test none
   267  	config["iso_checksum_type"] = "none"
   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.Fatalf("should not have error: %s", err)
   275  	}
   276  
   277  	if b.config.ISOChecksumType != "none" {
   278  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   279  	}
   280  }
   281  
   282  func TestBuilderPrepare_ISOUrl(t *testing.T) {
   283  	var b Builder
   284  	config := testConfig()
   285  	delete(config, "iso_url")
   286  	delete(config, "iso_urls")
   287  
   288  	// Test both epty
   289  	config["iso_url"] = ""
   290  	b = Builder{}
   291  	warns, err := b.Prepare(config)
   292  	if len(warns) > 0 {
   293  		t.Fatalf("bad: %#v", warns)
   294  	}
   295  	if err == nil {
   296  		t.Fatal("should have error")
   297  	}
   298  
   299  	// Test iso_url set
   300  	config["iso_url"] = "http://www.packer.io"
   301  	b = Builder{}
   302  	warns, err = b.Prepare(config)
   303  	if len(warns) > 0 {
   304  		t.Fatalf("bad: %#v", warns)
   305  	}
   306  	if err != nil {
   307  		t.Errorf("should not have error: %s", err)
   308  	}
   309  
   310  	expected := []string{"http://www.packer.io"}
   311  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   312  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   313  	}
   314  
   315  	// Test both set
   316  	config["iso_url"] = "http://www.packer.io"
   317  	config["iso_urls"] = []string{"http://www.packer.io"}
   318  	b = Builder{}
   319  	warns, err = b.Prepare(config)
   320  	if len(warns) > 0 {
   321  		t.Fatalf("bad: %#v", warns)
   322  	}
   323  	if err == nil {
   324  		t.Fatal("should have error")
   325  	}
   326  
   327  	// Test just iso_urls set
   328  	delete(config, "iso_url")
   329  	config["iso_urls"] = []string{
   330  		"http://www.packer.io",
   331  		"http://www.hashicorp.com",
   332  	}
   333  
   334  	b = Builder{}
   335  	warns, err = b.Prepare(config)
   336  	if len(warns) > 0 {
   337  		t.Fatalf("bad: %#v", warns)
   338  	}
   339  	if err != nil {
   340  		t.Errorf("should not have error: %s", err)
   341  	}
   342  
   343  	expected = []string{
   344  		"http://www.packer.io",
   345  		"http://www.hashicorp.com",
   346  	}
   347  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   348  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   349  	}
   350  }
   351  
   352  func TestBuilderPrepare_ParallelsToolsHostPath(t *testing.T) {
   353  	var b Builder
   354  	config := testConfig()
   355  	delete(config, "parallels_tools_host_path")
   356  
   357  	// Test that it is deprecated
   358  	config["parallels_tools_host_path"] = "/path/to/iso"
   359  	warns, err := b.Prepare(config)
   360  	if len(warns) == 0 {
   361  		t.Fatalf("should have warning")
   362  	}
   363  	if err != nil {
   364  		t.Fatalf("should not have error: %s", err)
   365  	}
   366  }