Skip to content

Commit e84b619

Browse files
committed
Update tests
1 parent b02f5b1 commit e84b619

File tree

2 files changed

+104
-178
lines changed

2 files changed

+104
-178
lines changed

pulsar/resource_pulsar_namespace_defaults_test.go

Lines changed: 46 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ func TestNamespaceParameterDefaults(t *testing.T) {
6161
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.max_producers_per_topic", "-1"),
6262
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.message_ttl_seconds", "-1"),
6363
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.subscription_expiration_time_minutes", "-1"),
64-
// Verify these are using broker defaults (not explicitly set to 0)
65-
testNamespaceUsesDefaults(resourceName),
6664
),
6765
},
6866
},
6967
})
7068
}
7169

72-
// TestNamespaceSubscriptionExpirationRemoval verifies subscription_expiration_time_minutes can be removed
73-
func TestNamespaceSubscriptionExpirationRemoval(t *testing.T) {
70+
// TestNamespaceConfigRemoval verifies subscription_expiration_time_minutes can be removed
71+
// This is the only removal supported by the API at this time
72+
func TestNamespaceConfigRemoval(t *testing.T) {
7473
resourceName := "pulsar_namespace.test"
7574
nsname := acctest.RandString(10)
7675

@@ -96,7 +95,6 @@ func TestNamespaceSubscriptionExpirationRemoval(t *testing.T) {
9695
Check: resource.ComposeTestCheckFunc(
9796
testPulsarNamespaceExists(resourceName),
9897
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.subscription_expiration_time_minutes", "60"),
99-
testNamespaceHasSubscriptionExpiration(resourceName, 60),
10098
),
10199
},
102100
{
@@ -116,8 +114,12 @@ func TestNamespaceSubscriptionExpirationRemoval(t *testing.T) {
116114
Check: resource.ComposeTestCheckFunc(
117115
testPulsarNamespaceExists(resourceName),
118116
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.subscription_expiration_time_minutes", "-1"),
119-
// Verify it was removed (should return 0 which is the broker default)
120-
testNamespaceHasSubscriptionExpiration(resourceName, 0),
117+
// Verify -1 values are actually set via API
118+
testNamespaceConfigValue(resourceName, "subscription expiration time", -1, func(c, ns interface{}) (int, error) {
119+
return c.(interface {
120+
GetSubscriptionExpirationTime(utils.NameSpaceName) (int, error)
121+
}).GetSubscriptionExpirationTime(*ns.(*utils.NameSpaceName))
122+
}),
121123
),
122124
},
123125
},
@@ -159,15 +161,45 @@ func TestNamespaceExplicitZeroValues(t *testing.T) {
159161
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.message_ttl_seconds", "0"),
160162
resource.TestCheckResourceAttr(resourceName, "namespace_config.0.subscription_expiration_time_minutes", "0"),
161163
// Verify 0 values are actually set via API
162-
testNamespaceHasExplicitZeros(resourceName),
164+
testNamespaceConfigValue(resourceName, "max consumers per subscription", 0, func(c, ns interface{}) (int, error) {
165+
return c.(interface {
166+
GetMaxConsumersPerSubscription(utils.NameSpaceName) (int, error)
167+
}).GetMaxConsumersPerSubscription(*ns.(*utils.NameSpaceName))
168+
}),
169+
testNamespaceConfigValue(resourceName, "max consumers per topic", 0, func(c, ns interface{}) (int, error) {
170+
return c.(interface {
171+
GetMaxConsumersPerTopic(utils.NameSpaceName) (int, error)
172+
}).GetMaxConsumersPerTopic(*ns.(*utils.NameSpaceName))
173+
}),
174+
testNamespaceConfigValue(resourceName, "max producers per topic", 0, func(c, ns interface{}) (int, error) {
175+
return c.(interface {
176+
GetMaxProducersPerTopic(utils.NameSpaceName) (int, error)
177+
}).GetMaxProducersPerTopic(*ns.(*utils.NameSpaceName))
178+
}),
179+
testNamespaceConfigValue(resourceName, "message TTL", 0, func(c, ns interface{}) (int, error) {
180+
return c.(interface {
181+
GetNamespaceMessageTTL(string) (int, error)
182+
}).GetNamespaceMessageTTL(ns.(*utils.NameSpaceName).String())
183+
}),
184+
testNamespaceConfigValue(resourceName, "subscription expiration time", 0, func(c, ns interface{}) (int, error) {
185+
return c.(interface {
186+
GetSubscriptionExpirationTime(utils.NameSpaceName) (int, error)
187+
}).GetSubscriptionExpirationTime(*ns.(*utils.NameSpaceName))
188+
}),
163189
),
164190
},
165191
},
166192
})
167193
}
168194

169-
// Helper function to verify namespace is using broker defaults
170-
func testNamespaceUsesDefaults(resourceName string) resource.TestCheckFunc {
195+
// Helper function to verify namespace has a specific config value
196+
// The getConfig parameter is a function that calls the specific API method on the namespace client
197+
func testNamespaceConfigValue(
198+
resourceName string,
199+
configName string,
200+
expectedValue int,
201+
getConfig func(client interface{}, ns interface{}) (int, error),
202+
) resource.TestCheckFunc {
171203
return func(s *terraform.State) error {
172204
rs, ok := s.RootModule().Resources[resourceName]
173205
if !ok {
@@ -182,111 +214,13 @@ func testNamespaceUsesDefaults(resourceName string) resource.TestCheckFunc {
182214
}
183215

184216
client := getClientFromMeta(testAccProvider.Meta()).Namespaces()
185-
186-
// When using defaults, these should return broker default values
187-
// The actual default values depend on the broker configuration
188-
// We're mainly checking that our -1 default didn't cause Set operations
189-
190-
// For parameters without Remove methods, they might return 0 if never set
191-
maxConsPerSub, err := client.GetMaxConsumersPerSubscription(*ns)
192-
if err != nil {
193-
return fmt.Errorf("failed to get max consumers per subscription: %w", err)
194-
}
195-
// If never set, broker typically returns 0
196-
if maxConsPerSub != 0 {
197-
return fmt.Errorf("expected max consumers per subscription to be broker default (0), got %d", maxConsPerSub)
198-
}
199-
200-
return nil
201-
}
202-
}
203-
204-
// Helper function to verify namespace has specific subscription expiration
205-
func testNamespaceHasSubscriptionExpiration(resourceName string, expected int) resource.TestCheckFunc {
206-
return func(s *terraform.State) error {
207-
rs, ok := s.RootModule().Resources[resourceName]
208-
if !ok {
209-
return fmt.Errorf("resource %s not found", resourceName)
210-
}
211-
212-
tenant := rs.Primary.Attributes["tenant"]
213-
namespace := rs.Primary.Attributes["namespace"]
214-
ns, err := utils.GetNameSpaceName(tenant, namespace)
215-
if err != nil {
216-
return err
217-
}
218-
219-
client := getClientFromMeta(testAccProvider.Meta()).Namespaces()
220-
221-
actual, err := client.GetSubscriptionExpirationTime(*ns)
222-
if err != nil {
223-
return fmt.Errorf("failed to get subscription expiration time: %w", err)
224-
}
225-
226-
if actual != expected {
227-
return fmt.Errorf("expected subscription expiration time %d, got %d", expected, actual)
228-
}
229-
230-
return nil
231-
}
232-
}
233-
234-
// Helper function to verify namespace has explicit zero values
235-
func testNamespaceHasExplicitZeros(resourceName string) resource.TestCheckFunc {
236-
return func(s *terraform.State) error {
237-
rs, ok := s.RootModule().Resources[resourceName]
238-
if !ok {
239-
return fmt.Errorf("resource %s not found", resourceName)
240-
}
241-
242-
tenant := rs.Primary.Attributes["tenant"]
243-
namespace := rs.Primary.Attributes["namespace"]
244-
ns, err := utils.GetNameSpaceName(tenant, namespace)
245-
if err != nil {
246-
return err
247-
}
248-
249-
client := getClientFromMeta(testAccProvider.Meta()).Namespaces()
250-
251-
// Verify all values are explicitly set to 0
252-
maxConsPerSub, err := client.GetMaxConsumersPerSubscription(*ns)
253-
if err != nil {
254-
return fmt.Errorf("failed to get max consumers per subscription: %w", err)
255-
}
256-
if maxConsPerSub != 0 {
257-
return fmt.Errorf("expected max consumers per subscription 0, got %d", maxConsPerSub)
258-
}
259-
260-
maxConsPerTopic, err := client.GetMaxConsumersPerTopic(*ns)
261-
if err != nil {
262-
return fmt.Errorf("failed to get max consumers per topic: %w", err)
263-
}
264-
if maxConsPerTopic != 0 {
265-
return fmt.Errorf("expected max consumers per topic 0, got %d", maxConsPerTopic)
266-
}
267-
268-
maxProdPerTopic, err := client.GetMaxProducersPerTopic(*ns)
269-
if err != nil {
270-
return fmt.Errorf("failed to get max producers per topic: %w", err)
271-
}
272-
if maxProdPerTopic != 0 {
273-
return fmt.Errorf("expected max producers per topic 0, got %d", maxProdPerTopic)
274-
}
275-
276-
messageTTL, err := client.GetNamespaceMessageTTL(ns.String())
217+
actualValue, err := getConfig(client, ns)
277218
if err != nil {
278-
return fmt.Errorf("failed to get message TTL: %w", err)
279-
}
280-
if messageTTL != 0 {
281-
return fmt.Errorf("expected message TTL 0, got %d", messageTTL)
219+
return fmt.Errorf("failed to get %s: %w", configName, err)
282220
}
283221

284-
subExpTime, err := client.GetSubscriptionExpirationTime(*ns)
285-
if err != nil {
286-
return fmt.Errorf("failed to get subscription expiration time: %w", err)
287-
}
288-
if subExpTime != 0 {
289-
return fmt.Errorf("expected subscription expiration time 0, got %d", subExpTime)
222+
if actualValue != expectedValue {
223+
return fmt.Errorf("expected %s %d, got %d", configName, expectedValue, actualValue)
290224
}
291225

292226
return nil

pulsar/resource_pulsar_topic_defaults_test.go

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ func TestTopicParameterDefaults(t *testing.T) {
6565
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_unacked_messages_per_subscription", "-1"),
6666
resource.TestCheckResourceAttr(resourceName, "topic_config.0.msg_publish_rate", "-1"),
6767
resource.TestCheckResourceAttr(resourceName, "topic_config.0.byte_publish_rate", "-1"),
68-
// Verify no topic-level policies are set via API
69-
testTopicHasNoExplicitConfig(resourceName),
7068
),
7169
},
7270
},
@@ -112,7 +110,21 @@ func TestTopicParameterRemoval(t *testing.T) {
112110
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_consumers", "100"),
113111
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_producers", "50"),
114112
resource.TestCheckResourceAttr(resourceName, "topic_config.0.message_ttl_seconds", "3600"),
115-
testTopicHasExplicitConfig(resourceName, 100, 50, 3600),
113+
testTopicConfigValue(resourceName, "max consumers", 100, func(c, tn interface{}) (int, error) {
114+
return c.(interface {
115+
GetMaxConsumers(utils.TopicName) (int, error)
116+
}).GetMaxConsumers(*tn.(*utils.TopicName))
117+
}),
118+
testTopicConfigValue(resourceName, "max producers", 50, func(c, tn interface{}) (int, error) {
119+
return c.(interface {
120+
GetMaxProducers(utils.TopicName) (int, error)
121+
}).GetMaxProducers(*tn.(*utils.TopicName))
122+
}),
123+
testTopicConfigValue(resourceName, "message TTL", 3600, func(c, tn interface{}) (int, error) {
124+
return c.(interface {
125+
GetMessageTTL(utils.TopicName) (int, error)
126+
}).GetMessageTTL(*tn.(*utils.TopicName))
127+
}),
116128
),
117129
},
118130
{
@@ -138,8 +150,22 @@ func TestTopicParameterRemoval(t *testing.T) {
138150
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_consumers", "-1"),
139151
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_producers", "-1"),
140152
resource.TestCheckResourceAttr(resourceName, "topic_config.0.message_ttl_seconds", "-1"),
141-
// Verify topic-level policies were removed
142-
testTopicHasNoExplicitConfig(resourceName),
153+
// Verify -1 values are actually set via API
154+
testTopicConfigValue(resourceName, "max consumers", -1, func(c, tn interface{}) (int, error) {
155+
return c.(interface {
156+
GetMaxConsumers(utils.TopicName) (int, error)
157+
}).GetMaxConsumers(*tn.(*utils.TopicName))
158+
}),
159+
testTopicConfigValue(resourceName, "max producers", -1, func(c, tn interface{}) (int, error) {
160+
return c.(interface {
161+
GetMaxProducers(utils.TopicName) (int, error)
162+
}).GetMaxProducers(*tn.(*utils.TopicName))
163+
}),
164+
testTopicConfigValue(resourceName, "message TTL", -1, func(c, tn interface{}) (int, error) {
165+
return c.(interface {
166+
GetMessageTTL(utils.TopicName) (int, error)
167+
}).GetMessageTTL(*tn.(*utils.TopicName))
168+
}),
143169
),
144170
},
145171
},
@@ -185,15 +211,35 @@ func TestTopicExplicitZeroValues(t *testing.T) {
185211
resource.TestCheckResourceAttr(resourceName, "topic_config.0.max_producers", "0"),
186212
resource.TestCheckResourceAttr(resourceName, "topic_config.0.message_ttl_seconds", "0"),
187213
// Verify 0 values are actually set via API
188-
testTopicHasExplicitConfig(resourceName, 0, 0, 0),
214+
testTopicConfigValue(resourceName, "max consumers", 0, func(c, tn interface{}) (int, error) {
215+
return c.(interface {
216+
GetMaxConsumers(utils.TopicName) (int, error)
217+
}).GetMaxConsumers(*tn.(*utils.TopicName))
218+
}),
219+
testTopicConfigValue(resourceName, "max producers", 0, func(c, tn interface{}) (int, error) {
220+
return c.(interface {
221+
GetMaxProducers(utils.TopicName) (int, error)
222+
}).GetMaxProducers(*tn.(*utils.TopicName))
223+
}),
224+
testTopicConfigValue(resourceName, "message TTL", 0, func(c, tn interface{}) (int, error) {
225+
return c.(interface {
226+
GetMessageTTL(utils.TopicName) (int, error)
227+
}).GetMessageTTL(*tn.(*utils.TopicName))
228+
}),
189229
),
190230
},
191231
},
192232
})
193233
}
194234

195-
// Helper function to verify no topic-level configuration is set
196-
func testTopicHasNoExplicitConfig(resourceName string) resource.TestCheckFunc {
235+
// Helper function to verify topic has a specific config value
236+
// The getConfig parameter is a function that calls the specific API method on the topic client
237+
func testTopicConfigValue(
238+
resourceName string,
239+
configName string,
240+
expectedValue int,
241+
getConfig func(client interface{}, topicName interface{}) (int, error),
242+
) resource.TestCheckFunc {
197243
return func(s *terraform.State) error {
198244
rs, ok := s.RootModule().Resources[resourceName]
199245
if !ok {
@@ -206,67 +252,13 @@ func testTopicHasNoExplicitConfig(resourceName string) resource.TestCheckFunc {
206252
}
207253

208254
client := getClientFromMeta(testAccProvider.Meta()).Topics()
209-
210-
// Check that getting these values returns errors (not found/not set)
211-
// The actual error type/message depends on whether topic policies are enabled
212-
_, err = client.GetMaxConsumers(*topicName)
213-
if err == nil {
214-
return fmt.Errorf("expected GetMaxConsumers to fail for unset value, but it succeeded")
215-
}
216-
217-
_, err = client.GetMaxProducers(*topicName)
218-
if err == nil {
219-
return fmt.Errorf("expected GetMaxProducers to fail for unset value, but it succeeded")
220-
}
221-
222-
_, err = client.GetMessageTTL(*topicName)
223-
if err == nil {
224-
return fmt.Errorf("expected GetMessageTTL to fail for unset value, but it succeeded")
225-
}
226-
227-
return nil
228-
}
229-
}
230-
231-
// Helper function to verify topic has explicit configuration
232-
func testTopicHasExplicitConfig(resourceName string, maxConsumers, maxProducers,
233-
messageTTL int) resource.TestCheckFunc {
234-
return func(s *terraform.State) error {
235-
rs, ok := s.RootModule().Resources[resourceName]
236-
if !ok {
237-
return fmt.Errorf("resource %s not found", resourceName)
238-
}
239-
240-
topicName, err := utils.GetTopicName(rs.Primary.ID)
255+
actualValue, err := getConfig(client, topicName)
241256
if err != nil {
242-
return err
257+
return fmt.Errorf("failed to get %s: %w", configName, err)
243258
}
244259

245-
client := getClientFromMeta(testAccProvider.Meta()).Topics()
246-
247-
// Verify values are set correctly
248-
actualMaxConsumers, err := client.GetMaxConsumers(*topicName)
249-
if err != nil {
250-
return fmt.Errorf("failed to get max consumers: %w", err)
251-
}
252-
if actualMaxConsumers != maxConsumers {
253-
return fmt.Errorf("expected max consumers %d, got %d", maxConsumers, actualMaxConsumers)
254-
}
255-
256-
actualMaxProducers, err := client.GetMaxProducers(*topicName)
257-
if err != nil {
258-
return fmt.Errorf("failed to get max producers: %w", err)
259-
}
260-
if actualMaxProducers != maxProducers {
261-
return fmt.Errorf("expected max producers %d, got %d", maxProducers, actualMaxProducers)
262-
}
263-
264-
actualMessageTTL, err := client.GetMessageTTL(*topicName)
265-
if err != nil {
266-
return fmt.Errorf("failed to get message TTL: %w", err)
267-
}
268-
if actualMessageTTL != messageTTL {
269-
return fmt.Errorf("expected message TTL %d, got %d", messageTTL, actualMessageTTL)
260+
if actualValue != expectedValue {
261+
return fmt.Errorf("expected %s %d, got %d", configName, expectedValue, actualValue)
270262
}
271263

272264
return nil

0 commit comments

Comments
 (0)