Skip to content

Commit

Permalink
Add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng committed Jan 29, 2024
1 parent dc9cf43 commit d3bd080
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pulsar/resource_pulsar_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ func unmarshalTopicAutoCreation(v *schema.Set) (*utils.TopicAutoCreationConfig,
topicAutoCreation.Type = utils.TopicType(data["type"].(string))
if topicAutoCreation.Type == utils.Partitioned {
partitions := data["partitions"].(int)
if partitions <= 0 {
return nil, fmt.Errorf("ERROR_PARSE_TOPIC_AUTO_CREATION: partitions must be greater than 0")
}
topicAutoCreation.Partitions = &partitions
} else if topicAutoCreation.Type != utils.NonPartitioned {
return nil, fmt.Errorf("ERROR_PARSE_TOPIC_AUTO_CREATION: unknown topic type %s", topicAutoCreation.Type)
Expand Down
110 changes: 110 additions & 0 deletions pulsar/resource_pulsar_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func TestNamespaceWithUpdate(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "permission_grant.1.actions.#", "2"),
resource.TestCheckResourceAttr(resourceName, "permission_grant.1.actions.0", "consume"),
resource.TestCheckResourceAttr(resourceName, "permission_grant.1.actions.1", "produce"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.#", "1"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.enable", "true"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.type", "partitioned"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.partitions", "3"),
),
},
},
Expand Down Expand Up @@ -245,6 +249,75 @@ func TestNamespaceWithPermissionGrantUpdate(t *testing.T) {
})
}

func TestNamespaceWithTopicAutoCreationUpdate(t *testing.T) {

resourceName := "pulsar_namespace.test"
cName := acctest.RandString(10)
tName := acctest.RandString(10)
nsName := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
IDRefreshName: resourceName,
CheckDestroy: testPulsarNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testPulsarNamespaceWithoutOptionals(testWebServiceURL, cName, tName, nsName),
Check: resource.ComposeTestCheckFunc(
testPulsarNamespaceExists(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "topic_auto_creation.#"),
),
},
{
Config: testPulsarNamespaceWithTopicAutoCreation(testWebServiceURL, cName, tName, nsName,
`topic_auto_creation {
enable = "false"
}`),
Check: resource.ComposeTestCheckFunc(
testPulsarNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.#", "1"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.enable", "false"),
),
},
{
Config: testPulsarNamespaceWithTopicAutoCreation(testWebServiceURL, cName, tName, nsName,
`topic_auto_creation {
enable = "true"
}`),
Check: resource.ComposeTestCheckFunc(
testPulsarNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.#", "1"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.enable", "true"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.type", "non-partitioned"),
),
},
{
Config: testPulsarNamespaceWithTopicAutoCreation(testWebServiceURL, cName, tName, nsName,
`topic_auto_creation {
enable = "true"
type = "partitioned"
partitions = 3
}`),
Check: resource.ComposeTestCheckFunc(
testPulsarNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.#", "1"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.enable", "true"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.type", "partitioned"),
resource.TestCheckResourceAttr(resourceName, "topic_auto_creation.0.partitions", "3"),
),
},
{
Config: testPulsarNamespaceWithoutOptionals(testWebServiceURL, cName, tName, nsName),
Check: resource.ComposeTestCheckFunc(
testPulsarNamespaceExists(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "topic_auto_creation.#"),
),
},
},
})
}

func TestImportExistingNamespace(t *testing.T) {
tname := "public"
ns := acctest.RandString(10)
Expand Down Expand Up @@ -462,6 +535,12 @@ resource "pulsar_namespace" "test" {
role = "some-role-2"
actions = ["produce", "consume"]
}
topic_auto_creation {
enable = true
type = "partitioned"
partitions = 3
}
}
`, wsURL, cluster, tenant, ns)
}
Expand Down Expand Up @@ -544,3 +623,34 @@ resource "pulsar_namespace" "test" {
}
`, wsURL, cluster, tenant, ns, permissionGrants)
}

func testPulsarNamespaceWithTopicAutoCreation(wsURL, cluster, tenant, ns string, topicAutoCreation string) string {
return fmt.Sprintf(`
provider "pulsar" {
web_service_url = "%s"
}
resource "pulsar_cluster" "test_cluster" {
cluster = "%s"
cluster_data {
web_service_url = "http://localhost:8080"
broker_service_url = "http://localhost:6050"
peer_clusters = ["standalone"]
}
}
resource "pulsar_tenant" "test_tenant" {
tenant = "%s"
allowed_clusters = [pulsar_cluster.test_cluster.cluster, "standalone"]
}
resource "pulsar_namespace" "test" {
tenant = pulsar_tenant.test_tenant.tenant
namespace = "%s"
%s
}
`, wsURL, cluster, tenant, ns, topicAutoCreation)
}

0 comments on commit d3bd080

Please sign in to comment.