Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unbind tests #31

Merged
merged 1 commit into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 105 additions & 19 deletions pkg/broker/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,46 @@ func (db mockDataStoreProvision) GetServiceBinding(id string) (*serviceinstance.
switch id {
case "err":
return nil, errors.New("test failure")
case "err-instance":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the simplest way to set up the different test cases, but I think it'd be cleaner long-term to make the changes required to use a dynamodbiface.DynamoDBAPI instead of the mockDataStoreProvision. Something for a future PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thought behind the extra layer of abstraction with the datastore interface was to make it easy to implement alternative persistent storage options like etcd. Not sure if it really does make it that much easier, or if it's something that we'll ever do, so I'm not opposed to getting rid of it.

return &serviceinstance.ServiceBinding{
ID: "err-instance",
InstanceID: "err",
PolicyArn: "exists",
RoleName: "exists",
}, nil
case "err-role-name":
return &serviceinstance.ServiceBinding{
ID: "err-role-name",
InstanceID: "exists",
PolicyArn: "exists",
RoleName: "err",
}, nil
case "exists":
return &serviceinstance.ServiceBinding{
ID: "exists",
InstanceID: "exists",
}, nil
case "exists-role-name":
return &serviceinstance.ServiceBinding{
ID: "exists-role-name",
InstanceID: "exists",
PolicyArn: "exists",
RoleName: "exists",
}, nil
case "foo-instance":
return &serviceinstance.ServiceBinding{
ID: "foo-instance",
InstanceID: "foo",
PolicyArn: "exists",
RoleName: "exists",
}, nil
case "foo-role-name":
return &serviceinstance.ServiceBinding{
ID: "foo-role-name",
InstanceID: "exists",
PolicyArn: "exists",
RoleName: "foo",
}, nil
default:
return nil, nil
}
Expand Down Expand Up @@ -581,26 +616,77 @@ func TestBind(t *testing.T) {
}

func TestUnbind(t *testing.T) {
assertor := assert.New(t)

opts := Options{
TableName: "testtable",
S3Bucket: "abucket",
S3Region: "us-east-1",
S3Key: "tempates/test",
Region: "us-east-1",
BrokerID: "awsservicebroker",
PrescribeOverrides: true,
tests := []struct {
name string
request *osb.UnbindRequest
expectedErr error
}{
{
name: "error_getting_binding",
request: &osb.UnbindRequest{
BindingID: "err",
},
expectedErr: newHTTPStatusCodeError(http.StatusInternalServerError, "", "Failed to get the service binding err: test failure"),
},
{
name: "binding_not_found",
request: &osb.UnbindRequest{
BindingID: "foo",
},
expectedErr: newHTTPStatusCodeError(http.StatusGone, "", "The service binding foo was not found."),
},
{
name: "success",
request: &osb.UnbindRequest{
BindingID: "exists",
},
},
{
name: "error_getting_instance",
request: &osb.UnbindRequest{
BindingID: "err-instance",
},
expectedErr: newHTTPStatusCodeError(http.StatusInternalServerError, "", "Failed to get the service instance err: test failure"),
},
{
name: "instance_not_found",
request: &osb.UnbindRequest{
BindingID: "foo-instance",
},
expectedErr: newHTTPStatusCodeError(http.StatusBadRequest, "", "The service instance foo was not found."),
},
{
name: "error_detaching_role_policy",
request: &osb.UnbindRequest{
BindingID: "err-role-name",
},
expectedErr: newHTTPStatusCodeError(http.StatusInternalServerError, "", "Failed to detach the policy exists from role err: test failure"),
},
{
name: "detach_role_policy",
request: &osb.UnbindRequest{
BindingID: "exists-role-name",
},
},
{
name: "role_not_found",
request: &osb.UnbindRequest{
BindingID: "foo-role-name",
},
},
}
bl, _ := NewAWSBroker(opts, mockGetAwsSession, mockClients, mockGetAccountId, mockUpdateCatalog, mockPollUpdate)
bl.db.DataStorePort = mockDataStoreProvision{}

unbindReq := &osb.UnbindRequest{BindingID: "exists"}
reqContext := &broker.RequestContext{}

expected := &broker.UnbindResponse{UnbindResponse: osb.UnbindResponse{}}
actual, err := bl.Unbind(unbindReq, reqContext)
assertor.Equal(nil, err, "err should be nil")
assertor.Equal(expected, actual, "should succeed")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, _ := NewAWSBroker(Options{}, mockGetAwsSession, mockClients, mockGetAccountId, mockUpdateCatalog, mockPollUpdate)
b.db.DataStorePort = mockDataStoreProvision{}

_, err := b.Unbind(tt.request, &broker.RequestContext{})
if tt.expectedErr != nil {
assert.EqualError(t, err, tt.expectedErr.Error())
} else {
assert.NoError(t, err)
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/broker/awsbroker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/awstesting/mock"
"github.com/aws/aws-sdk-go/service/cloudformation"
Expand Down Expand Up @@ -87,6 +88,15 @@ func (c *mockIAM) AttachRolePolicy(input *iam.AttachRolePolicyInput) (*iam.Attac
return &iam.AttachRolePolicyOutput{}, nil
}

func (c *mockIAM) DetachRolePolicy(input *iam.DetachRolePolicyInput) (*iam.DetachRolePolicyOutput, error) {
if aws.StringValue(input.RoleName) == "err" || aws.StringValue(input.PolicyArn) == "err" {
return nil, errors.New("test failure")
} else if aws.StringValue(input.RoleName) == "exists" && aws.StringValue(input.PolicyArn) == "exists" {
return &iam.DetachRolePolicyOutput{}, nil
}
return nil, awserr.New(iam.ErrCodeNoSuchEntityException, "", nil)
}

func mockAwsIamClientGetter(sess *session.Session) iamiface.IAMAPI {
return &mockIAM{}
}
Expand Down