From fd6cc544b9d731a63d79f44b467f6d243d044be2 Mon Sep 17 00:00:00 2001 From: Din Music Date: Fri, 20 Sep 2024 09:53:00 +0000 Subject: [PATCH] instance: Add test for instance rename Signed-off-by: Din Music --- internal/instance/resource_instance_test.go | 59 +++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/internal/instance/resource_instance_test.go b/internal/instance/resource_instance_test.go index 0b9452f6..ad8dcb15 100644 --- a/internal/instance/resource_instance_test.go +++ b/internal/instance/resource_instance_test.go @@ -216,6 +216,55 @@ func TestAccInstance_restartVirtualMachine(t *testing.T) { }) } +func TestAccInstance_renameInstance(t *testing.T) { + instanceNameA := acctest.GenerateName(3, "-") + instanceNameB := acctest.GenerateName(3, "-") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + // Launch a new instance. + Config: testAccInstance_rename(instanceNameA, true), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameA), + resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Running"), + ), + }, + { + // Stop and rename the instance. + Config: testAccInstance_rename(instanceNameB, false), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameB), + resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Stopped"), + ), + }, + { + // Rename the instance while stopped. + Config: testAccInstance_rename(instanceNameA, false), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameA), + resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Stopped"), + ), + }, + { + // Rename and start the instance. + Config: testAccInstance_rename(instanceNameB, true), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameB), + resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Running"), + ), + }, + { + // Ensure instance rename fails when instance is running. + Config: testAccInstance_rename(instanceNameA, true), + ExpectError: regexp.MustCompile("Renaming of running instance not allowed"), + }, + }, + }) +} + func TestAccInstance_remoteImage(t *testing.T) { instanceName := acctest.GenerateName(2, "-") @@ -1884,6 +1933,16 @@ resource "lxd_instance" "instance1" { `, instanceName, acctest.TestImage) } +func testAccInstance_rename(name string, running bool) string { + return fmt.Sprintf(` +resource "lxd_instance" "instance1" { + name = "%s" + running = %v + image = "%s" +} + `, name, running, acctest.TestImage) +} + func testAccInstance_remoteImage(name string) string { return fmt.Sprintf(` resource "lxd_instance" "instance1" {