@@ -5,32 +5,208 @@ import (
5
5
"testing"
6
6
7
7
"github.com/google/go-cmp/cmp"
8
+ "github.com/hashicorp/terraform-plugin-framework/diag"
8
9
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
9
- "github.com/hashicorp/terraform-plugin-framework/internal/testing/emptyprovider"
10
+ "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
11
+ "github.com/hashicorp/terraform-plugin-framework/tfsdk"
12
+ "github.com/hashicorp/terraform-plugin-framework/types"
13
+ "github.com/hashicorp/terraform-plugin-go/tftypes"
10
14
)
11
15
12
- // TODO: Migrate tfsdk.Provider bits of proto6server.testProviderServer to
13
- // new internal/testing/provider.Provider that allows customization of all
14
- // method implementations via struct fields. Then, create additional test
15
- // cases in this unit test.
16
- //
17
- // For now this testing is covered by proto6server.ReadDataSource.
18
- //
19
- // Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/215
20
16
func TestServerReadDataSource (t * testing.T ) {
21
17
t .Parallel ()
22
18
19
+ testType := tftypes.Object {
20
+ AttributeTypes : map [string ]tftypes.Type {
21
+ "test_computed" : tftypes .String ,
22
+ "test_required" : tftypes .String ,
23
+ },
24
+ }
25
+
26
+ testConfigValue := tftypes .NewValue (testType , map [string ]tftypes.Value {
27
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
28
+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
29
+ })
30
+
31
+ testStateValue := tftypes .NewValue (testType , map [string ]tftypes.Value {
32
+ "test_computed" : tftypes .NewValue (tftypes .String , "test-state-value" ),
33
+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
34
+ })
35
+
36
+ testSchema := tfsdk.Schema {
37
+ Attributes : map [string ]tfsdk.Attribute {
38
+ "test_computed" : {
39
+ Computed : true ,
40
+ Type : types .StringType ,
41
+ },
42
+ "test_required" : {
43
+ Required : true ,
44
+ Type : types .StringType ,
45
+ },
46
+ },
47
+ }
48
+
49
+ testConfig := & tfsdk.Config {
50
+ Raw : testConfigValue ,
51
+ Schema : testSchema ,
52
+ }
53
+
54
+ testStateUnchanged := & tfsdk.State {
55
+ Raw : testConfigValue ,
56
+ Schema : testSchema ,
57
+ }
58
+
59
+ testState := & tfsdk.State {
60
+ Raw : testStateValue ,
61
+ Schema : testSchema ,
62
+ }
63
+
23
64
testCases := map [string ]struct {
24
65
server * fwserver.Server
25
66
request * fwserver.ReadDataSourceRequest
26
67
expectedResponse * fwserver.ReadDataSourceResponse
27
68
}{
28
- "empty-provider " : {
69
+ "nil " : {
29
70
server : & fwserver.Server {
30
- Provider : & emptyprovider .Provider {},
71
+ Provider : & testprovider .Provider {},
31
72
},
32
73
expectedResponse : & fwserver.ReadDataSourceResponse {},
33
74
},
75
+ "request-config" : {
76
+ server : & fwserver.Server {
77
+ Provider : & testprovider.Provider {},
78
+ },
79
+ request : & fwserver.ReadDataSourceRequest {
80
+ Config : testConfig ,
81
+ DataSourceSchema : testSchema ,
82
+ DataSourceType : & testprovider.DataSourceType {
83
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
84
+ return testSchema , nil
85
+ },
86
+ NewDataSourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.DataSource , diag.Diagnostics ) {
87
+ return & testprovider.DataSource {
88
+ ReadMethod : func (ctx context.Context , req tfsdk.ReadDataSourceRequest , resp * tfsdk.ReadDataSourceResponse ) {
89
+ var config struct {
90
+ TestComputed types.String `tfsdk:"test_computed"`
91
+ TestRequired types.String `tfsdk:"test_required"`
92
+ }
93
+
94
+ resp .Diagnostics .Append (req .Config .Get (ctx , & config )... )
95
+
96
+ if config .TestRequired .Value != "test-config-value" {
97
+ resp .Diagnostics .AddError ("unexpected req.Config value: %s" , config .TestRequired .Value )
98
+ }
99
+ },
100
+ }, nil
101
+ },
102
+ },
103
+ },
104
+ expectedResponse : & fwserver.ReadDataSourceResponse {
105
+ State : testStateUnchanged ,
106
+ },
107
+ },
108
+ "request-providermeta" : {
109
+ server : & fwserver.Server {
110
+ Provider : & testprovider.Provider {},
111
+ },
112
+ request : & fwserver.ReadDataSourceRequest {
113
+ Config : testConfig ,
114
+ DataSourceSchema : testSchema ,
115
+ DataSourceType : & testprovider.DataSourceType {
116
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
117
+ return testSchema , nil
118
+ },
119
+ NewDataSourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.DataSource , diag.Diagnostics ) {
120
+ return & testprovider.DataSource {
121
+ ReadMethod : func (ctx context.Context , req tfsdk.ReadDataSourceRequest , resp * tfsdk.ReadDataSourceResponse ) {
122
+ var config struct {
123
+ TestComputed types.String `tfsdk:"test_computed"`
124
+ TestRequired types.String `tfsdk:"test_required"`
125
+ }
126
+
127
+ resp .Diagnostics .Append (req .ProviderMeta .Get (ctx , & config )... )
128
+
129
+ if config .TestRequired .Value != "test-config-value" {
130
+ resp .Diagnostics .AddError ("unexpected req.ProviderMeta value: %s" , config .TestRequired .Value )
131
+ }
132
+ },
133
+ }, nil
134
+ },
135
+ },
136
+ ProviderMeta : testConfig ,
137
+ },
138
+ expectedResponse : & fwserver.ReadDataSourceResponse {
139
+ State : testStateUnchanged ,
140
+ },
141
+ },
142
+ "response-diagnostics" : {
143
+ server : & fwserver.Server {
144
+ Provider : & testprovider.Provider {},
145
+ },
146
+ request : & fwserver.ReadDataSourceRequest {
147
+ Config : testConfig ,
148
+ DataSourceSchema : testSchema ,
149
+ DataSourceType : & testprovider.DataSourceType {
150
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
151
+ return testSchema , nil
152
+ },
153
+ NewDataSourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.DataSource , diag.Diagnostics ) {
154
+ return & testprovider.DataSource {
155
+ ReadMethod : func (ctx context.Context , req tfsdk.ReadDataSourceRequest , resp * tfsdk.ReadDataSourceResponse ) {
156
+ resp .Diagnostics .AddWarning ("warning summary" , "warning detail" )
157
+ resp .Diagnostics .AddError ("error summary" , "error detail" )
158
+ },
159
+ }, nil
160
+ },
161
+ },
162
+ },
163
+ expectedResponse : & fwserver.ReadDataSourceResponse {
164
+ Diagnostics : diag.Diagnostics {
165
+ diag .NewWarningDiagnostic (
166
+ "warning summary" ,
167
+ "warning detail" ,
168
+ ),
169
+ diag .NewErrorDiagnostic (
170
+ "error summary" ,
171
+ "error detail" ,
172
+ ),
173
+ },
174
+ State : testStateUnchanged ,
175
+ },
176
+ },
177
+ "response-state" : {
178
+ server : & fwserver.Server {
179
+ Provider : & testprovider.Provider {},
180
+ },
181
+ request : & fwserver.ReadDataSourceRequest {
182
+ Config : testConfig ,
183
+ DataSourceSchema : testSchema ,
184
+ DataSourceType : & testprovider.DataSourceType {
185
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
186
+ return testSchema , nil
187
+ },
188
+ NewDataSourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.DataSource , diag.Diagnostics ) {
189
+ return & testprovider.DataSource {
190
+ ReadMethod : func (ctx context.Context , req tfsdk.ReadDataSourceRequest , resp * tfsdk.ReadDataSourceResponse ) {
191
+ var data struct {
192
+ TestComputed types.String `tfsdk:"test_computed"`
193
+ TestRequired types.String `tfsdk:"test_required"`
194
+ }
195
+
196
+ resp .Diagnostics .Append (req .Config .Get (ctx , & data )... )
197
+
198
+ data .TestComputed = types.String {Value : "test-state-value" }
199
+
200
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
201
+ },
202
+ }, nil
203
+ },
204
+ },
205
+ },
206
+ expectedResponse : & fwserver.ReadDataSourceResponse {
207
+ State : testState ,
208
+ },
209
+ },
34
210
}
35
211
36
212
for name , testCase := range testCases {
0 commit comments