@@ -287,6 +287,61 @@ resource "libvirt_domain" "test" {
287287` , name , poolPath )
288288}
289289
290+ // TestAccVolumeResource_capacityUnit reproduces issue #1253: using capacity_unit
291+ // (e.g. "GiB") causes "Provider produced inconsistent result after apply" because
292+ // libvirt normalises the value to bytes on readback and the provider was always
293+ // reading the raw bytes value instead of preserving the plan's unit-converted value.
294+ func TestAccVolumeResource_capacityUnit (t * testing.T ) {
295+ poolPath := t .TempDir ()
296+
297+ resource .Test (t , resource.TestCase {
298+ PreCheck : func () { testAccPreCheck (t ) },
299+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
300+ Steps : []resource.TestStep {
301+ {
302+ // Create a 1 GiB volume using capacity_unit.
303+ // Before the fix this errors: "Provider produced inconsistent result
304+ // after apply: .capacity: was cty.NumberIntVal(1), but now
305+ // cty.NumberIntVal(1073741824)".
306+ Config : testAccVolumeResourceConfigCapacityUnit ("test-volume-cap-unit" , poolPath ),
307+ Check : resource .ComposeAggregateTestCheckFunc (
308+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "capacity" , "1" ),
309+ resource .TestCheckResourceAttr ("libvirt_volume.test" , "capacity_unit" , "GiB" ),
310+ ),
311+ },
312+ {
313+ // Ensure there is no perpetual diff after the first apply.
314+ Config : testAccVolumeResourceConfigCapacityUnit ("test-volume-cap-unit" , poolPath ),
315+ PlanOnly : true ,
316+ },
317+ },
318+ })
319+ }
320+
321+ func testAccVolumeResourceConfigCapacityUnit (name , poolPath string ) string {
322+ return fmt .Sprintf (`
323+ resource "libvirt_pool" "test" {
324+ name = "test-pool-cap-unit"
325+ type = "dir"
326+ target = {
327+ path = %[2]q
328+ }
329+ }
330+
331+ resource "libvirt_volume" "test" {
332+ name = "%[1]s.qcow2"
333+ pool = libvirt_pool.test.name
334+ capacity = 1
335+ capacity_unit = "GiB"
336+ target = {
337+ format = {
338+ type = "qcow2"
339+ }
340+ }
341+ }
342+ ` , name , poolPath )
343+ }
344+
290345func TestAccVolumeResource_uploadFromFile (t * testing.T ) {
291346 poolPath := t .TempDir ()
292347
0 commit comments