Skip to content

Commit

Permalink
added unit tests for loadbalancerDiffCheck function
Browse files Browse the repository at this point in the history
  • Loading branch information
apinonformoso committed Oct 10, 2023
1 parent bdaa2b5 commit ab05ef0
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions digitalocean/loadbalancer/resource_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/loadbalancer"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -737,6 +738,101 @@ func TestAccDigitalOceanLoadbalancer_Firewall(t *testing.T) {
})
}

func TestLoadbalancerDiffCheck(t *testing.T) {
cases := []struct {
name string
attrs map[string]interface{}
expectError bool
errorMessage string
}{
{
name: "Missing type and region",
expectError: true,
errorMessage: "missing 'region' value",
},
{
name: "Missing type",
expectError: false,
attrs: map[string]interface{}{
"region": "nyc3",
},
},
{
name: "Empty region",
expectError: true,
errorMessage: "missing 'region' value",
attrs: map[string]interface{}{
"region": "",
},
},
{
name: "Regional type without region",
expectError: true,
errorMessage: "'region' must be set and not be empty when 'type' is 'REGIONAL'",
attrs: map[string]interface{}{
"type": "REGIONAL",
},
},
{
name: "Regional type with empty region",
expectError: true,
errorMessage: "'region' must be set and not be empty when 'type' is 'REGIONAL'",
attrs: map[string]interface{}{
"type": "REGIONAL",
"region": "",
},
},
{
name: "Regional type with region",
expectError: false,
attrs: map[string]interface{}{
"type": "REGIONAL",
"region": "nyc3",
},
},
{
name: "Global type without region",
expectError: false,
attrs: map[string]interface{}{
"type": "GLOBAL",
},
},
{
name: "Global type with empty region",
expectError: false,
attrs: map[string]interface{}{
"type": "GLOBAL",
"region": "",
},
},
{
name: "Global type with region",
expectError: true,
errorMessage: "'region' must be empty or not set when 'type' is 'GLOBAL'",
attrs: map[string]interface{}{
"type": "GLOBAL",
"region": "nyc3",
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var s *terraform.InstanceState
conf := terraform.NewResourceConfigRaw(c.attrs)

r := loadbalancer.ResourceDigitalOceanLoadbalancer()
_, err := r.Diff(context.Background(), s, conf, nil)

if c.expectError {
if err.Error() != c.errorMessage {
t.Fatalf("Expected %s, got %s", c.errorMessage, err)
}
}
})
}
}

func testAccCheckDigitalOceanLoadbalancerDestroy(s *terraform.State) error {
client := acceptance.TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

Expand Down

0 comments on commit ab05ef0

Please sign in to comment.