From aaac4c219bc349fcbb55c3c1cf623d384edd44e4 Mon Sep 17 00:00:00 2001 From: Guangning E Date: Wed, 27 Dec 2023 19:13:31 +0800 Subject: [PATCH] Add instance test --- cloud/resource_pulsar_instance_test.go | 100 +++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 cloud/resource_pulsar_instance_test.go diff --git a/cloud/resource_pulsar_instance_test.go b/cloud/resource_pulsar_instance_test.go new file mode 100644 index 0000000..4b36ca5 --- /dev/null +++ b/cloud/resource_pulsar_instance_test.go @@ -0,0 +1,100 @@ +package cloud + +import ( + "context" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "strings" + "testing" +) + +func TestResourcePulsarInstance(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testCheckResourcePulsarInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testResourcePulsarInstance( + "sndev", + "terraform-test-pulsar-instance-b", + "zonal", + "shared-gcp", + "streamnative"), + Check: resource.ComposeTestCheckFunc( + testCheckResourcePulsarInstanceExists("streamnative_pulsar_instance.test-pulsar-instance"), + ), + }, + }, + }) +} + +func testCheckResourcePulsarInstanceDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "streamnative_pulsar_instance" { + continue + } + meta := testAccProvider.Meta() + clientSet, err := getClientSet(getFactoryFromMeta(meta)) + if err != nil { + return err + } + organizationInstance := strings.Split(rs.Primary.ID, "/") + _, err = clientSet.CloudV1alpha1(). + PulsarInstances(organizationInstance[0]). + Get(context.Background(), organizationInstance[1], metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + return fmt.Errorf(`ERROR_RESOURCE_PULSAR_INSTANCE_STILL_EXISTS: "%s"`, rs.Primary.ID) + } + return nil +} + +func testCheckResourcePulsarInstanceExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf(`ERROR_RESOURCE_PULSAR_INSTANCE_NOT_FOUND: "%s"`, name) + } + if rs.Primary.ID == "" { + return fmt.Errorf("ERROR_RESOURCE_PULSAR_INSTANCE_ID_NOT_SET") + } + meta := testAccProvider.Meta() + clientSet, err := getClientSet(getFactoryFromMeta(meta)) + if err != nil { + return err + } + organizationInstance := strings.Split(name, "/") + _, err = clientSet.CloudV1alpha1(). + PulsarInstances(organizationInstance[0]). + Get(context.Background(), organizationInstance[1], metav1.GetOptions{}) + if err != nil { + return err + } + return nil + } +} + +func testResourcePulsarInstance( + organization string, name string, availabilityMode string, poolName string, poolNamespace string) string { + return fmt.Sprintf(` +provider "streamnative" { +} +resource "streamnative_pulsar_instance" "test-pulsar-instance" { + organization = "%s" + name = "%s" + availability_mode = "%s" + pool_name = "%s" + pool_namespace = "%s" +} +`, organization, name, availabilityMode, poolName, poolNamespace) +}