Skip to content

Commit

Permalink
more acceptance tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonsnir committed Aug 31, 2024
1 parent c71d142 commit fec62aa
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/swagger_flat.json
/swagger_go.json
/.env
plan.out
*.out

*.dll
*.exe
Expand Down
5 changes: 3 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ openapi_generate: ## Generate the go code from the OpenAPI spec.
-g go \
-o /local/internal/netlifyapi ; \
sed -i '' 's/int32/int64/g' internal/netlifyapi/model_*.go ; \
sed -i '' 's/int32/int64/g' internal/netlifyapi/api_*.go
sed -i '' 's/int32/int64/g' internal/netlifyapi/api_*.go ; \
sed -i '' 's/return e.error/return fmt.Sprintf("%s: %q", e.error, e.body)/g' internal/netlifyapi/client.go

test: ## Test the go code.
go test -v ./...

testacc:
testacc: ## Test the go code and run acceptance tests.
TF_ACC=1 go test ./... -v $(TESTARGS)
# -timeout 120m
2 changes: 1 addition & 1 deletion internal/netlifyapi/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion internal/provider/common_tf_models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package provider

import "github.com/hashicorp/terraform-plugin-framework/types"
import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type netlifyDomainModel struct {
ID types.String `tfsdk:"id"`
Expand All @@ -11,3 +14,15 @@ type netlifyDomainModel struct {
AutoRenew types.Bool `tfsdk:"auto_renew"`
AutoRenewAt types.String `tfsdk:"auto_renew_at"`
}

func (m netlifyDomainModel) AttributeTypes() map[string]attr.Type {
return map[string]attr.Type{
"id": types.StringType,
"name": types.StringType,
"registered_at": types.StringType,
"expires_at": types.StringType,
"renewal_price": types.StringType,
"auto_renew": types.BoolType,
"auto_renew_at": types.StringType,
}
}
43 changes: 23 additions & 20 deletions internal/provider/dns_record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

var zoneId = "66afdbce3cf2b4f0fab520d9"

func TestAccDnsRecordA(t *testing.T) {
var zoneId = "66afdbce3cf2b4f0fab520d9"
accTest(t, []resource.TestStep{
{
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
Config: `resource "netlify_dns_record" "example" {
type = "A"
zone_id = "%s"
zone_id = "66afdbce3cf2b4f0fab520d9"
hostname = "testacc.examplepetstore.com"
value = "10.0.0.0"
}`, zoneId),
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
Expand All @@ -42,33 +42,36 @@ func TestAccDnsRecordA(t *testing.T) {
ImportStateVerifyIgnore: []string{"last_updated"},
},
{
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
Config: `resource "netlify_dns_record" "example" {
type = "A"
zone_id = "%s"
zone_id = "66afdbce3cf2b4f0fab520d9"
hostname = "testacc.examplepetstore.com"
value = "10.0.0.1"
}`, zoneId),
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("netlify_dns_record.example", plancheck.ResourceActionReplace),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
resource.TestCheckResourceAttr("netlify_dns_record.example", "hostname", "testacc.examplepetstore.com"),
resource.TestCheckResourceAttr("netlify_dns_record.example", "value", "10.0.0.1"),
),
},
}, testAccDnsRecordCheckDestroy("testacc.examplepetstore.com"))
}, testAccDnsRecordCheckDestroy)
}

func testAccDnsRecordCheckDestroy(hostname string) func(s *terraform.State) error {
return func(s *terraform.State) error {
records, _, err := testAccProvider.client.DNSZonesAPI.GetDnsRecords(context.Background(), zoneId).Execute()
if err != nil {
return err
}
for _, record := range records {
if record.Hostname == hostname {
return fmt.Errorf("DNS record still exists")
}
func testAccDnsRecordCheckDestroy(s *terraform.State) error {
records, _, err := testAccProvider.client.DNSZonesAPI.GetDnsRecords(context.Background(), "66afdbce3cf2b4f0fab520d9").Execute()
if err != nil {
return err
}
for _, record := range records {
if record.Hostname == "testacc.examplepetstore.com" {
return fmt.Errorf("DNS record still exists")
}
return nil
}
return nil
}
21 changes: 21 additions & 0 deletions internal/provider/dns_zone_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccDataDnsZone(t *testing.T) {
accTest(t, []resource.TestStep{
{
Config: `data "netlify_dns_zone" "example" {
name = "examplepetstore.com"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netlify_dns_zone.example", "id", "66afdbce3cf2b4f0fab520d9"),
),
},
}, func(s *terraform.State) error { return nil })
}
28 changes: 15 additions & 13 deletions internal/provider/dns_zone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ type dnsZoneResource struct {
}

type dnsZoneResourceModel struct {
ID types.String `tfsdk:"id"`
LastUpdated types.String `tfsdk:"last_updated"`
Name types.String `tfsdk:"name"`
TeamID types.String `tfsdk:"team_id"`
TeamSlug types.String `tfsdk:"team_slug"`
DnsServers types.List `tfsdk:"dns_servers"`
Domain *netlifyDomainModel `tfsdk:"domain"`
ID types.String `tfsdk:"id"`
LastUpdated types.String `tfsdk:"last_updated"`
Name types.String `tfsdk:"name"`
TeamID types.String `tfsdk:"team_id"`
TeamSlug types.String `tfsdk:"team_slug"`
DnsServers types.List `tfsdk:"dns_servers"`
Domain types.Object `tfsdk:"domain"`
}

func (r *dnsZoneResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -183,17 +183,18 @@ func (r *dnsZoneResource) Create(ctx context.Context, req resource.CreateRequest
plan.DnsServers, diags = types.ListValueFrom(ctx, types.StringType, dnsServers)
resp.Diagnostics.Append(diags...)
if dnsZone.Domain == nil {
plan.Domain = nil
plan.Domain = types.ObjectNull(netlifyDomainModel{}.AttributeTypes())
} else {
plan.Domain = &netlifyDomainModel{
plan.Domain, diags = types.ObjectValueFrom(ctx, netlifyDomainModel{}.AttributeTypes(), netlifyDomainModel{
ID: types.StringValue(dnsZone.Domain.Id),
Name: types.StringValue(dnsZone.Domain.Name),
RegisteredAt: types.StringValue(dnsZone.Domain.RegisteredAt.Format(time.RFC3339)),
ExpiresAt: types.StringValue(dnsZone.Domain.ExpiresAt.Format(time.RFC3339)),
RenewalPrice: types.StringValue(dnsZone.Domain.RenewalPrice),
AutoRenew: types.BoolValue(dnsZone.Domain.AutoRenew),
AutoRenewAt: types.StringValue(dnsZone.Domain.AutoRenewAt.Format(time.RFC3339)),
}
})
resp.Diagnostics.Append(diags...)
}

_, _, err = r.data.client.DNSZonesAPI.EnableDnsZoneIpv6(ctx, plan.ID.ValueString()).Execute()
Expand Down Expand Up @@ -244,17 +245,18 @@ func (r *dnsZoneResource) Read(ctx context.Context, req resource.ReadRequest, re
state.DnsServers, diags = types.ListValueFrom(ctx, types.StringType, dnsServers)
resp.Diagnostics.Append(diags...)
if dnsZone.Domain == nil {
state.Domain = nil
state.Domain = types.ObjectNull(netlifyDomainModel{}.AttributeTypes())
} else {
state.Domain = &netlifyDomainModel{
state.Domain, diags = types.ObjectValueFrom(ctx, netlifyDomainModel{}.AttributeTypes(), netlifyDomainModel{
ID: types.StringValue(dnsZone.Domain.Id),
Name: types.StringValue(dnsZone.Domain.Name),
RegisteredAt: types.StringValue(dnsZone.Domain.RegisteredAt.Format(time.RFC3339)),
ExpiresAt: types.StringValue(dnsZone.Domain.ExpiresAt.Format(time.RFC3339)),
RenewalPrice: types.StringValue(dnsZone.Domain.RenewalPrice),
AutoRenew: types.BoolValue(dnsZone.Domain.AutoRenew),
AutoRenewAt: types.StringValue(dnsZone.Domain.AutoRenewAt.Format(time.RFC3339)),
}
})
resp.Diagnostics.Append(diags...)
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
58 changes: 58 additions & 0 deletions internal/provider/dns_zone_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package provider

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccDnsZone(t *testing.T) {
accTest(t, []resource.TestStep{
{
Config: `resource "netlify_dns_zone" "example" {
name = "tfsoftsecretnetlifytestingexamplestore.com"
team_slug = "netlify-terraform-test"
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("netlify_dns_zone.example", "id"),
resource.TestCheckResourceAttr("netlify_dns_zone.example", "team_id", "66ae34e11a567e9092e3850f"),
),
},
{
ResourceName: "netlify_dns_zone.example",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_updated"},
},

{
Config: `resource "netlify_dns_zone" "example" {
name = "tfsoftsecretnetlifytestingexamplestore2.com"
team_slug = "netlify-terraform-test"
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("netlify_dns_zone.example", plancheck.ResourceActionReplace),
},
},
},
}, testAccDnsZoneDestroy)
}

func testAccDnsZoneDestroy(s *terraform.State) error {
for _, m := range s.Modules {
if v, ok := m.Resources["netlify_dns_zone.example"]; ok {
key, _, err := testAccProvider.client.DNSZonesAPI.GetDnsZone(context.Background(), v.Primary.Attributes["id"]).Execute()
if err != nil {
//lint:ignore nilerr we expect an error to know it was not found
return nil
}
return fmt.Errorf("DNS zone still exists: %s", key.Id)
}
}
return fmt.Errorf("not found in testAccDnsZoneDestroy check destroy")
}
Loading

0 comments on commit fec62aa

Please sign in to comment.