-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from aiven/savciuci-teacc-kafka-topic
testacc: kafka topic #151
- Loading branch information
Showing
3 changed files
with
183 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package aiven | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aiven/aiven-go-client" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestAccAivenKafkaTopic_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resourceName := "aiven_kafka_topic.foo" | ||
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAivenKafkaTopicResourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccKafkaTopicResource(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAivenKafkaTopicAttributes("data.aiven_kafka_topic.topic"), | ||
resource.TestCheckResourceAttr(resourceName, "project", fmt.Sprintf("test-acc-pr-%s", rName)), | ||
resource.TestCheckResourceAttr(resourceName, "service_name", fmt.Sprintf("test-acc-sr-%s", rName)), | ||
resource.TestCheckResourceAttr(resourceName, "topic_name", fmt.Sprintf("test-acc-topic-%s", rName)), | ||
resource.TestCheckResourceAttr(resourceName, "partitions", "3"), | ||
resource.TestCheckResourceAttr(resourceName, "replication", "2"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccKafkaTopicResource(name string) string { | ||
return fmt.Sprintf(` | ||
resource "aiven_project" "foo" { | ||
project = "test-acc-pr-%s" | ||
card_id="%s" | ||
} | ||
resource "aiven_service" "bar" { | ||
project = aiven_project.foo.project | ||
cloud_name = "google-europe-west1" | ||
plan = "business-4" | ||
service_name = "test-acc-sr-%s" | ||
service_type = "kafka" | ||
maintenance_window_dow = "monday" | ||
maintenance_window_time = "10:00:00" | ||
kafka_user_config { | ||
kafka_version = "2.4" | ||
kafka { | ||
group_max_session_timeout_ms = 70000 | ||
log_retention_bytes = 1000000000 | ||
} | ||
} | ||
} | ||
resource "aiven_kafka_topic" "foo" { | ||
project = aiven_project.foo.project | ||
service_name = aiven_service.bar.service_name | ||
topic_name = "test-acc-topic-%s" | ||
partitions = 3 | ||
replication = 2 | ||
} | ||
data "aiven_kafka_topic" "topic" { | ||
project = aiven_project.foo.project | ||
service_name = aiven_service.bar.service_name | ||
topic_name = aiven_kafka_topic.foo.topic_name | ||
} | ||
`, name, os.Getenv("AIVEN_CARD_ID"), name, name) | ||
} | ||
|
||
func testAccCheckAivenKafkaTopicAttributes(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
r := s.RootModule().Resources[n] | ||
a := r.Primary.Attributes | ||
|
||
log.Printf("[DEBUG] kafka topic attributes %v", a) | ||
|
||
if a["project"] == "" { | ||
return fmt.Errorf("expected to get a project name from Aiven") | ||
} | ||
|
||
if a["service_name"] == "" { | ||
return fmt.Errorf("expected to get a service_name from Aiven") | ||
} | ||
|
||
if a["topic_name"] == "" { | ||
return fmt.Errorf("expected to get a topic_name from Aiven") | ||
} | ||
|
||
if a["partitions"] == "" { | ||
return fmt.Errorf("expected to get partitions from Aiven") | ||
} | ||
|
||
if a["replication"] == "" { | ||
return fmt.Errorf("expected to get a replication from Aiven") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAivenKafkaTopicResourceDestroy(s *terraform.State) error { | ||
c := testAccProvider.Meta().(*aiven.Client) | ||
|
||
// loop through the resources in state, verifying each kafka topic is destroyed | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aiven_kafka_topic" { | ||
continue | ||
} | ||
project, serviceName, topicName := splitResourceID3(rs.Primary.ID) | ||
t, err := c.KafkaTopics.Get(project, serviceName, topicName) | ||
if err != nil { | ||
if err.(aiven.Error).Status != 404 { | ||
return err | ||
} | ||
} | ||
|
||
if t != nil { | ||
return fmt.Errorf("kafka topic (%s) still exists, id %s", topicName, rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters