Skip to content

Commit

Permalink
Add missing tests for non-happy scenarios on GetInstance
Browse files Browse the repository at this point in the history
* Fix log key for GetInstance

[#160819509]

Co-authored-by: Derik Evangelista <[email protected]>
  • Loading branch information
Winna Bridgewater and Derik Evangelista committed Oct 26, 2018
1 parent 8211136 commit d378e48
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
8 changes: 5 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
deprovisionLogKey = "deprovision"
bindLogKey = "bind"
getBindLogKey = "getBinding"
getInstanceLogKey = "getInstance"
unbindLogKey = "unbind"
updateLogKey = "update"
lastOperationLogKey = "lastOperation"
Expand Down Expand Up @@ -294,7 +295,7 @@ func (h serviceBrokerHandler) update(w http.ResponseWriter, req *http.Request) {
}
h.respond(w, statusCode, UpdateResponse{
OperationData: updateServiceSpec.OperationData,
DashboardURL: updateServiceSpec.DashboardURL,
DashboardURL: updateServiceSpec.DashboardURL,
})
}

Expand Down Expand Up @@ -362,7 +363,7 @@ func (h serviceBrokerHandler) getInstance(w http.ResponseWriter, req *http.Reque
vars := mux.Vars(req)
instanceID := vars["instance_id"]

logger := h.logger.Session(getBindLogKey, lager.Data{
logger := h.logger.Session(getInstanceLogKey, lager.Data{
instanceIDLogKey: instanceID,
})

Expand All @@ -375,8 +376,9 @@ func (h serviceBrokerHandler) getInstance(w http.ResponseWriter, req *http.Reque
return
}
if versionCompatibility.Minor < 14 {
err = errors.New("get instance endpoint only supported starting with OSB version 2.14")
h.respond(w, http.StatusPreconditionFailed, ErrorResponse{
Description: "get instance endpoint only supported starting with OSB version 2.14",
Description: err.Error(),
})
logger.Error(apiVersionInvalidKey, err)
return
Expand Down
54 changes: 54 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,60 @@ var _ = Describe("Service Broker API", func() {
})
})
})

Describe("getting instance", func() {
It("returns the appropriate status code when it fails with a known error", func() {
fakeServiceBroker.GetInstanceError = brokerapi.NewFailureResponse(errors.New("some error"), http.StatusUnprocessableEntity, "fire")

response := makeGetInstanceRequest("instance-id")

Expect(response.StatusCode).To(Equal(http.StatusUnprocessableEntity))
Expect(lastLogLine().Message).To(ContainSubstring("broker-api.getInstance.fire"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("some error"))
})

It("returns 500 when it fails with an unknown error", func() {
fakeServiceBroker.GetInstanceError = errors.New("failed to get instance")

response := makeGetInstanceRequest("instance-id")

Expect(response.StatusCode).To(Equal(500))
Expect(lastLogLine().Message).To(ContainSubstring("broker-api.getInstance.unknown-error"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("failed to get instance"))
})

Context("the request is malformed", func() {
It("missing header X-Broker-API-Version", func() {
apiVersion = ""
response := makeGetInstanceRequest("instance-id")
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring(".getInstance.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header not set"))
})

It("has wrong version of API", func() {
apiVersion = "1.1"
response := makeGetInstanceRequest("instance-id")
Expect(response.StatusCode).To(Equal(412))
Expect(lastLogLine().Message).To(ContainSubstring(".getInstance.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("X-Broker-API-Version Header must be 2.x"))
})

It("is using api version < 2.14", func() {
apiVersion = "2.13"
response := makeGetInstanceRequest("instance-id")
Expect(response.StatusCode).To(Equal(412))

Expect(lastLogLine().Message).To(ContainSubstring(".getInstance.broker-api-version-invalid"))
Expect(lastLogLine().Data["error"]).To(ContainSubstring("get instance endpoint only supported starting with OSB version 2.14"))
})

It("missing instance-id", func() {
response := makeGetInstanceRequest("")
Expect(response.StatusCode).To(Equal(404))
})
})
})
})

Describe("binding lifecycle endpoint", func() {
Expand Down
3 changes: 2 additions & 1 deletion fakes/fake_service_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type FakeServiceBroker struct {
DeprovisionError error
LastOperationError error
UpdateError error
GetInstanceError error

BrokerCalled bool
LastOperationState brokerapi.LastOperationState
Expand Down Expand Up @@ -248,7 +249,7 @@ func (fakeBroker *FakeServiceBroker) GetInstance(context context.Context, instan
Parameters: map[string]interface{}{
"param1": "value1",
},
}, nil
}, fakeBroker.GetInstanceError
}

func (fakeBroker *FakeServiceBroker) Deprovision(context context.Context, instanceID string, details brokerapi.DeprovisionDetails, asyncAllowed bool) (brokerapi.DeprovisionServiceSpec, error) {
Expand Down

0 comments on commit d378e48

Please sign in to comment.