This repository was archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Adding a fake broker server #928
Merged
pmorie
merged 6 commits into
kubernetes-retired:master
from
arschles:fake-broker-server
Jun 13, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdda355
Adding a fake broker server
arschles f8269b5
converting a single test to use the broker server
arschles 52807a0
adding boilerplate and docs
arschles 49ff83d
naming the pivotal brokerapi package
arschles ad5a45f
adding clarifying comment to the catalog requests var
arschles ae9d176
adding better godocs
arschles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProvisionRespis defined in theHandler, and I can't predict what a reasonable default would be. Going forward, if multiple tests continuously define the same mock value (forProvisionRespor otherwise), we can then put in a defaultThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/brokerapipackage in another PR.