-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/testing: Initial testsdk and testprovider packages (#166)
Reference: #151 Creates an internal terraform-plugin-go based SDK and declarative provider for unit testing for within this Go module. The new `helper/resource` tests should be equivalent to the existing terraform-plugin-sdk based `TestTest_TestStep_ExternalProviders_To_ProviderFactories` and `TestTest_TestStep_ProviderFactories_To_ExternalProviders` tests.
- Loading branch information
Showing
19 changed files
with
1,471 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// Package testing contains functionality and helpers for unit testing within | ||
// this Go module. | ||
package testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package testprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource" | ||
) | ||
|
||
var _ datasource.DataSource = DataSource{} | ||
|
||
type DataSource struct { | ||
ReadResponse *datasource.ReadResponse | ||
SchemaResponse *datasource.SchemaResponse | ||
ValidateConfigResponse *datasource.ValidateConfigResponse | ||
} | ||
|
||
func (d DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
if d.ReadResponse != nil { | ||
resp.Diagnostics = d.ReadResponse.Diagnostics | ||
resp.State = d.ReadResponse.State | ||
} | ||
} | ||
|
||
func (d DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
if d.SchemaResponse != nil { | ||
resp.Diagnostics = d.SchemaResponse.Diagnostics | ||
resp.Schema = d.SchemaResponse.Schema | ||
} | ||
} | ||
|
||
func (d DataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { | ||
if d.ValidateConfigResponse != nil { | ||
resp.Diagnostics = d.ValidateConfigResponse.Diagnostics | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// Package testprovider is a declarative provider for implementing unit testing | ||
// within this Go module. | ||
package testprovider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package testprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/provider" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource" | ||
) | ||
|
||
var _ provider.Provider = Provider{} | ||
|
||
// Provider is a declarative provider implementation for unit testing in this | ||
// Go module. | ||
type Provider struct { | ||
ConfigureResponse *provider.ConfigureResponse | ||
DataSources map[string]DataSource | ||
Resources map[string]Resource | ||
SchemaResponse *provider.SchemaResponse | ||
StopResponse *provider.StopResponse | ||
ValidateConfigResponse *provider.ValidateConfigResponse | ||
} | ||
|
||
func (p Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { | ||
if p.ConfigureResponse != nil { | ||
resp.Diagnostics = p.ConfigureResponse.Diagnostics | ||
} | ||
} | ||
|
||
func (p Provider) DataSourcesMap() map[string]datasource.DataSource { | ||
datasources := make(map[string]datasource.DataSource, len(p.DataSources)) | ||
|
||
for typeName, d := range p.DataSources { | ||
datasources[typeName] = d | ||
} | ||
|
||
return datasources | ||
} | ||
|
||
func (p Provider) ResourcesMap() map[string]resource.Resource { | ||
resources := make(map[string]resource.Resource, len(p.Resources)) | ||
|
||
for typeName, d := range p.Resources { | ||
resources[typeName] = d | ||
} | ||
|
||
return resources | ||
} | ||
|
||
func (p Provider) Stop(ctx context.Context, req provider.StopRequest, resp *provider.StopResponse) { | ||
if p.StopResponse != nil { | ||
resp.Error = p.StopResponse.Error | ||
} | ||
} | ||
|
||
func (p Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { | ||
if p.SchemaResponse != nil { | ||
resp.Diagnostics = p.SchemaResponse.Diagnostics | ||
resp.Schema = p.SchemaResponse.Schema | ||
} | ||
|
||
resp.Schema = &tfprotov6.Schema{ | ||
Block: &tfprotov6.SchemaBlock{}, | ||
} | ||
} | ||
|
||
func (p Provider) ValidateConfig(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) { | ||
if p.ValidateConfigResponse != nil { | ||
resp.Diagnostics = p.ValidateConfigResponse.Diagnostics | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package testprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource" | ||
) | ||
|
||
var _ resource.Resource = Resource{} | ||
|
||
type Resource struct { | ||
CreateResponse *resource.CreateResponse | ||
DeleteResponse *resource.DeleteResponse | ||
ImportStateResponse *resource.ImportStateResponse | ||
|
||
// Planning happens multiple ways during a single TestStep, so statically | ||
// defining only the response is very problematic. | ||
PlanChangeFunc func(context.Context, resource.PlanChangeRequest, *resource.PlanChangeResponse) | ||
|
||
ReadResponse *resource.ReadResponse | ||
SchemaResponse *resource.SchemaResponse | ||
UpdateResponse *resource.UpdateResponse | ||
UpgradeStateResponse *resource.UpgradeStateResponse | ||
ValidateConfigResponse *resource.ValidateConfigResponse | ||
} | ||
|
||
func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
if r.CreateResponse != nil { | ||
resp.Diagnostics = r.CreateResponse.Diagnostics | ||
resp.NewState = r.CreateResponse.NewState | ||
} | ||
} | ||
|
||
func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
if r.DeleteResponse != nil { | ||
resp.Diagnostics = r.DeleteResponse.Diagnostics | ||
} | ||
} | ||
|
||
func (r Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
if r.ImportStateResponse != nil { | ||
resp.Diagnostics = r.ImportStateResponse.Diagnostics | ||
resp.State = r.ImportStateResponse.State | ||
} | ||
} | ||
|
||
func (r Resource) PlanChange(ctx context.Context, req resource.PlanChangeRequest, resp *resource.PlanChangeResponse) { | ||
if r.PlanChangeFunc != nil { | ||
r.PlanChangeFunc(ctx, req, resp) | ||
} | ||
} | ||
|
||
func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
if r.ReadResponse != nil { | ||
resp.Diagnostics = r.ReadResponse.Diagnostics | ||
resp.NewState = r.ReadResponse.NewState | ||
} | ||
} | ||
|
||
func (r Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
if r.SchemaResponse != nil { | ||
resp.Diagnostics = r.SchemaResponse.Diagnostics | ||
resp.Schema = r.SchemaResponse.Schema | ||
} | ||
} | ||
|
||
func (r Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
if r.UpdateResponse != nil { | ||
resp.Diagnostics = r.UpdateResponse.Diagnostics | ||
resp.NewState = r.UpdateResponse.NewState | ||
} | ||
} | ||
|
||
func (r Resource) UpgradeState(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { | ||
if r.UpgradeStateResponse != nil { | ||
resp.Diagnostics = r.UpgradeStateResponse.Diagnostics | ||
resp.UpgradedState = r.UpgradeStateResponse.UpgradedState | ||
} | ||
} | ||
|
||
func (r Resource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
if r.ValidateConfigResponse != nil { | ||
resp.Diagnostics = r.ValidateConfigResponse.Diagnostics | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package datasource | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
type DataSource interface { | ||
Read(context.Context, ReadRequest, *ReadResponse) | ||
Schema(context.Context, SchemaRequest, *SchemaResponse) | ||
ValidateConfig(context.Context, ValidateConfigRequest, *ValidateConfigResponse) | ||
} | ||
|
||
type ReadRequest struct { | ||
Config tftypes.Value | ||
} | ||
|
||
type ReadResponse struct { | ||
Diagnostics []*tfprotov6.Diagnostic | ||
State tftypes.Value | ||
} | ||
|
||
type SchemaRequest struct{} | ||
|
||
type SchemaResponse struct { | ||
Diagnostics []*tfprotov6.Diagnostic | ||
Schema *tfprotov6.Schema | ||
} | ||
|
||
type ValidateConfigRequest struct { | ||
Config tftypes.Value | ||
} | ||
|
||
type ValidateConfigResponse struct { | ||
Diagnostics []*tfprotov6.Diagnostic | ||
} |
Oops, something went wrong.