From 1539688cb8ddce794c30fbf119fd92e0fcd87aa7 Mon Sep 17 00:00:00 2001 From: Cheng Sun Date: Fri, 2 Jan 2015 16:48:12 +0000 Subject: [PATCH] Update tests --- builder/xenserver/iso/builder_test.go | 24 +-- builder/xenserver/xva/builder.go | 2 - builder/xenserver/xva/builder_test.go | 250 ++++---------------------- 3 files changed, 51 insertions(+), 225 deletions(-) diff --git a/builder/xenserver/iso/builder_test.go b/builder/xenserver/iso/builder_test.go index 960b6e6..1a7a19b 100644 --- a/builder/xenserver/iso/builder_test.go +++ b/builder/xenserver/iso/builder_test.go @@ -8,9 +8,9 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ - "host_ip": "localhost", - "username": "admin", - "password": "admin", + "remote_host": "localhost", + "remote_username": "admin", + "remote_password": "admin", "vm_name": "foo", "iso_checksum": "foo", "iso_checksum_type": "md5", @@ -53,12 +53,12 @@ func TestBuilderPrepare_Defaults(t *testing.T) { t.Errorf("bad vm name: %s", b.config.VMName) } - if b.config.ExportFormat != "xva" { - t.Errorf("bad format: %s", b.config.ExportFormat) + if b.config.Format != "xva" { + t.Errorf("bad format: %s", b.config.Format) } - if b.config.KeepInstance != "never" { - t.Errorf("bad keep instance: %s", b.config.KeepInstance) + if b.config.KeepVM != "never" { + t.Errorf("bad keep instance: %s", b.config.KeepVM) } } @@ -99,7 +99,7 @@ func TestBuilderPrepare_Format(t *testing.T) { config := testConfig() // Bad - config["export_format"] = "foo" + config["format"] = "foo" warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) @@ -109,7 +109,7 @@ func TestBuilderPrepare_Format(t *testing.T) { } // Good - config["export_format"] = "vdi_raw" + config["format"] = "vdi_raw" b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { @@ -333,12 +333,12 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { } } -func TestBuilderPrepare_KeepInstance(t *testing.T) { +func TestBuilderPrepare_KeepVM(t *testing.T) { var b Builder config := testConfig() // Bad - config["keep_instance"] = "foo" + config["keep_vm"] = "foo" warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) @@ -348,7 +348,7 @@ func TestBuilderPrepare_KeepInstance(t *testing.T) { } // Good - config["keep_instance"] = "always" + config["keep_vm"] = "always" b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { diff --git a/builder/xenserver/xva/builder.go b/builder/xenserver/xva/builder.go index 1787c5d..9c4cbf1 100644 --- a/builder/xenserver/xva/builder.go +++ b/builder/xenserver/xva/builder.go @@ -17,7 +17,6 @@ type config struct { SourcePath string `mapstructure:"source_path"` VMMemory uint `mapstructure:"vm_memory"` - CloneTemplate string `mapstructure:"clone_template"` PlatformArgs map[string]string `mapstructure:"platform_args"` @@ -67,7 +66,6 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error templates := map[string]*string{ "source_path": &self.config.SourcePath, - "clone_template": &self.config.CloneTemplate, "network_name": &self.config.NetworkName, } diff --git a/builder/xenserver/xva/builder_test.go b/builder/xenserver/xva/builder_test.go index 5fbef57..f223767 100644 --- a/builder/xenserver/xva/builder_test.go +++ b/builder/xenserver/xva/builder_test.go @@ -2,21 +2,18 @@ package xva import ( "github.com/mitchellh/packer/packer" - "reflect" "testing" ) func testConfig() map[string]interface{} { return map[string]interface{}{ - "host_ip": "localhost", - "username": "admin", - "password": "admin", + "remote_host": "localhost", + "remote_username": "admin", + "remote_password": "admin", "vm_name": "foo", - "iso_checksum": "foo", - "iso_checksum_type": "md5", - "iso_url": "http://www.google.com/", "shutdown_command": "yes", "ssh_username": "foo", + "source_path": ".", packer.BuildNameConfigKey: "foo", } @@ -45,52 +42,16 @@ func TestBuilderPrepare_Defaults(t *testing.T) { t.Errorf("bad tools ISO name: %s", b.config.ToolsIsoName) } - if b.config.CloneTemplate != "Other install media" { - t.Errorf("bad clone template: %s", b.config.CloneTemplate) - } - if b.config.VMName == "" { t.Errorf("bad vm name: %s", b.config.VMName) } - if b.config.ExportFormat != "xva" { - t.Errorf("bad format: %s", b.config.ExportFormat) + if b.config.Format != "xva" { + t.Errorf("bad format: %s", b.config.Format) } - if b.config.KeepInstance != "never" { - t.Errorf("bad keep instance: %s", b.config.KeepInstance) - } -} - -func TestBuilderPrepare_DiskSize(t *testing.T) { - var b Builder - config := testConfig() - - delete(config, "disk_size") - warns, err := b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Fatalf("bad err: %s", err) - } - - if b.config.DiskSize != 40000 { - t.Fatalf("bad size: %d", b.config.DiskSize) - } - - config["disk_size"] = 60000 - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Fatalf("should not have error: %s", err) - } - - if b.config.DiskSize != 60000 { - t.Fatalf("bad size: %s", b.config.DiskSize) + if b.config.KeepVM != "never" { + t.Errorf("bad keep instance: %s", b.config.KeepVM) } } @@ -99,7 +60,7 @@ func TestBuilderPrepare_Format(t *testing.T) { config := testConfig() // Bad - config["export_format"] = "foo" + config["format"] = "foo" warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) @@ -109,7 +70,7 @@ func TestBuilderPrepare_Format(t *testing.T) { } // Good - config["export_format"] = "vdi_raw" + config["format"] = "vdi_raw" b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { @@ -174,171 +135,12 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { } } -func TestBuilderPrepare_ISOChecksum(t *testing.T) { - var b Builder - config := testConfig() - - // Test bad - config["iso_checksum"] = "" - warns, err := b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err == nil { - t.Fatal("should have error") - } - - // Test good - config["iso_checksum"] = "FOo" - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Fatalf("should not have error: %s", err) - } - - if b.config.ISOChecksum != "foo" { - t.Fatalf("should've lowercased: %s", b.config.ISOChecksum) - } -} - -func TestBuilderPrepare_ISOChecksumType(t *testing.T) { - var b Builder - config := testConfig() - - // Test bad - config["iso_checksum_type"] = "" - warns, err := b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err == nil { - t.Fatal("should have error") - } - - // Test good - config["iso_checksum_type"] = "mD5" - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Fatalf("should not have error: %s", err) - } - - if b.config.ISOChecksumType != "md5" { - t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType) - } - - // Test unknown - config["iso_checksum_type"] = "fake" - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err == nil { - t.Fatal("should have error") - } - - // Test none - config["iso_checksum_type"] = "none" - b = Builder{} - warns, err = b.Prepare(config) - // @todo: give warning in this case? - /* - if len(warns) == 0 { - t.Fatalf("bad: %#v", warns) - } - */ - if err != nil { - t.Fatalf("should not have error: %s", err) - } - - if b.config.ISOChecksumType != "none" { - t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType) - } -} - -func TestBuilderPrepare_ISOUrl(t *testing.T) { - var b Builder - config := testConfig() - delete(config, "iso_url") - delete(config, "iso_urls") - - // Test both epty - config["iso_url"] = "" - b = Builder{} - warns, err := b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err == nil { - t.Fatal("should have error") - } - - // Test iso_url set - config["iso_url"] = "http://www.packer.io" - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Errorf("should not have error: %s", err) - } - - expected := []string{"http://www.packer.io"} - if !reflect.DeepEqual(b.config.ISOUrls, expected) { - t.Fatalf("bad: %#v", b.config.ISOUrls) - } - - // Test both set - config["iso_url"] = "http://www.packer.io" - config["iso_urls"] = []string{"http://www.packer.io"} - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err == nil { - t.Fatal("should have error") - } - - // Test just iso_urls set - delete(config, "iso_url") - config["iso_urls"] = []string{ - "http://www.packer.io", - "http://www.hashicorp.com", - } - - b = Builder{} - warns, err = b.Prepare(config) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if err != nil { - t.Errorf("should not have error: %s", err) - } - - expected = []string{ - "http://www.packer.io", - "http://www.hashicorp.com", - } - if !reflect.DeepEqual(b.config.ISOUrls, expected) { - t.Fatalf("bad: %#v", b.config.ISOUrls) - } -} - -func TestBuilderPrepare_KeepInstance(t *testing.T) { +func TestBuilderPrepare_KeepVM(t *testing.T) { var b Builder config := testConfig() // Bad - config["keep_instance"] = "foo" + config["keep_vm"] = "foo" warns, err := b.Prepare(config) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) @@ -348,7 +150,33 @@ func TestBuilderPrepare_KeepInstance(t *testing.T) { } // Good - config["keep_instance"] = "always" + config["keep_vm"] = "always" + b = Builder{} + warns, err = b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + +func TestBuilderPrepare_SourcePath(t *testing.T) { + var b Builder + config := testConfig() + + // Bad + config["source_path"] = "" + warns, err := b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err == nil { + t.Fatal("should have error") + } + + // Good + config["source_path"] = "." b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 {