Skip to content

Commit cc4f4fc

Browse files
committed
tests: Add source_file to incus_image
Signed-off-by: Lucas Bremgartner <[email protected]>
1 parent 534483d commit cc4f4fc

File tree

1 file changed

+168
-1
lines changed

1 file changed

+168
-1
lines changed

internal/image/resource_image_test.go

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package image_test
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"regexp"
67
"strings"
78
"testing"
89

9-
"github.com/dustinkirkland/golang-petname"
10+
petname "github.com/dustinkirkland/golang-petname"
1011
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1112

1213
"github.com/lxc/terraform-provider-incus/internal/acctest"
@@ -324,6 +325,93 @@ func TestAccImage_sourceInstanceWithSnapshot(t *testing.T) {
324325
})
325326
}
326327

328+
func TestAccImage_sourceFileSplitImage(t *testing.T) {
329+
tmpDir := t.TempDir()
330+
targetMetadata := filepath.Join(tmpDir, `alpine-edge.img`)
331+
targetData := targetMetadata + ".root"
332+
333+
alias1 := petname.Generate(2, "-")
334+
alias2 := petname.Generate(2, "-")
335+
336+
resource.Test(t, resource.TestCase{
337+
PreCheck: func() {
338+
acctest.PreCheck(t)
339+
acctest.PreCheckAPIExtensions(t, "image_create_aliases")
340+
},
341+
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
342+
ExternalProviders: map[string]resource.ExternalProvider{
343+
"null": {
344+
Source: "null",
345+
VersionConstraint: ">= 3.0.0",
346+
},
347+
},
348+
Steps: []resource.TestStep{
349+
{
350+
Config: testAccSourceFileSplitImage_exportImage(targetMetadata),
351+
Check: resource.ComposeTestCheckFunc(
352+
resource.TestCheckResourceAttr("incus_image.img1", "source_image.remote", "images"),
353+
resource.TestCheckResourceAttr("incus_image.img1", "source_image.name", "alpine/edge"),
354+
resource.TestCheckResourceAttr("incus_image.img1", "source_image.copy_aliases", "true"),
355+
resource.TestCheckResourceAttr("incus_image.img1", "copied_aliases.#", "4"),
356+
resource.TestCheckResourceAttrSet("null_resource.export_img1", "id"),
357+
),
358+
},
359+
{
360+
Config: `#`, // Empty config to remove image. Comment is required, since empty string is seen as zero value.
361+
},
362+
{
363+
Config: testAccSourceFileSplitImage_fromFile(targetData, targetMetadata, alias1, alias2),
364+
Check: resource.ComposeTestCheckFunc(
365+
resource.TestCheckResourceAttr("incus_image.from_file", "source_file.data_path", targetData),
366+
resource.TestCheckResourceAttr("incus_image.from_file", "source_file.metadata_path", targetMetadata),
367+
resource.TestCheckResourceAttr("incus_image.from_file", "aliases.#", "2"),
368+
resource.TestCheckTypeSetElemAttr("incus_image.from_file", "aliases.*", alias1),
369+
resource.TestCheckTypeSetElemAttr("incus_image.from_file", "aliases.*", alias2),
370+
resource.TestCheckResourceAttr("incus_image.from_file", "copied_aliases.#", "0"),
371+
),
372+
},
373+
},
374+
})
375+
}
376+
377+
func TestAccImage_sourceFileUnifiedImage(t *testing.T) {
378+
name := petname.Generate(2, "-")
379+
tmpDir := t.TempDir()
380+
targetData := filepath.Join(tmpDir, name)
381+
382+
resource.Test(t, resource.TestCase{
383+
PreCheck: func() { acctest.PreCheck(t) },
384+
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
385+
ExternalProviders: map[string]resource.ExternalProvider{
386+
"null": {
387+
Source: "null",
388+
VersionConstraint: ">= 3.0.0",
389+
},
390+
},
391+
Steps: []resource.TestStep{
392+
{
393+
Config: testAccSourceFileUnifiedImage_exportImage(name, targetData),
394+
Check: resource.ComposeTestCheckFunc(
395+
resource.TestCheckResourceAttr("incus_instance.instance1", "name", name),
396+
resource.TestCheckResourceAttr("incus_instance.instance1", "image", "images:alpine/edge"),
397+
resource.TestCheckResourceAttr("incus_instance.instance1", "type", "container"),
398+
resource.TestCheckResourceAttr("incus_instance.instance1", "status", "Stopped"),
399+
resource.TestCheckResourceAttrSet("null_resource.publish_instance1", "id"),
400+
resource.TestCheckResourceAttrSet("null_resource.export_instance1_image", "id"),
401+
resource.TestCheckResourceAttrSet("null_resource.delete_instance1_image", "id"),
402+
),
403+
},
404+
{
405+
Config: testAccSourceFileUnifiedImage_fromFile(targetData),
406+
Check: resource.ComposeTestCheckFunc(
407+
resource.TestCheckResourceAttr("incus_image.from_file", "source_file.data_path", targetData+".tar.gz"),
408+
resource.TestCheckResourceAttr("incus_image.from_file", "copied_aliases.#", "0"),
409+
),
410+
},
411+
},
412+
})
413+
}
414+
327415
func testAccImage_basic() string {
328416
return `
329417
resource "incus_image" "img1" {
@@ -572,3 +660,82 @@ resource "incus_image" "img1" {
572660
}
573661
`, projectName, instanceName, acctest.TestImage)
574662
}
663+
664+
func testAccSourceFileSplitImage_exportImage(target string) string {
665+
return fmt.Sprintf(`
666+
resource "incus_image" "img1" {
667+
source_image = {
668+
remote = "images"
669+
name = "alpine/edge"
670+
copy_aliases = true
671+
}
672+
}
673+
674+
resource "null_resource" "export_img1" {
675+
provisioner "local-exec" {
676+
command = "incus image export ${incus_image.img1.fingerprint} %[1]s"
677+
}
678+
}
679+
`, target)
680+
}
681+
682+
func testAccSourceFileSplitImage_fromFile(targetData, targetMetadata string, aliases ...string) string {
683+
return fmt.Sprintf(`
684+
resource "incus_image" "from_file" {
685+
source_file = {
686+
data_path = "%[1]s"
687+
metadata_path = "%[2]s"
688+
}
689+
aliases = ["%[3]s"]
690+
}
691+
`, targetData, targetMetadata, strings.Join(aliases, `","`))
692+
}
693+
694+
func testAccSourceFileUnifiedImage_exportImage(name, targetData string) string {
695+
return fmt.Sprintf(`
696+
resource "incus_instance" "instance1" {
697+
name = "%[1]s"
698+
image = "images:alpine/edge"
699+
type = "container"
700+
running = false
701+
}
702+
703+
resource "null_resource" "publish_instance1" {
704+
depends_on = [
705+
incus_instance.instance1
706+
]
707+
provisioner "local-exec" {
708+
command = "incus publish --alias %[1]s %[1]s"
709+
}
710+
}
711+
712+
resource "null_resource" "export_instance1_image" {
713+
depends_on = [
714+
null_resource.publish_instance1
715+
]
716+
provisioner "local-exec" {
717+
command = "incus image export %[1]s %[2]s"
718+
}
719+
}
720+
721+
resource "null_resource" "delete_instance1_image" {
722+
depends_on = [
723+
null_resource.export_instance1_image
724+
]
725+
provisioner "local-exec" {
726+
command = "incus image delete %[1]s"
727+
}
728+
}
729+
`, name, targetData)
730+
}
731+
732+
func testAccSourceFileUnifiedImage_fromFile(targetData string) string {
733+
return fmt.Sprintf(`
734+
resource "incus_image" "from_file" {
735+
source_file = {
736+
data_path = "%[1]s.tar.gz"
737+
}
738+
}
739+
740+
`, targetData)
741+
}

0 commit comments

Comments
 (0)