@@ -2,11 +2,12 @@ package image_test
2
2
3
3
import (
4
4
"fmt"
5
+ "path/filepath"
5
6
"regexp"
6
7
"strings"
7
8
"testing"
8
9
9
- "github.com/dustinkirkland/golang-petname"
10
+ petname "github.com/dustinkirkland/golang-petname"
10
11
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11
12
12
13
"github.com/lxc/terraform-provider-incus/internal/acctest"
@@ -324,6 +325,93 @@ func TestAccImage_sourceInstanceWithSnapshot(t *testing.T) {
324
325
})
325
326
}
326
327
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
+
327
415
func testAccImage_basic () string {
328
416
return `
329
417
resource "incus_image" "img1" {
@@ -572,3 +660,82 @@ resource "incus_image" "img1" {
572
660
}
573
661
` , projectName , instanceName , acctest .TestImage )
574
662
}
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