github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/security_groups_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 . "code.cloudfoundry.org/cli/command/v2" 12 "code.cloudfoundry.org/cli/command/v2/v2fakes" 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("security-groups Command", func() { 21 var ( 22 cmd SecurityGroupsCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v2fakes.FakeSecurityGroupsActor 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(v2fakes.FakeSecurityGroupsActor) 36 37 cmd = SecurityGroupsCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 binaryName = "faceman" 45 fakeConfig.BinaryNameReturns(binaryName) 46 fakeConfig.ExperimentalReturns(true) 47 48 fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil) 49 }) 50 51 JustBeforeEach(func() { 52 executeErr = cmd.Execute(nil) 53 }) 54 55 Context("when getting the current user fails", func() { 56 var expectedErr error 57 58 BeforeEach(func() { 59 expectedErr = errors.New("getting user failed") 60 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 61 }) 62 63 It("returns the error", func() { 64 Expect(executeErr).To(MatchError(expectedErr)) 65 }) 66 }) 67 68 Context("when checking target fails", func() { 69 BeforeEach(func() { 70 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 71 }) 72 73 It("returns an error", func() { 74 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 75 76 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 77 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 78 Expect(checkTargetedOrg).To(BeFalse()) 79 Expect(checkTargetedSpace).To(BeFalse()) 80 }) 81 }) 82 83 Context("when the API version is low enough not to support fetching staging", func() { 84 BeforeEach(func() { 85 fakeActor.CloudControllerAPIVersionReturns("2.36.0") 86 }) 87 88 It("makes the fetch indicating that staging should not be included", func() { 89 Expect(executeErr).NotTo(HaveOccurred()) 90 91 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 92 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1)) 93 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleArgsForCall(0)).To(BeFalse()) 94 95 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1)) 96 }) 97 }) 98 99 Context("when the API version is high enough to support fetching staging", func() { 100 BeforeEach(func() { 101 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionLifecyleStagingV2) 102 }) 103 104 Context("when the list of security groups is returned", func() { 105 var secGroups []v2action.SecurityGroupWithOrganizationSpaceAndLifecycle 106 107 BeforeEach(func() { 108 secGroups = []v2action.SecurityGroupWithOrganizationSpaceAndLifecycle{ 109 { 110 SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"}, 111 Organization: &v2action.Organization{Name: "org-11"}, 112 Space: &v2action.Space{Name: "space-111"}, 113 Lifecycle: constant.SecurityGroupLifecycleRunning, 114 }, 115 { 116 SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"}, 117 Organization: &v2action.Organization{Name: "org-12"}, 118 Space: &v2action.Space{Name: "space-121"}, 119 Lifecycle: constant.SecurityGroupLifecycleRunning, 120 }, 121 { 122 SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"}, 123 Organization: &v2action.Organization{Name: "org-12"}, 124 Space: &v2action.Space{Name: "space-122"}, 125 Lifecycle: constant.SecurityGroupLifecycleStaging, 126 }, 127 { 128 SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-2"}, 129 Organization: &v2action.Organization{}, 130 Space: &v2action.Space{}, 131 }, 132 { 133 SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-3"}, 134 Organization: &v2action.Organization{Name: "org-31"}, 135 Space: &v2action.Space{Name: "space-311"}, 136 Lifecycle: constant.SecurityGroupLifecycleRunning, 137 }, 138 { 139 SecurityGroup: &v2action.SecurityGroup{ 140 Name: "seg-group-4", 141 RunningDefault: true, 142 }, 143 Organization: &v2action.Organization{Name: ""}, 144 Space: &v2action.Space{Name: ""}, 145 Lifecycle: constant.SecurityGroupLifecycleRunning, 146 }, 147 { 148 SecurityGroup: &v2action.SecurityGroup{ 149 Name: "seg-group-4", 150 StagingDefault: true, 151 }, 152 Organization: &v2action.Organization{Name: ""}, 153 Space: &v2action.Space{Name: ""}, 154 Lifecycle: constant.SecurityGroupLifecycleStaging, 155 }, 156 } 157 fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleReturns(secGroups, v2action.Warnings{"warning-1", "warning-2"}, nil) 158 }) 159 160 It("displays a table containing the security groups, the spaces to which they are bound, the spaces' orgs, and the lifecycle of the app they were assigned to", func() { 161 Expect(executeErr).To(BeNil()) 162 163 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 164 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1)) 165 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleArgsForCall(0)).To(BeTrue()) 166 167 Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1)) 168 169 Expect(testUI.Out).To(Say("Getting security groups as some-user\\.\\.\\.")) 170 Expect(testUI.Out).To(Say("OK\\n\\n")) 171 Expect(testUI.Out).To(Say("\\s+name\\s+organization\\s+space\\s+lifecycle")) 172 Expect(testUI.Out).To(Say("#0\\s+seg-group-1\\s+org-11\\s+space-111\\s+running")) 173 Expect(testUI.Out).To(Say("(?m)\\s+seg-group-1\\s+org-12\\s+space-121\\s+running")) 174 Expect(testUI.Out).To(Say("(?m)\\s+seg-group-1\\s+org-12\\s+space-122\\s+staging")) 175 Expect(testUI.Out).To(Say("#1\\s+seg-group-2\\s+")) 176 Expect(testUI.Out).To(Say("#2\\s+seg-group-3\\s+org-31\\s+space-311\\s+running")) 177 Expect(testUI.Out).To(Say("#3\\s+seg-group-4\\s+<all>\\s+<all>\\s+running")) 178 Expect(testUI.Out).To(Say("(?m)\\s+seg-group-4\\s+<all>\\s+<all>\\s+staging")) 179 Expect(testUI.Err).To(Say("warning-1")) 180 Expect(testUI.Err).To(Say("warning-2")) 181 }) 182 }) 183 184 Context("when an error is encountered fetching the security groups", func() { 185 BeforeEach(func() { 186 fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleReturns(nil, v2action.Warnings{"warning-1", "warning-2"}, errors.New("generic")) 187 }) 188 189 It("returns the error", func() { 190 Expect(executeErr).To(MatchError("generic")) 191 192 Expect(testUI.Err).To(Say("warning-1")) 193 Expect(testUI.Err).To(Say("warning-2")) 194 }) 195 }) 196 }) 197 })