diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 95e4c16..b402dfa 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -1,11 +1,14 @@ package provider import ( + "fmt" "os" + "strings" "testing" "github.com/hashicorp/terraform-plugin-framework/providerserver" "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/stretchr/testify/require" ) const ( @@ -41,6 +44,7 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe } type Config struct { + ResourceName string Name string Timeouts Timeouts MilliCPU int64 @@ -51,6 +55,52 @@ type Config struct { ReadReplicaSource string } +func (c *Config) String(t *testing.T) string { + c.setDefaults() + b := &strings.Builder{} + b.WriteString("\n \n") + write := func(format string, a ...any) { + _, err := fmt.Fprintf(b, format, a...) + require.NoError(t, err) + } + _, err := fmt.Fprintf(b, "resource timescale_service %q { \n", c.ResourceName) + require.NoError(t, err) + if c.Name != "" { + write("name = %q \n", c.Name) + } + if c.ReadReplicaSource != "" { + write("read_replica_source = %q \n", c.ReadReplicaSource) + } + if c.EnableHAReplica { + write("enable_ha_replica = %t \n", c.EnableHAReplica) + } + if c.RegionCode != "" { + write("region_code = %q \n", c.RegionCode) + } + write(` + milli_cpu = %d + memory_gb = %d + timeouts = { + create = %q + }`+"\n", + c.MilliCPU, c.MemoryGB, c.Timeouts.Create) + write("}") + res := b.String() + return res +} + +func (c *Config) setDefaults() { + if c.MilliCPU == 0 { + c.MilliCPU = 500 + } + if c.MemoryGB == 0 { + c.MemoryGB = 2 + } + if c.Timeouts.Create == "" { + c.Timeouts.Create = "10m" + } +} + type Timeouts struct { Create string } diff --git a/internal/provider/service_resource_test.go b/internal/provider/service_resource_test.go index 3554e11..9502352 100644 --- a/internal/provider/service_resource_test.go +++ b/internal/provider/service_resource_test.go @@ -96,6 +96,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { Name: "service resource test init", } replicaConfig = Config{ + ResourceName: "read_replica", ReadReplicaSource: "timescale_service.resource.id", MilliCPU: 500, MemoryGB: 2, @@ -106,7 +107,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, Steps: []resource.TestStep{ { - Config: newServiceWithReadReplica(primaryConfig, replicaConfig), + Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig), Check: resource.ComposeAggregateTestCheckFunc( // Verify service attributes resource.TestCheckResourceAttr("timescale_service.resource", "name", "service resource test init"), @@ -141,7 +142,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { Config: func() string { c := replicaConfig c.Name = "replica" - return newServiceWithReadReplica(primaryConfig, c) + return newServiceWithReadReplica(t, primaryConfig, c) }(), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("timescale_service.read_replica", "name", "replica"), @@ -153,7 +154,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { c := replicaConfig c.MilliCPU = 1000 c.MemoryGB = 4 - return newServiceWithReadReplica(primaryConfig, c) + return newServiceWithReadReplica(t, primaryConfig, c) }(), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("timescale_service.read_replica", "milli_cpu", "1000"), @@ -165,7 +166,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { Config: func() string { c := replicaConfig c.VpcID = DEFAULT_VPC_ID - return newServiceConfig(primaryConfig) + newReadReplicaVPC(c) + return newServiceConfig(primaryConfig) + c.String(t) }(), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("timescale_service.read_replica", "vpc_id", "2074"), @@ -173,7 +174,7 @@ func TestServiceResource_Read_Replica(t *testing.T) { }, // Remove VPC { - Config: newServiceWithReadReplica(primaryConfig, replicaConfig), + Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckNoResourceAttr("timescale_service.resource", "vpc_id"), ), @@ -360,35 +361,8 @@ func newServiceAddHAReplica(config Config) string { } // newServiceWithReadReplica creates two resources, a primary instance and a read replica -func newServiceWithReadReplica(primary, replica Config) string { - return newServiceConfig(primary) + newReadReplica(replica) -} - -func newReadReplica(config Config) string { - return fmt.Sprintf(` - resource "timescale_service" "read_replica" { - name = %q - read_replica_source = %q - milli_cpu = %d - memory_gb = %d - timeouts = { - create = %q - } - }`, config.Name, config.ReadReplicaSource, config.MilliCPU, config.MemoryGB, config.Timeouts.Create) -} - -func newReadReplicaVPC(config Config) string { - return fmt.Sprintf(` - resource "timescale_service" "read_replica" { - name = %q - read_replica_source = %q - milli_cpu = %d - memory_gb = %d - vpc_id = %d - timeouts = { - create = %q - } - }`, config.Name, config.ReadReplicaSource, config.MilliCPU, config.MemoryGB, config.VpcID, config.Timeouts.Create) +func newServiceWithReadReplica(t *testing.T, primary, replica Config) string { + return newServiceConfig(primary) + replica.String(t) } func newServiceCustomConfig(resourceName string, config Config) string {