Skip to content

Commit ad8caea

Browse files
committed
Add test scaffolding for Query mode
1 parent cf1f0ec commit ad8caea

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package query_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
10+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
11+
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
12+
)
13+
14+
func examplecloudResource() testprovider.Resource {
15+
return testprovider.Resource{
16+
CreateResponse: &resource.CreateResponse{
17+
NewState: tftypes.NewValue(
18+
tftypes.Object{
19+
AttributeTypes: map[string]tftypes.Type{
20+
"id": tftypes.String,
21+
"location": tftypes.String,
22+
"name": tftypes.String,
23+
},
24+
},
25+
map[string]tftypes.Value{
26+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
27+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
28+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
29+
},
30+
),
31+
NewIdentity: teststep.Pointer(tftypes.NewValue(
32+
tftypes.Object{
33+
AttributeTypes: map[string]tftypes.Type{
34+
"id": tftypes.String,
35+
},
36+
},
37+
map[string]tftypes.Value{
38+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
39+
},
40+
)),
41+
},
42+
ReadResponse: &resource.ReadResponse{
43+
NewState: tftypes.NewValue(
44+
tftypes.Object{
45+
AttributeTypes: map[string]tftypes.Type{
46+
"id": tftypes.String,
47+
"location": tftypes.String,
48+
"name": tftypes.String,
49+
},
50+
},
51+
map[string]tftypes.Value{
52+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
53+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
54+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
55+
},
56+
),
57+
NewIdentity: teststep.Pointer(tftypes.NewValue(
58+
tftypes.Object{
59+
AttributeTypes: map[string]tftypes.Type{
60+
"id": tftypes.String,
61+
},
62+
},
63+
map[string]tftypes.Value{
64+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
65+
},
66+
)),
67+
},
68+
ImportStateResponse: &resource.ImportStateResponse{
69+
State: tftypes.NewValue(
70+
tftypes.Object{
71+
AttributeTypes: map[string]tftypes.Type{
72+
"id": tftypes.String,
73+
"location": tftypes.String,
74+
"name": tftypes.String,
75+
},
76+
},
77+
map[string]tftypes.Value{
78+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
79+
"location": tftypes.NewValue(tftypes.String, "westeurope"),
80+
"name": tftypes.NewValue(tftypes.String, "somevalue"),
81+
},
82+
),
83+
Identity: teststep.Pointer(tftypes.NewValue(
84+
tftypes.Object{
85+
AttributeTypes: map[string]tftypes.Type{
86+
"id": tftypes.String,
87+
},
88+
},
89+
map[string]tftypes.Value{
90+
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
91+
},
92+
)),
93+
},
94+
SchemaResponse: &resource.SchemaResponse{
95+
Schema: &tfprotov6.Schema{
96+
Block: &tfprotov6.SchemaBlock{
97+
Attributes: []*tfprotov6.SchemaAttribute{
98+
ComputedStringAttribute("id"),
99+
RequiredStringAttribute("location"),
100+
RequiredStringAttribute("name"),
101+
},
102+
},
103+
},
104+
},
105+
IdentitySchemaResponse: &resource.IdentitySchemaResponse{
106+
Schema: &tfprotov6.ResourceIdentitySchema{
107+
Version: 1,
108+
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{
109+
{
110+
Name: "id",
111+
Type: tftypes.String,
112+
RequiredForImport: true,
113+
},
114+
},
115+
},
116+
},
117+
}
118+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package query_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
7+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
10+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
11+
)
12+
13+
func TestQuery(t *testing.T) {
14+
t.Parallel()
15+
16+
r.UnitTest(t, r.TestCase{
17+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
18+
tfversion.SkipBelow(tfversion.Version1_13_0), // Query mode requires Terraform 1.13.0 or later
19+
},
20+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
21+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
22+
Resources: map[string]testprovider.Resource{
23+
"examplecloud_container": examplecloudResource(),
24+
},
25+
}),
26+
},
27+
Steps: []r.TestStep{
28+
{
29+
Config: `
30+
resource "examplecloud_container" "test" {
31+
location = "westeurope"
32+
name = "somevalue"
33+
}`,
34+
},
35+
{
36+
ResourceName: "examplecloud_container.test",
37+
ImportState: true,
38+
ImportStateKind: r.ImportBlockWithID,
39+
},
40+
},
41+
})
42+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package query_test
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
)
10+
11+
func RequiredBoolAttribute(name string) *tfprotov6.SchemaAttribute {
12+
return &tfprotov6.SchemaAttribute{
13+
Name: name,
14+
Type: tftypes.Bool,
15+
Required: true,
16+
}
17+
}
18+
19+
func OptionalComputedListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute {
20+
return &tfprotov6.SchemaAttribute{
21+
Name: name,
22+
Type: tftypes.List{ElementType: elementType},
23+
Optional: true,
24+
Computed: true,
25+
}
26+
}
27+
28+
func RequiredListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute {
29+
return &tfprotov6.SchemaAttribute{
30+
Name: name,
31+
Type: tftypes.List{ElementType: elementType},
32+
Required: true,
33+
}
34+
}
35+
36+
func RequiredNumberAttribute(name string) *tfprotov6.SchemaAttribute {
37+
return &tfprotov6.SchemaAttribute{
38+
Name: name,
39+
Type: tftypes.Number,
40+
Required: true,
41+
}
42+
}
43+
44+
func ComputedStringAttribute(name string) *tfprotov6.SchemaAttribute {
45+
return &tfprotov6.SchemaAttribute{
46+
Name: name,
47+
Type: tftypes.String,
48+
Computed: true,
49+
}
50+
}
51+
52+
func OptionalStringAttribute(name string) *tfprotov6.SchemaAttribute {
53+
return &tfprotov6.SchemaAttribute{
54+
Name: name,
55+
Type: tftypes.String,
56+
Optional: true,
57+
}
58+
}
59+
60+
func RequiredStringAttribute(name string) *tfprotov6.SchemaAttribute {
61+
return &tfprotov6.SchemaAttribute{
62+
Name: name,
63+
Type: tftypes.String,
64+
Required: true,
65+
}
66+
}

tfversion/versions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ var (
3939
Version1_10_0 *version.Version = version.Must(version.NewVersion("1.10.0"))
4040
Version1_11_0 *version.Version = version.Must(version.NewVersion("1.11.0"))
4141
Version1_12_0 *version.Version = version.Must(version.NewVersion("1.12.0"))
42+
Version1_13_0 *version.Version = version.Must(version.NewVersion("1.13.0"))
4243
)

0 commit comments

Comments
 (0)