Skip to content

Commit

Permalink
refactor test config
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronblevy committed Jan 4, 2024
1 parent 430ef41 commit e38198e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
github.com/hashicorp/terraform-plugin-testing v1.2.0
github.com/stretchr/testify v1.7.2
)

require (
Expand All @@ -23,6 +24,7 @@ require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand Down Expand Up @@ -55,6 +57,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand All @@ -72,4 +75,5 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
69 changes: 69 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 @@ -50,6 +54,71 @@ type Config struct {
VpcID int64
}

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) String(t *testing.T) string {
c.setDefaults()
b := &strings.Builder{}
write := func(format string, a ...any) {
_, err := fmt.Fprintf(b, format, a...)
require.NoError(t, err)
}
_, err := fmt.Fprintf(b, "\n\n resource timescale_service %q { \n", c.ResourceName)
require.NoError(t, err)
if c.Name != "" {
write("name = %q \n", c.Name)
}
if c.EnableHAReplica {
write("enable_ha_replica = %t \n", c.EnableHAReplica)
}
if c.RegionCode != "" {
write("region_code = %q \n", c.RegionCode)
}
if c.VpcID != 0 {
write("vpc_id = %d \n", c.VpcID)
}
write(`
milli_cpu = %d
memory_gb = %d
timeouts = {
create = %q
}`+"\n",
c.MilliCPU, c.MemoryGB, c.Timeouts.Create)
write("}")
return b.String()
}

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
64 changes: 30 additions & 34 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 All @@ -13,19 +14,20 @@ import (
const DEFAULT_VPC_ID = 2074 // Default vpc id for test acc

func TestServiceResource_Default_Success(t *testing.T) {
// Test resource creation succeeds and update is not allowed
// Test resource creation succeeds
config := &Config{
ResourceName: "resource",
}
resource.ParallelTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
// Create default and Read testing
{
Config: newServiceConfig(Config{
Name: "service resource test init",
}),
Config: getConfig(t, config),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify the name is set.
resource.TestCheckResourceAttr("timescale_service.resource", "name", "service resource test init"),
resource.TestCheckResourceAttrSet("timescale_service.resource", "name"),
// Verify ID value is set in state.
resource.TestCheckResourceAttrSet("timescale_service.resource", "id"),
resource.TestCheckResourceAttrSet("timescale_service.resource", "password"),
Expand All @@ -39,47 +41,31 @@ func TestServiceResource_Default_Success(t *testing.T) {
resource.TestCheckNoResourceAttr("timescale_service.resource", "vpc_id"),
),
},
// Update service name
// Do a compute resize
{
Config: newServiceConfig(Config{
Name: "service resource test update",
}),
Config: getConfig(t, config.WithSpec(1000, 4)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.resource", "name", "service resource test update"),
resource.TestCheckResourceAttr("timescale_service.resource", "milli_cpu", "1000"),
resource.TestCheckResourceAttr("timescale_service.resource", "memory_gb", "4"),
),
},
// Do a compute resize
// Update service name
{
Config: newServiceComputeResizeConfig(Config{
Name: "service resource test update",
MilliCPU: 1000,
MemoryGB: 4,
}),
Config: getConfig(t, config.WithName("service resource test update")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.resource", "milli_cpu", "1000"),
resource.TestCheckResourceAttr("timescale_service.resource", "memory_gb", "4"),
resource.TestCheckResourceAttr("timescale_service.resource", "name", "service resource test update"),
),
},
// Add VPC
{
Config: newServiceAddVpc(Config{
Name: "service resource test update",
VpcID: DEFAULT_VPC_ID,
MilliCPU: 1000,
MemoryGB: 4,
}),
Config: getConfig(t, config.WithVPC(DEFAULT_VPC_ID)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.resource", "vpc_id", "2074"),
),
},
// Add HA replica and remove VPC
{
Config: newServiceAddHAReplica(Config{
Name: "service resource test update",
EnableHAReplica: true,
MilliCPU: 1000,
MemoryGB: 4,
}),
Config: getConfig(t, config.WithVPC(0).WithHAReplica(true)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckNoResourceAttr("timescale_service.resource", "vpc_id"),
resource.TestCheckResourceAttr("timescale_service.resource", "enable_ha_replica", "true"),
Expand All @@ -89,6 +75,16 @@ func TestServiceResource_Default_Success(t *testing.T) {
})
}

// 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()
}

func TestServiceResource_Timeout(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand Down Expand Up @@ -154,7 +150,7 @@ func TestServiceResource_CustomConf(t *testing.T) {
},
// Create with HA and VPC attached
{
Config: newServiceCustomVpcConfig("hareplica_vpc", Config{
Config: newServiceCustomVpcConfig("hareplica", Config{
Name: "service resource test HA",
RegionCode: "us-east-1",
MilliCPU: 500,
Expand All @@ -163,9 +159,9 @@ func TestServiceResource_CustomConf(t *testing.T) {
VpcID: DEFAULT_VPC_ID,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("timescale_service.hareplica_vpc", "name", "service resource test HA"),
resource.TestCheckResourceAttr("timescale_service.hareplica_vpc", "enable_ha_replica", "true"),
resource.TestCheckResourceAttr("timescale_service.hareplica_vpc", "vpc_id", "2074"),
resource.TestCheckResourceAttr("timescale_service.hareplica", "name", "service resource test HA"),
resource.TestCheckResourceAttr("timescale_service.hareplica", "enable_ha_replica", "true"),
resource.TestCheckResourceAttr("timescale_service.hareplica", "vpc_id", "2074"),
),
},
},
Expand Down

0 comments on commit e38198e

Please sign in to comment.