Skip to content

Commit bcea61e

Browse files
committed
DNS record acceptance tests
1 parent d0c3e09 commit bcea61e

File tree

8 files changed

+178
-34
lines changed

8 files changed

+178
-34
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
# list whatever Terraform versions here you would like to support
6464
terraform:
6565
- '1.0.*'
66-
- '1.6.*'
66+
# - '1.6.*'
6767
steps:
6868
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
6969
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
@@ -77,5 +77,6 @@ jobs:
7777
- run: go mod download
7878
- env:
7979
TF_ACC: "1"
80+
NETLIFY_API_TOKEN: ${{ secrets.NETLIFY_API_TOKEN }}
8081
run: go test -v -cover ./internal/provider/
8182
timeout-minutes: 10

GNUmakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ test: ## Test the go code.
3030
go test -v ./...
3131

3232
testacc:
33-
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
33+
TF_ACC=1 go test ./... -v $(TESTARGS)
34+
# -timeout 120m

go.mod

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ require (
1010
)
1111

1212
require (
13+
github.com/agext/levenshtein v1.2.2 // indirect
1314
github.com/go-openapi/errors v0.22.0 // indirect
1415
github.com/go-openapi/runtime v0.28.0 // indirect
1516
github.com/go-openapi/strfmt v0.23.0 // indirect
1617
github.com/go-openapi/swag v0.23.0 // indirect
1718
github.com/go-openapi/validate v0.24.0 // indirect
19+
github.com/google/go-cmp v0.6.0 // indirect
20+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
21+
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
22+
github.com/hashicorp/hcl/v2 v2.21.0 // indirect
23+
github.com/hashicorp/logutils v1.0.0 // indirect
24+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 // indirect
25+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
26+
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
27+
google.golang.org/appengine v1.6.8 // indirect
1828
)
1929

2030
require (
@@ -51,13 +61,14 @@ require (
5161
github.com/hashicorp/go-plugin v1.6.0 // indirect
5262
github.com/hashicorp/go-uuid v1.0.3 // indirect
5363
github.com/hashicorp/go-version v1.7.0 // indirect
54-
github.com/hashicorp/hc-install v0.7.0 // indirect
64+
github.com/hashicorp/hc-install v0.8.0 // indirect
5565
github.com/hashicorp/hcl v1.0.0 // indirect
5666
github.com/hashicorp/terraform-exec v0.21.0 // indirect
5767
github.com/hashicorp/terraform-json v0.22.1 // indirect
5868
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
59-
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
69+
github.com/hashicorp/terraform-plugin-go v0.23.0
6070
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
71+
github.com/hashicorp/terraform-plugin-testing v1.10.0
6172
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
6273
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
6374
github.com/hashicorp/yamux v0.1.1 // indirect
@@ -97,18 +108,18 @@ require (
97108
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
98109
github.com/yuin/goldmark v1.7.1 // indirect
99110
github.com/yuin/goldmark-meta v1.1.0 // indirect
100-
github.com/zclconf/go-cty v1.14.4 // indirect
111+
github.com/zclconf/go-cty v1.15.0 // indirect
101112
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
102113
go.mongodb.org/mongo-driver v1.15.0 // indirect
103114
go.uber.org/multierr v1.11.0 // indirect
104-
golang.org/x/crypto v0.23.0 // indirect
115+
golang.org/x/crypto v0.26.0 // indirect
105116
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
106-
golang.org/x/mod v0.17.0 // indirect
117+
golang.org/x/mod v0.19.0 // indirect
107118
golang.org/x/net v0.25.0 // indirect
108-
golang.org/x/sync v0.7.0 // indirect
109-
golang.org/x/sys v0.20.0 // indirect
110-
golang.org/x/text v0.15.0 // indirect
111-
golang.org/x/tools v0.21.0 // indirect
119+
golang.org/x/sync v0.8.0 // indirect
120+
golang.org/x/sys v0.23.0 // indirect
121+
golang.org/x/text v0.17.0 // indirect
122+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
112123
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
113124
google.golang.org/grpc v1.63.2 // indirect
114125
google.golang.org/protobuf v1.34.1 // indirect

go.sum

Lines changed: 59 additions & 20 deletions
Large diffs are not rendered by default.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
)
11+
12+
var zoneId = "66afdbce3cf2b4f0fab520d9"
13+
14+
func TestAccDnsRecordA(t *testing.T) {
15+
accTest(t, []resource.TestStep{
16+
{
17+
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
18+
type = "A"
19+
zone_id = "%s"
20+
hostname = "testacc.examplepetstore.com"
21+
value = "10.0.0.0"
22+
}`, zoneId),
23+
Check: resource.ComposeTestCheckFunc(
24+
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
25+
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
26+
resource.TestCheckResourceAttr("netlify_dns_record.example", "hostname", "testacc.examplepetstore.com"),
27+
resource.TestCheckResourceAttr("netlify_dns_record.example", "value", "10.0.0.0"),
28+
),
29+
},
30+
{
31+
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
32+
type = "A"
33+
zone_id = "%s"
34+
hostname = "testacc.examplepetstore.com"
35+
value = "10.0.0.1"
36+
}`, zoneId),
37+
Check: resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
39+
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
40+
resource.TestCheckResourceAttr("netlify_dns_record.example", "hostname", "testacc.examplepetstore.com"),
41+
resource.TestCheckResourceAttr("netlify_dns_record.example", "value", "10.0.0.1"),
42+
),
43+
},
44+
}, testAccDnsRecordCheckDestroy("testacc.examplepetstore.com"))
45+
}
46+
47+
func testAccDnsRecordCheckDestroy(hostname string) func(s *terraform.State) error {
48+
return func(s *terraform.State) error {
49+
records, _, err := testAccProvider.client.DNSZonesAPI.GetDnsRecords(context.Background(), zoneId).Execute()
50+
if err != nil {
51+
return err
52+
}
53+
for _, record := range records {
54+
if record.Hostname == hostname {
55+
return fmt.Errorf("DNS record still exists")
56+
}
57+
}
58+
return nil
59+
}
60+
}

internal/provider/environment_variable_resource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
206206
}
207207
var values []netlifyapi.EnvVarValue
208208
var isSecret bool
209-
if plan.SecretValues != nil && len(plan.SecretValues) > 0 {
209+
if len(plan.SecretValues) > 0 {
210210
values = serializeValues(plan.SecretValues)
211211
isSecret = true
212212
} else {
@@ -308,7 +308,7 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
308308
}
309309
var values []netlifyapi.EnvVarValue
310310
var isSecret bool
311-
if plan.SecretValues != nil && len(plan.SecretValues) > 0 {
311+
if len(plan.SecretValues) > 0 {
312312
values = serializeValues(plan.SecretValues)
313313
isSecret = true
314314
} else {

internal/provider/provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var _ provider.Provider = &NetlifyProvider{}
2121

2222
type NetlifyProvider struct {
2323
version string
24+
client *netlifyapi.APIClient
2425
}
2526

2627
type NetlifyProviderModel struct {
@@ -158,7 +159,8 @@ func (p *NetlifyProvider) Configure(ctx context.Context, req provider.ConfigureR
158159
apiConfig.UserAgent = "Terraform Provider"
159160
apiConfig.AddDefaultHeader("Authorization", "Bearer "+token)
160161
// TODO: Add debug/trace logging to the API client, perhaps by overriding the behavior of apiConfig.Debug
161-
data.client = netlifyapi.NewAPIClient(apiConfig)
162+
p.client = netlifyapi.NewAPIClient(apiConfig)
163+
data.client = p.client
162164

163165
var account *netlifyapi.Account
164166

internal/provider/test_utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package provider
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/providerserver"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
)
11+
12+
var testAccProvider = &NetlifyProvider{version: "0.0.0-test"}
13+
14+
func accTest(t *testing.T, steps []resource.TestStep, checkDestroy resource.TestCheckFunc) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
18+
"netlify": providerserver.NewProtocol6WithError(testAccProvider),
19+
},
20+
CheckDestroy: checkDestroy,
21+
Steps: steps,
22+
})
23+
}
24+
25+
func testAccPreCheck(t *testing.T) {
26+
v := os.Getenv("NETLIFY_API_TOKEN")
27+
if v == "" {
28+
t.Fatal("NETLIFY_API_TOKEN must be set for acceptance tests")
29+
}
30+
}

0 commit comments

Comments
 (0)