Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Adding a fake broker server (#928)
Browse files Browse the repository at this point in the history
* Adding a fake broker server

This is a continuation of
#533, and
is a pre-requisite for
#923

* converting a single test to use the broker server

* adding boilerplate and docs

* naming the pivotal brokerapi package

to eliminate possible ambiguity

* adding clarifying comment to the catalog requests var

* adding better godocs
  • Loading branch information
arschles authored and pmorie committed Jun 13, 2017
1 parent 6403076 commit 1ae26db
Show file tree
Hide file tree
Showing 71 changed files with 6,532 additions and 99 deletions.
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
}

// 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

0 comments on commit 1ae26db

Please sign in to comment.