Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.
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
73 changes: 70 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ import:
version: v1.1.0
- package: github.com/onsi/ginkgo
version: v1.3.1
- package: github.com/pivotal-cf/brokerapi
- package: code.cloudfoundry.org/lager
28 changes: 28 additions & 0 deletions pkg/brokerapi/fake/server/bind_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
"github.com/pivotal-cf/brokerapi"
)

// BindRequest is the struct to contain details of a bind request
type BindRequest struct {
InstanceID string
BindingID string
Details brokerapi.BindDetails
}
60 changes: 60 additions & 0 deletions pkg/brokerapi/fake/server/convert_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
pkgbrokerapi "github.com/kubernetes-incubator/service-catalog/pkg/brokerapi"
pivbrokerapi "github.com/pivotal-cf/brokerapi"
)

// ConvertCatalog converts a (github.com/kubernetes-incubator/service-catalog/pkg/brokerapi).Catalog
// to an array of brokerapi.Services
func ConvertCatalog(cat *pkgbrokerapi.Catalog) []pivbrokerapi.Service {
ret := make([]pivbrokerapi.Service, len(cat.Services))
for i, svc := range cat.Services {
ret[i] = convertService(svc)
}
return ret
}

func convertService(svc *pkgbrokerapi.Service) pivbrokerapi.Service {
return pivbrokerapi.Service{
ID: svc.ID,
Name: svc.Name,
Description: svc.Description,
Bindable: svc.Bindable,
Tags: svc.Tags,
PlanUpdatable: svc.PlanUpdateable,
Plans: convertPlans(svc.Plans),
// TODO: convert Requires, Metadata, DashboardClient
}
}

func convertPlans(plans []pkgbrokerapi.ServicePlan) []pivbrokerapi.ServicePlan {
ret := make([]pivbrokerapi.ServicePlan, len(plans))
for i, plan := range plans {
ret[i] = pivbrokerapi.ServicePlan{
ID: plan.ID,
Name: plan.Name,
Description: plan.Description,
Free: &plan.Free,
Bindable: plan.Bindable,
// TODO: convert Metadata
}
}
return ret
}
33 changes: 33 additions & 0 deletions pkg/brokerapi/fake/server/create_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
"net/http/httptest"

"github.com/kubernetes-incubator/service-catalog/pkg/brokerapi"
"github.com/kubernetes-incubator/service-catalog/pkg/brokerapi/openservicebroker"
)

// NewCreateFunc creates a new brokerapi.CreateFunc according to a broker server running
// in srv
func NewCreateFunc(srv *httptest.Server, user, pass string) brokerapi.CreateFunc {
// type CreateFunc func(name, url, username, password string) BrokerClient
return brokerapi.CreateFunc(func(name, url, username, password string) brokerapi.BrokerClient {
return openservicebroker.NewClient("testclient", srv.URL, user, pass)
})
}
27 changes: 27 additions & 0 deletions pkg/brokerapi/fake/server/deprovision_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
"github.com/pivotal-cf/brokerapi"
)

// DeprovisionRequest is the struct to contain details of a single deprovision request
type DeprovisionRequest struct {
InstanceID string
Details brokerapi.DeprovisionDetails
}
133 changes: 133 additions & 0 deletions pkg/brokerapi/fake/server/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
"context"

"github.com/pivotal-cf/brokerapi"
)

// Handler is a fake implementation oif a brokerapi.ServiceBroker. It's useful as a mock
// because it has pre-canned response values for use in testing, and also keeps track of calls
// made to it. Handler is not concurrency-safe
type Handler struct {
Catalog []brokerapi.Service
// Since there are no data passed to catalog calls, this is just the number of calls
// that were made to the catalog endpoint
CatalogRequests int

ProvisionResp brokerapi.ProvisionedServiceSpec
ProvisionRespError error
ProvisionRequests []ProvisionRequest

DeprovisionResp brokerapi.DeprovisionServiceSpec
DeprovisonRespErr error
DeprovisionRequests []DeprovisionRequest

BindResp brokerapi.Binding
BindRespErr error
BindRequests []BindRequest

UnbindRespErr error
UnbindRequests []UnbindRequest

UpdateResp brokerapi.UpdateServiceSpec
UpdateRespErr error
UpdateRequests []UpdateRequest

LastOperationResp brokerapi.LastOperation
LastOperationRespErr error
LastOperationRequests []LastOperationRequest
}

// NewHandler creates a new fake server handler
func NewHandler() *Handler {
return &Handler{}
}

// Services increments h.CatalogRequests and returns h.Catalog
func (h *Handler) Services(ctx context.Context) []brokerapi.Service {
h.CatalogRequests++
return h.Catalog
}

// Provision adds an element to h.ProvisionRequests and returns
// h.ProvisionResp, h.ProvisionRespError
func (h *Handler) Provision(
ctx context.Context,
instanceID string,
details brokerapi.ProvisionDetails,
asyncAllowed bool,
) (brokerapi.ProvisionedServiceSpec, error) {
h.ProvisionRequests = append(h.ProvisionRequests, ProvisionRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.ProvisionResp, h.ProvisionRespError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't see where things like ProvisionResp is defined - I'm guessing a follow-on PR will do that and I'm also guessing that it'll be up to the client/tester to do it. If so, would it make sense to have some default logic here so that the client/tester doesn't need to hard code all responses, but instead the fake broker will do what's expected in most cases and the client just needs to provide these canned responses if they want something other than the default/happy-path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ProvisionResp is defined in the Handler, and I can't predict what a reasonable default would be. Going forward, if multiple tests continuously define the same mock value (for ProvisionResp or otherwise), we can then put in a default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was thinking that the "reasonable defaults" would be based on previous data. For example, we have a canned catalog and someone asks to provision an instance - we know what we should return from the provision request, no? Even the case of the client defining the catalog for us we can still generate an instance and even a binding if a custom one isn't created during the test - I would think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@duglin I see what you're suggesting here. That kind of functionality wouldn't be appropriate here because this is a test mock, which exists to help unit tests exercise code-under-test in a controller manner.

It's not appropriate for this mock server to do much more than record request details and regurgitate pre-programmed responses. More advanced servers are more suitable for use in higher-level tests. One could be certainly built using github.com/pivotal-cf/brokerapi package in another PR.

}

// Deprovision adds an element to h.DeprovisionRequests and returns
// h.DeprovisionResp, h.DeprovisionRespErr
func (h *Handler) Deprovision(context context.Context, instanceID string, details brokerapi.DeprovisionDetails, asyncAllowed bool) (brokerapi.DeprovisionServiceSpec, error) {
h.DeprovisionRequests = append(h.DeprovisionRequests, DeprovisionRequest{
InstanceID: instanceID,
Details: details,
})
return h.DeprovisionResp, h.DeprovisonRespErr
}

// Bind adds an element to h.BindRequqests and returns h.BindResp, h.BindRespErr
func (h *Handler) Bind(context context.Context, instanceID, bindingID string, details brokerapi.BindDetails) (brokerapi.Binding, error) {
h.BindRequests = append(h.BindRequests, BindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.BindResp, h.BindRespErr
}

// Unbind adds an element to h.UnbindRequests and returns h.UnbindRespErr
func (h *Handler) Unbind(context context.Context, instanceID, bindingID string, details brokerapi.UnbindDetails) error {
h.UnbindRequests = append(h.UnbindRequests, UnbindRequest{
InstanceID: instanceID,
BindingID: bindingID,
Details: details,
})
return h.UnbindRespErr
}

// Update adds an element to h.UpdateRequests and returns h.UpdateResp, h.UpdateRespErr
func (h *Handler) Update(context context.Context, instanceID string, details brokerapi.UpdateDetails, asyncAllowed bool) (brokerapi.UpdateServiceSpec, error) {
h.UpdateRequests = append(h.UpdateRequests, UpdateRequest{
InstanceID: instanceID,
Details: details,
AsyncAllowed: asyncAllowed,
})
return h.UpdateResp, h.UpdateRespErr
}

// LastOperation adds an element to h.LastOperationRequests and returns
// h.LastOperationResp, h.LastOperationRespErr
func (h *Handler) LastOperation(context context.Context, instanceID, operationData string) (brokerapi.LastOperation, error) {
h.LastOperationRequests = append(h.LastOperationRequests, LastOperationRequest{
InstanceID: instanceID,
OperationData: operationData,
})
return h.LastOperationResp, h.LastOperationRespErr
}
Loading