Skip to content

Commit a6b3925

Browse files
committed
fix linting test and add a custom acc test func
1 parent 67f4628 commit a6b3925

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

kafka/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func (c *Client) getKafkaTopics() ([]Topic, error) {
605605
return nil, err
606606
}
607607
topicList := make([]Topic, len(topics))
608-
for i, _ := range topicList {
608+
for i := range topicList {
609609
topicList[i], err = c.ReadTopic(topics[i], true)
610610
if err != nil {
611611
return nil, err

kafka/data_source_kafka_topics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package kafka
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7-
"time"
88
)
99

1010
func kafkaTopicsDataSource() *schema.Resource {
@@ -60,13 +60,13 @@ func dataSourceTopicsRead(ctx context.Context, d *schema.ResourceData, meta inte
6060
if err != nil {
6161
return diag.FromErr(err)
6262
}
63-
d.SetId(time.Now().UTC().String())
63+
d.SetId(fmt.Sprint(len(topics)))
6464
return diags
6565
}
6666

6767
func flattenTopicsData(topicList *[]Topic) []interface{} {
6868
if topicList != nil {
69-
topics := make([]interface{}, len(*topicList), len(*topicList))
69+
topics := make([]interface{}, len(*topicList))
7070
for i, topic := range *topicList {
7171
topicObj := make(map[string]interface{})
7272
topicObj["topic_name"] = topic.Name

kafka/data_source_kafka_topics_test.go

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,77 @@
11
package kafka
22

33
import (
4+
"fmt"
5+
"github.com/hashicorp/go-uuid"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
47
"testing"
58

69
r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
710
)
811

912
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+
1019
bs := testBootstrapServers[0]
11-
// Should be only one topic in a brand new kafka cluster
12-
expectedTopic := "__confluent.support.metrics"
1320
r.Test(t, r.TestCase{
1421
ProviderFactories: overrideProviderFactory(),
1522
Steps: []r.TestStep{
1623
{
17-
Config: cfg(t, bs, testDataSourceKafkaTopics),
24+
Config: cfg(t, bs, fmt.Sprintf(testDataSourceKafkaTopics, topicName)),
1825
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,
2327
),
2428
},
2529
},
2630
})
2731
}
2832

2933
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+
}
3141
}
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]
3944
}
45+
`
4046

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+
}
4464

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
4777
}
48-
`

0 commit comments

Comments
 (0)