github.com/sneal/packer@v0.5.2/builder/virtualbox/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  
    17  		packer.BuildNameConfigKey: "foo",
    18  	}
    19  }
    20  
    21  func TestBuilder_ImplementsBuilder(t *testing.T) {
    22  	var raw interface{}
    23  	raw = &Builder{}
    24  	if _, ok := raw.(packer.Builder); !ok {
    25  		t.Error("Builder must implement builder.")
    26  	}
    27  }
    28  
    29  func TestBuilderPrepare_Defaults(t *testing.T) {
    30  	var b Builder
    31  	config := testConfig()
    32  	warns, err := b.Prepare(config)
    33  	if len(warns) > 0 {
    34  		t.Fatalf("bad: %#v", warns)
    35  	}
    36  	if err != nil {
    37  		t.Fatalf("should not have error: %s", err)
    38  	}
    39  
    40  	if b.config.GuestAdditionsMode != GuestAdditionsModeUpload {
    41  		t.Errorf("bad guest additions mode: %s", b.config.GuestAdditionsMode)
    42  	}
    43  
    44  	if b.config.GuestOSType != "Other" {
    45  		t.Errorf("bad guest OS type: %s", b.config.GuestOSType)
    46  	}
    47  
    48  	if b.config.VMName != "packer-foo" {
    49  		t.Errorf("bad vm name: %s", b.config.VMName)
    50  	}
    51  
    52  	if b.config.Format != "ovf" {
    53  		t.Errorf("bad format: %s", b.config.Format)
    54  	}
    55  }
    56  
    57  func TestBuilderPrepare_DiskSize(t *testing.T) {
    58  	var b Builder
    59  	config := testConfig()
    60  
    61  	delete(config, "disk_size")
    62  	warns, err := b.Prepare(config)
    63  	if len(warns) > 0 {
    64  		t.Fatalf("bad: %#v", warns)
    65  	}
    66  	if err != nil {
    67  		t.Fatalf("bad err: %s", err)
    68  	}
    69  
    70  	if b.config.DiskSize != 40000 {
    71  		t.Fatalf("bad size: %d", b.config.DiskSize)
    72  	}
    73  
    74  	config["disk_size"] = 60000
    75  	b = Builder{}
    76  	warns, err = b.Prepare(config)
    77  	if len(warns) > 0 {
    78  		t.Fatalf("bad: %#v", warns)
    79  	}
    80  	if err != nil {
    81  		t.Fatalf("should not have error: %s", err)
    82  	}
    83  
    84  	if b.config.DiskSize != 60000 {
    85  		t.Fatalf("bad size: %s", b.config.DiskSize)
    86  	}
    87  }
    88  
    89  func TestBuilderPrepare_GuestAdditionsMode(t *testing.T) {
    90  	var b Builder
    91  	config := testConfig()
    92  
    93  	// test default mode
    94  	delete(config, "guest_additions_mode")
    95  	warns, err := b.Prepare(config)
    96  	if len(warns) > 0 {
    97  		t.Fatalf("bad: %#v", warns)
    98  	}
    99  	if err != nil {
   100  		t.Fatalf("bad err: %s", err)
   101  	}
   102  
   103  	// Test another mode
   104  	config["guest_additions_mode"] = "attach"
   105  	b = Builder{}
   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("should not have error: %s", err)
   112  	}
   113  
   114  	if b.config.GuestAdditionsMode != GuestAdditionsModeAttach {
   115  		t.Fatalf("bad: %s", b.config.GuestAdditionsMode)
   116  	}
   117  
   118  	// Test bad mode
   119  	config["guest_additions_mode"] = "teleport"
   120  	b = Builder{}
   121  	warns, err = b.Prepare(config)
   122  	if len(warns) > 0 {
   123  		t.Fatalf("bad: %#v", warns)
   124  	}
   125  	if err == nil {
   126  		t.Fatal("should error")
   127  	}
   128  }
   129  
   130  func TestBuilderPrepare_GuestAdditionsPath(t *testing.T) {
   131  	var b Builder
   132  	config := testConfig()
   133  
   134  	delete(config, "guest_additions_path")
   135  	warns, err := b.Prepare(config)
   136  	if len(warns) > 0 {
   137  		t.Fatalf("bad: %#v", warns)
   138  	}
   139  	if err != nil {
   140  		t.Fatalf("bad err: %s", err)
   141  	}
   142  
   143  	if b.config.GuestAdditionsPath != "VBoxGuestAdditions.iso" {
   144  		t.Fatalf("bad: %s", b.config.GuestAdditionsPath)
   145  	}
   146  
   147  	config["guest_additions_path"] = "foo"
   148  	b = Builder{}
   149  	warns, err = b.Prepare(config)
   150  	if len(warns) > 0 {
   151  		t.Fatalf("bad: %#v", warns)
   152  	}
   153  	if err != nil {
   154  		t.Fatalf("should not have error: %s", err)
   155  	}
   156  
   157  	if b.config.GuestAdditionsPath != "foo" {
   158  		t.Fatalf("bad size: %s", b.config.GuestAdditionsPath)
   159  	}
   160  }
   161  
   162  func TestBuilderPrepare_GuestAdditionsSHA256(t *testing.T) {
   163  	var b Builder
   164  	config := testConfig()
   165  
   166  	delete(config, "guest_additions_sha256")
   167  	warns, err := b.Prepare(config)
   168  	if len(warns) > 0 {
   169  		t.Fatalf("bad: %#v", warns)
   170  	}
   171  	if err != nil {
   172  		t.Fatalf("bad err: %s", err)
   173  	}
   174  
   175  	if b.config.GuestAdditionsSHA256 != "" {
   176  		t.Fatalf("bad: %s", b.config.GuestAdditionsSHA256)
   177  	}
   178  
   179  	config["guest_additions_sha256"] = "FOO"
   180  	b = Builder{}
   181  	warns, err = b.Prepare(config)
   182  	if len(warns) > 0 {
   183  		t.Fatalf("bad: %#v", warns)
   184  	}
   185  	if err != nil {
   186  		t.Fatalf("should not have error: %s", err)
   187  	}
   188  
   189  	if b.config.GuestAdditionsSHA256 != "foo" {
   190  		t.Fatalf("bad size: %s", b.config.GuestAdditionsSHA256)
   191  	}
   192  }
   193  
   194  func TestBuilderPrepare_GuestAdditionsURL(t *testing.T) {
   195  	var b Builder
   196  	config := testConfig()
   197  
   198  	config["guest_additions_url"] = ""
   199  	warns, err := b.Prepare(config)
   200  	if len(warns) > 0 {
   201  		t.Fatalf("bad: %#v", warns)
   202  	}
   203  	if err != nil {
   204  		t.Fatalf("err: %s", err)
   205  	}
   206  
   207  	if b.config.GuestAdditionsURL != "" {
   208  		t.Fatalf("should be empty: %s", b.config.GuestAdditionsURL)
   209  	}
   210  
   211  	config["guest_additions_url"] = "http://www.packer.io"
   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.Errorf("should not have error: %s", err)
   219  	}
   220  }
   221  
   222  func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
   223  	var b Builder
   224  	config := testConfig()
   225  
   226  	// Test a default boot_wait
   227  	delete(config, "hard_drive_interface")
   228  	warns, err := b.Prepare(config)
   229  	if len(warns) > 0 {
   230  		t.Fatalf("bad: %#v", warns)
   231  	}
   232  	if err != nil {
   233  		t.Fatalf("err: %s", err)
   234  	}
   235  
   236  	if b.config.HardDriveInterface != "ide" {
   237  		t.Fatalf("bad: %s", b.config.HardDriveInterface)
   238  	}
   239  
   240  	// Test with a bad
   241  	config["hard_drive_interface"] = "fake"
   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.Fatal("should have error")
   249  	}
   250  
   251  	// Test with a good
   252  	config["hard_drive_interface"] = "sata"
   253  	b = Builder{}
   254  	warns, err = b.Prepare(config)
   255  	if len(warns) > 0 {
   256  		t.Fatalf("bad: %#v", warns)
   257  	}
   258  	if err != nil {
   259  		t.Fatalf("should not have error: %s", err)
   260  	}
   261  }
   262  
   263  func TestBuilderPrepare_HTTPPort(t *testing.T) {
   264  	var b Builder
   265  	config := testConfig()
   266  
   267  	// Bad
   268  	config["http_port_min"] = 1000
   269  	config["http_port_max"] = 500
   270  	warns, err := b.Prepare(config)
   271  	if len(warns) > 0 {
   272  		t.Fatalf("bad: %#v", warns)
   273  	}
   274  	if err == nil {
   275  		t.Fatal("should have error")
   276  	}
   277  
   278  	// Bad
   279  	config["http_port_min"] = -500
   280  	b = Builder{}
   281  	warns, err = b.Prepare(config)
   282  	if len(warns) > 0 {
   283  		t.Fatalf("bad: %#v", warns)
   284  	}
   285  	if err == nil {
   286  		t.Fatal("should have error")
   287  	}
   288  
   289  	// Good
   290  	config["http_port_min"] = 500
   291  	config["http_port_max"] = 1000
   292  	b = Builder{}
   293  	warns, err = b.Prepare(config)
   294  	if len(warns) > 0 {
   295  		t.Fatalf("bad: %#v", warns)
   296  	}
   297  	if err != nil {
   298  		t.Fatalf("should not have error: %s", err)
   299  	}
   300  }
   301  
   302  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   303  	var b Builder
   304  	config := testConfig()
   305  
   306  	// Add a random key
   307  	config["i_should_not_be_valid"] = true
   308  	warns, err := b.Prepare(config)
   309  	if len(warns) > 0 {
   310  		t.Fatalf("bad: %#v", warns)
   311  	}
   312  	if err == nil {
   313  		t.Fatal("should have error")
   314  	}
   315  }
   316  
   317  func TestBuilderPrepare_ISOChecksum(t *testing.T) {
   318  	var b Builder
   319  	config := testConfig()
   320  
   321  	// Test bad
   322  	config["iso_checksum"] = ""
   323  	warns, err := b.Prepare(config)
   324  	if len(warns) > 0 {
   325  		t.Fatalf("bad: %#v", warns)
   326  	}
   327  	if err == nil {
   328  		t.Fatal("should have error")
   329  	}
   330  
   331  	// Test good
   332  	config["iso_checksum"] = "FOo"
   333  	b = Builder{}
   334  	warns, err = b.Prepare(config)
   335  	if len(warns) > 0 {
   336  		t.Fatalf("bad: %#v", warns)
   337  	}
   338  	if err != nil {
   339  		t.Fatalf("should not have error: %s", err)
   340  	}
   341  
   342  	if b.config.ISOChecksum != "foo" {
   343  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
   344  	}
   345  }
   346  
   347  func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
   348  	var b Builder
   349  	config := testConfig()
   350  
   351  	// Test bad
   352  	config["iso_checksum_type"] = ""
   353  	warns, err := b.Prepare(config)
   354  	if len(warns) > 0 {
   355  		t.Fatalf("bad: %#v", warns)
   356  	}
   357  	if err == nil {
   358  		t.Fatal("should have error")
   359  	}
   360  
   361  	// Test good
   362  	config["iso_checksum_type"] = "mD5"
   363  	b = Builder{}
   364  	warns, err = b.Prepare(config)
   365  	if len(warns) > 0 {
   366  		t.Fatalf("bad: %#v", warns)
   367  	}
   368  	if err != nil {
   369  		t.Fatalf("should not have error: %s", err)
   370  	}
   371  
   372  	if b.config.ISOChecksumType != "md5" {
   373  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   374  	}
   375  
   376  	// Test unknown
   377  	config["iso_checksum_type"] = "fake"
   378  	b = Builder{}
   379  	warns, err = b.Prepare(config)
   380  	if len(warns) > 0 {
   381  		t.Fatalf("bad: %#v", warns)
   382  	}
   383  	if err == nil {
   384  		t.Fatal("should have error")
   385  	}
   386  
   387  	// Test none
   388  	config["iso_checksum_type"] = "none"
   389  	b = Builder{}
   390  	warns, err = b.Prepare(config)
   391  	if len(warns) == 0 {
   392  		t.Fatalf("bad: %#v", warns)
   393  	}
   394  	if err != nil {
   395  		t.Fatalf("should not have error: %s", err)
   396  	}
   397  
   398  	if b.config.ISOChecksumType != "none" {
   399  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   400  	}
   401  }
   402  
   403  func TestBuilderPrepare_ISOUrl(t *testing.T) {
   404  	var b Builder
   405  	config := testConfig()
   406  	delete(config, "iso_url")
   407  	delete(config, "iso_urls")
   408  
   409  	// Test both epty
   410  	config["iso_url"] = ""
   411  	b = Builder{}
   412  	warns, err := b.Prepare(config)
   413  	if len(warns) > 0 {
   414  		t.Fatalf("bad: %#v", warns)
   415  	}
   416  	if err == nil {
   417  		t.Fatal("should have error")
   418  	}
   419  
   420  	// Test iso_url set
   421  	config["iso_url"] = "http://www.packer.io"
   422  	b = Builder{}
   423  	warns, err = b.Prepare(config)
   424  	if len(warns) > 0 {
   425  		t.Fatalf("bad: %#v", warns)
   426  	}
   427  	if err != nil {
   428  		t.Errorf("should not have error: %s", err)
   429  	}
   430  
   431  	expected := []string{"http://www.packer.io"}
   432  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   433  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   434  	}
   435  
   436  	// Test both set
   437  	config["iso_url"] = "http://www.packer.io"
   438  	config["iso_urls"] = []string{"http://www.packer.io"}
   439  	b = Builder{}
   440  	warns, err = b.Prepare(config)
   441  	if len(warns) > 0 {
   442  		t.Fatalf("bad: %#v", warns)
   443  	}
   444  	if err == nil {
   445  		t.Fatal("should have error")
   446  	}
   447  
   448  	// Test just iso_urls set
   449  	delete(config, "iso_url")
   450  	config["iso_urls"] = []string{
   451  		"http://www.packer.io",
   452  		"http://www.hashicorp.com",
   453  	}
   454  
   455  	b = Builder{}
   456  	warns, err = b.Prepare(config)
   457  	if len(warns) > 0 {
   458  		t.Fatalf("bad: %#v", warns)
   459  	}
   460  	if err != nil {
   461  		t.Errorf("should not have error: %s", err)
   462  	}
   463  
   464  	expected = []string{
   465  		"http://www.packer.io",
   466  		"http://www.hashicorp.com",
   467  	}
   468  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   469  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   470  	}
   471  }