Skip to content

Commit

Permalink
Merge pull request #31 from vsomayaji/unbind-tests
Browse files Browse the repository at this point in the history
Add Unbind tests
  • Loading branch information
jaymccon authored Sep 22, 2018
2 parents f157621 + 008f169 commit e8f37a4
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 19 deletions.
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":
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

0 comments on commit e8f37a4

Please sign in to comment.