From 5fb24b3c6f70fc5609adbd96575f84b0f4836bb7 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 10:59:05 +0800 Subject: [PATCH 01/11] fix topic not found namespace --- pulsar/resource_pulsar_topic.go | 5 + pulsar/resource_pulsar_topic_test.go | 151 +++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/pulsar/resource_pulsar_topic.go b/pulsar/resource_pulsar_topic.go index bb2e19c..bc5dfc8 100644 --- a/pulsar/resource_pulsar_topic.go +++ b/pulsar/resource_pulsar_topic.go @@ -21,6 +21,7 @@ import ( "context" "fmt" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/cenkalti/backoff/v4" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -162,6 +163,10 @@ func resourcePulsarTopicRead(ctx context.Context, d *schema.ResourceData, meta i topicName, found, err := getTopic(d, meta) if err != nil { + if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { + d.SetId("") + return nil + } return diag.Errorf("%v", err) } if !found { diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 8386c00..73be963 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -93,6 +93,54 @@ func TestPartionedTopicWithPermissionGrantUpdate(t *testing.T) { testTopicWithPermissionGrantUpdate(t, 10) } +func TestTopicNamespaceExternallyRemoved(t *testing.T) { + + resourceName := "pulsar_topic.test" + cName := acctest.RandString(10) + tName := acctest.RandString(10) + nsName := acctest.RandString(10) + topicName := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + IDRefreshName: resourceName, + CheckDestroy: testPulsarNamespaceDestroy, + Steps: []resource.TestStep{ + { + Config: testPulsarNamespaceWithTopic(testWebServiceURL, cName, tName, nsName, topicName), + Check: resource.ComposeTestCheckFunc( + testPulsarTopicExists(resourceName), + ), + }, + { + PreConfig: func() { + client, err := sharedClient(testWebServiceURL) + if err != nil { + t.Fatalf("ERROR_GETTING_PULSAR_CLIENT: %v", err) + } + + conn := client.(admin.Client) + topicName, err := utils.GetTopicName(fmt.Sprintf("persistent://%s/%s/%s", tName, nsName, topicName)) + if err != nil { + t.Fatalf("ERROR_GETTING_TOPIC_NAME: %v", err) + } + if err = conn.Topics().Delete(*topicName, false, false); err != nil { + t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) + } + if err = conn.Namespaces().DeleteNamespace(tName + "/" + nsName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) + } + }, + Config: testPulsarNamespaceWithTopic(testWebServiceURL, cName, tName, nsName, topicName), + PlanOnly: true, + ExpectNonEmptyPlan: true, + ExpectError: nil, + }, + }, + }) +} + func testTopicWithPermissionGrantUpdate(t *testing.T, pnum int) { resourceName := "pulsar_topic.test" tname := acctest.RandString(10) @@ -338,3 +386,106 @@ resource "pulsar_topic" "test" { } `, url, ttype, tname, pnum, permissionGrants) } + +func testPulsarNamespaceWithTopic(wsURL, cluster, tenant, ns, topicName 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" + + enable_deduplication = true + + namespace_config { + anti_affinity = "anti-aff" + max_consumers_per_subscription = "50" + max_consumers_per_topic = "50" + max_producers_per_topic = "50" + message_ttl_seconds = "86400" + replication_clusters = ["standalone"] + is_allow_auto_update_schema = false + offload_threshold_size_in_mb = "100" + } + + dispatch_rate { + dispatch_msg_throttling_rate = 50 + rate_period_seconds = 50 + dispatch_byte_throttling_rate = 2048 + } + + subscription_dispatch_rate { + dispatch_msg_throttling_rate = 50 + rate_period_seconds = 50 + dispatch_byte_throttling_rate = 2048 + } + + retention_policies { + retention_minutes = "1600" + retention_size_in_mb = "10000" + } + + persistence_policies { + bookkeeper_ensemble = 2 + bookkeeper_write_quorum = 2 + bookkeeper_ack_quorum = 2 + managed_ledger_max_mark_delete_rate = 0.0 + } + + backlog_quota { + limit_bytes = "10000000000" + limit_seconds = "-1" + policy = "producer_request_hold" + type = "destination_storage" + } + + permission_grant { + role = "some-role-1" + actions = ["produce", "consume", "functions"] + } + + permission_grant { + role = "some-role-2" + actions = ["produce", "consume"] + } + + topic_auto_creation { + enable = false + } + + depends_on = [ + pulsar_tenant.test_tenant + ] +} + +resource "pulsar_topic" "test" { + tenant = "%s" + namespace = "%s" + topic_type = "persistent" + topic_name = "%s" + partitions = 0 + depends_on = [ + pulsar_namespace.test, + pulsar_tenant.test_tenant + ] +} +`, wsURL, cluster, tenant, ns, tenant, ns, topicName) +} From 6763424d6919e296f087fd692232fac7d885aeaf Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 11:44:50 +0800 Subject: [PATCH 02/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 73be963..ba94148 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -105,7 +105,7 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: testAccProviderFactories, IDRefreshName: resourceName, - CheckDestroy: testPulsarNamespaceDestroy, + CheckDestroy: testPulsarTopicDestroy, Steps: []resource.TestStep{ { Config: testPulsarNamespaceWithTopic(testWebServiceURL, cName, tName, nsName, topicName), From c3114d38e21bafcffe191331da9666171016c1b8 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 14:02:34 +0800 Subject: [PATCH 03/11] fix ci --- pulsar/resource_pulsar_cluster_test.go | 10 ++++++++ pulsar/resource_pulsar_namespace_test.go | 5 ++++ pulsar/resource_pulsar_sink_test.go | 17 ++++++++++---- pulsar/resource_pulsar_source_test.go | 16 +++++++++---- pulsar/resource_pulsar_tenant_test.go | 10 ++++++++ pulsar/resource_pulsar_topic_test.go | 29 +++++++++--------------- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/pulsar/resource_pulsar_cluster_test.go b/pulsar/resource_pulsar_cluster_test.go index 360b1b7..840595e 100644 --- a/pulsar/resource_pulsar_cluster_test.go +++ b/pulsar/resource_pulsar_cluster_test.go @@ -56,6 +56,11 @@ func TestHandleExistingCluster(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createCluster(t, cName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Clusters().Delete(cName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_CLUSTER: %v", err) + } + }) }, CheckDestroy: testPulsarClusterDestroy, ProviderFactories: testAccProviderFactories, @@ -75,6 +80,11 @@ func TestImportExistingCluster(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createCluster(t, cName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Clusters().Delete(cName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_CLUSTER: %v", err) + } + }) }, CheckDestroy: testPulsarClusterDestroy, ProviderFactories: testAccProviderFactories, diff --git a/pulsar/resource_pulsar_namespace_test.go b/pulsar/resource_pulsar_namespace_test.go index 1e2b7a2..1f15fd0 100644 --- a/pulsar/resource_pulsar_namespace_test.go +++ b/pulsar/resource_pulsar_namespace_test.go @@ -332,6 +332,11 @@ func TestImportExistingNamespace(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createNamespace(t, id) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Namespaces().DeleteNamespace(id); err != nil { + t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) + } + }) }, CheckDestroy: testPulsarNamespaceDestroy, ProviderFactories: testAccProviderFactories, diff --git a/pulsar/resource_pulsar_sink_test.go b/pulsar/resource_pulsar_sink_test.go index 8545334..601a49e 100644 --- a/pulsar/resource_pulsar_sink_test.go +++ b/pulsar/resource_pulsar_sink_test.go @@ -115,12 +115,21 @@ func testPulsarSinkDestroy(s *terraform.State) error { func TestImportExistingSink(t *testing.T) { sinkName := acctest.RandString(6) - err := createSampleSink(sinkName) - if err != nil { - t.Fatal(err) - } resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + createSampleSink(sinkName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Sinks().DeleteSink( + "public", + "default", + sinkName, + ); err != nil { + t.Fatalf("ERROR_DELETING_TEST_SINK: %v", err) + } + }) + }, ProviderFactories: testAccProviderFactories, CheckDestroy: testPulsarSinkDestroy, Steps: []resource.TestStep{ diff --git a/pulsar/resource_pulsar_source_test.go b/pulsar/resource_pulsar_source_test.go index 4c2405c..d1574de 100644 --- a/pulsar/resource_pulsar_source_test.go +++ b/pulsar/resource_pulsar_source_test.go @@ -133,12 +133,20 @@ func getPulsarSourceByResourceID(id string) (*utils.SourceConfig, error) { func TestImportExistingSource(t *testing.T) { sourceName := acctest.RandString(6) - err := createSampleSource(sourceName) - if err != nil { - t.Fatal(err) - } resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + createSampleSource(sourceName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Sources().DeleteSource( + "public", + "default", + sourceName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_SOURCE: %v", err) + } + }) + }, ProviderFactories: testAccProviderFactories, CheckDestroy: testPulsarSourceDestroy, Steps: []resource.TestStep{ diff --git a/pulsar/resource_pulsar_tenant_test.go b/pulsar/resource_pulsar_tenant_test.go index 59cf358..14031f4 100644 --- a/pulsar/resource_pulsar_tenant_test.go +++ b/pulsar/resource_pulsar_tenant_test.go @@ -57,6 +57,11 @@ func TestHandleExistingTenant(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createTenant(t, tName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Tenants().Delete(tName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_TENANT: %v", err) + } + }) }, ProviderFactories: testAccProviderFactories, PreventPostDestroyRefresh: false, @@ -77,6 +82,11 @@ func TestImportExistingTenant(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createTenant(t, tName) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Tenants().Delete(tName); err != nil { + t.Fatalf("ERROR_DELETING_TEST_TENANT: %v", err) + } + }) }, CheckDestroy: testPulsarTenantDestroy, ProviderFactories: testAccProviderFactories, diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index ba94148..4ed98ad 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -70,6 +70,11 @@ func TestImportExistingTopic(t *testing.T) { PreCheck: func() { testAccPreCheck(t) createTopic(t, fullID, pnum) + t.Cleanup(func() { + if err := getClientFromMeta(testAccProvider.Meta()).Topics().Delete(fullID); err != nil { + t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) + } + }) }, ProviderFactories: testAccProviderFactories, CheckDestroy: testPulsarTopicDestroy, @@ -96,7 +101,6 @@ func TestPartionedTopicWithPermissionGrantUpdate(t *testing.T) { func TestTopicNamespaceExternallyRemoved(t *testing.T) { resourceName := "pulsar_topic.test" - cName := acctest.RandString(10) tName := acctest.RandString(10) nsName := acctest.RandString(10) topicName := acctest.RandString(10) @@ -108,7 +112,7 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { CheckDestroy: testPulsarTopicDestroy, Steps: []resource.TestStep{ { - Config: testPulsarNamespaceWithTopic(testWebServiceURL, cName, tName, nsName, topicName), + Config: testPulsarNamespaceWithTopic(testWebServiceURL, tName, nsName, topicName), Check: resource.ComposeTestCheckFunc( testPulsarTopicExists(resourceName), ), @@ -132,7 +136,7 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) } }, - Config: testPulsarNamespaceWithTopic(testWebServiceURL, cName, tName, nsName, topicName), + Config: testPulsarNamespaceWithTopic(testWebServiceURL, tName, nsName, topicName), PlanOnly: true, ExpectNonEmptyPlan: true, ExpectError: nil, @@ -387,26 +391,15 @@ resource "pulsar_topic" "test" { `, url, ttype, tname, pnum, permissionGrants) } -func testPulsarNamespaceWithTopic(wsURL, cluster, tenant, ns, topicName string) string { +func testPulsarNamespaceWithTopic(wsURL, tenant, ns, topicName 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"] + allowed_clusters = ["standalone"] } resource "pulsar_namespace" "test" { @@ -423,7 +416,7 @@ resource "pulsar_namespace" "test" { message_ttl_seconds = "86400" replication_clusters = ["standalone"] is_allow_auto_update_schema = false - offload_threshold_size_in_mb = "100" + offload_threshold_size_in_mb = "100" } dispatch_rate { @@ -487,5 +480,5 @@ resource "pulsar_topic" "test" { pulsar_tenant.test_tenant ] } -`, wsURL, cluster, tenant, ns, tenant, ns, topicName) +`, wsURL, tenant, ns, tenant, ns, topicName) } From 9d64f9cf0a44afaa5361acf55f31bd90e3112495 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 14:21:08 +0800 Subject: [PATCH 04/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 4ed98ad..e607f13 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -65,13 +65,17 @@ func TestImportExistingTopic(t *testing.T) { pnum := 10 fullID := strings.Join([]string{ttype + ":/", "public", "default", tname}, "/") + topicName, err := utils.GetTopicName(fullID) + if err != nil { + t.Fatalf("ERROR_GETTING_TOPIC_NAME: %v", err) + } resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) createTopic(t, fullID, pnum) t.Cleanup(func() { - if err := getClientFromMeta(testAccProvider.Meta()).Topics().Delete(fullID); err != nil { + if err := getClientFromMeta(testAccProvider.Meta()).Topics().Delete(*topicName, true, pnum == 0); err != nil { t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) } }) From 2ac42dd94a938851d5b61e077df89eb5eede5faf Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 14:47:03 +0800 Subject: [PATCH 05/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 54 ---------------------------- 1 file changed, 54 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index e607f13..50125d0 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -410,60 +410,6 @@ resource "pulsar_namespace" "test" { tenant = pulsar_tenant.test_tenant.tenant namespace = "%s" - enable_deduplication = true - - namespace_config { - anti_affinity = "anti-aff" - max_consumers_per_subscription = "50" - max_consumers_per_topic = "50" - max_producers_per_topic = "50" - message_ttl_seconds = "86400" - replication_clusters = ["standalone"] - is_allow_auto_update_schema = false - offload_threshold_size_in_mb = "100" - } - - dispatch_rate { - dispatch_msg_throttling_rate = 50 - rate_period_seconds = 50 - dispatch_byte_throttling_rate = 2048 - } - - subscription_dispatch_rate { - dispatch_msg_throttling_rate = 50 - rate_period_seconds = 50 - dispatch_byte_throttling_rate = 2048 - } - - retention_policies { - retention_minutes = "1600" - retention_size_in_mb = "10000" - } - - persistence_policies { - bookkeeper_ensemble = 2 - bookkeeper_write_quorum = 2 - bookkeeper_ack_quorum = 2 - managed_ledger_max_mark_delete_rate = 0.0 - } - - backlog_quota { - limit_bytes = "10000000000" - limit_seconds = "-1" - policy = "producer_request_hold" - type = "destination_storage" - } - - permission_grant { - role = "some-role-1" - actions = ["produce", "consume", "functions"] - } - - permission_grant { - role = "some-role-2" - actions = ["produce", "consume"] - } - topic_auto_creation { enable = false } From 149ebaac55d46b31880583cbdf8459c9f002863f Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 14:57:45 +0800 Subject: [PATCH 06/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 50125d0..1d60406 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -110,10 +110,10 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { topicName := acctest.RandString(10) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories, - IDRefreshName: resourceName, - CheckDestroy: testPulsarTopicDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + PreventPostDestroyRefresh: false, + CheckDestroy: testPulsarTopicDestroy, Steps: []resource.TestStep{ { Config: testPulsarNamespaceWithTopic(testWebServiceURL, tName, nsName, topicName), @@ -133,8 +133,22 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { if err != nil { t.Fatalf("ERROR_GETTING_TOPIC_NAME: %v", err) } - if err = conn.Topics().Delete(*topicName, false, false); err != nil { - t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) + namespace, err := utils.GetNameSpaceName(topicName.GetTenant(), topicName.GetNamespace()) + if err != nil { + t.Fatalf("ERROR_READ_NAMESPACE: %w", err) + } + + partitionedTopics, nonPartitionedTopics, err := conn.Topics().List(*namespace) + if err != nil { + t.Fatalf("ERROR_READ_TOPIC_DATA: %w", err) + } + + for _, topic := range append(partitionedTopics, nonPartitionedTopics...) { + if topicName.String() == topic { + if err = conn.Topics().Delete(*topicName, false, false); err != nil { + t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) + } + } } if err = conn.Namespaces().DeleteNamespace(tName + "/" + nsName); err != nil { t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) From eb32b85d6f92fe7cfadc9d4901d416d56f021667 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 18:03:41 +0800 Subject: [PATCH 07/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 1d60406..b336f0f 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -135,12 +135,12 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { } namespace, err := utils.GetNameSpaceName(topicName.GetTenant(), topicName.GetNamespace()) if err != nil { - t.Fatalf("ERROR_READ_NAMESPACE: %w", err) + t.Fatalf("ERROR_READ_NAMESPACE: %v", err) } partitionedTopics, nonPartitionedTopics, err := conn.Topics().List(*namespace) if err != nil { - t.Fatalf("ERROR_READ_TOPIC_DATA: %w", err) + t.Fatalf("ERROR_READ_TOPIC_DATA: %v", err) } for _, topic := range append(partitionedTopics, nonPartitionedTopics...) { From 53187dd0bfed5515374939bd7b3274d5046d46e7 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 21:11:18 +0800 Subject: [PATCH 08/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index b336f0f..205cef4 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -123,12 +123,9 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { }, { PreConfig: func() { - client, err := sharedClient(testWebServiceURL) - if err != nil { - t.Fatalf("ERROR_GETTING_PULSAR_CLIENT: %v", err) - } + client := getClientFromMeta(testAccProvider.Meta()) + t.Logf("tName: %s, nsName: %s, topicName: %s", tName, nsName, topicName) - conn := client.(admin.Client) topicName, err := utils.GetTopicName(fmt.Sprintf("persistent://%s/%s/%s", tName, nsName, topicName)) if err != nil { t.Fatalf("ERROR_GETTING_TOPIC_NAME: %v", err) @@ -138,19 +135,21 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { t.Fatalf("ERROR_READ_NAMESPACE: %v", err) } - partitionedTopics, nonPartitionedTopics, err := conn.Topics().List(*namespace) + t.Logf("topicName: %v, namespace: %v", topicName, namespace) + + partitionedTopics, nonPartitionedTopics, err := client.Topics().List(*namespace) if err != nil { t.Fatalf("ERROR_READ_TOPIC_DATA: %v", err) } for _, topic := range append(partitionedTopics, nonPartitionedTopics...) { if topicName.String() == topic { - if err = conn.Topics().Delete(*topicName, false, false); err != nil { + if err = client.Topics().Delete(*topicName, false, false); err != nil { t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) } } } - if err = conn.Namespaces().DeleteNamespace(tName + "/" + nsName); err != nil { + if err = client.Namespaces().DeleteNamespace(tName + "/" + nsName); err != nil { t.Fatalf("ERROR_DELETING_TEST_NS: %v", err) } }, From 62717605453e810186e8bf720b174f9084cab9f5 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 21:26:14 +0800 Subject: [PATCH 09/11] fix --- pulsar/resource_pulsar_topic_test.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index 205cef4..b2f96b7 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -110,10 +110,9 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { topicName := acctest.RandString(10) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories, - PreventPostDestroyRefresh: false, - CheckDestroy: testPulsarTopicDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testPulsarTopicDestroy, Steps: []resource.TestStep{ { Config: testPulsarNamespaceWithTopic(testWebServiceURL, tName, nsName, topicName), @@ -135,13 +134,15 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { t.Fatalf("ERROR_READ_NAMESPACE: %v", err) } - t.Logf("topicName: %v, namespace: %v", topicName, namespace) + t.Logf("topicName: %v, namespace: %s", topicName, namespace) partitionedTopics, nonPartitionedTopics, err := client.Topics().List(*namespace) if err != nil { t.Fatalf("ERROR_READ_TOPIC_DATA: %v", err) } + t.Logf("partitionedTopics: %v, nonPartitionedTopics: %v", partitionedTopics, nonPartitionedTopics) + for _, topic := range append(partitionedTopics, nonPartitionedTopics...) { if topicName.String() == topic { if err = client.Topics().Delete(*topicName, false, false); err != nil { @@ -426,10 +427,6 @@ resource "pulsar_namespace" "test" { topic_auto_creation { enable = false } - - depends_on = [ - pulsar_tenant.test_tenant - ] } resource "pulsar_topic" "test" { @@ -438,10 +435,6 @@ resource "pulsar_topic" "test" { topic_type = "persistent" topic_name = "%s" partitions = 0 - depends_on = [ - pulsar_namespace.test, - pulsar_tenant.test_tenant - ] } `, wsURL, tenant, ns, tenant, ns, topicName) } From 29dfaa35909ad2412d0651b875132383068e21a1 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 23:07:29 +0800 Subject: [PATCH 10/11] fix ci --- pulsar/resource_pulsar_topic_test.go | 35 +++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pulsar/resource_pulsar_topic_test.go b/pulsar/resource_pulsar_topic_test.go index b2f96b7..237c44f 100644 --- a/pulsar/resource_pulsar_topic_test.go +++ b/pulsar/resource_pulsar_topic_test.go @@ -44,15 +44,15 @@ func TestTopic(t *testing.T) { { Config: testPulsarPartitionTopic, Check: resource.ComposeTestCheckFunc( - testPulsarTopicExists("pulsar_topic.sample-topic-1"), - testPulsarTopicExists("pulsar_topic.sample-topic-2"), + testPulsarTopicExists("pulsar_topic.sample-topic-1", t), + testPulsarTopicExists("pulsar_topic.sample-topic-2", t), ), }, { Config: testPulsarNonPartitionTopic, Check: resource.ComposeTestCheckFunc( - testPulsarTopicExists("pulsar_topic.sample-topic-3"), - testPulsarTopicExists("pulsar_topic.sample-topic-4"), + testPulsarTopicExists("pulsar_topic.sample-topic-3", t), + testPulsarTopicExists("pulsar_topic.sample-topic-4", t), ), }, }, @@ -117,14 +117,13 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { { Config: testPulsarNamespaceWithTopic(testWebServiceURL, tName, nsName, topicName), Check: resource.ComposeTestCheckFunc( - testPulsarTopicExists(resourceName), + testPulsarTopicExists(resourceName, t), ), + ExpectError: nil, }, { PreConfig: func() { client := getClientFromMeta(testAccProvider.Meta()) - t.Logf("tName: %s, nsName: %s, topicName: %s", tName, nsName, topicName) - topicName, err := utils.GetTopicName(fmt.Sprintf("persistent://%s/%s/%s", tName, nsName, topicName)) if err != nil { t.Fatalf("ERROR_GETTING_TOPIC_NAME: %v", err) @@ -133,19 +132,14 @@ func TestTopicNamespaceExternallyRemoved(t *testing.T) { if err != nil { t.Fatalf("ERROR_READ_NAMESPACE: %v", err) } - - t.Logf("topicName: %v, namespace: %s", topicName, namespace) - partitionedTopics, nonPartitionedTopics, err := client.Topics().List(*namespace) if err != nil { t.Fatalf("ERROR_READ_TOPIC_DATA: %v", err) } - t.Logf("partitionedTopics: %v, nonPartitionedTopics: %v", partitionedTopics, nonPartitionedTopics) - for _, topic := range append(partitionedTopics, nonPartitionedTopics...) { if topicName.String() == topic { - if err = client.Topics().Delete(*topicName, false, false); err != nil { + if err = client.Topics().Delete(*topicName, true, true); err != nil { t.Fatalf("ERROR_DELETING_TEST_TOPIC: %v", err) } } @@ -184,7 +178,7 @@ func testTopicWithPermissionGrantUpdate(t *testing.T, pnum int) { actions = ["produce", "consume"] }`), Check: resource.ComposeTestCheckFunc( - testPulsarTopicExists(resourceName), + testPulsarTopicExists(resourceName, t), resource.TestCheckResourceAttr(resourceName, "permission_grant.#", "2"), resource.TestCheckResourceAttr(resourceName, "permission_grant.0.role", "some-role-1"), resource.TestCheckResourceAttr(resourceName, "permission_grant.0.actions.#", "3"), @@ -204,7 +198,7 @@ func testTopicWithPermissionGrantUpdate(t *testing.T, pnum int) { actions = ["produce"] }`), Check: resource.ComposeTestCheckFunc( - testPulsarTopicExists(resourceName), + testPulsarTopicExists(resourceName, t), resource.TestCheckResourceAttr(resourceName, "permission_grant.#", "1"), resource.TestCheckResourceAttr(resourceName, "permission_grant.0.role", "some-role-2"), resource.TestCheckResourceAttr(resourceName, "permission_grant.0.actions.#", "1"), @@ -247,7 +241,7 @@ func testPulsarTopicDestroy(s *terraform.State) error { return nil } -func testPulsarTopicExists(topic string) resource.TestCheckFunc { +func testPulsarTopicExists(topic string, t *testing.T) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[topic] if !ok { @@ -258,6 +252,7 @@ func testPulsarTopicExists(topic string) resource.TestCheckFunc { if err != nil { return fmt.Errorf("ERROR_READ_TOPIC: %w", err) } + t.Logf("topicName: %v", topicName) namespace, err := utils.GetNameSpaceName(topicName.GetTenant(), topicName.GetNamespace()) if err != nil { return fmt.Errorf("ERROR_READ_NAMESPACE: %w", err) @@ -427,6 +422,10 @@ resource "pulsar_namespace" "test" { topic_auto_creation { enable = false } + + depends_on = [ + pulsar_tenant.test_tenant + ] } resource "pulsar_topic" "test" { @@ -435,6 +434,10 @@ resource "pulsar_topic" "test" { topic_type = "persistent" topic_name = "%s" partitions = 0 + + depends_on = [ + pulsar_namespace.test + ] } `, wsURL, tenant, ns, tenant, ns, topicName) } From 6fccfb697912c4f3578848b931df0bb6c1ad07c0 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Wed, 18 Dec 2024 23:16:32 +0800 Subject: [PATCH 11/11] fix ci --- pulsar/resource_pulsar_sink_test.go | 3 +++ pulsar/resource_pulsar_source_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pulsar/resource_pulsar_sink_test.go b/pulsar/resource_pulsar_sink_test.go index 601a49e..5d1784c 100644 --- a/pulsar/resource_pulsar_sink_test.go +++ b/pulsar/resource_pulsar_sink_test.go @@ -126,6 +126,9 @@ func TestImportExistingSink(t *testing.T) { "default", sinkName, ); err != nil { + if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { + return + } t.Fatalf("ERROR_DELETING_TEST_SINK: %v", err) } }) diff --git a/pulsar/resource_pulsar_source_test.go b/pulsar/resource_pulsar_source_test.go index d1574de..0ca7bf5 100644 --- a/pulsar/resource_pulsar_source_test.go +++ b/pulsar/resource_pulsar_source_test.go @@ -143,6 +143,9 @@ func TestImportExistingSource(t *testing.T) { "public", "default", sourceName); err != nil { + if cliErr, ok := err.(rest.Error); ok && cliErr.Code == 404 { + return + } t.Fatalf("ERROR_DELETING_TEST_SOURCE: %v", err) } })