github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/space_feature_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/resources" 8 9 . "code.cloudfoundry.org/cli/actor/v7action" 10 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("space features", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 24 fakeConfig := new(v7actionfakes.FakeConfig) 25 actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, nil) 26 }) 27 28 Describe("GetSpaceFeature", func() { 29 var ( 30 spaceName string 31 orgGUID string 32 featureName string 33 enabled bool 34 warnings Warnings 35 executeErr error 36 ) 37 38 BeforeEach(func() { 39 spaceName = "some-space-name" 40 orgGUID = "some-org-guid" 41 featureName = "ssh" 42 43 fakeCloudControllerClient.GetSpacesReturns( 44 []resources.Space{ 45 resources.Space{ 46 Name: spaceName, 47 GUID: "some-space-guid", 48 }, 49 }, 50 ccv3.IncludedResources{}, 51 ccv3.Warnings{"get-space-warning"}, 52 nil, 53 ) 54 55 fakeCloudControllerClient.GetSpaceFeatureReturns( 56 true, 57 ccv3.Warnings{"get-space-feature-warning"}, 58 nil, 59 ) 60 }) 61 62 JustBeforeEach(func() { 63 enabled, warnings, executeErr = actor.GetSpaceFeature(spaceName, orgGUID, featureName) 64 }) 65 66 It("finds the space and retrieves the feature value", func() { 67 Expect(executeErr).NotTo(HaveOccurred()) 68 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 69 query := fakeCloudControllerClient.GetSpacesArgsForCall(0) 70 Expect(query).To(ConsistOf( 71 ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}}, 72 ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}}, 73 )) 74 75 Expect(fakeCloudControllerClient.GetSpaceFeatureCallCount()).To(Equal(1)) 76 inputSpaceGUID, inputFeature := fakeCloudControllerClient.GetSpaceFeatureArgsForCall(0) 77 Expect(inputSpaceGUID).To(Equal("some-space-guid")) 78 Expect(inputFeature).To(Equal(featureName)) 79 80 Expect(enabled).To(BeTrue()) 81 Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning")) 82 }) 83 84 Context("when the space does not exist", func() { 85 BeforeEach(func() { 86 fakeCloudControllerClient.GetSpacesReturns( 87 []resources.Space{}, 88 ccv3.IncludedResources{}, 89 ccv3.Warnings{"get-space-warning"}, 90 nil, 91 ) 92 }) 93 94 It("returns a SpaceNotFoundError", func() { 95 Expect(executeErr).To(HaveOccurred()) 96 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName})) 97 Expect(warnings).To(ConsistOf("get-space-warning")) 98 }) 99 }) 100 101 Context("when an API error occurs", func() { 102 BeforeEach(func() { 103 fakeCloudControllerClient.GetSpaceFeatureReturns( 104 true, 105 ccv3.Warnings{"get-space-feature-warning"}, 106 errors.New("space-feature-error"), 107 ) 108 }) 109 110 It("returns the error and warnings", func() { 111 Expect(executeErr).To(HaveOccurred()) 112 Expect(executeErr).To(MatchError("space-feature-error")) 113 Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning")) 114 }) 115 }) 116 }) 117 118 Describe("AllowSpaceSSH", func() { 119 120 var ( 121 spaceName string 122 spaceGUID string 123 orgGUID string 124 enabled bool 125 feature = "ssh" 126 warnings Warnings 127 executeErr error 128 ) 129 130 BeforeEach(func() { 131 spaceName = "some-space-name" 132 spaceGUID = "some-space-guid" 133 enabled = true 134 feature = "ssh" 135 orgGUID = "some-org-guid" 136 fakeCloudControllerClient.GetSpacesReturns( 137 []resources.Space{ 138 resources.Space{ 139 Name: spaceName, 140 GUID: spaceGUID, 141 }, 142 }, 143 ccv3.IncludedResources{}, 144 ccv3.Warnings{"get-space-warning"}, 145 nil, 146 ) 147 148 fakeCloudControllerClient.UpdateSpaceFeatureReturns( 149 ccv3.Warnings{"update-space-feature-warning"}, 150 nil, 151 ) 152 }) 153 154 JustBeforeEach(func() { 155 warnings, executeErr = actor.UpdateSpaceFeature(spaceName, orgGUID, enabled, feature) 156 }) 157 158 When("the GetSpaceFeature returns an error", func() { 159 BeforeEach(func() { 160 fakeCloudControllerClient.GetSpaceFeatureReturns( 161 false, 162 ccv3.Warnings{"get-space-feature-warning"}, 163 errors.New("get-ssh-error"), 164 ) 165 }) 166 167 It("returns a the error", func() { 168 Expect(executeErr).To(MatchError("get-ssh-error")) 169 }) 170 171 }) 172 173 When("space ssh is already enabled", func() { 174 BeforeEach(func() { 175 fakeCloudControllerClient.GetSpaceFeatureReturns( 176 true, 177 ccv3.Warnings{"get-space-feature-warning"}, 178 nil, 179 ) 180 }) 181 182 When("trying to set ssh to enabled", func() { 183 It("returns a descriptive error", func() { 184 Expect(executeErr).To(MatchError(actionerror.SpaceSSHAlreadyEnabledError{ 185 Space: spaceName})) 186 }) 187 188 }) 189 190 When("trying to set ssh to disabled", func() { 191 192 BeforeEach(func() { 193 enabled = false 194 }) 195 196 It("disables the ssh feature for the space", func() { 197 Expect(executeErr).NotTo(HaveOccurred()) 198 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 199 query := fakeCloudControllerClient.GetSpacesArgsForCall(0) 200 Expect(query).To(ConsistOf( 201 ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}}, 202 ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}}, 203 )) 204 205 Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1)) 206 207 spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0) 208 209 Expect(spaceGUID).To(Equal(spaceGUID)) 210 Expect(enable).To(Equal(false)) 211 Expect(feature).To(Equal("ssh")) 212 Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning", "update-space-feature-warning")) 213 214 }) 215 }) 216 }) 217 218 When("space ssh is already disabled", func() { 219 BeforeEach(func() { 220 fakeCloudControllerClient.GetSpaceFeatureReturns( 221 false, 222 ccv3.Warnings{"get-space-feature-warning"}, 223 nil, 224 ) 225 }) 226 227 When("trying to set ssh to disabled", func() { 228 229 BeforeEach(func() { 230 enabled = false 231 }) 232 233 It("returns a descriptive error", func() { 234 Expect(executeErr).To(MatchError(actionerror.SpaceSSHAlreadyDisabledError{ 235 Space: spaceName})) 236 }) 237 238 }) 239 240 When("trying to set ssh to enabled", func() { 241 It("enables the ssh feature for the space", func() { 242 Expect(executeErr).NotTo(HaveOccurred()) 243 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 244 query := fakeCloudControllerClient.GetSpacesArgsForCall(0) 245 Expect(query).To(ConsistOf( 246 ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}}, 247 ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}}, 248 )) 249 250 Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1)) 251 252 spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0) 253 254 Expect(spaceGUID).To(Equal(spaceGUID)) 255 Expect(enable).To(Equal(true)) 256 Expect(feature).To(Equal("ssh")) 257 Expect(warnings).To(ConsistOf("get-space-warning", "get-space-feature-warning", "update-space-feature-warning")) 258 259 }) 260 }) 261 }) 262 When("the update space feature returns an error", func() { 263 BeforeEach(func() { 264 fakeCloudControllerClient.UpdateSpaceFeatureReturns( 265 ccv3.Warnings{"get-space-feature-warning"}, 266 errors.New("update-ssh-error"), 267 ) 268 }) 269 270 It("returns a the error", func() { 271 Expect(executeErr).To(MatchError("update-ssh-error")) 272 }) 273 274 }) 275 276 It("enables the ssh feature for the space", func() { 277 Expect(executeErr).NotTo(HaveOccurred()) 278 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 279 query := fakeCloudControllerClient.GetSpacesArgsForCall(0) 280 Expect(query).To(ConsistOf( 281 ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}}, 282 ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}}, 283 )) 284 285 Expect(fakeCloudControllerClient.UpdateSpaceFeatureCallCount()).To(Equal(1)) 286 287 spaceGUID, enable, feature := fakeCloudControllerClient.UpdateSpaceFeatureArgsForCall(0) 288 289 Expect(spaceGUID).To(Equal(spaceGUID)) 290 Expect(enable).To(Equal(true)) 291 Expect(feature).To(Equal("ssh")) 292 Expect(warnings).To(ConsistOf("get-space-warning", "update-space-feature-warning")) 293 294 }) 295 }) 296 })