From b111f08697a6fe26d928099d6f5a62fc6fb8fef7 Mon Sep 17 00:00:00 2001 From: Yannick Struyf Date: Tue, 17 Mar 2020 12:07:17 +0100 Subject: [PATCH 1/2] Added tests and code for power_state_mechanism_bug --- nutanix/resource_nutanix_virtual_machine.go | 21 +++++++++ .../resource_nutanix_virtual_machine_test.go | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/nutanix/resource_nutanix_virtual_machine.go b/nutanix/resource_nutanix_virtual_machine.go index 3b1de6a03..bcd1ad377 100644 --- a/nutanix/resource_nutanix_virtual_machine.go +++ b/nutanix/resource_nutanix_virtual_machine.go @@ -1182,9 +1182,14 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{ } if d.HasChange("disk_list") { + preCdromCount, err := CountDiskListCdrom(res.DiskList) if res.DiskList, err = expandDiskList(d, false); err != nil { return err } + postCdromCount, err := CountDiskListCdrom(res.DiskList) + if preCdromCount != postCdromCount { + hotPlugChange = false + } } if d.HasChange("serial_port_list") { @@ -1546,6 +1551,10 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error { vm.VgaConsoleEnabled = utils.BoolPtr(v.(bool)) } if v, ok := d.GetOk("power_state_mechanism"); ok { + if vm.PowerStateMechanism == nil { + log.Printf("m.PowerStateMechanism was nil, setting correct value!") + vm.PowerStateMechanism = &v3.VMPowerStateMechanism{} + } vm.PowerStateMechanism.Mechanism = utils.StringPtr(v.(string)) } if v, ok := d.GetOk("should_fail_on_script_failure"); ok { @@ -1913,3 +1922,15 @@ func waitForIPRefreshFunc(client *v3.Client, vmUUID string) resource.StateRefres return resp, WAITING, nil } } + +func CountDiskListCdrom(dl []*v3.VMDisk) (int, error) { + log.Printf("=====") + counter := 0 + for _, v := range dl { + if *v.DeviceProperties.DeviceType == "CDROM" { + counter++ + } + } + log.Printf("=====") + return counter, nil +} diff --git a/nutanix/resource_nutanix_virtual_machine_test.go b/nutanix/resource_nutanix_virtual_machine_test.go index acc6954d3..fbb966366 100644 --- a/nutanix/resource_nutanix_virtual_machine_test.go +++ b/nutanix/resource_nutanix_virtual_machine_test.go @@ -185,6 +185,31 @@ func TestAccNutanixVirtualMachine_WithSerialPortList(t *testing.T) { }) } +func TestAccNutanixVirtualMachine_PowerStateMechanism(t *testing.T) { + r := acctest.RandInt() + resourceName := "nutanix_virtual_machine.vm6" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNutanixVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: testAccNutanixVMConfigPowerStateMechanism(r), + Check: resource.ComposeTestCheckFunc( + testAccCheckNutanixVirtualMachineExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "power_state_mechanism", "ACPI"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"disk_list"}, + }, + }, + }) +} + func testAccCheckNutanixVirtualMachineExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -531,3 +556,24 @@ resource "nutanix_virtual_machine" "vm5" { } `, r) } + +func testAccNutanixVMConfigPowerStateMechanism(r int) string { + return fmt.Sprintf(` +data "nutanix_clusters" "clusters" {} + +locals { + cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL" + ? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}" +} + +resource "nutanix_virtual_machine" "vm6" { + name = "test-dou-%d" + cluster_uuid = "${local.cluster1}" + + num_vcpus_per_socket = 1 + num_sockets = 1 + memory_size_mib = 186 + power_state_mechanism = "ACPI" +} +`, r) +} From 6671496c0fb15ccb6b7909e0057cb5aa0e2f4b80 Mon Sep 17 00:00:00 2001 From: Yannick Struyf Date: Tue, 17 Mar 2020 13:53:56 +0100 Subject: [PATCH 2/2] Added test to check reboots in case of guest customisation is used (remove CDrom) --- nutanix/resource_nutanix_virtual_machine.go | 6 +++ .../resource_nutanix_virtual_machine_test.go | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/nutanix/resource_nutanix_virtual_machine.go b/nutanix/resource_nutanix_virtual_machine.go index bcd1ad377..ed434440d 100644 --- a/nutanix/resource_nutanix_virtual_machine.go +++ b/nutanix/resource_nutanix_virtual_machine.go @@ -1183,10 +1183,16 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{ if d.HasChange("disk_list") { preCdromCount, err := CountDiskListCdrom(res.DiskList) + if err != nil { + return err + } if res.DiskList, err = expandDiskList(d, false); err != nil { return err } postCdromCount, err := CountDiskListCdrom(res.DiskList) + if err != nil { + return err + } if preCdromCount != postCdromCount { hotPlugChange = false } diff --git a/nutanix/resource_nutanix_virtual_machine_test.go b/nutanix/resource_nutanix_virtual_machine_test.go index fbb966366..291d18c84 100644 --- a/nutanix/resource_nutanix_virtual_machine_test.go +++ b/nutanix/resource_nutanix_virtual_machine_test.go @@ -210,6 +210,37 @@ func TestAccNutanixVirtualMachine_PowerStateMechanism(t *testing.T) { }) } +func TestAccNutanixVirtualMachine_CdromGuestCustomisationReboot(t *testing.T) { + r := acctest.RandInt() + resourceName := "nutanix_virtual_machine.vm7" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNutanixVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: testAccNutanixVMConfigCdromGuestCustomisationReboot(r), + Check: resource.ComposeTestCheckFunc( + testAccCheckNutanixVirtualMachineExists(resourceName), + ), + ExpectNonEmptyPlan: true, + }, + { + Config: testAccNutanixVMConfigCdromGuestCustomisationReboot(r), + Check: resource.ComposeTestCheckFunc( + testAccCheckNutanixVirtualMachineExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"disk_list"}, + }, + }, + }) +} + func testAccCheckNutanixVirtualMachineExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -577,3 +608,24 @@ resource "nutanix_virtual_machine" "vm6" { } `, r) } + +func testAccNutanixVMConfigCdromGuestCustomisationReboot(r int) string { + return fmt.Sprintf(` +data "nutanix_clusters" "clusters" {} + +locals { + cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL" + ? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}" +} + +resource "nutanix_virtual_machine" "vm7" { + name = "test-dou-%d" + cluster_uuid = "${local.cluster1}" + + num_vcpus_per_socket = 1 + num_sockets = 1 + memory_size_mib = 186 + guest_customization_cloud_init_user_data = base64encode("#cloud-config\nfqdn: test.domain.local") +} +`, r) +}