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

Commit

Permalink
Add basic register broker command to svcat
Browse files Browse the repository at this point in the history
- refactor pkg/svcat to use an interface
- begin adding unit tests to cmd/svcat w/ faked out pkg interface
  • Loading branch information
jberkhahn committed Jul 5, 2018
1 parent 414619c commit cb41072
Show file tree
Hide file tree
Showing 30 changed files with 2,084 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cmd/svcat/binding/bind_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func (c *bindCmd) Run() error {
}

func (c *bindCmd) bind() error {
binding, err := c.App.Bind(c.Namespace, c.bindingName, c.externalID, c.instanceName, c.secretName, c.params, c.secrets)
binding, err := c.App.SDK.Bind(c.Namespace, c.bindingName, c.externalID, c.instanceName, c.secretName, c.params, c.secrets)
if err != nil {
return err
}

if c.Wait {
fmt.Fprintln(c.Output, "Waiting for binding to be injected...")
finalBinding, err := c.App.WaitForBinding(binding.Namespace, binding.Name, c.Interval, c.Timeout)
finalBinding, err := c.App.SDK.WaitForBinding(binding.Namespace, binding.Name, c.Interval, c.Timeout)
if err == nil {
binding = finalBinding
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/svcat/binding/describe_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (c *describeCmd) Run() error {
}

func (c *describeCmd) describe() error {
binding, err := c.App.RetrieveBinding(c.Namespace, c.name)
binding, err := c.App.SDK.RetrieveBinding(c.Namespace, c.name)
if err != nil {
return err
}

output.WriteBindingDetails(c.Output, binding)

secret, err := c.App.RetrieveSecretByBinding(binding)
secret, err := c.App.SDK.RetrieveSecretByBinding(binding)
output.WriteAssociatedSecret(c.Output, secret, err, c.showSecrets)

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/svcat/binding/get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *getCmd) Run() error {
}

func (c *getCmd) getAll() error {
bindings, err := c.App.RetrieveBindings(c.Namespace)
bindings, err := c.App.SDK.RetrieveBindings(c.Namespace)
if err != nil {
return err
}
Expand All @@ -81,7 +81,7 @@ func (c *getCmd) getAll() error {
}

func (c *getCmd) get() error {
binding, err := c.App.RetrieveBinding(c.Namespace, c.name)
binding, err := c.App.SDK.RetrieveBinding(c.Namespace, c.name)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/svcat/binding/unbind_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func (c *unbindCmd) Run() error {
var err error

if c.instanceName != "" {
bindings, err = c.App.Unbind(c.Namespace, c.instanceName)
bindings, err = c.App.SDK.Unbind(c.Namespace, c.instanceName)
} else {
bindings, err = c.App.DeleteBindings(c.getBindingsToDelete())
bindings, err = c.App.SDK.DeleteBindings(c.getBindingsToDelete())
}

if err != nil {
Expand Down Expand Up @@ -138,15 +138,15 @@ func (c *unbindCmd) waitForBindingDeletes(waitMessage string, bindings ...types.
go func(ns, name string) {
defer g.Done()

binding, err := c.App.WaitForBinding(ns, name, c.Interval, c.Timeout)
binding, err := c.App.SDK.WaitForBinding(ns, name, c.Interval, c.Timeout)

mutex.Lock()
defer mutex.Unlock()

if err != nil && !apierrors.IsNotFound(errors.Cause(err)) {
hasErrors = true
fmt.Fprintln(c.Output, err)
} else if c.App.IsBindingFailed(binding) {
} else if c.App.SDK.IsBindingFailed(binding) {
hasErrors = true
fmt.Fprintf(c.Output, "could not delete binding %s/%s\n", ns, name)
} else {
Expand Down
15 changes: 15 additions & 0 deletions cmd/svcat/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package broker_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"

_ "github.com/kubernetes-incubator/service-catalog/internal/test"
)

func TestBroker(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Broker Suite")
}
2 changes: 1 addition & 1 deletion cmd/svcat/broker/describe_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *describeCmd) Run() error {
}

func (c *describeCmd) Describe() error {
broker, err := c.App.RetrieveBroker(c.name)
broker, err := c.App.SDK.RetrieveBroker(c.name)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/svcat/broker/get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *getCmd) Run() error {
}

func (c *getCmd) getAll() error {
brokers, err := c.App.RetrieveBrokers()
brokers, err := c.App.SDK.RetrieveBrokers()
if err != nil {
return err
}
Expand All @@ -77,7 +77,7 @@ func (c *getCmd) getAll() error {
}

func (c *getCmd) get() error {
broker, err := c.App.RetrieveBroker(c.name)
broker, err := c.App.SDK.RetrieveBroker(c.name)
if err != nil {
return err
}
Expand Down
74 changes: 74 additions & 0 deletions cmd/svcat/broker/register_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2018 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 broker

import (
"fmt"

"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command"
"github.com/spf13/cobra"
)

type RegisterCmd struct {
BrokerName string
Context *command.Context
URL string
}

// NewRegisterCmd builds a "svcat register" command
func NewRegisterCmd(cxt *command.Context) *cobra.Command {
registerCmd := &RegisterCmd{
Context: cxt,
}
cmd := &cobra.Command{
Use: "register NAME --url URL",
Short: "Registers a new broker with service catalog",
Example: command.NormalizeExamples(`
svcat register mysqlbroker --url http://mysqlbroker.com
`),
PreRunE: command.PreRunE(registerCmd),
RunE: command.RunE(registerCmd),
}
cmd.Flags().StringVar(&registerCmd.URL, "url", "",
"The broker URL (Required)")
cmd.MarkFlagRequired("url")
return cmd
}

func (c *RegisterCmd) Validate(args []string) error {
if len(args) == 0 {
return fmt.Errorf("a broker name is required")
}
c.BrokerName = args[0]

return nil
}

func (c *RegisterCmd) Run() error {
return c.Register()
}

func (c *RegisterCmd) Register() error {
_, err := c.Context.App.SDK.Register(c.BrokerName, c.URL)
if err != nil {
return err
}

outputBytes := []byte(fmt.Sprintf("Successfully created ClusterServiceBroker \"%v\"", c.BrokerName))
c.Context.Output.Write(outputBytes)
return nil
}
96 changes: 96 additions & 0 deletions cmd/svcat/broker/register_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2018 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 broker_test

import (
"bytes"

. "github.com/kubernetes-incubator/service-catalog/cmd/svcat/broker"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/test"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kubernetes-incubator/service-catalog/pkg/svcat"
sdkFakes "github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Broker", func() {
var (
cxt *command.Context
)

BeforeEach(func() {
cxt = &command.Context{}
})

Describe("NewRegisterCmd", func() {
It("Builds and returns a cobra command", func() {
cmd := NewRegisterCmd(cxt)
Expect(*cmd).NotTo(BeNil())
Expect(cmd.Use).To(Equal("register NAME --url URL"))
Expect(cmd.Short).To(Equal("Registers a new broker with service catalog"))
Expect(cmd.Example).To(Equal(" svcat register mysqlbroker --url http://mysqlbroker.com"))
Expect(len(cmd.Aliases)).To(Equal(0))
})
})
Describe("Validate", func() {
It("errors if a broker name is not provided", func() {
cmd := RegisterCmd{
BrokerName: "",
Context: nil,
URL: "http://bananabroker.com",
}
err := cmd.Validate([]string{})
Expect(err).To(HaveOccurred())
})
})
Describe("Register", func() {
It("Calls the pkg/svcat libs Register method with the passed in variables and prints output to the user", func() {
brokerName := "foobarbroker"
brokerURL := "http://foobar.com"

brokerToReturn := &v1beta1.ClusterServiceBroker{
ObjectMeta: v1.ObjectMeta{
Name: brokerName,
},
}

outputBuffer := &bytes.Buffer{}

fakeApp, _ := svcat.NewApp(nil, nil, "default")
fakeSDK := new(sdkFakes.FakeSvcatAPI)
fakeSDK.RegisterReturns(brokerToReturn, nil)
fakeApp.SDK = fakeSDK
cmd := RegisterCmd{
Context: svcattest.NewContext(outputBuffer, fakeApp),
BrokerName: brokerName,
URL: brokerURL,
}
err := cmd.Register()

Expect(err).NotTo(HaveOccurred())
returnedName, returnedURL := fakeSDK.RegisterArgsForCall(0)
Expect(returnedName).To(Equal(brokerName))
Expect(returnedURL).To(Equal(brokerURL))

output := outputBuffer.String()
Expect(output).To(Equal("Successfully created ClusterServiceBroker \"foobarbroker\""))
})
})
})
2 changes: 1 addition & 1 deletion cmd/svcat/broker/sync_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *syncCmd) Run() error {

func (c *syncCmd) sync() error {
const retries = 3
err := c.App.Sync(c.name, retries)
err := c.App.SDK.Sync(c.name, retries)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/svcat/class/describe_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ func (c *describeCmd) describe() error {
var class *v1beta1.ClusterServiceClass
var err error
if c.lookupByUUID {
class, err = c.App.RetrieveClassByID(c.uuid)
class, err = c.App.SDK.RetrieveClassByID(c.uuid)
} else {
class, err = c.App.RetrieveClassByName(c.name)
class, err = c.App.SDK.RetrieveClassByName(c.name)
}
if err != nil {
return err
}

output.WriteClassDetails(c.Output, class)

plans, err := c.App.RetrievePlansByClass(class)
plans, err := c.App.SDK.RetrievePlansByClass(class)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/svcat/class/get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *getCmd) Run() error {
}

func (c *getCmd) getAll() error {
classes, err := c.App.RetrieveClasses()
classes, err := c.App.SDK.RetrieveClasses()
if err != nil {
return err
}
Expand All @@ -96,9 +96,9 @@ func (c *getCmd) get() error {
var err error

if c.lookupByUUID {
class, err = c.App.RetrieveClassByID(c.uuid)
class, err = c.App.SDK.RetrieveClassByID(c.uuid)
} else if c.name != "" {
class, err = c.App.RetrieveClassByName(c.name)
class, err = c.App.SDK.RetrieveClassByName(c.name)
}
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/svcat/instance/deprovision_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *deprovisonCmd) Run() error {
}

func (c *deprovisonCmd) deprovision() error {
err := c.App.Deprovision(c.Namespace, c.instanceName)
err := c.App.SDK.Deprovision(c.Namespace, c.instanceName)
if err != nil {
return err
}
Expand All @@ -77,10 +77,10 @@ func (c *deprovisonCmd) deprovision() error {
fmt.Fprintln(c.Output, "Waiting for the instance to be deleted...")

var instance *v1beta1.ServiceInstance
instance, err = c.App.WaitForInstance(c.Namespace, c.instanceName, c.Interval, c.Timeout)
instance, err = c.App.SDK.WaitForInstance(c.Namespace, c.instanceName, c.Interval, c.Timeout)

// The instance failed to deprovision cleanly, dump out more information on why
if c.App.IsInstanceFailed(instance) {
if c.App.SDK.IsInstanceFailed(instance) {
output.WriteInstanceDetails(c.Output, instance)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/svcat/instance/describe_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func (c *describeCmd) Run() error {
}

func (c *describeCmd) describe() error {
instance, err := c.App.RetrieveInstance(c.Namespace, c.name)
instance, err := c.App.SDK.RetrieveInstance(c.Namespace, c.name)
if err != nil {
return err
}

output.WriteInstanceDetails(c.Output, instance)

bindings, err := c.App.RetrieveBindingsByInstance(instance)
bindings, err := c.App.SDK.RetrieveBindingsByInstance(instance)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cb41072

Please sign in to comment.