github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/isolation_segments_command_test.go (about) 1 package v6_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 . "code.cloudfoundry.org/cli/command/v6" 12 "code.cloudfoundry.org/cli/command/v6/v6fakes" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("isolation-segments Command", func() { 21 var ( 22 cmd IsolationSegmentsCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v6fakes.FakeIsolationSegmentsActor 27 binaryName string 28 executeErr error 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v6fakes.FakeIsolationSegmentsActor) 36 37 cmd = IsolationSegmentsCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 }) 47 48 JustBeforeEach(func() { 49 executeErr = cmd.Execute(nil) 50 }) 51 52 When("the API version is below the minimum", func() { 53 BeforeEach(func() { 54 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion) 55 }) 56 57 It("returns a MinimumAPIVersionNotMetError", func() { 58 Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{ 59 CurrentVersion: ccversion.MinV3ClientVersion, 60 MinimumVersion: ccversion.MinVersionIsolationSegmentV3, 61 })) 62 }) 63 }) 64 65 When("checking target fails", func() { 66 BeforeEach(func() { 67 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 68 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 69 }) 70 71 It("returns an error", func() { 72 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 73 74 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 75 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 76 Expect(checkTargetedOrg).To(BeFalse()) 77 Expect(checkTargetedSpace).To(BeFalse()) 78 }) 79 }) 80 81 When("checking target does not fail", func() { 82 BeforeEach(func() { 83 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3) 84 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 85 }) 86 87 When("an error is not encountered getting the isolation segment summaries", func() { 88 When("there are isolation segments", func() { 89 BeforeEach(func() { 90 fakeActor.GetIsolationSegmentSummariesReturns( 91 []v3action.IsolationSegmentSummary{ 92 { 93 Name: "some-iso-1", 94 EntitledOrgs: []string{}, 95 }, 96 { 97 Name: "some-iso-2", 98 EntitledOrgs: []string{"some-org-1"}, 99 }, 100 { 101 Name: "some-iso-3", 102 EntitledOrgs: []string{"some-org-1", "some-org-2"}, 103 }, 104 }, 105 v3action.Warnings{"warning-1", "warning-2"}, 106 nil, 107 ) 108 }) 109 110 It("displays the isolation segment summaries and all warnings", func() { 111 Expect(executeErr).ToNot(HaveOccurred()) 112 113 Expect(testUI.Out).To(Say("Getting isolation segments as banana...")) 114 Expect(testUI.Out).To(Say("OK\n\n")) 115 Expect(testUI.Out).To(Say(`name\s+orgs`)) 116 Expect(testUI.Out).To(Say("some-iso-1")) 117 Expect(testUI.Out).To(Say(`some-iso-2\s+some-org-1`)) 118 Expect(testUI.Out).To(Say(`some-iso-3\s+some-org-1, some-org-2`)) 119 120 Expect(testUI.Err).To(Say("warning-1")) 121 Expect(testUI.Err).To(Say("warning-2")) 122 123 Expect(fakeActor.GetIsolationSegmentSummariesCallCount()).To(Equal(1)) 124 }) 125 }) 126 127 When("there are no isolation segments", func() { 128 BeforeEach(func() { 129 fakeActor.GetIsolationSegmentSummariesReturns( 130 []v3action.IsolationSegmentSummary{}, 131 nil, 132 nil, 133 ) 134 }) 135 It("displays the empty table", func() { 136 Expect(executeErr).ToNot(HaveOccurred()) 137 Expect(testUI.Out).To(Say("Getting isolation segments as banana...")) 138 Expect(testUI.Out).To(Say("OK\n\n")) 139 Expect(testUI.Out).To(Say(`name\s+orgs`)) 140 Expect(testUI.Out).NotTo(Say("[a-zA-Z]+")) 141 142 Expect(fakeActor.GetIsolationSegmentSummariesCallCount()).To(Equal(1)) 143 }) 144 }) 145 }) 146 147 When("an error is encountered getting the isolation segment summaries", func() { 148 var expectedError error 149 BeforeEach(func() { 150 expectedError = errors.New("some-error") 151 fakeActor.GetIsolationSegmentSummariesReturns( 152 []v3action.IsolationSegmentSummary{}, 153 v3action.Warnings{"warning-1", "warning-2"}, 154 expectedError, 155 ) 156 }) 157 158 It("displays warnings and returns the error", func() { 159 Expect(executeErr).To(MatchError(expectedError)) 160 161 Expect(testUI.Out).To(Say("Getting isolation segments as banana...")) 162 Expect(testUI.Out).NotTo(Say("OK")) 163 164 Expect(testUI.Err).To(Say("warning-1")) 165 Expect(testUI.Err).To(Say("warning-2")) 166 }) 167 }) 168 }) 169 })