Skip to content

Commit c132058

Browse files
authored
helper/resource: Refactor various unit testing to use inline providers (#170)
Reference: #151 This will reduce CI time and dependency on networking/registry calls.
1 parent d6717ae commit c132058

File tree

4 files changed

+612
-219
lines changed

4 files changed

+612
-219
lines changed

helper/resource/testing_new_config_test.go

Lines changed: 241 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,52 @@ import (
88
"regexp"
99
"testing"
1010

11+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
12+
"github.com/hashicorp/terraform-plugin-go/tftypes"
13+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
14+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
15+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
1116
"github.com/hashicorp/terraform-plugin-testing/plancheck"
1217
)
1318

1419
func TestTest_TestStep_ExpectError_NewConfig(t *testing.T) {
1520
t.Parallel()
1621

17-
Test(t, TestCase{
18-
ExternalProviders: map[string]ExternalProvider{
19-
"random": {
20-
Source: "registry.terraform.io/hashicorp/random",
21-
VersionConstraint: "3.4.3",
22-
},
22+
UnitTest(t, TestCase{
23+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
24+
"test": providerserver.NewProviderServer(testprovider.Provider{
25+
Resources: map[string]testprovider.Resource{
26+
"test_resource": {
27+
SchemaResponse: &resource.SchemaResponse{
28+
Schema: &tfprotov6.Schema{
29+
Block: &tfprotov6.SchemaBlock{
30+
Attributes: []*tfprotov6.SchemaAttribute{
31+
{
32+
Name: "id",
33+
Type: tftypes.String,
34+
Required: true,
35+
},
36+
},
37+
},
38+
},
39+
},
40+
ValidateConfigResponse: &resource.ValidateConfigResponse{
41+
Diagnostics: []*tfprotov6.Diagnostic{
42+
{
43+
Severity: tfprotov6.DiagnosticSeverityError,
44+
Summary: "Invalid Attribute Value",
45+
Detail: "Diagnostic details",
46+
},
47+
},
48+
},
49+
},
50+
},
51+
}),
2352
},
2453
Steps: []TestStep{
2554
{
26-
Config: `resource "random_string" "one" {
27-
length = 2
28-
min_upper = 4
55+
Config: `resource "test_resource" "test" {
56+
id = "invalid-value"
2957
}`,
3058
ExpectError: regexp.MustCompile(`Error: Invalid Attribute Value`),
3159
},
@@ -38,17 +66,43 @@ func Test_ConfigPlanChecks_PreApply_Called(t *testing.T) {
3866

3967
spy1 := &planCheckSpy{}
4068
spy2 := &planCheckSpy{}
41-
Test(t, TestCase{
42-
ExternalProviders: map[string]ExternalProvider{
43-
"random": {
44-
Source: "registry.terraform.io/hashicorp/random",
45-
},
69+
UnitTest(t, TestCase{
70+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
71+
"test": providerserver.NewProviderServer(testprovider.Provider{
72+
Resources: map[string]testprovider.Resource{
73+
"test_resource": {
74+
CreateResponse: &resource.CreateResponse{
75+
NewState: tftypes.NewValue(
76+
tftypes.Object{
77+
AttributeTypes: map[string]tftypes.Type{
78+
"id": tftypes.String,
79+
},
80+
},
81+
map[string]tftypes.Value{
82+
"id": tftypes.NewValue(tftypes.String, "test"),
83+
},
84+
),
85+
},
86+
SchemaResponse: &resource.SchemaResponse{
87+
Schema: &tfprotov6.Schema{
88+
Block: &tfprotov6.SchemaBlock{
89+
Attributes: []*tfprotov6.SchemaAttribute{
90+
{
91+
Name: "id",
92+
Type: tftypes.String,
93+
Computed: true,
94+
},
95+
},
96+
},
97+
},
98+
},
99+
},
100+
},
101+
}),
46102
},
47103
Steps: []TestStep{
48104
{
49-
Config: `resource "random_string" "one" {
50-
length = 16
51-
}`,
105+
Config: `resource "test_resource" "test" {}`,
52106
ConfigPlanChecks: ConfigPlanChecks{
53107
PreApply: []plancheck.PlanCheck{
54108
spy1,
@@ -78,17 +132,43 @@ func Test_ConfigPlanChecks_PreApply_Errors(t *testing.T) {
78132
spy3 := &planCheckSpy{
79133
err: errors.New("spy3 check failed"),
80134
}
81-
Test(t, TestCase{
82-
ExternalProviders: map[string]ExternalProvider{
83-
"random": {
84-
Source: "registry.terraform.io/hashicorp/random",
85-
},
135+
UnitTest(t, TestCase{
136+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
137+
"test": providerserver.NewProviderServer(testprovider.Provider{
138+
Resources: map[string]testprovider.Resource{
139+
"test_resource": {
140+
CreateResponse: &resource.CreateResponse{
141+
NewState: tftypes.NewValue(
142+
tftypes.Object{
143+
AttributeTypes: map[string]tftypes.Type{
144+
"id": tftypes.String,
145+
},
146+
},
147+
map[string]tftypes.Value{
148+
"id": tftypes.NewValue(tftypes.String, "test"),
149+
},
150+
),
151+
},
152+
SchemaResponse: &resource.SchemaResponse{
153+
Schema: &tfprotov6.Schema{
154+
Block: &tfprotov6.SchemaBlock{
155+
Attributes: []*tfprotov6.SchemaAttribute{
156+
{
157+
Name: "id",
158+
Type: tftypes.String,
159+
Computed: true,
160+
},
161+
},
162+
},
163+
},
164+
},
165+
},
166+
},
167+
}),
86168
},
87169
Steps: []TestStep{
88170
{
89-
Config: `resource "random_string" "one" {
90-
length = 16
91-
}`,
171+
Config: `resource "test_resource" "test" {}`,
92172
ConfigPlanChecks: ConfigPlanChecks{
93173
PreApply: []plancheck.PlanCheck{
94174
spy1,
@@ -107,17 +187,43 @@ func Test_ConfigPlanChecks_PostApplyPreRefresh_Called(t *testing.T) {
107187

108188
spy1 := &planCheckSpy{}
109189
spy2 := &planCheckSpy{}
110-
Test(t, TestCase{
111-
ExternalProviders: map[string]ExternalProvider{
112-
"random": {
113-
Source: "registry.terraform.io/hashicorp/random",
114-
},
190+
UnitTest(t, TestCase{
191+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
192+
"test": providerserver.NewProviderServer(testprovider.Provider{
193+
Resources: map[string]testprovider.Resource{
194+
"test_resource": {
195+
CreateResponse: &resource.CreateResponse{
196+
NewState: tftypes.NewValue(
197+
tftypes.Object{
198+
AttributeTypes: map[string]tftypes.Type{
199+
"id": tftypes.String,
200+
},
201+
},
202+
map[string]tftypes.Value{
203+
"id": tftypes.NewValue(tftypes.String, "test"),
204+
},
205+
),
206+
},
207+
SchemaResponse: &resource.SchemaResponse{
208+
Schema: &tfprotov6.Schema{
209+
Block: &tfprotov6.SchemaBlock{
210+
Attributes: []*tfprotov6.SchemaAttribute{
211+
{
212+
Name: "id",
213+
Type: tftypes.String,
214+
Computed: true,
215+
},
216+
},
217+
},
218+
},
219+
},
220+
},
221+
},
222+
}),
115223
},
116224
Steps: []TestStep{
117225
{
118-
Config: `resource "random_string" "one" {
119-
length = 16
120-
}`,
226+
Config: `resource "test_resource" "test" {}`,
121227
ConfigPlanChecks: ConfigPlanChecks{
122228
PostApplyPreRefresh: []plancheck.PlanCheck{
123229
spy1,
@@ -147,17 +253,43 @@ func Test_ConfigPlanChecks_PostApplyPreRefresh_Errors(t *testing.T) {
147253
spy3 := &planCheckSpy{
148254
err: errors.New("spy3 check failed"),
149255
}
150-
Test(t, TestCase{
151-
ExternalProviders: map[string]ExternalProvider{
152-
"random": {
153-
Source: "registry.terraform.io/hashicorp/random",
154-
},
256+
UnitTest(t, TestCase{
257+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
258+
"test": providerserver.NewProviderServer(testprovider.Provider{
259+
Resources: map[string]testprovider.Resource{
260+
"test_resource": {
261+
CreateResponse: &resource.CreateResponse{
262+
NewState: tftypes.NewValue(
263+
tftypes.Object{
264+
AttributeTypes: map[string]tftypes.Type{
265+
"id": tftypes.String,
266+
},
267+
},
268+
map[string]tftypes.Value{
269+
"id": tftypes.NewValue(tftypes.String, "test"),
270+
},
271+
),
272+
},
273+
SchemaResponse: &resource.SchemaResponse{
274+
Schema: &tfprotov6.Schema{
275+
Block: &tfprotov6.SchemaBlock{
276+
Attributes: []*tfprotov6.SchemaAttribute{
277+
{
278+
Name: "id",
279+
Type: tftypes.String,
280+
Computed: true,
281+
},
282+
},
283+
},
284+
},
285+
},
286+
},
287+
},
288+
}),
155289
},
156290
Steps: []TestStep{
157291
{
158-
Config: `resource "random_string" "one" {
159-
length = 16
160-
}`,
292+
Config: `resource "test_resource" "test" {}`,
161293
ConfigPlanChecks: ConfigPlanChecks{
162294
PostApplyPreRefresh: []plancheck.PlanCheck{
163295
spy1,
@@ -176,17 +308,43 @@ func Test_ConfigPlanChecks_PostApplyPostRefresh_Called(t *testing.T) {
176308

177309
spy1 := &planCheckSpy{}
178310
spy2 := &planCheckSpy{}
179-
Test(t, TestCase{
180-
ExternalProviders: map[string]ExternalProvider{
181-
"random": {
182-
Source: "registry.terraform.io/hashicorp/random",
183-
},
311+
UnitTest(t, TestCase{
312+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
313+
"test": providerserver.NewProviderServer(testprovider.Provider{
314+
Resources: map[string]testprovider.Resource{
315+
"test_resource": {
316+
CreateResponse: &resource.CreateResponse{
317+
NewState: tftypes.NewValue(
318+
tftypes.Object{
319+
AttributeTypes: map[string]tftypes.Type{
320+
"id": tftypes.String,
321+
},
322+
},
323+
map[string]tftypes.Value{
324+
"id": tftypes.NewValue(tftypes.String, "test"),
325+
},
326+
),
327+
},
328+
SchemaResponse: &resource.SchemaResponse{
329+
Schema: &tfprotov6.Schema{
330+
Block: &tfprotov6.SchemaBlock{
331+
Attributes: []*tfprotov6.SchemaAttribute{
332+
{
333+
Name: "id",
334+
Type: tftypes.String,
335+
Computed: true,
336+
},
337+
},
338+
},
339+
},
340+
},
341+
},
342+
},
343+
}),
184344
},
185345
Steps: []TestStep{
186346
{
187-
Config: `resource "random_string" "one" {
188-
length = 16
189-
}`,
347+
Config: `resource "test_resource" "test" {}`,
190348
ConfigPlanChecks: ConfigPlanChecks{
191349
PostApplyPostRefresh: []plancheck.PlanCheck{
192350
spy1,
@@ -216,17 +374,43 @@ func Test_ConfigPlanChecks_PostApplyPostRefresh_Errors(t *testing.T) {
216374
spy3 := &planCheckSpy{
217375
err: errors.New("spy3 check failed"),
218376
}
219-
Test(t, TestCase{
220-
ExternalProviders: map[string]ExternalProvider{
221-
"random": {
222-
Source: "registry.terraform.io/hashicorp/random",
223-
},
377+
UnitTest(t, TestCase{
378+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
379+
"test": providerserver.NewProviderServer(testprovider.Provider{
380+
Resources: map[string]testprovider.Resource{
381+
"test_resource": {
382+
CreateResponse: &resource.CreateResponse{
383+
NewState: tftypes.NewValue(
384+
tftypes.Object{
385+
AttributeTypes: map[string]tftypes.Type{
386+
"id": tftypes.String,
387+
},
388+
},
389+
map[string]tftypes.Value{
390+
"id": tftypes.NewValue(tftypes.String, "test"),
391+
},
392+
),
393+
},
394+
SchemaResponse: &resource.SchemaResponse{
395+
Schema: &tfprotov6.Schema{
396+
Block: &tfprotov6.SchemaBlock{
397+
Attributes: []*tfprotov6.SchemaAttribute{
398+
{
399+
Name: "id",
400+
Type: tftypes.String,
401+
Computed: true,
402+
},
403+
},
404+
},
405+
},
406+
},
407+
},
408+
},
409+
}),
224410
},
225411
Steps: []TestStep{
226412
{
227-
Config: `resource "random_string" "one" {
228-
length = 16
229-
}`,
413+
Config: `resource "test_resource" "test" {}`,
230414
ConfigPlanChecks: ConfigPlanChecks{
231415
PostApplyPostRefresh: []plancheck.PlanCheck{
232416
spy1,

0 commit comments

Comments
 (0)