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

Test svcat gracefully handles the ns-broker flag being disabled #2234

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
83 changes: 83 additions & 0 deletions cmd/svcat/svcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,100 @@ import (
"github.com/kubernetes-incubator/service-catalog/internal/test"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/fake"
svcatfake "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/fake"
"github.com/kubernetes-incubator/service-catalog/pkg/svcat"
"github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8stesting "k8s.io/client-go/testing"
)

var catalogRequestRegex = regexp.MustCompile("/apis/servicecatalog.k8s.io/v1beta1/(.*)")
var coreRequestRegex = regexp.MustCompile("/api/v1/(.*)")

// Verify that svcat gracefully handles when the namespaced broker feature flag is disabled
// TODO: Once we take Namespaced brokers out from behind the feature flag, this test won't be necessary
Copy link
Contributor

Choose a reason for hiding this comment

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

probably once we get it out from the feature flag + some number of versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think you are right, I'm just not sure how long yet. Maybe until 1.0 honestly...

func TestGetSvcatWithNamespacedBrokerFeatureDisabled(t *testing.T) {
// Verify that commands work with the feature disabled, and don't return errors
testcases := []struct {
cmd string
wantOutput string
}{
{"get brokers", "my-cluster-broker"},
{"get classes", "my-cluster-class"},
{"get class my-cluster-class", "my-cluster-class"},
{"get plans", "my-cluster-plan"},
{"get plan my-cluster-plan", "my-cluster-plan"},
}

for _, tc := range testcases {
t.Run(tc.cmd, func(t *testing.T) {

// Setup fake data for the app
var fakes = []runtime.Object{
&v1beta1.ClusterServiceBroker{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster-broker",
},
},
&v1beta1.ClusterServiceClass{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster-class",
},
Spec: v1beta1.ClusterServiceClassSpec{
CommonServiceClassSpec: v1beta1.CommonServiceClassSpec{
ExternalName: "my-cluster-class",
},
},
},
&v1beta1.ClusterServicePlan{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster-plan",
},
Spec: v1beta1.ClusterServicePlanSpec{
CommonServicePlanSpec: v1beta1.CommonServicePlanSpec{
ExternalName: "my-cluster-plan",
},
},
},
}
svcatClient := svcatfake.NewSimpleClientset(fakes...)

// When the feature flag isn't enabled, the server will return resource not found
svcatClient.PrependReactor("list", "servicebrokers",
func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, k8serrors.NewNotFound(v1beta1.Resource("servicebrokers"), "")
})
svcatClient.PrependReactor("list", "serviceclasses",
func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, k8serrors.NewNotFound(v1beta1.Resource("serviceclasses"), "")
})
svcatClient.PrependReactor("list", "serviceplans",
func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, k8serrors.NewNotFound(v1beta1.Resource("serviceplans"), "")
})

cxt := newContext()
cxt.App = &svcat.App{
CurrentNamespace: "default",
SvcatClient: &servicecatalog.SDK{ServiceCatalogClient: svcatClient},
}

gotOutput := executeFakeCommand(t, tc.cmd, cxt, false)

if !strings.Contains(gotOutput, tc.wantOutput) {
t.Fatalf("unexpected command output \n\nWANT:\n%q\n\nGOT:\n%q\n", tc.wantOutput, gotOutput)
}
})
}

}

func TestCommandValidation(t *testing.T) {
testcases := []struct {
name string // Test Name
Expand Down
5 changes: 5 additions & 0 deletions pkg/svcat/service-catalog/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
)
Expand Down Expand Up @@ -62,6 +63,10 @@ func (sdk *SDK) RetrieveClasses(opts ScopeOptions) ([]Class, error) {
if opts.Scope.Matches(NamespaceScope) {
sc, err := sdk.ServiceCatalog().ServiceClasses(opts.Namespace).List(v1.ListOptions{})
if err != nil {
// Gracefully handle when the feature-flag for namespaced broker resources isn't enabled on the server.
if errors.IsNotFound(err) {
return classes, nil
}
return nil, fmt.Errorf("unable to list classes in %q (%s)", opts.Namespace, err)
}
for _, c := range sc.Items {
Expand Down