github.com/mitchellh/packer@v1.3.2/builder/vmware/iso/builder_test.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  func testConfig() map[string]interface{} {
    14  	return map[string]interface{}{
    15  		"iso_checksum":      "foo",
    16  		"iso_checksum_type": "md5",
    17  		"iso_url":           "http://www.packer.io",
    18  		"shutdown_command":  "foo",
    19  		"ssh_username":      "foo",
    20  
    21  		packer.BuildNameConfigKey: "foo",
    22  	}
    23  }
    24  
    25  func TestBuilder_ImplementsBuilder(t *testing.T) {
    26  	var raw interface{}
    27  	raw = &Builder{}
    28  	if _, ok := raw.(packer.Builder); !ok {
    29  		t.Error("Builder must implement builder.")
    30  	}
    31  }
    32  
    33  func TestBuilderPrepare_Defaults(t *testing.T) {
    34  	var b Builder
    35  	config := testConfig()
    36  	warns, err := b.Prepare(config)
    37  	if len(warns) > 0 {
    38  		t.Fatalf("bad: %#v", warns)
    39  	}
    40  	if err != nil {
    41  		t.Fatalf("should not have error: %s", err)
    42  	}
    43  
    44  	if b.config.DiskName != "disk" {
    45  		t.Errorf("bad disk name: %s", b.config.DiskName)
    46  	}
    47  
    48  	if b.config.OutputDir != "output-foo" {
    49  		t.Errorf("bad output dir: %s", b.config.OutputDir)
    50  	}
    51  
    52  	if b.config.Version != "9" {
    53  		t.Errorf("bad Version: %s", b.config.Version)
    54  	}
    55  
    56  	if b.config.VMName != "packer-foo" {
    57  		t.Errorf("bad vm name: %s", b.config.VMName)
    58  	}
    59  }
    60  
    61  func TestBuilderPrepare_DiskSize(t *testing.T) {
    62  	var b Builder
    63  	config := testConfig()
    64  
    65  	delete(config, "disk_size")
    66  	warns, err := b.Prepare(config)
    67  	if len(warns) > 0 {
    68  		t.Fatalf("bad: %#v", warns)
    69  	}
    70  	if err != nil {
    71  		t.Fatalf("bad err: %s", err)
    72  	}
    73  
    74  	if b.config.DiskSize != 40000 {
    75  		t.Fatalf("bad size: %d", b.config.DiskSize)
    76  	}
    77  
    78  	config["disk_size"] = 60000
    79  	b = Builder{}
    80  	warns, err = b.Prepare(config)
    81  	if len(warns) > 0 {
    82  		t.Fatalf("bad: %#v", warns)
    83  	}
    84  	if err != nil {
    85  		t.Fatalf("should not have error: %s", err)
    86  	}
    87  
    88  	if b.config.DiskSize != 60000 {
    89  		t.Fatalf("bad size: %d", b.config.DiskSize)
    90  	}
    91  }
    92  
    93  func TestBuilderPrepare_FloppyFiles(t *testing.T) {
    94  	var b Builder
    95  	config := testConfig()
    96  
    97  	delete(config, "floppy_files")
    98  	warns, err := b.Prepare(config)
    99  	if len(warns) > 0 {
   100  		t.Fatalf("bad: %#v", warns)
   101  	}
   102  	if err != nil {
   103  		t.Fatalf("bad err: %s", err)
   104  	}
   105  
   106  	if len(b.config.FloppyFiles) != 0 {
   107  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
   108  	}
   109  
   110  	floppies_path := "../../../common/test-fixtures/floppies"
   111  	config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
   112  	b = Builder{}
   113  	warns, err = b.Prepare(config)
   114  	if len(warns) > 0 {
   115  		t.Fatalf("bad: %#v", warns)
   116  	}
   117  	if err != nil {
   118  		t.Fatalf("should not have error: %s", err)
   119  	}
   120  
   121  	expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
   122  	if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
   123  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
   124  	}
   125  }
   126  
   127  func TestBuilderPrepare_InvalidFloppies(t *testing.T) {
   128  	var b Builder
   129  	config := testConfig()
   130  	config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
   131  	b = Builder{}
   132  	_, errs := b.Prepare(config)
   133  	if errs == nil {
   134  		t.Fatalf("Nonexistent floppies should trigger multierror")
   135  	}
   136  
   137  	if len(errs.(*packer.MultiError).Errors) != 2 {
   138  		t.Fatalf("Multierror should work and report 2 errors")
   139  	}
   140  }
   141  
   142  func TestBuilderPrepare_RemoteType(t *testing.T) {
   143  	var b Builder
   144  	config := testConfig()
   145  
   146  	config["format"] = "ovf"
   147  	config["remote_host"] = "foobar.example.com"
   148  	config["remote_password"] = "supersecret"
   149  	// Bad
   150  	config["remote_type"] = "foobar"
   151  	warns, err := b.Prepare(config)
   152  	if len(warns) > 0 {
   153  		t.Fatalf("bad: %#v", warns)
   154  	}
   155  	if err == nil {
   156  		t.Fatal("should have error")
   157  	}
   158  
   159  	config["remote_type"] = "esx5"
   160  	// Bad
   161  	config["remote_host"] = ""
   162  	b = Builder{}
   163  	warns, err = b.Prepare(config)
   164  	if len(warns) > 0 {
   165  		t.Fatalf("bad: %#v", warns)
   166  	}
   167  	if err == nil {
   168  		t.Fatal("should have error")
   169  	}
   170  
   171  	// Good
   172  	config["remote_type"] = ""
   173  	config["remote_host"] = ""
   174  	config["remote_password"] = ""
   175  	config["remote_private_key_file"] = ""
   176  	b = Builder{}
   177  	warns, err = b.Prepare(config)
   178  	if len(warns) > 0 {
   179  		t.Fatalf("bad: %#v", warns)
   180  	}
   181  	if err != nil {
   182  		t.Fatalf("should not have error: %s", err)
   183  	}
   184  
   185  	// Good
   186  	config["remote_type"] = "esx5"
   187  	config["remote_host"] = "foobar.example.com"
   188  	config["remote_password"] = "supersecret"
   189  	b = Builder{}
   190  	warns, err = b.Prepare(config)
   191  	if len(warns) > 0 {
   192  		t.Fatalf("bad: %#v", warns)
   193  	}
   194  	if err != nil {
   195  		t.Fatalf("should not have error: %s", err)
   196  	}
   197  }
   198  
   199  func TestBuilderPrepare_RemoteExport(t *testing.T) {
   200  	var b Builder
   201  	config := testConfig()
   202  
   203  	config["remote_type"] = "esx5"
   204  	config["remote_host"] = "foobar.example.com"
   205  	// Bad
   206  	config["remote_password"] = ""
   207  	warns, err := b.Prepare(config)
   208  	if len(warns) != 0 {
   209  		t.Fatalf("bad: %#v", warns)
   210  	}
   211  	if err == nil {
   212  		t.Fatal("should have error")
   213  	}
   214  
   215  	// Good
   216  	config["remote_password"] = "supersecret"
   217  	b = Builder{}
   218  	warns, err = b.Prepare(config)
   219  	if len(warns) != 0 {
   220  		t.Fatalf("err: %s", err)
   221  	}
   222  	if err != nil {
   223  		t.Fatalf("should not have error: %s", err)
   224  	}
   225  }
   226  
   227  func TestBuilderPrepare_Format(t *testing.T) {
   228  	var b Builder
   229  	config := testConfig()
   230  
   231  	// Bad
   232  	config["format"] = "foobar"
   233  	warns, err := b.Prepare(config)
   234  	if len(warns) > 0 {
   235  		t.Fatalf("bad: %#v", warns)
   236  	}
   237  	if err == nil {
   238  		t.Fatal("should have error")
   239  	}
   240  
   241  	goodFormats := []string{"ova", "ovf", "vmx"}
   242  
   243  	for _, format := range goodFormats {
   244  		// Good
   245  		config["format"] = format
   246  		b = Builder{}
   247  		warns, err = b.Prepare(config)
   248  		if len(warns) > 0 {
   249  			t.Fatalf("bad: %#v", warns)
   250  		}
   251  		if err != nil {
   252  			t.Fatalf("should not have error: %s", err)
   253  		}
   254  	}
   255  }
   256  
   257  func TestBuilderPrepare_InvalidKey(t *testing.T) {
   258  	var b Builder
   259  	config := testConfig()
   260  
   261  	// Add a random key
   262  	config["i_should_not_be_valid"] = true
   263  	warns, err := b.Prepare(config)
   264  	if len(warns) > 0 {
   265  		t.Fatalf("bad: %#v", warns)
   266  	}
   267  	if err == nil {
   268  		t.Fatal("should have error")
   269  	}
   270  }
   271  
   272  func TestBuilderPrepare_OutputDir(t *testing.T) {
   273  	var b Builder
   274  	config := testConfig()
   275  
   276  	// Test with existing dir
   277  	dir, err := ioutil.TempDir("", "packer")
   278  	if err != nil {
   279  		t.Fatalf("err: %s", err)
   280  	}
   281  	defer os.RemoveAll(dir)
   282  
   283  	config["output_directory"] = dir
   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.Fatalf("err: %s", err)
   291  	}
   292  
   293  	// Test with a good one
   294  	config["output_directory"] = "i-hope-i-dont-exist"
   295  	b = Builder{}
   296  	warns, err = b.Prepare(config)
   297  	if len(warns) > 0 {
   298  		t.Fatalf("bad: %#v", warns)
   299  	}
   300  	if err != nil {
   301  		t.Fatalf("should not have error: %s", err)
   302  	}
   303  }
   304  
   305  func TestBuilderPrepare_ToolsUploadPath(t *testing.T) {
   306  	var b Builder
   307  	config := testConfig()
   308  
   309  	// Test a default
   310  	delete(config, "tools_upload_path")
   311  	warns, err := b.Prepare(config)
   312  	if len(warns) > 0 {
   313  		t.Fatalf("bad: %#v", warns)
   314  	}
   315  	if err != nil {
   316  		t.Fatalf("err: %s", err)
   317  	}
   318  
   319  	if b.config.ToolsUploadPath == "" {
   320  		t.Fatalf("bad value: %s", b.config.ToolsUploadPath)
   321  	}
   322  
   323  	// Test with a bad value
   324  	config["tools_upload_path"] = "{{{nope}"
   325  	b = Builder{}
   326  	warns, err = b.Prepare(config)
   327  	if len(warns) > 0 {
   328  		t.Fatalf("bad: %#v", warns)
   329  	}
   330  	if err == nil {
   331  		t.Fatal("should have error")
   332  	}
   333  
   334  	// Test with a good one
   335  	config["tools_upload_path"] = "hey"
   336  	b = Builder{}
   337  	warns, err = b.Prepare(config)
   338  	if len(warns) > 0 {
   339  		t.Fatalf("bad: %#v", warns)
   340  	}
   341  	if err != nil {
   342  		t.Fatalf("should not have error: %s", err)
   343  	}
   344  }
   345  
   346  func TestBuilderPrepare_VMXTemplatePath(t *testing.T) {
   347  	var b Builder
   348  	config := testConfig()
   349  
   350  	// Test bad
   351  	config["vmx_template_path"] = "/i/dont/exist/forreal"
   352  	warns, err := b.Prepare(config)
   353  	if len(warns) > 0 {
   354  		t.Fatalf("bad: %#v", warns)
   355  	}
   356  	if err == nil {
   357  		t.Fatal("should have error")
   358  	}
   359  
   360  	// Test good
   361  	tf, err := ioutil.TempFile("", "packer")
   362  	if err != nil {
   363  		t.Fatalf("err: %s", err)
   364  	}
   365  	defer os.Remove(tf.Name())
   366  	defer tf.Close()
   367  
   368  	if _, err := tf.Write([]byte("HELLO!")); err != nil {
   369  		t.Fatalf("err: %s", err)
   370  	}
   371  
   372  	config["vmx_template_path"] = tf.Name()
   373  	b = Builder{}
   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("should not have error: %s", err)
   380  	}
   381  
   382  	// Bad template
   383  	tf2, err := ioutil.TempFile("", "packer")
   384  	if err != nil {
   385  		t.Fatalf("err: %s", err)
   386  	}
   387  	defer os.Remove(tf2.Name())
   388  	defer tf2.Close()
   389  
   390  	if _, err := tf2.Write([]byte("{{foo}")); err != nil {
   391  		t.Fatalf("err: %s", err)
   392  	}
   393  
   394  	config["vmx_template_path"] = tf2.Name()
   395  	b = Builder{}
   396  	warns, err = b.Prepare(config)
   397  	if len(warns) > 0 {
   398  		t.Fatalf("bad: %#v", warns)
   399  	}
   400  	if err == nil {
   401  		t.Fatal("should have error")
   402  	}
   403  }
   404  
   405  func TestBuilderPrepare_VNCPort(t *testing.T) {
   406  	var b Builder
   407  	config := testConfig()
   408  
   409  	// Bad
   410  	config["vnc_port_min"] = 1000
   411  	config["vnc_port_max"] = 500
   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  	// Bad
   421  	config["vnc_port_min"] = -500
   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.Fatal("should have error")
   429  	}
   430  
   431  	// Good
   432  	config["vnc_port_min"] = 500
   433  	config["vnc_port_max"] = 1000
   434  	b = Builder{}
   435  	warns, err = b.Prepare(config)
   436  	if len(warns) > 0 {
   437  		t.Fatalf("bad: %#v", warns)
   438  	}
   439  	if err != nil {
   440  		t.Fatalf("should not have error: %s", err)
   441  	}
   442  }
   443  
   444  func TestBuilderPrepare_CommConfig(t *testing.T) {
   445  	// Test Winrm
   446  	{
   447  		config := testConfig()
   448  		config["communicator"] = "winrm"
   449  		config["winrm_username"] = "username"
   450  		config["winrm_password"] = "password"
   451  		config["winrm_host"] = "1.2.3.4"
   452  
   453  		var b Builder
   454  		warns, err := b.Prepare(config)
   455  		if len(warns) > 0 {
   456  			t.Fatalf("bad: %#v", warns)
   457  		}
   458  		if err != nil {
   459  			t.Fatalf("should not have error: %s", err)
   460  		}
   461  
   462  		if b.config.CommConfig.WinRMUser != "username" {
   463  			t.Errorf("bad winrm_username: %s", b.config.CommConfig.WinRMUser)
   464  		}
   465  		if b.config.CommConfig.WinRMPassword != "password" {
   466  			t.Errorf("bad winrm_password: %s", b.config.CommConfig.WinRMPassword)
   467  		}
   468  		if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
   469  			t.Errorf("bad host: %s", host)
   470  		}
   471  	}
   472  
   473  	// Test SSH
   474  	{
   475  		config := testConfig()
   476  		config["communicator"] = "ssh"
   477  		config["ssh_username"] = "username"
   478  		config["ssh_password"] = "password"
   479  		config["ssh_host"] = "1.2.3.4"
   480  
   481  		var b Builder
   482  		warns, err := b.Prepare(config)
   483  		if len(warns) > 0 {
   484  			t.Fatalf("bad: %#v", warns)
   485  		}
   486  		if err != nil {
   487  			t.Fatalf("should not have error: %s", err)
   488  		}
   489  
   490  		if b.config.CommConfig.SSHUsername != "username" {
   491  			t.Errorf("bad ssh_username: %s", b.config.CommConfig.SSHUsername)
   492  		}
   493  		if b.config.CommConfig.SSHPassword != "password" {
   494  			t.Errorf("bad ssh_password: %s", b.config.CommConfig.SSHPassword)
   495  		}
   496  		if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
   497  			t.Errorf("bad host: %s", host)
   498  		}
   499  	}
   500  }