|
1 | 1 | package kafka
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/go-uuid" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
4 | 7 | "testing"
|
5 | 8 |
|
6 | 9 | r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
7 | 10 | )
|
8 | 11 |
|
9 | 12 | func TestAcc_Topics(t *testing.T) {
|
| 13 | + u, err := uuid.GenerateUUID() |
| 14 | + if err != nil { |
| 15 | + t.Fatal(err) |
| 16 | + } |
| 17 | + topicName := fmt.Sprintf("syslog-%s", u) |
| 18 | + |
10 | 19 | bs := testBootstrapServers[0]
|
11 |
| - // Should be only one topic in a brand new kafka cluster |
12 |
| - expectedTopic := "__confluent.support.metrics" |
13 | 20 | r.Test(t, r.TestCase{
|
14 | 21 | ProviderFactories: overrideProviderFactory(),
|
15 | 22 | Steps: []r.TestStep{
|
16 | 23 | {
|
17 |
| - Config: cfg(t, bs, testDataSourceKafkaTopics), |
| 24 | + Config: cfg(t, bs, fmt.Sprintf(testDataSourceKafkaTopics, topicName)), |
18 | 25 | Check: r.ComposeTestCheckFunc(
|
19 |
| - r.TestCheckOutput("partitions", "1"), |
20 |
| - r.TestCheckOutput("replication_factor", "3"), |
21 |
| - r.TestCheckOutput("topic_name", expectedTopic), |
22 |
| - r.TestCheckOutput("retention_ms", "31536000000"), |
| 26 | + testDatasourceTopics, |
23 | 27 | ),
|
24 | 28 | },
|
25 | 29 | },
|
26 | 30 | })
|
27 | 31 | }
|
28 | 32 |
|
29 | 33 | const testDataSourceKafkaTopics = `
|
30 |
| -data "kafka_topics" "test" { |
| 34 | +resource "kafka_topic" "test" { |
| 35 | + name = "%[1]s" |
| 36 | + replication_factor = 1 |
| 37 | + partitions = 1 |
| 38 | + config = { |
| 39 | + "retention.ms" = "22222" |
| 40 | + } |
31 | 41 | }
|
32 |
| -
|
33 |
| -output "partitions" { |
34 |
| - value = data.kafka_topics.test.list[0].partitions |
35 |
| -} |
36 |
| -
|
37 |
| -output "replication_factor" { |
38 |
| - value = data.kafka_topics.test.list[0].replication_factor |
| 42 | +data "kafka_topics" "test" { |
| 43 | + depends_on = [kafka_topic.test] |
39 | 44 | }
|
| 45 | +` |
40 | 46 |
|
41 |
| -output "topic_name" { |
42 |
| - value = data.kafka_topics.test.list[0].topic_name |
43 |
| -} |
| 47 | +func testDatasourceTopics(s *terraform.State) error { |
| 48 | + resourceState := s.Modules[0].Resources["data.kafka_topics.test"] |
| 49 | + if resourceState == nil { |
| 50 | + return fmt.Errorf("resource not found in state") |
| 51 | + } |
| 52 | + instanceState := resourceState.Primary |
| 53 | + client := testProvider.Meta().(*LazyClient) |
| 54 | + expectedTopics, err := client.GetKafkaTopics() |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf(err.Error()) |
| 57 | + } |
| 58 | + for i := 0; i < len(expectedTopics); i++ { |
| 59 | + expectedTopicName := instanceState.Attributes[fmt.Sprintf("list.%d.topic_name", i)] |
| 60 | + expectedTopicOutput, err := client.ReadTopic(expectedTopicName, true) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf(err.Error()) |
| 63 | + } |
44 | 64 |
|
45 |
| -output "retention_ms" { |
46 |
| - value = data.kafka_topics.test.list[0].config["retention.ms"] |
| 65 | + if instanceState.Attributes[fmt.Sprintf("list.%d.partitions", i)] != fmt.Sprint(expectedTopicOutput.Partitions) { |
| 66 | + return fmt.Errorf("expected %d for topic %s partition, got %s", expectedTopicOutput.Partitions, expectedTopicOutput.Name, instanceState.Attributes[fmt.Sprintf("list.%d.partitions", i)]) |
| 67 | + } |
| 68 | + if instanceState.Attributes[fmt.Sprintf("list.%d.replication_factor", i)] != fmt.Sprint(expectedTopicOutput.ReplicationFactor) { |
| 69 | + return fmt.Errorf("expected %d for topic %s replication factor, got %s", expectedTopicOutput.ReplicationFactor, expectedTopicOutput.Name, instanceState.Attributes[fmt.Sprintf("list.%d.replication_factor", i)]) |
| 70 | + } |
| 71 | + retentionMs := expectedTopicOutput.Config["retention.ms"] |
| 72 | + if instanceState.Attributes[fmt.Sprintf("list.%d.config.retention.ms", i)] != *retentionMs { |
| 73 | + return fmt.Errorf("expected %s for topic %s config retention.ms, got %s", *retentionMs, expectedTopicOutput.Name, instanceState.Attributes[fmt.Sprintf("list.%d.config.retention.ms", i)]) |
| 74 | + } |
| 75 | + } |
| 76 | + return nil |
47 | 77 | }
|
48 |
| -` |
|
0 commit comments