generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docker-backed acceptance test suite
- Loading branch information
1 parent
6233244
commit d2a9ecb
Showing
7 changed files
with
223 additions
and
36 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,49 @@ | ||
package modifier | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
// DefaultAttributePlanModifier is to set default value for an attribute | ||
// https://github.com/hashicorp/terraform-plugin-framework/issues/285 | ||
type DefaultAttributePlanModifier struct { | ||
value attr.Value | ||
} | ||
|
||
func DefaultAttribute(value attr.Value) DefaultAttributePlanModifier { | ||
return DefaultAttributePlanModifier{value: value} | ||
} | ||
|
||
func (m DefaultAttributePlanModifier) Modify( | ||
ctx context.Context, | ||
req tfsdk.ModifyAttributePlanRequest, | ||
resp *tfsdk.ModifyAttributePlanResponse, | ||
) { | ||
if req.AttributeConfig == nil || resp.AttributePlan == nil { | ||
return | ||
} | ||
|
||
// if configuration was provided, then don't use the default | ||
if !req.AttributeConfig.IsNull() { | ||
return | ||
} | ||
|
||
// If the plan is known and not null (for example due to another plan modifier), | ||
// don't set the default value | ||
if !resp.AttributePlan.IsUnknown() && !resp.AttributePlan.IsNull() { | ||
return | ||
} | ||
|
||
resp.AttributePlan = m.value | ||
} | ||
|
||
func (m DefaultAttributePlanModifier) Description(ctx context.Context) string { | ||
return "Use a static default value for an attribute" | ||
} | ||
|
||
func (m DefaultAttributePlanModifier) MarkdownDescription(ctx context.Context) string { | ||
return m.Description(ctx) | ||
} |
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,105 @@ | ||
package provider | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ory/dockertest/v3" | ||
"github.com/ory/dockertest/v3/docker" | ||
kafka "github.com/segmentio/kafka-go" | ||
) | ||
|
||
const ( | ||
existingTopic = "read.me" | ||
) | ||
|
||
// Configure mock Kafka cluster and teardown | ||
func TestMain(t *testing.M) { | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
log.Fatalf("Could not connect to docker: %s", err) | ||
} | ||
|
||
network, err := pool.CreateNetwork("terraform-provider-kafka") | ||
if err != nil { | ||
log.Fatalf("Could not start network: %s", err) | ||
} | ||
|
||
zkContainer, err := pool.RunWithOptions(&dockertest.RunOptions{ | ||
Repository: "docker.io/bitnami/zookeeper", | ||
Tag: "3.8", | ||
Env: []string{"ALLOW_ANONYMOUS_LOGIN=yes"}, | ||
Hostname: "zookeeper", | ||
NetworkID: network.Network.ID, | ||
ExposedPorts: []string{"2181"}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Could not start resource: %s", err) | ||
} | ||
|
||
kafkaContainer, err := pool.RunWithOptions(&dockertest.RunOptions{ | ||
Repository: "docker.io/bitnami/kafka", | ||
Tag: "3.3", | ||
Env: []string{ | ||
"KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181", | ||
"KAFKA_CFG_BROKER_ID=0", | ||
"ALLOW_PLAINTEXT_LISTENER=yes", | ||
"KAFKA_CFG_LISTENERS=PLAINTEXT://:9092", | ||
"KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092", | ||
}, | ||
Hostname: "kafka-1", | ||
NetworkID: network.Network.ID, | ||
PortBindings: map[docker.Port][]docker.PortBinding{ | ||
"9092/tcp": {{HostIP: "localhost", HostPort: "9092/tcp"}}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Could not start resource: %s", err) | ||
} | ||
|
||
waitForKafka := func() error { | ||
conn, err := kafka.Dial("tcp", "localhost:9092") | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
_, err = conn.ApiVersions() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Bootstrap test topic | ||
topic := kafka.TopicConfig{ | ||
Topic: existingTopic, | ||
NumPartitions: 1, | ||
ReplicationFactor: 1, | ||
} | ||
err = conn.CreateTopics(topic) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
if err = pool.Retry(waitForKafka); err != nil { | ||
log.Fatalf("could not connect to kafka: %s", err) | ||
} | ||
|
||
code := t.Run() | ||
|
||
// You can't defer this because os.Exit doesn't care for defer | ||
if err := pool.Purge(kafkaContainer); err != nil { | ||
log.Fatalf("Could not purge resource: %s", err) | ||
} | ||
if err := pool.Purge(zkContainer); err != nil { | ||
log.Fatalf("Could not purge resource: %s", err) | ||
} | ||
if err := pool.RemoveNetwork(network); err != nil { | ||
log.Fatalf("Could not purge network: %s", err) | ||
} | ||
|
||
os.Exit(code) | ||
} |
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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccExampleDataSource(t *testing.T) { | ||
func TestAccTopicDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
Config: testAccExampleDataSourceConfig, | ||
Config: testAccTopicDataSourceConfig(existingTopic), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "id", "example"), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "name", "example"), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "partitions", "3"), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "replication_factor", "3"), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "id", existingTopic), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "name", existingTopic), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "partitions", "1"), | ||
resource.TestCheckResourceAttr("data.kafka_topic.test", "replication_factor", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccExampleDataSourceConfig = providerConfig + ` | ||
func testAccTopicDataSourceConfig(name string) string { | ||
return fmt.Sprintf(providerConfig+` | ||
data "kafka_topic" "test" { | ||
name = "example" | ||
name = %[1]q | ||
} | ||
`, name) | ||
} | ||
` |
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