From d154a0ff32aef69332357cfe038c4afd7f0c939f Mon Sep 17 00:00:00 2001 From: Andrey Tonkikh Date: Wed, 25 Oct 2017 23:54:13 +0300 Subject: [PATCH] Improve `TestVMAcc_running_snapshot` and `TestVMAcc_template` --- builder_acc_test.go | 112 +++++++++------------------------- driver/testing/utility.go | 92 ++++++++++++++++++---------- driver/testing/vm_acc_test.go | 13 ++-- 3 files changed, 93 insertions(+), 124 deletions(-) diff --git a/builder_acc_test.go b/builder_acc_test.go index ab51d4ade..b33de8d95 100644 --- a/builder_acc_test.go +++ b/builder_acc_test.go @@ -1,12 +1,12 @@ package main import ( - "testing" + "encoding/json" builderT "github.com/hashicorp/packer/helper/builder/testing" "github.com/hashicorp/packer/packer" - "encoding/json" "github.com/jetbrains-infra/packer-builder-vsphere/driver" driverT "github.com/jetbrains-infra/packer-builder-vsphere/driver/testing" + "testing" ) func TestBuilderAcc_default(t *testing.T) { @@ -14,7 +14,12 @@ func TestBuilderAcc_default(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: renderConfig(config), - Check: checkDefault(t, config["vm_name"].(string), config["host"].(string), "datastore1"), + Check: func(artifacts []packer.Artifact) error { + d := driverT.NewTestDriver(t) + driverT.VMCheckDefault(t, d, getVM(t, d, artifacts), config["vm_name"].(string), + config["host"].(string), driverT.DefaultDatastore) + return nil + }, }) } @@ -35,14 +40,6 @@ func defaultConfig() map[string]interface{} { return config } -func checkDefault(t *testing.T, name string, host string, datastore string) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := driverT.NewTestDriver(t) - vm := getVM(t, d, artifacts) - return driverT.VMCheckDefault(t, d, vm, name, host, datastore) - } -} - func TestBuilderAcc_artifact(t *testing.T) { config := defaultConfig() builderT.Test(t, builderT.TestCase{ @@ -149,7 +146,11 @@ func TestBuilderAcc_datastore(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: datastoreConfig(), - Check: checkDatastore(t, "datastore1"), // on esxi-1.vsphere55.test + Check: func(artifacts []packer.Artifact) error { + d := driverT.NewTestDriver(t) + driverT.VMCheckDatastore(t, d, getVM(t, d, artifacts), driverT.DefaultDatastore) + return nil + }, }) } @@ -159,30 +160,8 @@ func datastoreConfig() string { return renderConfig(config) } -func checkDatastore(t *testing.T, name string) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := driverT.NewTestDriver(t) - vm := getVM(t, d, artifacts) - - vmInfo, err := vm.Info("datastore") - if err != nil { - t.Fatalf("Cannot read VM properties: %v", err) - } - - n := len(vmInfo.Datastore) - if n != 1 { - t.Fatalf("VM should have 1 datastore, got %v", n) - } - - ds := d.NewDatastore(&vmInfo.Datastore[0]) - driverT.CheckDatastoreName(t, ds, name) - - return nil - } -} - func TestBuilderAcc_multipleDatastores(t *testing.T) { - t.Skip("test must fail") // FIXME + t.Skip("test must fail") builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, @@ -232,7 +211,11 @@ func TestBuilderAcc_hardware(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: hardwareConfig(), - Check: checkHardware(t), + Check: func(artifacts []packer.Artifact) error { + d := driverT.NewTestDriver(t) + driverT.VMCheckHardware(t, d, getVM(t, d, artifacts)) + return nil + }, }) } @@ -248,14 +231,6 @@ func hardwareConfig() string { return renderConfig(config) } -func checkHardware(t *testing.T) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := driverT.NewTestDriver(t) - vm := getVM(t, d, artifacts) - return driverT.VMCheckHardware(t, d, vm) - } -} - func TestBuilderAcc_RAMReservation(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, @@ -309,7 +284,11 @@ func TestBuilderAcc_snapshot(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: snapshotConfig(), - Check: checkSnapshot(t), + Check: func(artifacts []packer.Artifact) error { + d := driverT.NewTestDriver(t) + driverT.VMCheckSnapshor(t, d, getVM(t, d, artifacts)) + return nil + }, }) } @@ -319,30 +298,15 @@ func snapshotConfig() string { return renderConfig(config) } -func checkSnapshot(t *testing.T) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := driverT.NewTestDriver(t) - - vm := getVM(t, d, artifacts) - vmInfo, err := vm.Info("layoutEx.disk") - if err != nil { - t.Fatalf("Cannot read VM properties: %v", err) - } - - layers := len(vmInfo.LayoutEx.Disk[0].Chain) - if layers != 2 { - t.Errorf("VM should have a single snapshot. expected 2 disk layers, got %v", layers) - } - - return nil - } -} - func TestBuilderAcc_template(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: templateConfig(), - Check: checkTemplate(t), + Check: func(artifacts []packer.Artifact) error { + d := driverT.NewTestDriver(t) + driverT.VMCheckTemplate(t, d, getVM(t, d, artifacts)) + return nil + }, }) } @@ -353,24 +317,6 @@ func templateConfig() string { return renderConfig(config) } -func checkTemplate(t *testing.T) builderT.TestCheckFunc { - return func(artifacts []packer.Artifact) error { - d := driverT.NewTestDriver(t) - - vm := getVM(t, d, artifacts) - vmInfo, err := vm.Info("config.template") - if err != nil { - t.Fatalf("Cannot read VM properties: %v", err) - } - - if vmInfo.Config.Template != true { - t.Error("Not a template") - } - - return nil - } -} - func renderConfig(config map[string]interface{}) string { t := map[string][]map[string]interface{}{ "builders": { diff --git a/driver/testing/utility.go b/driver/testing/utility.go index 8b2e007e7..3c1170ebd 100644 --- a/driver/testing/utility.go +++ b/driver/testing/utility.go @@ -17,7 +17,7 @@ func NewTestDriver(t *testing.T) *driver.Driver { InsecureConnection: true, }) if err != nil { - t.Fatal("Cannot connect: ", err) + t.Fatalf("Cannot connect: %v", err) } return d } @@ -28,11 +28,10 @@ func NewVMName() string { } func CheckDatastoreName(t *testing.T, ds *driver.Datastore, name string) { - info, err := ds.Info("name") - if err != nil { - t.Fatalf("Cannot read datastore properties: %v", err) - } - if info.Name != name { + switch info, err := ds.Info("name"); { + case err != nil: + t.Errorf("Cannot read datastore properties: %v", err) + case info.Name != name: t.Errorf("Wrong datastore. expected: %v, got: %v", name, info.Name) } } @@ -53,7 +52,7 @@ func initDriverAcceptanceTest(t *testing.T) { } func VMCheckDefault(t *testing.T, d *driver.Driver, vm *driver.VirtualMachine, - name string, host string, datastore string) error { + name string, host string, datastore string) { vmInfo, err := vm.Info("name", "parent", "runtime.host", "resourcePool", "datastore", "layoutEx.disk") if err != nil { t.Fatalf("Cannot read VM properties: %v", err) @@ -64,50 +63,45 @@ func VMCheckDefault(t *testing.T, d *driver.Driver, vm *driver.VirtualMachine, } f := d.NewFolder(vmInfo.Parent) - folderPath, err := f.Path() - if err != nil { - t.Fatalf("Cannot read folder name: %v", err) - } - if folderPath != "" { + switch folderPath, err := f.Path(); { + case err != nil: + t.Errorf("Cannot read folder name: %v", err) + case folderPath != "": t.Errorf("Invalid folder: expected '/', got '%v'", folderPath) } h := d.NewHost(vmInfo.Runtime.Host) - hostInfo, err := h.Info("name") - if err != nil { - t.Fatal("Cannot read host properties: ", err) - } - if hostInfo.Name != host { + switch hostInfo, err := h.Info("name"); { + case err != nil: + t.Errorf("Cannot read host properties: %v", err) + case hostInfo.Name != host: t.Errorf("Invalid host name: expected '%v', got '%v'", host, hostInfo.Name) } p := d.NewResourcePool(vmInfo.ResourcePool) - poolPath, err := p.Path() - if err != nil { - t.Fatalf("Cannot read resource pool name: %v", err) - } - if poolPath != "" { + switch poolPath, err := p.Path(); { + case err != nil: + t.Errorf("Cannot read resource pool name: %v", err) + case poolPath != "": t.Error("Invalid resource pool: expected '/', got '%v'", poolPath) } + dsr := vmInfo.Datastore[0].Reference() ds := d.NewDatastore(&dsr) - dsInfo, err := ds.Info("name") - if err != nil { - t.Fatal("Cannot read datastore properties: ", err) - } - if dsInfo.Name != datastore { + switch dsInfo, err := ds.Info("name"); { + case err != nil: + t.Errorf("Cannot read datastore properties: %v", err) + case dsInfo.Name != datastore: t.Errorf("Invalid datastore name: expected '%v', got '%v'", datastore, dsInfo.Name) } if len(vmInfo.LayoutEx.Disk[0].Chain) != 1 { t.Error("Not a full clone") } - - return nil } -func VMCheckHardware(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) error { +func VMCheckHardware(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) { vmInfo, err := vm.Info("config") if err != nil { t.Fatalf("Cannot read VM properties: %v", err) @@ -137,6 +131,40 @@ func VMCheckHardware(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) if ramReservation != 1024 { t.Errorf("VM should have RAM reservation for 1024 MB, got %v", ramReservation) } - - return nil +} + +func VMCheckTemplate(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) { + switch vmInfo, err := vm.Info("config.template"); { + case err != nil: + t.Errorf("Cannot read VM properties: %v", err) + case !vmInfo.Config.Template: + t.Error("Not a template") + } +} + +func VMCheckDatastore(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine, name string) { + vmInfo, err := vm.Info("datastore") + if err != nil { + t.Fatalf("Cannot read VM properties: %v", err) + } + + n := len(vmInfo.Datastore) + if n != 1 { + t.Fatalf("VM should have 1 datastore, got %v", n) + } + + ds := d.NewDatastore(&vmInfo.Datastore[0]) + CheckDatastoreName(t, ds, name) +} + +func VMCheckSnapshor(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) { + vmInfo, err := vm.Info("layoutEx.disk") + if err != nil { + t.Fatalf("Cannot read VM properties: %v", err) + } + + layers := len(vmInfo.LayoutEx.Disk[0].Chain) + if layers != 2 { + t.Errorf("VM should have a single snapshot. expected 2 disk layers, got %v", layers) + } } diff --git a/driver/testing/vm_acc_test.go b/driver/testing/vm_acc_test.go index 7f93ce59d..b216a7c48 100644 --- a/driver/testing/vm_acc_test.go +++ b/driver/testing/vm_acc_test.go @@ -107,25 +107,20 @@ func TestVMAcc_running(t *testing.T) { } func TestVMAcc_running_snapshot(t *testing.T) { - _ /*d*/, vm, vmName, vmDestructor := initVMAccTest(t) + d, vm, vmName, vmDestructor := initVMAccTest(t) defer vmDestructor() stopper := startVM(t, vm, vmName) defer stopper() vm.CreateSnapshot("test-snapshot") - // TODO: check + VMCheckSnapshor(t, d, vm) } func TestVMAcc_template(t *testing.T) { - _ /*d*/, vm, vmName, vmDestructor := initVMAccTest(t) + d, vm, _ /*vmName*/, vmDestructor := initVMAccTest(t) defer vmDestructor() vm.ConvertToTemplate() - switch info, err := vm.Info("config"); { - case err != nil: - t.Fatalf("Cannot read VM properties for '%v': %v", vmName, err) - case !info.Config.Template: - t.Fatalf("'%v' seems not to be a template", vmName) - } + VMCheckTemplate(t, d, vm) }