Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Mar 6, 2025
1 parent 78b6847 commit 7a7fbc7
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 3 deletions.
83 changes: 83 additions & 0 deletions internal/configs/configschema/internal_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,89 @@ func TestBlockInternalValidate(t *testing.T) {
}
}

func TestObjectInternalValidate(t *testing.T) {
tests := map[string]struct {
Object *Object
Errs []string
}{
"empty": {
&Object{},
[]string{},
},
"valid": {
&Object{
Attributes: map[string]*Attribute{
"foo": {
Type: cty.String,
Required: true,
},
"bar": {
Type: cty.String,
Optional: true,
},
},
Nesting: NestingSingle,
},
[]string{},
},
"attribute with no flags set": {
&Object{
Attributes: map[string]*Attribute{
"foo": {
Type: cty.String,
},
},
Nesting: NestingSingle,
},
[]string{"foo: must set Optional, Required or Computed"},
},
"attribute required and optional": {
&Object{
Attributes: map[string]*Attribute{
"foo": {
Type: cty.String,
Required: true,
Optional: true,
},
},
Nesting: NestingSingle,
},
[]string{"foo: cannot set both Optional and Required"},
},
"attribute with missing type": {
&Object{
Attributes: map[string]*Attribute{
"foo": {
Optional: true,
},
},
Nesting: NestingSingle,
},
[]string{"foo: either Type or NestedType must be defined"},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
errs := joinedErrors(test.Object.InternalValidate())
if got, want := len(errs), len(test.Errs); got != want {
t.Errorf("wrong number of errors %d; want %d", got, want)
for _, err := range errs {
t.Logf("- %s", err.Error())
}
} else {
if len(errs) > 0 {
for i := range errs {
if errs[i].Error() != test.Errs[i] {
t.Errorf("wrong error: got %s, want %s", errs[i].Error(), test.Errs[i])
}
}
}
}
})
}
}

func joinedErrors(err error) []error {
if err == nil {
return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/plugin/convert/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var ignoreUnexported = cmpopts.IgnoreUnexported(
proto.Schema_Block{},
proto.Schema_NestedBlock{},
proto.Schema_Attribute{},
proto.ResourceIdentitySchema{},
proto.ResourceIdentitySchema_IdentityAttribute{},
)

func TestProtoDiagnostics(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions internal/plugin/convert/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
proto "github.com/hashicorp/terraform/internal/tfplugin5"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -400,3 +401,52 @@ func TestProtoToResourceIdentitySchema(t *testing.T) {
})
}
}

func TestResourceIdentitySchemaToProto(t *testing.T) {
tests := map[string]struct {
Want *proto.ResourceIdentitySchema
Schema providers.IdentitySchema
}{
"attributes": {
&proto.ResourceIdentitySchema{
Version: 1,
IdentityAttributes: []*proto.ResourceIdentitySchema_IdentityAttribute{
{
Name: "optional",
Type: []byte(`"string"`),
OptionalForImport: true,
},
{
Name: "required",
Type: []byte(`"number"`),
RequiredForImport: true,
},
},
},
providers.IdentitySchema{
Version: 1,
Body: &configschema.Object{
Attributes: map[string]*configschema.Attribute{
"optional": {
Type: cty.String,
Optional: true,
},
"required": {
Type: cty.Number,
Required: true,
},
},
},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
converted := ResourceIdentitySchemaToProto(tc.Schema)
if !cmp.Equal(converted, tc.Want, typeComparer, equateEmpty, ignoreUnexported) {
t.Fatal(cmp.Diff(converted, tc.Want, typeComparer, equateEmpty, ignoreUnexported))
}
})
}
}
78 changes: 78 additions & 0 deletions internal/plugin/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func providerResourceIdentitySchemas() *proto.GetResourceIdentitySchemas_Respons
IdentityAttributes: []*proto.ResourceIdentitySchema_IdentityAttribute{
{
Name: "attr",
Type: []byte(`"string"`),
RequiredForImport: true,
},
},
Expand Down Expand Up @@ -219,6 +220,50 @@ func TestGRPCProvider_GetSchema_ResponseErrorDiagnostic(t *testing.T) {
checkDiagsHasError(t, resp.Diagnostics)
}

func TestGRPCProvider_GetSchema_IdentityError(t *testing.T) {
ctrl := gomock.NewController(t)
client := mockproto.NewMockProviderClient(ctrl)

client.EXPECT().GetSchema(
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(providerProtoSchema(), nil)

client.EXPECT().GetResourceIdentitySchemas(
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(&proto.GetResourceIdentitySchemas_Response{}, fmt.Errorf("test error"))

p := &GRPCProvider{
client: client,
}

resp := p.GetProviderSchema()

checkDiags(t, resp.Diagnostics)
}

func TestGRPCProvider_GetResourceIdentitySchemas(t *testing.T) {
ctrl := gomock.NewController(t)
client := mockproto.NewMockProviderClient(ctrl)

client.EXPECT().GetResourceIdentitySchemas(
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(providerResourceIdentitySchemas(), nil)

p := &GRPCProvider{
client: client,
}

resp := p.GetResourceIdentitySchemas()

checkDiags(t, resp.Diagnostics)
}

func TestGRPCProvider_PrepareProviderConfig(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down Expand Up @@ -335,6 +380,39 @@ func TestGRPCProvider_UpgradeResourceStateJSON(t *testing.T) {
}
}

func TestGRPCProvider_UpgradeResourceIdentity(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
client: client,
}

client.EXPECT().UpgradeResourceIdentity(
gomock.Any(),
gomock.Any(),
).Return(&proto.UpgradeResourceIdentity_Response{
UpgradedIdentity: &proto.ResourceIdentityData{
IdentityData: &proto.DynamicValue{
Json: []byte(`{"attr":"bar"}`),
},
},
}, nil)

resp := p.UpgradeResourceIdentity(providers.UpgradeResourceIdentityRequest{
TypeName: "resource",
Version: 0,
RawIdentityJSON: []byte(`{"old_attr":"bar"}`),
})
checkDiags(t, resp.Diagnostics)

expected := cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("bar"),
})

if !cmp.Equal(expected, resp.UpgradedIdentity, typeComparer, valueComparer, equateEmpty) {
t.Fatal(cmp.Diff(expected, resp.UpgradedIdentity, typeComparer, valueComparer, equateEmpty))
}
}

func TestGRPCProvider_Configure(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down
2 changes: 2 additions & 0 deletions internal/plugin6/convert/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var ignoreUnexported = cmpopts.IgnoreUnexported(
proto.Schema_NestedBlock{},
proto.Schema_Attribute{},
proto.Schema_Object{},
proto.ResourceIdentitySchema{},
proto.ResourceIdentitySchema_IdentityAttribute{},
)

func TestProtoDiagnostics(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions internal/plugin6/convert/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
proto "github.com/hashicorp/terraform/internal/tfplugin6"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -662,3 +663,52 @@ func TestProtoToResourceIdentitySchema(t *testing.T) {
})
}
}

func TestResourceIdentitySchemaToProto(t *testing.T) {
tests := map[string]struct {
Want *proto.ResourceIdentitySchema
Schema providers.IdentitySchema
}{
"attributes": {
&proto.ResourceIdentitySchema{
Version: 1,
IdentityAttributes: []*proto.ResourceIdentitySchema_IdentityAttribute{
{
Name: "optional",
Type: []byte(`"string"`),
OptionalForImport: true,
},
{
Name: "required",
Type: []byte(`"number"`),
RequiredForImport: true,
},
},
},
providers.IdentitySchema{
Version: 1,
Body: &configschema.Object{
Attributes: map[string]*configschema.Attribute{
"optional": {
Type: cty.String,
Optional: true,
},
"required": {
Type: cty.Number,
Required: true,
},
},
},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
converted := ResourceIdentitySchemaToProto(tc.Schema)
if !cmp.Equal(converted, tc.Want, typeComparer, equateEmpty, ignoreUnexported) {
t.Fatal(cmp.Diff(converted, tc.Want, typeComparer, equateEmpty, ignoreUnexported))
}
})
}
}
Loading

0 comments on commit 7a7fbc7

Please sign in to comment.