github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/vmware/builder_test.go (about)

     1  package vmware
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func testConfig() map[string]interface{} {
    13  	return map[string]interface{}{
    14  		"iso_checksum":      "foo",
    15  		"iso_checksum_type": "md5",
    16  		"iso_url":           "http://www.packer.io",
    17  		"ssh_username":      "foo",
    18  
    19  		packer.BuildNameConfigKey: "foo",
    20  	}
    21  }
    22  
    23  func TestBuilder_ImplementsBuilder(t *testing.T) {
    24  	var raw interface{}
    25  	raw = &Builder{}
    26  	if _, ok := raw.(packer.Builder); !ok {
    27  		t.Error("Builder must implement builder.")
    28  	}
    29  }
    30  
    31  func TestBuilderPrepare_BootWait(t *testing.T) {
    32  	var b Builder
    33  	config := testConfig()
    34  
    35  	// Test a default boot_wait
    36  	delete(config, "boot_wait")
    37  	err := b.Prepare(config)
    38  	if err != nil {
    39  		t.Fatalf("err: %s", err)
    40  	}
    41  
    42  	if b.config.RawBootWait != "10s" {
    43  		t.Fatalf("bad value: %s", b.config.RawBootWait)
    44  	}
    45  
    46  	// Test with a bad boot_wait
    47  	config["boot_wait"] = "this is not good"
    48  	err = b.Prepare(config)
    49  	if err == nil {
    50  		t.Fatal("should have error")
    51  	}
    52  
    53  	// Test with a good one
    54  	config["boot_wait"] = "5s"
    55  	b = Builder{}
    56  	err = b.Prepare(config)
    57  	if err != nil {
    58  		t.Fatalf("should not have error: %s", err)
    59  	}
    60  }
    61  
    62  func TestBuilderPrepare_ISOChecksum(t *testing.T) {
    63  	var b Builder
    64  	config := testConfig()
    65  
    66  	// Test bad
    67  	config["iso_checksum"] = ""
    68  	err := b.Prepare(config)
    69  	if err == nil {
    70  		t.Fatal("should have error")
    71  	}
    72  
    73  	// Test good
    74  	config["iso_checksum"] = "FOo"
    75  	b = Builder{}
    76  	err = b.Prepare(config)
    77  	if err != nil {
    78  		t.Fatalf("should not have error: %s", err)
    79  	}
    80  
    81  	if b.config.ISOChecksum != "foo" {
    82  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
    83  	}
    84  }
    85  
    86  func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
    87  	var b Builder
    88  	config := testConfig()
    89  
    90  	// Test bad
    91  	config["iso_checksum_type"] = ""
    92  	err := b.Prepare(config)
    93  	if err == nil {
    94  		t.Fatal("should have error")
    95  	}
    96  
    97  	// Test good
    98  	config["iso_checksum_type"] = "mD5"
    99  	b = Builder{}
   100  	err = b.Prepare(config)
   101  	if err != nil {
   102  		t.Fatalf("should not have error: %s", err)
   103  	}
   104  
   105  	if b.config.ISOChecksumType != "md5" {
   106  		t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
   107  	}
   108  
   109  	// Test unknown
   110  	config["iso_checksum_type"] = "fake"
   111  	b = Builder{}
   112  	err = b.Prepare(config)
   113  	if err == nil {
   114  		t.Fatal("should have error")
   115  	}
   116  }
   117  func TestBuilderPrepare_Defaults(t *testing.T) {
   118  	var b Builder
   119  	config := testConfig()
   120  	err := b.Prepare(config)
   121  	if err != nil {
   122  		t.Fatalf("should not have error: %s", err)
   123  	}
   124  
   125  	if b.config.DiskName != "disk" {
   126  		t.Errorf("bad disk name: %s", b.config.DiskName)
   127  	}
   128  
   129  	if b.config.OutputDir != "output-foo" {
   130  		t.Errorf("bad output dir: %s", b.config.OutputDir)
   131  	}
   132  
   133  	if b.config.sshWaitTimeout != (20 * time.Minute) {
   134  		t.Errorf("bad wait timeout: %s", b.config.sshWaitTimeout)
   135  	}
   136  
   137  	if b.config.VMName != "packer-foo" {
   138  		t.Errorf("bad vm name: %s", b.config.VMName)
   139  	}
   140  }
   141  
   142  func TestBuilderPrepare_DiskSize(t *testing.T) {
   143  	var b Builder
   144  	config := testConfig()
   145  
   146  	delete(config, "disk_size")
   147  	err := b.Prepare(config)
   148  	if err != nil {
   149  		t.Fatalf("bad err: %s", err)
   150  	}
   151  
   152  	if b.config.DiskSize != 40000 {
   153  		t.Fatalf("bad size: %d", b.config.DiskSize)
   154  	}
   155  
   156  	config["disk_size"] = 60000
   157  	b = Builder{}
   158  	err = b.Prepare(config)
   159  	if err != nil {
   160  		t.Fatalf("should not have error: %s", err)
   161  	}
   162  
   163  	if b.config.DiskSize != 60000 {
   164  		t.Fatalf("bad size: %s", b.config.DiskSize)
   165  	}
   166  }
   167  
   168  func TestBuilderPrepare_FloppyFiles(t *testing.T) {
   169  	var b Builder
   170  	config := testConfig()
   171  
   172  	delete(config, "floppy_files")
   173  	err := b.Prepare(config)
   174  	if err != nil {
   175  		t.Fatalf("bad err: %s", err)
   176  	}
   177  
   178  	if len(b.config.FloppyFiles) != 0 {
   179  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
   180  	}
   181  
   182  	config["floppy_files"] = []string{"foo", "bar"}
   183  	b = Builder{}
   184  	err = b.Prepare(config)
   185  	if err != nil {
   186  		t.Fatalf("should not have error: %s", err)
   187  	}
   188  
   189  	expected := []string{"foo", "bar"}
   190  	if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
   191  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
   192  	}
   193  }
   194  
   195  func TestBuilderPrepare_HTTPPort(t *testing.T) {
   196  	var b Builder
   197  	config := testConfig()
   198  
   199  	// Bad
   200  	config["http_port_min"] = 1000
   201  	config["http_port_max"] = 500
   202  	err := b.Prepare(config)
   203  	if err == nil {
   204  		t.Fatal("should have error")
   205  	}
   206  
   207  	// Bad
   208  	config["http_port_min"] = -500
   209  	b = Builder{}
   210  	err = b.Prepare(config)
   211  	if err == nil {
   212  		t.Fatal("should have error")
   213  	}
   214  
   215  	// Good
   216  	config["http_port_min"] = 500
   217  	config["http_port_max"] = 1000
   218  	b = Builder{}
   219  	err = b.Prepare(config)
   220  	if err != nil {
   221  		t.Fatalf("should not have error: %s", err)
   222  	}
   223  }
   224  
   225  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   226  	var b Builder
   227  	config := testConfig()
   228  
   229  	// Add a random key
   230  	config["i_should_not_be_valid"] = true
   231  	err := b.Prepare(config)
   232  	if err == nil {
   233  		t.Fatal("should have error")
   234  	}
   235  }
   236  
   237  func TestBuilderPrepare_ISOUrl(t *testing.T) {
   238  	var b Builder
   239  	config := testConfig()
   240  	delete(config, "iso_url")
   241  	delete(config, "iso_urls")
   242  
   243  	// Test both epty
   244  	config["iso_url"] = ""
   245  	b = Builder{}
   246  	err := b.Prepare(config)
   247  	if err == nil {
   248  		t.Fatal("should have error")
   249  	}
   250  
   251  	// Test iso_url set
   252  	config["iso_url"] = "http://www.packer.io"
   253  	b = Builder{}
   254  	err = b.Prepare(config)
   255  	if err != nil {
   256  		t.Errorf("should not have error: %s", err)
   257  	}
   258  
   259  	expected := []string{"http://www.packer.io"}
   260  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   261  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   262  	}
   263  
   264  	// Test both set
   265  	config["iso_url"] = "http://www.packer.io"
   266  	config["iso_urls"] = []string{"http://www.packer.io"}
   267  	b = Builder{}
   268  	err = b.Prepare(config)
   269  	if err == nil {
   270  		t.Fatal("should have error")
   271  	}
   272  
   273  	// Test just iso_urls set
   274  	delete(config, "iso_url")
   275  	config["iso_urls"] = []string{
   276  		"http://www.packer.io",
   277  		"http://www.hashicorp.com",
   278  	}
   279  
   280  	b = Builder{}
   281  	err = b.Prepare(config)
   282  	if err != nil {
   283  		t.Errorf("should not have error: %s", err)
   284  	}
   285  
   286  	expected = []string{
   287  		"http://www.packer.io",
   288  		"http://www.hashicorp.com",
   289  	}
   290  	if !reflect.DeepEqual(b.config.ISOUrls, expected) {
   291  		t.Fatalf("bad: %#v", b.config.ISOUrls)
   292  	}
   293  }
   294  
   295  func TestBuilderPrepare_OutputDir(t *testing.T) {
   296  	var b Builder
   297  	config := testConfig()
   298  
   299  	// Test with existing dir
   300  	dir, err := ioutil.TempDir("", "packer")
   301  	if err != nil {
   302  		t.Fatalf("err: %s", err)
   303  	}
   304  	defer os.RemoveAll(dir)
   305  
   306  	config["output_directory"] = dir
   307  	b = Builder{}
   308  	err = b.Prepare(config)
   309  	if err == nil {
   310  		t.Fatal("should have error")
   311  	}
   312  
   313  	// Test with a good one
   314  	config["output_directory"] = "i-hope-i-dont-exist"
   315  	b = Builder{}
   316  	err = b.Prepare(config)
   317  	if err != nil {
   318  		t.Fatalf("should not have error: %s", err)
   319  	}
   320  }
   321  
   322  func TestBuilderPrepare_ShutdownTimeout(t *testing.T) {
   323  	var b Builder
   324  	config := testConfig()
   325  
   326  	// Test with a bad value
   327  	config["shutdown_timeout"] = "this is not good"
   328  	err := b.Prepare(config)
   329  	if err == nil {
   330  		t.Fatal("should have error")
   331  	}
   332  
   333  	// Test with a good one
   334  	config["shutdown_timeout"] = "5s"
   335  	b = Builder{}
   336  	err = b.Prepare(config)
   337  	if err != nil {
   338  		t.Fatalf("should not have error: %s", err)
   339  	}
   340  }
   341  
   342  func TestBuilderPrepare_SSHUser(t *testing.T) {
   343  	var b Builder
   344  	config := testConfig()
   345  
   346  	config["ssh_username"] = ""
   347  	err := b.Prepare(config)
   348  	if err == nil {
   349  		t.Fatal("should have error")
   350  	}
   351  
   352  	config["ssh_username"] = "exists"
   353  	b = Builder{}
   354  	err = b.Prepare(config)
   355  	if err != nil {
   356  		t.Fatalf("should not have error: %s", err)
   357  	}
   358  }
   359  
   360  func TestBuilderPrepare_SSHPort(t *testing.T) {
   361  	var b Builder
   362  	config := testConfig()
   363  
   364  	// Test with a bad value
   365  	delete(config, "ssh_port")
   366  	err := b.Prepare(config)
   367  	if err != nil {
   368  		t.Fatalf("bad err: %s", err)
   369  	}
   370  
   371  	if b.config.SSHPort != 22 {
   372  		t.Fatalf("bad ssh port: %d", b.config.SSHPort)
   373  	}
   374  
   375  	// Test with a good one
   376  	config["ssh_port"] = 44
   377  	b = Builder{}
   378  	err = b.Prepare(config)
   379  	if err != nil {
   380  		t.Fatalf("should not have error: %s", err)
   381  	}
   382  
   383  	if b.config.SSHPort != 44 {
   384  		t.Fatalf("bad ssh port: %d", b.config.SSHPort)
   385  	}
   386  }
   387  
   388  func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) {
   389  	var b Builder
   390  	config := testConfig()
   391  
   392  	// Test with a bad value
   393  	config["ssh_wait_timeout"] = "this is not good"
   394  	err := b.Prepare(config)
   395  	if err == nil {
   396  		t.Fatal("should have error")
   397  	}
   398  
   399  	// Test with a good one
   400  	config["ssh_wait_timeout"] = "5s"
   401  	b = Builder{}
   402  	err = b.Prepare(config)
   403  	if err != nil {
   404  		t.Fatalf("should not have error: %s", err)
   405  	}
   406  }
   407  
   408  func TestBuilderPrepare_ToolsUploadPath(t *testing.T) {
   409  	var b Builder
   410  	config := testConfig()
   411  
   412  	// Test a default
   413  	delete(config, "tools_upload_path")
   414  	err := b.Prepare(config)
   415  	if err != nil {
   416  		t.Fatalf("err: %s", err)
   417  	}
   418  
   419  	if b.config.ToolsUploadPath == "" {
   420  		t.Fatalf("bad value: %s", b.config.ToolsUploadPath)
   421  	}
   422  
   423  	// Test with a bad value
   424  	config["tools_upload_path"] = "{{{nope}"
   425  	b = Builder{}
   426  	err = b.Prepare(config)
   427  	if err == nil {
   428  		t.Fatal("should have error")
   429  	}
   430  
   431  	// Test with a good one
   432  	config["tools_upload_path"] = "hey"
   433  	b = Builder{}
   434  	err = b.Prepare(config)
   435  	if err != nil {
   436  		t.Fatalf("should not have error: %s", err)
   437  	}
   438  }
   439  
   440  func TestBuilderPrepare_VNCPort(t *testing.T) {
   441  	var b Builder
   442  	config := testConfig()
   443  
   444  	// Bad
   445  	config["vnc_port_min"] = 1000
   446  	config["vnc_port_max"] = 500
   447  	err := b.Prepare(config)
   448  	if err == nil {
   449  		t.Fatal("should have error")
   450  	}
   451  
   452  	// Bad
   453  	config["vnc_port_min"] = -500
   454  	b = Builder{}
   455  	err = b.Prepare(config)
   456  	if err == nil {
   457  		t.Fatal("should have error")
   458  	}
   459  
   460  	// Good
   461  	config["vnc_port_min"] = 500
   462  	config["vnc_port_max"] = 1000
   463  	b = Builder{}
   464  	err = b.Prepare(config)
   465  	if err != nil {
   466  		t.Fatalf("should not have error: %s", err)
   467  	}
   468  }
   469  
   470  func TestBuilderPrepare_VMXData(t *testing.T) {
   471  	var b Builder
   472  	config := testConfig()
   473  
   474  	config["vmx_data"] = map[interface{}]interface{}{
   475  		"one": "foo",
   476  		"two": "bar",
   477  	}
   478  
   479  	err := b.Prepare(config)
   480  	if err != nil {
   481  		t.Fatalf("should not have error: %s", err)
   482  	}
   483  
   484  	if len(b.config.VMXData) != 2 {
   485  		t.Fatal("should have two items in VMXData")
   486  	}
   487  }