From 31b6812ce281da214261786f633ed7e93c197fb0 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Sat, 6 Apr 2024 08:27:55 -0400 Subject: [PATCH] fix(vm): regression: provider always tries to update `memory.*hugepages` even if it is not specified (#1188) Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- fwprovider/tests/resource_vm_test.go | 64 ++++++++++++++++++++++++---- proxmoxtf/resource/vm/vm.go | 20 ++++++--- testacc | 3 ++ 3 files changed, 73 insertions(+), 14 deletions(-) create mode 100755 testacc diff --git a/fwprovider/tests/resource_vm_test.go b/fwprovider/tests/resource_vm_test.go index 0e34d79d..16c1e8b0 100644 --- a/fwprovider/tests/resource_vm_test.go +++ b/fwprovider/tests/resource_vm_test.go @@ -35,7 +35,9 @@ func TestAccResourceVM(t *testing.T) { EOT }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm1", "description", "my\ndescription\nvalue"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm1", map[string]string{ + "description": "my\ndescription\nvalue", + }), ), }}}, {"single line description", []resource.TestStep{{ @@ -47,7 +49,9 @@ func TestAccResourceVM(t *testing.T) { description = "my description value" }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm2", "description", "my description value"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm2", map[string]string{ + "description": "my description value", + }), ), }}}, {"no description", []resource.TestStep{{ @@ -59,7 +63,9 @@ func TestAccResourceVM(t *testing.T) { description = "" }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm3", "description", ""), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm3", map[string]string{ + "description": "", + }), ), }}}, { @@ -72,7 +78,9 @@ func TestAccResourceVM(t *testing.T) { protection = true }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm4", "protection", "true"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ + "protection": "true", + }), ), }, { Config: ` @@ -83,12 +91,14 @@ func TestAccResourceVM(t *testing.T) { protection = false }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm4", "protection", "false"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ + "protection": "false", + }), ), }}, }, { - "update CPU block", []resource.TestStep{{ + "update cpu block", []resource.TestStep{{ Config: ` resource "proxmox_virtual_environment_vm" "test_vm5" { node_name = "pve" @@ -99,7 +109,9 @@ func TestAccResourceVM(t *testing.T) { } }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm5", "cpu.0.cores", "2"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ + "cpu.0.sockets": "1", + }), ), }, { Config: ` @@ -112,7 +124,42 @@ func TestAccResourceVM(t *testing.T) { } }`, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("proxmox_virtual_environment_vm.test_vm5", "cpu.0.cores", "1"), + testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ + "cpu.0.sockets": "1", + }), + ), + }}, + }, + { + "update memory block", []resource.TestStep{{ + Config: ` + resource "proxmox_virtual_environment_vm" "test_vm6" { + node_name = "pve" + started = false + + memory { + dedicated = 2048 + } + }`, + Check: resource.ComposeTestCheckFunc( + testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ + "memory.0.dedicated": "2048", + }), + ), + }, { + Config: ` + resource "proxmox_virtual_environment_vm" "test_vm6" { + node_name = "pve" + started = false + + memory { + dedicated = 1024 + } + }`, + Check: resource.ComposeTestCheckFunc( + testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ + "memory.0.dedicated": "1024", + }), ), }}, }, @@ -496,6 +543,7 @@ func TestAccResourceVMDisks(t *testing.T) { Check: resource.ComposeTestCheckFunc( // fully cloned disk, does not have any attributes in state resource.TestCheckNoResourceAttr("proxmox_virtual_environment_vm.test_disk3", "disk.0"), + testResourceAttributes("proxmox_virtual_environment_vm.test_disk3", map[string]string{}), ), }, { diff --git a/proxmoxtf/resource/vm/vm.go b/proxmoxtf/resource/vm/vm.go index db4660b0..14c56607 100644 --- a/proxmoxtf/resource/vm/vm.go +++ b/proxmoxtf/resource/vm/vm.go @@ -5013,12 +5013,20 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D } } - if memoryHugepages != "" { - updateBody.Hugepages = &memoryHugepages - updateBody.KeepHugepages = &memoryKeepHugepages - } else { - del = append(del, "hugepages") - del = append(del, "keephugepages") + if d.HasChange(mkMemory + ".0." + mkMemoryHugepages) { + if memoryHugepages != "" { + updateBody.Hugepages = &memoryHugepages + } else { + del = append(del, "hugepages") + } + } + + if d.HasChange(mkMemory + ".0." + mkMemoryKeepHugepages) { + if memoryHugepages != "" { + updateBody.KeepHugepages = &memoryKeepHugepages + } else { + del = append(del, "keephugepages") + } } rebootRequired = true diff --git a/testacc b/testacc new file mode 100755 index 00000000..428bb128 --- /dev/null +++ b/testacc @@ -0,0 +1,3 @@ +#!/bin/sh + +TF_ACC=1 env $(cat testacc.env | xargs) go test -v -timeout 120s -run "$1" github.com/bpg/terraform-provider-proxmox/fwprovider/tests $2