Skip to content

Commit

Permalink
test: Add source_file to incus_instance
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Bremgartner <[email protected]>
  • Loading branch information
breml committed Nov 21, 2024
1 parent 636ecbe commit 6f6050e
Showing 1 changed file with 133 additions and 1 deletion.
134 changes: 133 additions & 1 deletion internal/instance/resource_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package instance_test

import (
"fmt"
"path/filepath"
"regexp"
"testing"

"github.com/dustinkirkland/golang-petname"
petname "github.com/dustinkirkland/golang-petname"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/lxc/terraform-provider-incus/internal/acctest"
Expand Down Expand Up @@ -857,6 +858,90 @@ func TestAccInstance_sourceInstanceWithSnapshot(t *testing.T) {
})
}

func TestAccInstance_sourceFile(t *testing.T) {
tmpDir := t.TempDir()
backupFile := filepath.Join(tmpDir, "backup.tar.gz")

sourceInstanceName := petname.Generate(2, "-")
instanceName := petname.Generate(2, "-")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"null": {
Source: "null",
VersionConstraint: ">= 3.0.0",
},
},
Steps: []resource.TestStep{
{
Config: testAccInstance_sourceFileExportInstance(sourceInstanceName, backupFile),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("incus_instance.instance1", "name", sourceInstanceName),
resource.TestCheckResourceAttr("incus_instance.instance1", "image", acctest.TestImage),
resource.TestCheckResourceAttr("incus_instance.instance1", "status", "Stopped"),
),
},
{
Config: `#`, // Empty config to remove instance. Comment is required, since empty string is seen as zero value.
},
{
Config: testAccInstance_sourceFile(instanceName, backupFile),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("incus_instance.instance1", "name", instanceName),
resource.TestCheckResourceAttr("incus_instance.instance1", "source_file", backupFile),
resource.TestCheckResourceAttr("incus_instance.instance1", "status", "Stopped"),
),
},
},
})
}

func TestAccInstance_sourceFileWithStorage(t *testing.T) {
tmpDir := t.TempDir()
backupFile := filepath.Join(tmpDir, "backup.tar.gz")

sourceInstanceName := petname.Generate(2, "-")
instanceName := petname.Generate(2, "-")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"null": {
Source: "null",
VersionConstraint: ">= 3.0.0",
},
},
Steps: []resource.TestStep{
{
Config: testAccInstance_sourceFileExportInstance(sourceInstanceName, backupFile),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("incus_instance.instance1", "name", sourceInstanceName),
resource.TestCheckResourceAttr("incus_instance.instance1", "image", acctest.TestImage),
resource.TestCheckResourceAttr("incus_instance.instance1", "status", "Stopped"),
),
},
{
Config: `#`, // Empty config to remove instance. Comment is required, since empty string is seen as zero value.
},
{
Config: testAccInstance_sourceFileWithStorage(instanceName, backupFile),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("incus_instance.instance1", "name", instanceName),
resource.TestCheckResourceAttr("incus_instance.instance1", "source_file", backupFile),
resource.TestCheckResourceAttr("incus_instance.instance1", "status", "Running"),
resource.TestCheckResourceAttr("incus_instance.instance1", "device.#", "1"),
resource.TestCheckResourceAttr("incus_instance.instance1", "device.0.name", "storage"),
resource.TestCheckResourceAttr("incus_instance.instance1", "device.0.type", "disk"),
resource.TestCheckResourceAttr("incus_instance.instance1", "device.0.properties.pool", "default"),
),
},
},
})
}

func testAccInstance_basic(name string, image string) string {
return fmt.Sprintf(`
resource "incus_instance" "instance1" {
Expand Down Expand Up @@ -1479,3 +1564,50 @@ resource "incus_instance" "instance2" {
}
`, projectName, sourceInstanceName, snapshotName, instanceName, acctest.TestImage)
}

func testAccInstance_sourceFileExportInstance(sourceInstanceName, backupFile string) string {
return fmt.Sprintf(`
resource "incus_instance" "instance1" {
name = "%[1]s"
image = "%[2]s"
running = false
}
resource "null_resource" "export_instance1" {
provisioner "local-exec" {
command = "incus export ${incus_instance.instance1.name} %[3]s"
}
}
`, sourceInstanceName, acctest.TestImage, backupFile)
}

func testAccInstance_sourceFile(instanceName, backupFile string) string {
return fmt.Sprintf(`
resource "incus_instance" "instance1" {
name = "%[1]s"
source_file = "%[2]s"
running = false
}
`, instanceName, backupFile)
}

func testAccInstance_sourceFileWithStorage(instanceName, backupFile string) string {
return fmt.Sprintf(`
resource "incus_instance" "instance1" {
name = "%[1]s"
source_file = "%[2]s"
device {
name = "storage"
type = "disk"
properties = {
"pool" = "default"
}
}
running = true
}
`, instanceName, backupFile)
}

0 comments on commit 6f6050e

Please sign in to comment.