@@ -2,6 +2,7 @@ package image_test
2
2
3
3
import (
4
4
"fmt"
5
+ "path/filepath"
5
6
"regexp"
6
7
"strings"
7
8
"testing"
@@ -324,6 +325,99 @@ 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
+ alias1 := petname .Generate (2 , "-" )
383
+ alias2 := petname .Generate (2 , "-" )
384
+
385
+ resource .Test (t , resource.TestCase {
386
+ PreCheck : func () { acctest .PreCheck (t ) },
387
+ ProtoV6ProviderFactories : acctest .ProtoV6ProviderFactories ,
388
+ ExternalProviders : map [string ]resource.ExternalProvider {
389
+ "null" : {
390
+ Source : "null" ,
391
+ VersionConstraint : ">= 3.0.0" ,
392
+ },
393
+ },
394
+ Steps : []resource.TestStep {
395
+ {
396
+ Config : testAccSourceFileUnifiedImage_exportImage (name , targetData ),
397
+ Check : resource .ComposeTestCheckFunc (
398
+ resource .TestCheckResourceAttr ("incus_instance.instance1" , "name" , name ),
399
+ resource .TestCheckResourceAttr ("incus_instance.instance1" , "image" , "images:alpine/edge" ),
400
+ resource .TestCheckResourceAttr ("incus_instance.instance1" , "type" , "container" ),
401
+ resource .TestCheckResourceAttr ("incus_instance.instance1" , "status" , "Stopped" ),
402
+ resource .TestCheckResourceAttrSet ("null_resource.publish_instance1" , "id" ),
403
+ resource .TestCheckResourceAttrSet ("null_resource.export_instance1_image" , "id" ),
404
+ resource .TestCheckResourceAttrSet ("null_resource.delete_instance1_image" , "id" ),
405
+ ),
406
+ },
407
+ {
408
+ Config : testAccSourceFileUnifiedImage_fromFile (targetData , alias1 , alias2 ),
409
+ Check : resource .ComposeTestCheckFunc (
410
+ resource .TestCheckResourceAttr ("incus_image.from_file" , "source_file.data_path" , targetData + ".tar.gz" ),
411
+ resource .TestCheckResourceAttr ("incus_image.from_file" , "aliases.#" , "2" ),
412
+ resource .TestCheckTypeSetElemAttr ("incus_image.from_file" , "aliases.*" , alias1 ),
413
+ resource .TestCheckTypeSetElemAttr ("incus_image.from_file" , "aliases.*" , alias2 ),
414
+ resource .TestCheckResourceAttr ("incus_image.from_file" , "copied_aliases.#" , "0" ),
415
+ ),
416
+ },
417
+ },
418
+ })
419
+ }
420
+
327
421
func testAccImage_basic () string {
328
422
return `
329
423
resource "incus_image" "img1" {
@@ -572,3 +666,83 @@ resource "incus_image" "img1" {
572
666
}
573
667
` , projectName , instanceName , acctest .TestImage )
574
668
}
669
+
670
+ func testAccSourceFileSplitImage_exportImage (target string ) string {
671
+ return fmt .Sprintf (`
672
+ resource "incus_image" "img1" {
673
+ source_image = {
674
+ remote = "images"
675
+ name = "alpine/edge"
676
+ copy_aliases = true
677
+ }
678
+ }
679
+
680
+ resource "null_resource" "export_img1" {
681
+ provisioner "local-exec" {
682
+ command = "incus image export ${incus_image.img1.fingerprint} %[1]s"
683
+ }
684
+ }
685
+ ` , target )
686
+ }
687
+
688
+ func testAccSourceFileSplitImage_fromFile (targetData , targetMetadata string , aliases ... string ) string {
689
+ return fmt .Sprintf (`
690
+ resource "incus_image" "from_file" {
691
+ source_file = {
692
+ data_path = "%[1]s"
693
+ metadata_path = "%[2]s"
694
+ }
695
+ aliases = ["%[3]s"]
696
+ }
697
+ ` , targetData , targetMetadata , strings .Join (aliases , `","` ))
698
+ }
699
+
700
+ func testAccSourceFileUnifiedImage_exportImage (name , targetData string ) string {
701
+ return fmt .Sprintf (`
702
+ resource "incus_instance" "instance1" {
703
+ name = "%[1]s"
704
+ image = "images:alpine/edge"
705
+ type = "container"
706
+ running = false
707
+ }
708
+
709
+ resource "null_resource" "publish_instance1" {
710
+ depends_on = [
711
+ incus_instance.instance1
712
+ ]
713
+ provisioner "local-exec" {
714
+ command = "incus publish --alias %[1]s %[1]s"
715
+ }
716
+ }
717
+
718
+ resource "null_resource" "export_instance1_image" {
719
+ depends_on = [
720
+ null_resource.publish_instance1
721
+ ]
722
+ provisioner "local-exec" {
723
+ command = "incus image export %[1]s %[2]s"
724
+ }
725
+ }
726
+
727
+ resource "null_resource" "delete_instance1_image" {
728
+ depends_on = [
729
+ null_resource.export_instance1_image
730
+ ]
731
+ provisioner "local-exec" {
732
+ command = "incus image delete %[1]s"
733
+ }
734
+ }
735
+ ` , name , targetData )
736
+ }
737
+
738
+ func testAccSourceFileUnifiedImage_fromFile (targetData string , aliases ... string ) string {
739
+ return fmt .Sprintf (`
740
+ resource "incus_image" "from_file" {
741
+ source_file = {
742
+ data_path = "%[1]s.tar.gz"
743
+ }
744
+ aliases = ["%[2]s"]
745
+ }
746
+
747
+ ` , targetData , strings .Join (aliases , `","` ))
748
+ }
0 commit comments