Skip to content

Commit

Permalink
instance: Add test for instance rename
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Sep 20, 2024
1 parent 2713cd0 commit 4e5f92b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions internal/instance/resource_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,62 @@ func TestAccInstance_restartVirtualMachine(t *testing.T) {
})
}

func TestAccInstance_renameInstance(t *testing.T) {
instanceNameA := acctest.GenerateName(3, "-")
instanceNameB := acctest.GenerateName(3, "-")
instanceNameC := acctest.GenerateName(3, "-")
instanceNameD := acctest.GenerateName(3, "-")
instanceNameE := 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(instanceNameC, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameC),
resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Stopped"),
),
},
{
// Rename and start the instance.
Config: testAccInstance_rename(instanceNameD, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameD),
resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Running"),
),
},
{
// Ensure instance rename fails when instance is running.
Config: testAccInstance_rename(instanceNameE, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_instance.instance1", "name", instanceNameD), // Ensure name is unchanged.
resource.TestCheckResourceAttr("lxd_instance.instance1", "status", "Running"),
),
ExpectError: regexp.MustCompile("Renaming of running instance not allowed"),
},
},
})
}

func TestAccInstance_remoteImage(t *testing.T) {
instanceName := acctest.GenerateName(2, "-")

Expand Down Expand Up @@ -1884,6 +1940,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" {
Expand Down

0 comments on commit 4e5f92b

Please sign in to comment.