Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronblevy committed Jan 4, 2024
1 parent bdda91e commit 77cf7e4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
26 changes: 26 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ type Config struct {
ReadReplicaSource string
}

func (c *Config) WithName(name string) *Config {
c.Name = name
return c
}

func (c *Config) WithSpec(milliCPU, memoryGB int64) *Config {
c.MilliCPU = milliCPU
c.MemoryGB = memoryGB
return c
}

func (c *Config) WithVPC(ID int64) *Config {
c.VpcID = ID
return c
}

func (c *Config) WithHAReplica(enableHAReplica bool) *Config {
c.EnableHAReplica = enableHAReplica
return c
}

func (c *Config) WithReadReplica(source string) *Config {
c.ReadReplicaSource = source
return c
}

func (c *Config) String(t *testing.T) string {
c.setDefaults()
b := &strings.Builder{}
Expand Down
47 changes: 20 additions & 27 deletions internal/provider/service_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -94,10 +95,10 @@ func TestServiceResource_Read_Replica(t *testing.T) {
resourceNameReadReplica = "read_replica"
)
var (
primaryConfig = Config{
primaryConfig = &Config{
Name: "service resource test init",
}
replicaConfig = Config{
replicaConfig = &Config{
ResourceName: resourceNameReadReplica,
ReadReplicaSource: "timescale_service.resource.id",
MilliCPU: 500,
Expand All @@ -110,7 +111,7 @@ func TestServiceResource_Read_Replica(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig),
Config: getConfig(t, primaryConfig, replicaConfig),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify service attributes
resource.TestCheckResourceAttr("timescale_service.resource", "name", "service resource test init"),
Expand Down Expand Up @@ -142,49 +143,36 @@ func TestServiceResource_Read_Replica(t *testing.T) {
},
// Update replica name
{
Config: func() string {
c := replicaConfig
c.Name = "replica"
return newServiceWithReadReplica(t, primaryConfig, c)
}(),
Config: getConfig(t, primaryConfig, replicaConfig.WithName("replica")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.read_replica", "name", "replica"),
),
},
// Do a compute resize
{
Config: func() string {
c := replicaConfig
c.MilliCPU = 1000
c.MemoryGB = 4
return newServiceWithReadReplica(t, primaryConfig, c)
}(),
Config: getConfig(t, primaryConfig, replicaConfig.WithSpec(1000, 4)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.read_replica", "milli_cpu", "1000"),
resource.TestCheckResourceAttr("timescale_service.read_replica", "memory_gb", "4"),
),
},
// Add VPC to the read replica
{
Config: func() string {
c := replicaConfig
c.VpcID = DEFAULT_VPC_ID
return newServiceConfig(primaryConfig) + c.String(t)
}(),
Config: getConfig(t, primaryConfig, replicaConfig.WithVPC(DEFAULT_VPC_ID)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.read_replica", "vpc_id", "2074"),
),
},
// Remove VPC
{
Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig),
Config: getConfig(t, primaryConfig, replicaConfig.WithVPC(0)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckNoResourceAttr("timescale_service.resource", "vpc_id"),
),
},
// Remove Replica
{
Config: newServiceConfig(primaryConfig),
Config: getConfig(t, primaryConfig),
Check: func(state *terraform.State) error {
resources := state.RootModule().Resources
if _, ok := resources["timescale_service."+resourceNameReadReplica]; ok {
Expand All @@ -206,7 +194,7 @@ func TestServiceResource_Read_Replica(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: newServiceWithReadReplica(t, primaryConfig, replicaConfig) + secondReadReplica.String(t),
Config: getConfig(t, primaryConfig, replicaConfig, secondReadReplica),
ExpectError: regexp.MustCompile(errMultipleReadReplicas),
},
},
Expand Down Expand Up @@ -390,11 +378,6 @@ func newServiceAddHAReplica(config Config) string {
}`, config.Name, config.MilliCPU, config.MemoryGB, config.EnableHAReplica, config.Timeouts.Create)
}

// newServiceWithReadReplica creates two resources, a primary instance and a read replica
func newServiceWithReadReplica(t *testing.T, primary, replica Config) string {
return newServiceConfig(primary) + replica.String(t)
}

func newServiceCustomConfig(resourceName string, config Config) string {
if config.Timeouts.Create == "" {
config.Timeouts.Create = "30m"
Expand Down Expand Up @@ -429,3 +412,13 @@ func newServiceCustomVpcConfig(resourceName string, config Config) string {
enable_ha_replica = %t
}`, resourceName, config.Name, config.Timeouts.Create, config.MilliCPU, config.MemoryGB, config.RegionCode, config.VpcID, config.EnableHAReplica)
}

// getConfig returns a configuration for a test step
func getConfig(t *testing.T, cfgs ...*Config) string {
res := strings.Builder{}
res.WriteString(providerConfig)
for _, cfg := range cfgs {
res.WriteString(cfg.String(t))
}
return res.String()
}

0 comments on commit 77cf7e4

Please sign in to comment.