Skip to content

Commit

Permalink
Add Volume Delete Test Step
Browse files Browse the repository at this point in the history
Add test step ensuring that volumes are deleted correctly.
This test works independent of the timeout since in case the timeout is
exceeded the delete operation will already be considered a fail.
Thus, if the test step succeeds, the delete would not have timed out.
The test step also ensures that the resource is actually removed.

related-to: harvester/harvester#6415

Signed-off-by: Moritz Röhrich <[email protected]>
  • Loading branch information
m-ildefons committed Jan 3, 2025
1 parent 416edd7 commit 2995def
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/tests/resource_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/harvester/terraform-provider-harvester/pkg/client"
Expand Down Expand Up @@ -58,10 +59,48 @@ func TestAccVolume_basic(t *testing.T) {
resource.TestCheckResourceAttr(testAccVolumeResourceName, constants.FieldVolumeSize, testAccVolumeSize),
),
},
{
Config: buildVolumeConfig(testAccVolumeName, testAccVolumeDescription, testAccVolumeSize),
Destroy: true,
Check: resource.ComposeTestCheckFunc(
testAccVolumeDoesNotExist(ctx, testAccVolumeResourceName),
),
},
},
})
}

func testAccVolumeDoesNotExist(ctx context.Context, rn string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("Resource %s not found. ", rn)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Resource %s ID not set. ", rn)
}

id := rs.Primary.ID
namespace, name, err := helper.IDParts(id)
if err != nil {
return err
}

c := testAccProvider.Meta().(*client.Client)
_, err = c.KubeClient.
CoreV1().
PersistentVolumeClaims(namespace).
Get(ctx, name, metav1.GetOptions{})
if err == nil {
return fmt.Errorf("Volume %v/%v unexpectedly found", namespace, name)
} else if !apierrors.IsNotFound(err) {
return err
}
return nil
}
}

func testAccVolumeExists(ctx context.Context, n string, volume *corev1.PersistentVolumeClaim) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down

0 comments on commit 2995def

Please sign in to comment.