Add mock-backed integration testing tier and fake Dynu API

This commit is contained in:
beatz174-bit committed 2026-04-21 15:19:20 +10:00
1 parent 7f161fb19b
commit 293fe7af28
10 files changed
+564 -154

No files matched your search

+12 -7
View File
@@ -100,18 +100,20 @@ func (d *dnsRecordsDataSource) Configure(_ context.Context, req datasource.Confi
}
func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state dnsRecordsDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
var config struct {
Hostname types.String `tfsdk:"hostname"`
}
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
if state.Hostname.IsUnknown() || state.Hostname.IsNull() {
if config.Hostname.IsUnknown() || config.Hostname.IsNull() {
resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.")
return
}
domainID, domainName, err := d.clientProvider.client.GetRootDomain(ctx, state.Hostname.ValueString())
domainID, domainName, err := d.clientProvider.client.GetRootDomain(ctx, config.Hostname.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to resolve Dynu domain from hostname", err.Error())
return
@@ -125,9 +127,12 @@ func (d *dnsRecordsDataSource) Read(ctx context.Context, req datasource.ReadRequ
sortDNSRecords(records)
state.DomainID = types.Int64Value(domainID)
state.DomainName = types.StringValue(domainName)
state.Records = make([]dnsRecordStateItem, 0, len(records))
state := dnsRecordsDataSourceModel{
Hostname: config.Hostname,
DomainID: types.Int64Value(domainID),
DomainName: types.StringValue(domainName),
Records: make([]dnsRecordStateItem, 0, len(records)),
}
for _, record := range records {
state.Records = append(state.Records, dnsRecordStateItem{
ID: types.Int64Value(record.ID),
+10 -5
View File
@@ -73,18 +73,20 @@ func (d *domainDataSource) Configure(_ context.Context, req datasource.Configure
}
func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state domainDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
var config struct {
Hostname types.String `tfsdk:"hostname"`
}
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
if state.Hostname.IsUnknown() || state.Hostname.IsNull() {
if config.Hostname.IsUnknown() || config.Hostname.IsNull() {
resp.Diagnostics.AddAttributeError(path.Root("hostname"), "Invalid hostname", "The hostname must be known and non-null.")
return
}
domainID, _, err := d.clientProvider.client.GetRootDomain(ctx, state.Hostname.ValueString())
domainID, _, err := d.clientProvider.client.GetRootDomain(ctx, config.Hostname.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to resolve Dynu domain from hostname", err.Error())
return
@@ -101,7 +103,10 @@ func (d *domainDataSource) Read(ctx context.Context, req datasource.ReadRequest,
if resp.Diagnostics.HasError() {
return
}
state.Domain = domainObject
state := domainDataSourceModel{
Hostname: config.Hostname,
Domain: domainObject,
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
@@ -0,0 +1,169 @@
package provider
import (
"context"
"strings"
"testing"
"github.com/dynu/terraform-provider-dynu/internal/testutil/fakedynu"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestIntegrationDataSourceDomains(t *testing.T) {
fake := fakedynu.NewServer()
defer fake.Close()
ds := NewDomainsDataSource().(*domainsDataSource)
configureDataSource(t, ds, fake.BaseURL())
var schemaResp datasource.SchemaResponse
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
resp := datasource.ReadResponse{State: tfsdk.State{Schema: schemaResp.Schema}}
ds.Read(context.Background(), datasource.ReadRequest{}, &resp)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
var state domainsDataSourceModel
diags := resp.State.Get(context.Background(), &state)
if diags.HasError() {
t.Fatalf("state get diagnostics: %v", diags)
}
if len(state.Domains) != 2 {
t.Fatalf("expected 2 domains, got %d", len(state.Domains))
}
if state.Domains[0].Name.ValueString() != "a.example.com" {
t.Fatalf("expected sorted domain a.example.com first, got %q", state.Domains[0].Name.ValueString())
}
}
func TestIntegrationDataSourceDomain(t *testing.T) {
fake := fakedynu.NewServer()
defer fake.Close()
ds := NewDomainDataSource().(*domainDataSource)
configureDataSource(t, ds, fake.BaseURL())
var schemaResp datasource.SchemaResponse
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
req := datasource.ReadRequest{
Config: tfsdk.Config{
Schema: schemaResp.Schema,
Raw: tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"hostname": tftypes.String,
}}, map[string]tftypes.Value{
"hostname": tftypes.NewValue(tftypes.String, "www.a.example.com"),
}),
},
}
resp := datasource.ReadResponse{State: tfsdk.State{Schema: schemaResp.Schema}}
ds.Read(context.Background(), req, &resp)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
var state domainDataSourceModel
diags := resp.State.Get(context.Background(), &state)
if diags.HasError() {
t.Fatalf("state get diagnostics: %v", diags)
}
var domainID int64
diags = resp.State.GetAttribute(context.Background(), path.Root("domain").AtName("id"), &domainID)
if diags.HasError() {
t.Fatalf("domain.id decode diagnostics: %v", diags)
}
var domainName string
diags = resp.State.GetAttribute(context.Background(), path.Root("domain").AtName("name"), &domainName)
if diags.HasError() {
t.Fatalf("domain.name decode diagnostics: %v", diags)
}
if domainID != 1001 || domainName != "a.example.com" {
t.Fatalf("unexpected domain state: id=%d name=%q", domainID, domainName)
}
}
func TestIntegrationDataSourceDNSRecords(t *testing.T) {
fake := fakedynu.NewServer()
defer fake.Close()
ds := NewDNSRecordsDataSource().(*dnsRecordsDataSource)
configureDataSource(t, ds, fake.BaseURL())
var schemaResp datasource.SchemaResponse
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
req := datasource.ReadRequest{
Config: tfsdk.Config{
Schema: schemaResp.Schema,
Raw: tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"hostname": tftypes.String,
}}, map[string]tftypes.Value{
"hostname": tftypes.NewValue(tftypes.String, "www.a.example.com"),
}),
},
}
resp := datasource.ReadResponse{State: tfsdk.State{Schema: schemaResp.Schema}}
ds.Read(context.Background(), req, &resp)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
var state dnsRecordsDataSourceModel
diags := resp.State.Get(context.Background(), &state)
if diags.HasError() {
t.Fatalf("state get diagnostics: %v", diags)
}
if state.DomainID.ValueInt64() != 1001 || state.DomainName.ValueString() != "a.example.com" {
t.Fatalf("unexpected domain resolution: domain_id=%d domain_name=%q", state.DomainID.ValueInt64(), state.DomainName.ValueString())
}
if len(state.Records) != 2 || state.Records[0].ID.ValueInt64() != 10 {
t.Fatalf("unexpected records state: %#v", state.Records)
}
}
func TestIntegrationDataSourceDiagnosticsFromAPIError(t *testing.T) {
fake := fakedynu.NewServer()
defer fake.Close()
fake.SetAPIError("/dns/getroot/www.a.example.com", fakedynu.APIError{HTTPStatus: 400, StatusCode: 400, Type: "Validation Exception", Message: "bad hostname"})
ds := NewDomainDataSource().(*domainDataSource)
configureDataSource(t, ds, fake.BaseURL())
var schemaResp datasource.SchemaResponse
ds.Schema(context.Background(), datasource.SchemaRequest{}, &schemaResp)
req := datasource.ReadRequest{Config: tfsdk.Config{
Schema: schemaResp.Schema,
Raw: tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{"hostname": tftypes.String}}, map[string]tftypes.Value{
"hostname": tftypes.NewValue(tftypes.String, "www.a.example.com"),
}),
}}
resp := datasource.ReadResponse{State: tfsdk.State{Schema: schemaResp.Schema}}
ds.Read(context.Background(), req, &resp)
if !resp.Diagnostics.HasError() {
t.Fatal("expected diagnostics error")
}
if !strings.Contains(resp.Diagnostics[0].Summary(), "Unable to resolve Dynu domain from hostname") {
t.Fatalf("unexpected diagnostics summary: %s", resp.Diagnostics[0].Summary())
}
}
func configureDataSource(t *testing.T, ds datasource.DataSourceWithConfigure, baseURL string) {
t.Helper()
resp := datasource.ConfigureResponse{}
ds.Configure(context.Background(), datasource.ConfigureRequest{
ProviderData: &providerData{client: newDynuClient("dummy-local-key", types.StringValue(baseURL))},
}, &resp)
if resp.Diagnostics.HasError() {
t.Fatalf("configure diagnostics: %v", resp.Diagnostics)
}
}