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 29, 2024
1 parent c71d142 commit 006c049
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/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,
}
}
13 changes: 9 additions & 4 deletions internal/provider/dns_record_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ 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" {
Expand Down Expand Up @@ -48,17 +48,22 @@ func TestAccDnsRecordA(t *testing.T) {
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(zoneId, "testacc.examplepetstore.com"))
}

func testAccDnsRecordCheckDestroy(hostname string) func(s *terraform.State) error {
func testAccDnsRecordCheckDestroy(zoneId string, 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 {
Expand Down
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")
}
85 changes: 85 additions & 0 deletions internal/provider/log_drain_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 TestAccLogDrain(t *testing.T) {
var siteId = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
accTest(t, []resource.TestStep{
{
Config: fmt.Sprintf(`resource "netlify_log_drain" "example" {
site_id = "%s"
destination = "http"
log_types = ["deploys"]
format = "ndjson"
exclude_pii = false
service_config = {
url = "https://httpstat.us/200"
}
}`, siteId),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("netlify_log_drain.example", "id"),
resource.TestCheckResourceAttr("netlify_log_drain.example", "site_id", siteId),
resource.TestCheckResourceAttr("netlify_log_drain.example", "destination", "http"),
resource.TestCheckResourceAttr("netlify_log_drain.example", "exclude_pii", "false"),
),
},
{
ResourceName: "netlify_log_drain.example",
ImportState: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
for _, m := range s.Modules {
if v, ok := m.Resources["netlify_log_drain.example"]; ok {
return fmt.Sprintf("%s:%s", v.Primary.Attributes["site_id"], v.Primary.Attributes["id"]), nil
}
}
return "", fmt.Errorf("not found in TestAccLogDrain import test step")
},
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_updated"},
},

{
Config: fmt.Sprintf(`resource "netlify_log_drain" "example" {
site_id = "%s"
destination = "http"
log_types = ["deploys"]
format = "ndjson"
exclude_pii = true
service_config = {
url = "https://httpstat.us/200"
}
}`, siteId),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("netlify_log_drain.example", plancheck.ResourceActionUpdate),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("netlify_log_drain.example", "site_id", siteId),
resource.TestCheckResourceAttr("netlify_log_drain.example", "exclude_pii", "true"),
),
},
}, testAccLogDrainDestroy)
}

func testAccLogDrainDestroy(s *terraform.State) error {
for _, m := range s.Modules {
if v, ok := m.Resources["netlify_log_drain.example"]; ok {
key, _, err := testAccProvider.client.LogDrainsAPI.LogDrainsIndex(context.Background(), v.Primary.Attributes["site_id"]).Execute()
if err != nil {
//lint:ignore nilerr we expect an error to know it was not found
return nil
}
return fmt.Errorf("Log drain still exists: %s", key.Id)
}
}
return fmt.Errorf("not found in testAccLogDrainDestroy check destroy")
}
27 changes: 27 additions & 0 deletions internal/provider/site_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package provider

import (
"testing"

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

func TestAccDataSite(t *testing.T) {
accTest(t, []resource.TestStep{
{
Config: `data "netlify_site" "example_by_id" {
id = "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"
}
data "netlify_site" "example_by_name" {
team_slug = "netlify-terraform-test"
name = "tf-test-1"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netlify_site.example_by_id", "name", "tf-test-1"),
resource.TestCheckResourceAttr("data.netlify_site.example_by_name", "id", "5b407d6d-9385-4e7a-a4c4-8efc11ea3c26"),
),
},
}, func(s *terraform.State) error { return nil })
}
35 changes: 35 additions & 0 deletions internal/provider/sites_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package provider

import (
"fmt"
"strings"
"testing"

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

func TestAccDataSites(t *testing.T) {
accTest(t, []resource.TestStep{
{
Config: `data "netlify_sites" "example" {
team_slug = "netlify-terraform-test"
}`,
Check: resource.ComposeTestCheckFunc(
func(s *terraform.State) error {
for _, m := range s.Modules {
if v, ok := m.Resources["data.netlify_sites.example"]; ok {
for k, v := range v.Primary.Attributes {
if strings.HasPrefix(k, "sites.") && strings.HasSuffix(k, ".name") && v == "tf-test-1" {
return nil
}
}
return fmt.Errorf("not found in sites list at TestAccDataSites test step")
}
}
return fmt.Errorf("sites list not found in TestAccDataSites test step")
},
),
},
}, func(s *terraform.State) error { return nil })
}
Loading

0 comments on commit 006c049

Please sign in to comment.