Skip to content

Commit f971348

Browse files
committed
add mock tests for GetProviderSchemaOptional
1 parent f907df8 commit f971348

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

internal/plugin/grpc_provider_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/golang/mock/gomock"
1212
"github.com/google/go-cmp/cmp"
13+
"github.com/hashicorp/terraform/internal/addrs"
1314
"github.com/hashicorp/terraform/internal/configs/hcl2shim"
1415
"github.com/hashicorp/terraform/internal/providers"
1516
"github.com/hashicorp/terraform/internal/tfdiags"
@@ -92,6 +93,9 @@ func providerProtoSchema() *proto.GetProviderSchema_Response {
9293
},
9394
},
9495
},
96+
ServerCapabilities: &proto.ServerCapabilities{
97+
GetProviderSchemaOptional: true,
98+
},
9599
}
96100
}
97101

@@ -104,6 +108,27 @@ func TestGRPCProvider_GetSchema(t *testing.T) {
104108
checkDiags(t, resp.Diagnostics)
105109
}
106110

111+
// ensure that the global schema cache is used when the provider supports
112+
// GetProviderSchemaOptional
113+
func TestGRPCProvider_GetSchema_globalCache(t *testing.T) {
114+
p := &GRPCProvider{
115+
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
116+
client: mockProviderClient(t),
117+
}
118+
119+
// first call primes the cache
120+
resp := p.GetProviderSchema()
121+
122+
// create a new provider instance which does not expect a GetProviderSchemaCall
123+
p = &GRPCProvider{
124+
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
125+
client: mockproto.NewMockProviderClient(gomock.NewController(t)),
126+
}
127+
128+
resp = p.GetProviderSchema()
129+
checkDiags(t, resp.Diagnostics)
130+
}
131+
107132
// Ensure that gRPC errors are returned early.
108133
// Reference: https://github.com/hashicorp/terraform/issues/31047
109134
func TestGRPCProvider_GetSchema_GRPCError(t *testing.T) {

internal/plugin6/grpc_provider_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/golang/mock/gomock"
1212
"github.com/google/go-cmp/cmp"
1313
"github.com/google/go-cmp/cmp/cmpopts"
14+
"github.com/hashicorp/terraform/internal/addrs"
1415
"github.com/hashicorp/terraform/internal/configs/hcl2shim"
1516
"github.com/hashicorp/terraform/internal/providers"
1617
"github.com/hashicorp/terraform/internal/tfdiags"
@@ -99,6 +100,9 @@ func providerProtoSchema() *proto.GetProviderSchema_Response {
99100
},
100101
},
101102
},
103+
ServerCapabilities: &proto.ServerCapabilities{
104+
GetProviderSchemaOptional: true,
105+
},
102106
}
103107
}
104108

@@ -111,6 +115,27 @@ func TestGRPCProvider_GetSchema(t *testing.T) {
111115
checkDiags(t, resp.Diagnostics)
112116
}
113117

118+
// ensure that the global schema cache is used when the provider supports
119+
// GetProviderSchemaOptional
120+
func TestGRPCProvider_GetSchema_globalCache(t *testing.T) {
121+
p := &GRPCProvider{
122+
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
123+
client: mockProviderClient(t),
124+
}
125+
126+
// first call primes the cache
127+
resp := p.GetProviderSchema()
128+
129+
// create a new provider instance which does not expect a GetProviderSchemaCall
130+
p = &GRPCProvider{
131+
Addr: addrs.ImpliedProviderForUnqualifiedType("test"),
132+
client: mockproto.NewMockProviderClient(gomock.NewController(t)),
133+
}
134+
135+
resp = p.GetProviderSchema()
136+
checkDiags(t, resp.Diagnostics)
137+
}
138+
114139
// Ensure that gRPC errors are returned early.
115140
// Reference: https://github.com/hashicorp/terraform/issues/31047
116141
func TestGRPCProvider_GetSchema_GRPCError(t *testing.T) {

0 commit comments

Comments
 (0)