Skip to content

Commit

Permalink
Add test for a topic with no config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mongey committed Dec 5, 2018
1 parent 3949240 commit 8540be8
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion kafka/resource_kafka_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package kafka

import (
"fmt"
"log"
"testing"

r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func testAccTopicConfigUpdate(t *testing.T) {
func TestAccBasicTopic(t *testing.T) {
r.Test(t, r.TestCase{
Providers: accProvider(),
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
{
Config: testResourceTopic_noConfig,
Check: testResourceTopic_noConfigCheck,
},
},
})
}

func TestAccTopicConfigUpdate(t *testing.T) {
r.Test(t, r.TestCase{
Providers: accProvider(),
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -42,6 +56,44 @@ func TestAccTopicUpdatePartitions(t *testing.T) {
})
}

func testResourceTopic_noConfigCheck(s *terraform.State) error {
resourceState := s.Modules[0].Resources["kafka_topic.test"]
if resourceState == nil {
return fmt.Errorf("resource not found in state")
}

instanceState := resourceState.Primary
if instanceState == nil {
return fmt.Errorf("resource has no primary instance")
}

name := instanceState.ID

if name != instanceState.Attributes["name"] {
return fmt.Errorf("id doesn't match name")
}

if name != "syslog" {
return fmt.Errorf("unexpected topic name %s", name)
}

client := testProvider.Meta().(*Client)

topic, err := client.ReadTopic("syslog")

if err != nil {
return err
}

log.Println("[INFO] Hello from tests")

if len(topic.Config) != 0 {
return fmt.Errorf("expected no configs for %s, got %v", name, topic.Config)
}

return nil
}

func testResourceTopic_initialCheck(s *terraform.State) error {
resourceState := s.Modules[0].Resources["kafka_topic.test"]
if resourceState == nil {
Expand Down Expand Up @@ -120,6 +172,18 @@ func testResourceTopic_updatePartitionsCheck(s *terraform.State) error {
return nil
}

const testResourceTopic_noConfig = `
provider "kafka" {
bootstrap_servers = ["localhost:9092"]
}
resource "kafka_topic" "test" {
name = "syslog"
replication_factor = 1
partitions = 1
}
`

const testResourceTopic_initialConfig = `
provider "kafka" {
bootstrap_servers = ["localhost:9092"]
Expand Down

0 comments on commit 8540be8

Please sign in to comment.