github.com/sneal/packer@v0.5.2/builder/vmware/iso/builder_test.go (about)

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