-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathserver_configureprovider_test.go
More file actions
170 lines (152 loc) · 5.16 KB
/
Copy pathserver_configureprovider_test.go
File metadata and controls
170 lines (152 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright IBM Corp. 2021, 2025
// SPDX-License-Identifier: MPL-2.0
package proto5server
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestServerConfigureProvider(t *testing.T) {
t.Parallel()
testType := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
}
testValue := tftypes.NewValue(testType, map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "test-value"),
})
testDynamicValue, err := tfprotov5.NewDynamicValue(testType, testValue)
if err != nil {
t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err)
}
testSchema := schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
Required: true,
},
},
}
type testCase struct {
server *Server
request *tfprotov5.ConfigureProviderRequest
expectedError error
expectedResponse *tfprotov5.ConfigureProviderResponse
}
testCases := map[string]testCase{
"nil": {
server: &Server{
FrameworkServer: fwserver.Server{
Provider: &testprovider.Provider{},
},
},
request: nil,
expectedResponse: &tfprotov5.ConfigureProviderResponse{},
},
"no-schema": {
server: &Server{
FrameworkServer: fwserver.Server{
Provider: &testprovider.Provider{
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Intentially empty, test is passing if it makes it this far
},
},
},
},
request: &tfprotov5.ConfigureProviderRequest{},
expectedResponse: &tfprotov5.ConfigureProviderResponse{},
},
"request-config": {
server: &Server{
FrameworkServer: fwserver.Server{
Provider: &testprovider.Provider{
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = testSchema
},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var got types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("test"), &got)...)
if resp.Diagnostics.HasError() {
return
}
if got.ValueString() != "test-value" {
resp.Diagnostics.AddError("Incorrect req.Config", "expected test-value, got "+got.ValueString())
}
},
},
},
},
request: &tfprotov5.ConfigureProviderRequest{
Config: &testDynamicValue,
},
expectedResponse: &tfprotov5.ConfigureProviderResponse{},
},
"request-terraformversion": {
server: &Server{
FrameworkServer: fwserver.Server{
Provider: &testprovider.Provider{
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
if req.TerraformVersion != "1.0.0" {
resp.Diagnostics.AddError("Incorrect req.TerraformVersion", "expected 1.0.0, got "+req.TerraformVersion)
}
},
},
},
},
request: &tfprotov5.ConfigureProviderRequest{
TerraformVersion: "1.0.0",
},
expectedResponse: &tfprotov5.ConfigureProviderResponse{},
},
"response-diagnostics": {
server: &Server{
FrameworkServer: fwserver.Server{
Provider: &testprovider.Provider{
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
resp.Diagnostics.AddWarning("warning summary", "warning detail")
resp.Diagnostics.AddError("error summary", "error detail")
},
},
},
},
request: &tfprotov5.ConfigureProviderRequest{},
expectedResponse: &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "warning summary",
Detail: "warning detail",
},
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error summary",
Detail: "error detail",
},
},
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := testCase.server.ConfigureProvider(context.Background(), testCase.request)
if diff := cmp.Diff(testCase.expectedError, err); diff != "" {
t.Errorf("unexpected error difference: %s", diff)
}
if diff := cmp.Diff(testCase.expectedResponse, got); diff != "" {
t.Errorf("unexpected response difference: %s", diff)
}
})
}
}