From d33ffc178d80b860d8f38dfa5c597a76e65f9af9 Mon Sep 17 00:00:00 2001 From: Andrey Tonkikh Date: Thu, 26 Oct 2017 00:51:54 +0300 Subject: [PATCH] Add an acceptance test for `driver.ResourcePool` --- builder_acc_test.go | 43 ++++++++++-------------- driver/testing/config.go | 27 ++++++++------- driver/testing/datastore_acc_test.go | 8 ++--- driver/testing/resource_pool_acc_test.go | 14 ++++++++ driver/testing/utility.go | 18 ++++++++-- driver/testing/vm_acc_test.go | 20 +++++------ 6 files changed, 74 insertions(+), 56 deletions(-) create mode 100644 driver/testing/resource_pool_acc_test.go diff --git a/builder_acc_test.go b/builder_acc_test.go index b33de8d95..0d7e77f6c 100644 --- a/builder_acc_test.go +++ b/builder_acc_test.go @@ -17,7 +17,7 @@ func TestBuilderAcc_default(t *testing.T) { 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) + config["host"].(string), driverT.TestDatastore) return nil }, }) @@ -25,13 +25,13 @@ func TestBuilderAcc_default(t *testing.T) { func defaultConfig() map[string]interface{} { config := map[string]interface{}{ - "vcenter_server": driverT.DefaultVCenterServer, - "username": driverT.DefaultVCenterUsername, - "password": driverT.DefaultVCenterPassword, + "vcenter_server": driverT.TestVCenterServer, + "username": driverT.TestVCenterUsername, + "password": driverT.TestVCenterPassword, "insecure_connection": true, - "template": driverT.DefaultTemplate, - "host": driverT.DefaultHost, + "template": driverT.TestTemplate, + "host": driverT.TestHost, "ssh_username": "root", "ssh_password": "jetbrains", @@ -69,13 +69,13 @@ func TestBuilderAcc_folder(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: folderConfig(), - Check: checkFolder(t, "folder1/folder2"), + Check: checkFolder(t, driverT.TestFolder), }) } func folderConfig() string { config := defaultConfig() - config["folder"] = "folder1/folder2" + config["folder"] = driverT.TestFolder config["linked_clone"] = true // speed up return renderConfig(config) } @@ -107,13 +107,13 @@ func TestBuilderAcc_resourcePool(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: resourcePoolConfig(), - Check: checkResourcePool(t, "pool1/pool2"), + Check: checkResourcePool(t, driverT.TestResourcePool), }) } func resourcePoolConfig() string { config := defaultConfig() - config["resource_pool"] = "pool1/pool2" + config["resource_pool"] = driverT.TestResourcePool config["linked_clone"] = true // speed up return renderConfig(config) } @@ -128,27 +128,18 @@ func checkResourcePool(t *testing.T, pool string) builderT.TestCheckFunc { t.Fatalf("Cannot read VM properties: %v", err) } - p := d.NewResourcePool(vmInfo.ResourcePool) - path, err := p.Path() - if err != nil { - t.Fatalf("Cannot read resource pool name: %v", err) - } - if path != pool { - t.Errorf("Wrong folder. expected: %v, got: %v", pool, path) - } - + driverT.CheckResourcePoolPath(t, d.NewResourcePool(vmInfo.ResourcePool), pool) return nil } } -// FIXME: why do we need this??? Why don't perform these checks in checkDefault? func TestBuilderAcc_datastore(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: datastoreConfig(), Check: func(artifacts []packer.Artifact) error { d := driverT.NewTestDriver(t) - driverT.VMCheckDatastore(t, d, getVM(t, d, artifacts), driverT.DefaultDatastore) + driverT.VMCheckDatastore(t, d, getVM(t, d, artifacts), driverT.TestDatastore) return nil }, }) @@ -221,11 +212,11 @@ func TestBuilderAcc_hardware(t *testing.T) { func hardwareConfig() string { config := defaultConfig() - config["CPUs"] = driverT.DefaultCPUs - config["CPU_reservation"] = driverT.DefaultCPUReservation - config["CPU_limit"] = driverT.DefaultCPULimit - config["RAM"] = driverT.DefaultRAM - config["RAM_reservation"] = driverT.DefaultRAMReservation + config["CPUs"] = driverT.TestCPUs + config["CPU_reservation"] = driverT.TestCPUReservation + config["CPU_limit"] = driverT.TestCPULimit + config["RAM"] = driverT.TestRAM + config["RAM_reservation"] = driverT.TestRAMReservation config["linked_clone"] = true // speed up return renderConfig(config) diff --git a/driver/testing/config.go b/driver/testing/config.go index 947aeb90e..9e085dd6c 100644 --- a/driver/testing/config.go +++ b/driver/testing/config.go @@ -4,16 +4,19 @@ package testing const TestEnvVar = "VSPHERE_DRIVER_ACC" // Describe the environment to run tests in -const DefaultDatastore = "datastore1" -const DefaultTemplate = "alpine" -const DefaultHost = "esxi-1.vsphere55.test" -const DefaultVCenterServer = "vcenter.vsphere55.test" -const DefaultVCenterUsername = "root" -const DefaultVCenterPassword = "jetbrains" +const TestDatastore = "datastore1" +const TestTemplate = "alpine" +const TestHost = "esxi-1.vsphere55.test" +const TestVCenterServer = "vcenter.vsphere55.test" +const TestVCenterUsername = "root" +const TestVCenterPassword = "jetbrains" -// Default hardware settings for -const DefaultCPUs = 2 -const DefaultCPUReservation = 1000 -const DefaultCPULimit = 1500 -const DefaultRAM = 2048 -const DefaultRAMReservation = 1024 +// For test of hardware settings +const TestCPUs = 2 +const TestCPUReservation = 1000 +const TestCPULimit = 1500 +const TestRAM = 2048 +const TestRAMReservation = 1024 + +const TestFolder = "folder1/folder2" +const TestResourcePool = "pool1/pool2" diff --git a/driver/testing/datastore_acc_test.go b/driver/testing/datastore_acc_test.go index ca0bcb726..17eecf1ee 100644 --- a/driver/testing/datastore_acc_test.go +++ b/driver/testing/datastore_acc_test.go @@ -2,17 +2,15 @@ package testing import ( "testing" - "github.com/vmware/govmomi/vim25/mo" - "github.com/jetbrains-infra/packer-builder-vsphere/driver" ) func TestDatastoreAcc(t *testing.T) { initDriverAcceptanceTest(t) d := NewTestDriver(t) - ds, err := d.FindDatastore(DefaultDatastore) + ds, err := d.FindDatastore(TestDatastore) if err != nil { - t.Fatalf("Cannot find default datastore '%v': %v", DefaultDatastore, err) + t.Fatalf("Cannot find the default datastore '%v': %v", TestDatastore, err) } - CheckDatastoreName(t, ds, DefaultDatastore) + CheckDatastoreName(t, ds, TestDatastore) } diff --git a/driver/testing/resource_pool_acc_test.go b/driver/testing/resource_pool_acc_test.go new file mode 100644 index 000000000..7b6177f90 --- /dev/null +++ b/driver/testing/resource_pool_acc_test.go @@ -0,0 +1,14 @@ +package testing + +import "testing" + +func TestResourcePoolAcc(t *testing.T) { + initDriverAcceptanceTest(t) + + d := NewTestDriver(t) + p, err := d.FindResourcePool(TestHost, TestResourcePool) + if err != nil { + t.Fatalf("Cannot find the default resource pool '%v': %v", TestResourcePool, err) + } + CheckResourcePoolPath(t, p, TestResourcePool) +} diff --git a/driver/testing/utility.go b/driver/testing/utility.go index 3c1170ebd..97ea812e6 100644 --- a/driver/testing/utility.go +++ b/driver/testing/utility.go @@ -7,13 +7,14 @@ import ( "os" "testing" "time" + "github.com/vmware/govmomi/govc/pool" ) func NewTestDriver(t *testing.T) *driver.Driver { d, err := driver.NewDriver(&driver.ConnectConfig{ - VCenterServer: DefaultVCenterServer, - Username: DefaultVCenterUsername, - Password: DefaultVCenterPassword, + VCenterServer: TestVCenterServer, + Username: TestVCenterUsername, + Password: TestVCenterPassword, InsecureConnection: true, }) if err != nil { @@ -36,6 +37,15 @@ func CheckDatastoreName(t *testing.T, ds *driver.Datastore, name string) { } } +func CheckResourcePoolPath(t *testing.T, p *driver.ResourcePool, pool string) { + switch path, err := p.Path(); { + case err != nil: + t.Errorf("Cannot read resource pool name: %v", err) + case path != pool: + t.Errorf("Wrong folder. expected: %v, got: %v", pool, path) + } +} + func initDriverAcceptanceTest(t *testing.T) { // We only run acceptance tests if an env var is set because they're // slow and require outside configuration. @@ -168,3 +178,5 @@ func VMCheckSnapshor(t* testing.T, d *driver.Driver, vm *driver.VirtualMachine) 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 b216a7c48..594430ff2 100644 --- a/driver/testing/vm_acc_test.go +++ b/driver/testing/vm_acc_test.go @@ -12,19 +12,19 @@ func initVMAccTest(t *testing.T) (d *driver.Driver, vm *driver.VirtualMachine, v initDriverAcceptanceTest(t) d = NewTestDriver(t) - template, err := d.FindVM(DefaultTemplate) // Don't destroy this VM! + template, err := d.FindVM(TestTemplate) // Don't destroy this VM! if err != nil { - t.Fatalf("Cannot find template vm '%v': %v", DefaultTemplate, err) + t.Fatalf("Cannot find template vm '%v': %v", TestTemplate, err) } log.Printf("[DEBUG] Clonning VM") vmName = NewVMName() vm, err = template.Clone(&driver.CloneConfig{ Name: vmName, - Host: DefaultHost, + Host: TestHost, }) if err != nil { - t.Fatalf("Cannot clone vm '%v': %v", DefaultTemplate, err) + t.Fatalf("Cannot clone vm '%v': %v", TestTemplate, err) } vmDestructor = func() { @@ -53,7 +53,7 @@ func TestVMAcc_default(t *testing.T) { // Run checks log.Printf("[DEBUG] Running check function") - VMCheckDefault(t, d, vm, vmName, DefaultHost, DefaultDatastore) + VMCheckDefault(t, d, vm, vmName, TestHost, TestDatastore) } func TestVMAcc_hardware(t *testing.T) { @@ -62,11 +62,11 @@ func TestVMAcc_hardware(t *testing.T) { log.Printf("[DEBUG] Configuring the vm") vm.Configure(&driver.HardwareConfig{ - CPUs: DefaultCPUs, - CPUReservation: DefaultCPUReservation, - CPULimit: DefaultCPULimit, - RAM: DefaultRAM, - RAMReservation: DefaultRAMReservation, + CPUs: TestCPUs, + CPUReservation: TestCPUReservation, + CPULimit: TestCPULimit, + RAM: TestRAM, + RAMReservation: TestRAMReservation, }) log.Printf("[DEBUG] Running check function") VMCheckHardware(t, d, vm)