Skip to content

Commit 9878226

Browse files
Add sdkv2 shim translation tests (#2222)
Follow up on #2187 with shim translation tests for sdkv2 schemas. In sdkv2 the translations map 1:1 as far as I can tell.
1 parent e665ff6 commit 9878226

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed

pkg/tfshim/sdk-v2/shim_test.go

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
// Copyright 2016-2024, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sdkv2
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
"github.com/hexops/autogold/v2"
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
26+
)
27+
28+
// Test how various SDKv2-based schemata translate to the shim.Schema layer.
29+
func TestSchemaShimRepresentations(t *testing.T) {
30+
type testCase struct {
31+
name string
32+
resourceSchema map[string]*schema.Schema
33+
expect autogold.Value
34+
}
35+
36+
testCases := []testCase{
37+
{
38+
"string attribute",
39+
map[string]*schema.Schema{
40+
"field_attr": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
},
44+
},
45+
autogold.Expect(`{
46+
"resources": {
47+
"res": {
48+
"field_attr": {
49+
"optional": true,
50+
"type": 4
51+
}
52+
}
53+
}
54+
}`),
55+
},
56+
{
57+
"list attribute",
58+
map[string]*schema.Schema{
59+
"field_attr": {
60+
Type: schema.TypeList,
61+
Optional: true,
62+
Elem: &schema.Schema{
63+
Type: schema.TypeString,
64+
},
65+
},
66+
},
67+
autogold.Expect(`{
68+
"resources": {
69+
"res": {
70+
"field_attr": {
71+
"element": {
72+
"schema": {
73+
"type": 4
74+
}
75+
},
76+
"optional": true,
77+
"type": 5
78+
}
79+
}
80+
}
81+
}`),
82+
},
83+
{
84+
"list block",
85+
map[string]*schema.Schema{
86+
"block_field": {
87+
Type: schema.TypeList,
88+
Optional: true,
89+
Elem: &schema.Resource{
90+
Schema: map[string]*schema.Schema{
91+
"field_attr": {
92+
Type: schema.TypeString,
93+
Optional: true,
94+
},
95+
},
96+
},
97+
},
98+
},
99+
autogold.Expect(`{
100+
"resources": {
101+
"res": {
102+
"block_field": {
103+
"element": {
104+
"resource": {
105+
"field_attr": {
106+
"optional": true,
107+
"type": 4
108+
}
109+
}
110+
},
111+
"optional": true,
112+
"type": 5
113+
}
114+
}
115+
}
116+
}`),
117+
},
118+
{
119+
"list nested block",
120+
map[string]*schema.Schema{
121+
"block_field": {
122+
Type: schema.TypeList,
123+
Optional: true,
124+
Elem: &schema.Resource{
125+
Schema: map[string]*schema.Schema{
126+
"nested_field": {
127+
Type: schema.TypeList,
128+
Optional: true,
129+
Elem: &schema.Resource{
130+
Schema: map[string]*schema.Schema{
131+
"field_attr": {
132+
Type: schema.TypeString,
133+
Optional: true,
134+
},
135+
},
136+
},
137+
},
138+
},
139+
},
140+
},
141+
},
142+
autogold.Expect(`{
143+
"resources": {
144+
"res": {
145+
"block_field": {
146+
"element": {
147+
"resource": {
148+
"nested_field": {
149+
"element": {
150+
"resource": {
151+
"field_attr": {
152+
"optional": true,
153+
"type": 4
154+
}
155+
}
156+
},
157+
"optional": true,
158+
"type": 5
159+
}
160+
}
161+
},
162+
"optional": true,
163+
"type": 5
164+
}
165+
}
166+
}
167+
}`),
168+
},
169+
{
170+
"list attribute max items one",
171+
map[string]*schema.Schema{
172+
"field_attr": {
173+
Type: schema.TypeList,
174+
Optional: true,
175+
MaxItems: 1,
176+
Elem: &schema.Schema{
177+
Type: schema.TypeString,
178+
},
179+
},
180+
},
181+
autogold.Expect(`{
182+
"resources": {
183+
"res": {
184+
"field_attr": {
185+
"element": {
186+
"schema": {
187+
"type": 4
188+
}
189+
},
190+
"maxItems": 1,
191+
"optional": true,
192+
"type": 5
193+
}
194+
}
195+
}
196+
}`),
197+
},
198+
{
199+
"list block",
200+
map[string]*schema.Schema{
201+
"block_field": {
202+
Type: schema.TypeList,
203+
Optional: true,
204+
MaxItems: 1,
205+
Elem: &schema.Resource{
206+
Schema: map[string]*schema.Schema{
207+
"field_attr": {
208+
Type: schema.TypeString,
209+
Optional: true,
210+
},
211+
},
212+
},
213+
},
214+
},
215+
autogold.Expect(`{
216+
"resources": {
217+
"res": {
218+
"block_field": {
219+
"element": {
220+
"resource": {
221+
"field_attr": {
222+
"optional": true,
223+
"type": 4
224+
}
225+
}
226+
},
227+
"maxItems": 1,
228+
"optional": true,
229+
"type": 5
230+
}
231+
}
232+
}
233+
}`),
234+
},
235+
}
236+
237+
for _, tc := range testCases {
238+
t.Run(tc.name, func(t *testing.T) {
239+
provider := &schema.Provider{
240+
ResourcesMap: map[string]*schema.Resource{
241+
"res": {
242+
Schema: tc.resourceSchema,
243+
},
244+
},
245+
}
246+
shimmedProvider := NewProvider(provider)
247+
require.NoError(t, shimmedProvider.InternalValidate())
248+
249+
m := tfbridge.MarshalProvider(shimmedProvider)
250+
bytes, err := json.Marshal(m)
251+
require.NoError(t, err)
252+
253+
var pretty map[string]any
254+
err = json.Unmarshal(bytes, &pretty)
255+
require.NoError(t, err)
256+
257+
prettyBytes, err := json.MarshalIndent(pretty, "", " ")
258+
require.NoError(t, err)
259+
260+
tc.expect.Equal(t, string(prettyBytes))
261+
})
262+
}
263+
}

pkg/tfshim/shim.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ type Schema interface {
128128
//
129129
// A test suite [3] is provided to explore how Plugin Framework constructs map to Schema.
130130
//
131+
// A test suite [4] is provided to explore how SDKv2 constructs map to Schema.
131132
// [1]: https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/schema/schema.go#L231
132133
// [2]: https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/schema/core_schema_test.go#L220
133134
// [3]: https://github.com/pulumi/pulumi-terraform-bridge/blob/master/pf/tests/schemashim_test.go#L34
135+
// [4]: https://github.com/pulumi/pulumi-terraform-bridge/blob/master/pkg/tfshim/sdk-v2/shim_test.go#L29
134136
Elem() interface{}
135137

136138
MaxItems() int

0 commit comments

Comments
 (0)