@@ -5,32 +5,174 @@ 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
+ "response-diagnostics" : {
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
+ resp .Diagnostics .AddWarning ("warning summary" , "warning detail" )
123
+ resp .Diagnostics .AddError ("error summary" , "error detail" )
124
+ },
125
+ }, nil
126
+ },
127
+ },
128
+ },
129
+ expectedResponse : & fwserver.ReadDataSourceResponse {
130
+ Diagnostics : diag.Diagnostics {
131
+ diag .NewWarningDiagnostic (
132
+ "warning summary" ,
133
+ "warning detail" ,
134
+ ),
135
+ diag .NewErrorDiagnostic (
136
+ "error summary" ,
137
+ "error detail" ,
138
+ ),
139
+ },
140
+ State : testStateUnchanged ,
141
+ },
142
+ },
143
+ "response-state" : {
144
+ server : & fwserver.Server {
145
+ Provider : & testprovider.Provider {},
146
+ },
147
+ request : & fwserver.ReadDataSourceRequest {
148
+ Config : testConfig ,
149
+ DataSourceSchema : testSchema ,
150
+ DataSourceType : & testprovider.DataSourceType {
151
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
152
+ return testSchema , nil
153
+ },
154
+ NewDataSourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.DataSource , diag.Diagnostics ) {
155
+ return & testprovider.DataSource {
156
+ ReadMethod : func (ctx context.Context , req tfsdk.ReadDataSourceRequest , resp * tfsdk.ReadDataSourceResponse ) {
157
+ var data struct {
158
+ TestComputed types.String `tfsdk:"test_computed"`
159
+ TestRequired types.String `tfsdk:"test_required"`
160
+ }
161
+
162
+ resp .Diagnostics .Append (req .Config .Get (ctx , & data )... )
163
+
164
+ data .TestComputed = types.String {Value : "test-state-value" }
165
+
166
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
167
+ },
168
+ }, nil
169
+ },
170
+ },
171
+ },
172
+ expectedResponse : & fwserver.ReadDataSourceResponse {
173
+ State : testState ,
174
+ },
175
+ },
34
176
}
35
177
36
178
for name , testCase := range testCases {
0 commit comments