@@ -77,11 +77,59 @@ func TestAccVolumeResource_basic(t *testing.T) {
7777 resource .TestCheckResourceAttrSet ("libvirt_volume.test" , "allocation" ),
7878 ),
7979 },
80+ {
81+ Config : testAccVolumeResourceConfigBasic ("test-volume" , poolPath ),
82+ PlanOnly : true ,
83+ },
8084 // Delete testing automatically occurs in TestCase
8185 },
8286 })
8387}
8488
89+ func TestAccVolumeResource_explicitSparseAllocation (t * testing.T ) {
90+ poolPath := t .TempDir ()
91+
92+ resource .Test (t , resource.TestCase {
93+ PreCheck : func () { testAccPreCheck (t ) },
94+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
95+ Steps : []resource.TestStep {
96+ {
97+ Config : testAccVolumeResourceConfigAllocation ("test-volume-sparse" , poolPath , 0 ),
98+ Check : resource .ComposeAggregateTestCheckFunc (
99+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "capacity" , "1048576" ),
100+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "allocation" , "0" ),
101+ ),
102+ },
103+ {
104+ Config : testAccVolumeResourceConfigAllocation ("test-volume-sparse" , poolPath , 0 ),
105+ PlanOnly : true ,
106+ },
107+ },
108+ })
109+ }
110+
111+ func TestAccVolumeResource_explicitFullAllocation (t * testing.T ) {
112+ poolPath := t .TempDir ()
113+
114+ resource .Test (t , resource.TestCase {
115+ PreCheck : func () { testAccPreCheck (t ) },
116+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
117+ Steps : []resource.TestStep {
118+ {
119+ Config : testAccVolumeResourceConfigAllocation ("test-volume-full" , poolPath , 1024 * 1024 ),
120+ Check : resource .ComposeAggregateTestCheckFunc (
121+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "capacity" , "1048576" ),
122+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "allocation" , "1048576" ),
123+ ),
124+ },
125+ {
126+ Config : testAccVolumeResourceConfigAllocation ("test-volume-full" , poolPath , 1024 * 1024 ),
127+ PlanOnly : true ,
128+ },
129+ },
130+ })
131+ }
132+
85133func TestAccVolumeResource_importByKey (t * testing.T ) {
86134 poolPath := t .TempDir ()
87135
@@ -497,6 +545,41 @@ func TestAccVolumeResource_uploadFromHTTPWithContentLength(t *testing.T) {
497545 })
498546}
499547
548+ func TestAccVolumeResource_uploadIntoLargerCapacity (t * testing.T ) {
549+ poolPath := t .TempDir ()
550+ testContent := bytes .Repeat ([]byte ("d" ), 1024 * 1024 )
551+ requestedCapacity := int64 (2 * 1024 * 1024 )
552+
553+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
554+ if r .URL .Path != "/image.raw" {
555+ http .NotFound (w , r )
556+ return
557+ }
558+
559+ http .ServeContent (w , r , "image.raw" , time.Time {}, bytes .NewReader (testContent ))
560+ }))
561+ defer server .Close ()
562+
563+ resource .Test (t , resource.TestCase {
564+ PreCheck : func () { testAccPreCheck (t ) },
565+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
566+ Steps : []resource.TestStep {
567+ {
568+ Config : testAccVolumeResourceConfigUploadFromURL (
569+ "test-volume-upload-larger" ,
570+ poolPath ,
571+ server .URL + "/image.raw" ,
572+ & requestedCapacity ,
573+ ),
574+ Check : resource .ComposeAggregateTestCheckFunc (
575+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "capacity" , "2097152" ),
576+ testAccCheckVolumeFileSize ("libvirt_volume.test" , requestedCapacity ),
577+ ),
578+ },
579+ },
580+ })
581+ }
582+
500583func TestAccVolumeResource_uploadFromHTTPWithoutContentLengthUsesCapacity (t * testing.T ) {
501584 poolPath := t .TempDir ()
502585 testContent := bytes .Repeat ([]byte ("b" ), 1024 * 1024 )
@@ -620,6 +703,53 @@ resource "libvirt_volume" "test" {
620703` , name , poolPath , sourceFile )
621704}
622705
706+ func testAccVolumeResourceConfigAllocation (name , poolPath string , allocation int64 ) string {
707+ return fmt .Sprintf (`
708+ resource "libvirt_pool" "test" {
709+ name = "test-pool-allocation-%[1]s"
710+ type = "dir"
711+ target = {
712+ path = %[2]q
713+ }
714+ }
715+
716+ resource "libvirt_volume" "test" {
717+ name = "%[1]s.raw"
718+ pool = libvirt_pool.test.name
719+ capacity = 1048576
720+ allocation = %[3]d
721+ target = {
722+ format = {
723+ type = "raw"
724+ }
725+ }
726+ }
727+ ` , name , poolPath , allocation )
728+ }
729+
730+ func testAccCheckVolumeFileSize (resourceName string , expected int64 ) resource.TestCheckFunc {
731+ return func (state * terraform.State ) error {
732+ volume , ok := state .RootModule ().Resources [resourceName ]
733+ if ! ok {
734+ return fmt .Errorf ("%s not found in state" , resourceName )
735+ }
736+
737+ path := volume .Primary .Attributes ["path" ]
738+ if path == "" {
739+ return fmt .Errorf ("%s path not found in state" , resourceName )
740+ }
741+
742+ info , err := os .Stat (path )
743+ if err != nil {
744+ return fmt .Errorf ("stat volume %s: %w" , path , err )
745+ }
746+ if info .Size () != expected {
747+ return fmt .Errorf ("expected volume file size %d, got %d" , expected , info .Size ())
748+ }
749+ return nil
750+ }
751+ }
752+
623753func testAccVolumeResourceConfigUploadFromURL (name , poolPath , sourceURL string , capacity * int64 ) string {
624754 capacityConfig := ""
625755 if capacity != nil {
0 commit comments