Skip to content

Commit

Permalink
refactor to reuse config
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronblevy committed Dec 22, 2023
1 parent 6f9c7ca commit 295b6aa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
50 changes: 50 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -41,6 +44,7 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
}

type Config struct {
ResourceName string
Name string
Timeouts Timeouts
MilliCPU int64
Expand All @@ -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
}
Expand Down
42 changes: 8 additions & 34 deletions internal/provider/service_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -165,15 +166,15 @@ 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"),
),
},
// Remove VPC
{
Config: newServiceWithReadReplica(primaryConfig, replicaConfig),
Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckNoResourceAttr("timescale_service.resource", "vpc_id"),
),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 295b6aa

Please sign in to comment.