This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 382
Add ginkgo tests for svcat get classes #2230
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-retired:master
from
carolynvs:ginkgo-tests-for-get-classes
Jul 30, 2018
Merged
Changes from 1 commit
Commits
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
This file contains 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 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 |
---|---|---|
|
@@ -28,6 +28,9 @@ import ( | |
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/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog/service-catalogfakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
k8sfake "k8s.io/client-go/kubernetes/fake" | ||
|
@@ -174,3 +177,126 @@ func TestListClasses(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
var _ = Describe("Get Classes Command", func() { | ||
Describe("NewGetClassesCmd", func() { | ||
It("Builds and returns a cobra command", func() { | ||
cxt := &command.Context{} | ||
cmd := NewGetCmd(cxt) | ||
Expect(*cmd).NotTo(BeNil()) | ||
Expect(cmd.Use).To(Equal("classes [NAME]")) | ||
Expect(cmd.Short).To(ContainSubstring("List classes, optionally filtered by name, scope or namespace")) | ||
Expect(cmd.Example).To(ContainSubstring("svcat get classes")) | ||
Expect(cmd.Example).To(ContainSubstring("svcat get classes --scope cluster")) | ||
Expect(cmd.Example).To(ContainSubstring("svcat get classes --scope namespace --namespace dev")) | ||
Expect(len(cmd.Aliases)).To(Equal(2)) | ||
}) | ||
}) | ||
Describe("Validate", func() { | ||
It("allows class name arg to be empty", func() { | ||
cmd := &getCmd{} | ||
err := cmd.Validate([]string{}) | ||
Expect(err).To(BeNil()) | ||
}) | ||
It("optionally parses the class name argument", func() { | ||
cmd := &getCmd{} | ||
err := cmd.Validate([]string{"mysqldb"}) | ||
Expect(err).To(BeNil()) | ||
Expect(cmd.name).To(Equal("mysqldb")) | ||
}) | ||
}) | ||
Describe("getAll", func() { | ||
It("Calls the pkg/svcat libs RetrieveClasses with namespace scope and current namespace", func() { | ||
outputBuffer := &bytes.Buffer{} | ||
|
||
fakeApp, _ := svcat.NewApp(nil, nil, "default") | ||
fakeSDK := new(servicecatalogfakes.FakeSvcatClient) | ||
fakeSDK.RetrieveClassesReturns( | ||
[]servicecatalog.Class{&v1beta1.ServiceClass{ObjectMeta: metav1.ObjectMeta{Name: "mysqldb", Namespace: "default"}}}, | ||
nil) | ||
fakeApp.SvcatClient = fakeSDK | ||
cmd := getCmd{ | ||
Namespaced: &command.Namespaced{Context: svcattest.NewContext(outputBuffer, fakeApp)}, | ||
Scoped: command.NewScoped(), | ||
} | ||
cmd.Namespace = "default" | ||
cmd.Scope = servicecatalog.NamespaceScope | ||
|
||
err := cmd.getAll() | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
scopeArg := fakeSDK.RetrieveClassesArgsForCall(0) | ||
Expect(scopeArg).To(Equal(servicecatalog.ScopeOptions{ | ||
Namespace: "default", | ||
Scope: servicecatalog.NamespaceScope, | ||
})) | ||
|
||
output := outputBuffer.String() | ||
Expect(output).To(ContainSubstring("mysqldb")) | ||
}) | ||
It("Calls the pkg/svcat libs RetrieveClasses with namespace scope and all namespaces", func() { | ||
outputBuffer := &bytes.Buffer{} | ||
|
||
fakeApp, _ := svcat.NewApp(nil, nil, "default") | ||
fakeSDK := new(servicecatalogfakes.FakeSvcatClient) | ||
fakeSDK.RetrieveClassesReturns( | ||
[]servicecatalog.Class{ | ||
&v1beta1.ServiceClass{ObjectMeta: metav1.ObjectMeta{Name: "mysqldb", Namespace: "default"}}, | ||
&v1beta1.ServiceClass{ObjectMeta: metav1.ObjectMeta{Name: "postgresdb", Namespace: "test-ns"}}, | ||
}, | ||
nil) | ||
fakeApp.SvcatClient = fakeSDK | ||
cmd := getCmd{ | ||
Namespaced: &command.Namespaced{Context: svcattest.NewContext(outputBuffer, fakeApp)}, | ||
Scoped: command.NewScoped(), | ||
} | ||
cmd.Namespace = "" | ||
cmd.Scope = servicecatalog.NamespaceScope | ||
|
||
err := cmd.getAll() | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
scopeArg := fakeSDK.RetrieveClassesArgsForCall(0) | ||
Expect(scopeArg).To(Equal(servicecatalog.ScopeOptions{ | ||
Namespace: "", | ||
Scope: servicecatalog.NamespaceScope, | ||
})) | ||
|
||
output := outputBuffer.String() | ||
Expect(output).To(ContainSubstring("mysqldb")) | ||
Expect(output).To(ContainSubstring("postgresdb")) | ||
}) | ||
It("Calls the pkg/svcat libs RetrieveClasses with all scope and current namespaces", func() { | ||
outputBuffer := &bytes.Buffer{} | ||
|
||
fakeApp, _ := svcat.NewApp(nil, nil, "default") | ||
fakeSDK := new(servicecatalogfakes.FakeSvcatClient) | ||
fakeSDK.RetrieveClassesReturns( | ||
[]servicecatalog.Class{ | ||
&v1beta1.ClusterServiceClass{ObjectMeta: metav1.ObjectMeta{Name: "mysqldb"}}, | ||
&v1beta1.ServiceClass{ObjectMeta: metav1.ObjectMeta{Name: "postgresdb", Namespace: "default"}}, | ||
}, | ||
nil) | ||
fakeApp.SvcatClient = fakeSDK | ||
cmd := getCmd{ | ||
Namespaced: &command.Namespaced{Context: svcattest.NewContext(outputBuffer, fakeApp)}, | ||
Scoped: command.NewScoped(), | ||
} | ||
cmd.Namespace = "default" | ||
cmd.Scope = servicecatalog.AllScope | ||
|
||
err := cmd.getAll() | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
scopeArg := fakeSDK.RetrieveClassesArgsForCall(0) | ||
Expect(scopeArg).To(Equal(servicecatalog.ScopeOptions{ | ||
Namespace: "default", | ||
Scope: servicecatalog.AllScope, | ||
})) | ||
|
||
output := outputBuffer.String() | ||
Expect(output).To(ContainSubstring("mysqldb")) | ||
Expect(output).To(ContainSubstring("postgresdb")) | ||
}) | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would also be nice to have a set of cases for when you're just getting a single class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the next PR. This change was only for getting a list of classes. 😀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
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.
generally you don't have to test private methods, as they should be subsumed in whatever public methods use them. I would change this to test Run(), and control the different cases by modifying whether you pass in a uuid or name.
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.
What's wrong with a unit test testing a single function?
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.
it's not the testing a single function, it's that it's a private function - if the content of getAll was just inlined in Run, it wouldn't make sense to try to test the functionality of that block specifically, and because it's private, it kinda is just inlined.
This wouldn't be a big change, just switch the describe to "Run", and change the calls to to getAll with calls to Run with the appropriate fields filled in.
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.
Add a comment to the test about not worrying about it if it breaks. I'm not hung up on idealogical test purity that says you only test public methods.
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.
Why would we add a test that we don't care if it breaks? And you do test private methods, but implicitly. A test for Run(), which I think we should definitely have, would be 99% the same lines of code as the tests she's written here for getAll() and a hypothetical test for get()