From d98e41f91c2997dd76ab54c7faa6a6552bd21a83 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Mon, 27 Sep 2021 16:47:51 -0700 Subject: [PATCH] adding some coverage for some error cases --- plugin/plugin.go | 2 +- plugin/plugin_test.go | 117 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index d853746..a92130b 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -45,7 +45,7 @@ func (p *AwsPlugin) OnCreateCatalog(ctx context.Context, req *pb.OnCreateCatalog } attrs := catalog.GetAttributes() - if secrets == nil { + if attrs == nil { return nil, errors.New("attributes are required") } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index b9f8e13..253e19d 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -621,3 +621,120 @@ func requireCredentialsValid(t *testing.T, accessKeyId, secretAccessKey string) _, err = c.GetCallerIdentity() require.NoError(err) } + +func TestOnCreateCatalogErr(t *testing.T) { + cases := []struct { + name string + req *pb.OnCreateCatalogRequest + expected string + }{ + { + name: "nil catalog", + req: &pb.OnCreateCatalogRequest{}, + expected: "catalog is nil", + }, + { + name: "nil secrets", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{}, + }, + expected: "secrets are required", + }, + { + name: "nil attributes", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: new(structpb.Struct), + }, + }, + expected: "attributes are required", + }, + { + name: "invalid region", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: new(structpb.Struct), + Attributes: mustStruct(map[string]interface{}{ + "region": "foobar", + }), + }, + }, + expected: "not a valid region: foobar", + }, + { + name: "missing access key ID", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: new(structpb.Struct), + Attributes: mustStruct(map[string]interface{}{ + "region": "us-west-2", + }), + }, + }, + expected: "missing required value \"access_key_id\"", + }, + { + name: "missing secret access key", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: mustStruct(map[string]interface{}{ + "access_key_id": "foobar", + }), + Attributes: mustStruct(map[string]interface{}{ + "region": "us-west-2", + }), + }, + }, + expected: "missing required value \"secret_access_key\"", + }, + { + name: "missing secret access key", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: mustStruct(map[string]interface{}{ + "access_key_id": "foobar", + }), + Attributes: mustStruct(map[string]interface{}{ + "region": "us-west-2", + }), + }, + }, + expected: "missing required value \"secret_access_key\"", + }, + { + name: "invalid value for skipping rotation", + req: &pb.OnCreateCatalogRequest{ + Catalog: &hostcatalogs.HostCatalog{ + Secrets: mustStruct(map[string]interface{}{ + "access_key_id": "foobar", + "secret_access_key": "bazqux", + }), + Attributes: mustStruct(map[string]interface{}{ + "region": "us-west-2", + "disable_credential_rotation": "sure", + }), + }, + }, + expected: "unexpected type for value \"disable_credential_rotation\": want bool, got string", + }, + } + + for _, tc := range cases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + require := require.New(t) + p := &AwsPlugin{} + _, err := p.OnCreateCatalog(context.Background(), tc.req) + require.EqualError(err, tc.expected) + }) + } +} + +func mustStruct(in map[string]interface{}) *structpb.Struct { + out, err := structpb.NewStruct(in) + if err != nil { + panic(err) + } + + return out +}