From d3bd080641c961075e59b0e2b0456e06ca9c3903 Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Mon, 29 Jan 2024 15:11:03 +0800 Subject: [PATCH] Add testcase --- pulsar/resource_pulsar_namespace.go | 3 + pulsar/resource_pulsar_namespace_test.go | 110 +++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/pulsar/resource_pulsar_namespace.go b/pulsar/resource_pulsar_namespace.go index e06f919..54a6e83 100644 --- a/pulsar/resource_pulsar_namespace.go +++ b/pulsar/resource_pulsar_namespace.go @@ -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) diff --git a/pulsar/resource_pulsar_namespace_test.go b/pulsar/resource_pulsar_namespace_test.go index 7afac7a..193e09c 100644 --- a/pulsar/resource_pulsar_namespace_test.go +++ b/pulsar/resource_pulsar_namespace_test.go @@ -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"), ), }, }, @@ -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) @@ -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) } @@ -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) +}