Skip to content

Commit

Permalink
Add instance test
Browse files Browse the repository at this point in the history
  • Loading branch information
tuteng committed Dec 27, 2023
1 parent 3b66e30 commit aaac4c2
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions cloud/resource_pulsar_instance_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit aaac4c2

Please sign in to comment.