github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/application_feature_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 "time" 6 7 . "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/resources" 11 "code.cloudfoundry.org/clock/fakeclock" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Application Feature Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 20 fakeConfig *v7actionfakes.FakeConfig 21 fakeClock *fakeclock.FakeClock 22 ) 23 24 BeforeEach(func() { 25 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 26 fakeConfig = new(v7actionfakes.FakeConfig) 27 fakeClock = fakeclock.NewFakeClock(time.Now()) 28 actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, fakeClock) 29 }) 30 31 Describe("GetAppFeature", func() { 32 var ( 33 appGUID = "some-app-guid" 34 warnings Warnings 35 executeErr error 36 appFeature resources.ApplicationFeature 37 ) 38 39 JustBeforeEach(func() { 40 appFeature, warnings, executeErr = actor.GetAppFeature(appGUID, "ssh") 41 }) 42 43 Context("Getting SSH", func() { 44 When("it succeeds", func() { 45 BeforeEach(func() { 46 fakeCloudControllerClient.GetAppFeatureReturns( 47 resources.ApplicationFeature{Name: "ssh", Enabled: true}, 48 ccv3.Warnings{}, 49 nil, 50 ) 51 }) 52 53 It("calls ccv3 to check current ssh ability", func() { 54 Expect(fakeCloudControllerClient.GetAppFeatureCallCount()).To(Equal(1)) 55 appGuid, featureName := fakeCloudControllerClient.GetAppFeatureArgsForCall(0) 56 Expect(appGuid).To(Equal(appGUID)) 57 Expect(featureName).To(Equal("ssh")) 58 }) 59 60 It("returns an app feature", func() { 61 Expect(appFeature.Name).To(Equal("ssh")) 62 Expect(appFeature.Enabled).To(BeTrue()) 63 }) 64 65 When("desired enabled state is already, the current state", func() { 66 BeforeEach(func() { 67 fakeCloudControllerClient.GetAppFeatureReturns( 68 resources.ApplicationFeature{Enabled: true}, 69 ccv3.Warnings{"some-ccv3-warning"}, 70 nil, 71 ) 72 }) 73 74 It("returns a waring", func() { 75 Expect(warnings).To(ConsistOf("some-ccv3-warning")) 76 }) 77 }) 78 }) 79 80 When("when the API layer call returns an error", func() { 81 BeforeEach(func() { 82 fakeCloudControllerClient.GetAppFeatureReturns( 83 resources.ApplicationFeature{Enabled: false}, 84 ccv3.Warnings{"some-get-ssh-warning"}, 85 errors.New("some-get-ssh-error"), 86 ) 87 }) 88 89 It("returns the error and prints warnings", func() { 90 Expect(executeErr).To(MatchError("some-get-ssh-error")) 91 Expect(warnings).To(ConsistOf("some-get-ssh-warning")) 92 93 Expect(fakeCloudControllerClient.GetAppFeatureCallCount()).To(Equal(1)) 94 }) 95 }) 96 }) 97 }) 98 99 Describe("UpdateAppFeature", func() { 100 var ( 101 app = resources.Application{Name: "some-app", GUID: "some-app-guid"} 102 enabled = true 103 warnings Warnings 104 executeErr error 105 ) 106 107 JustBeforeEach(func() { 108 warnings, executeErr = actor.UpdateAppFeature(app, true, "ssh") 109 }) 110 111 Context("Getting SSH", func() { 112 When("it succeeds", func() { 113 It("calls ccv3 to enable ssh", func() { 114 Expect(fakeCloudControllerClient.UpdateAppFeatureCallCount()).To(Equal(1)) 115 actualApp, actualEnabled, featureName := fakeCloudControllerClient.UpdateAppFeatureArgsForCall(0) 116 Expect(actualApp).To(Equal(app.GUID)) 117 Expect(actualEnabled).To(Equal(enabled)) 118 Expect(featureName).To(Equal("ssh")) 119 }) 120 121 When("the API layer call is successful", func() { 122 BeforeEach(func() { 123 fakeCloudControllerClient.UpdateAppFeatureReturns(ccv3.Warnings{"some-update-ssh-warning"}, nil) 124 }) 125 126 It("does not error", func() { 127 Expect(executeErr).ToNot(HaveOccurred()) 128 Expect(warnings).To(ConsistOf("some-update-ssh-warning")) 129 }) 130 }) 131 }) 132 133 When("when the API layer call returns an error", func() { 134 BeforeEach(func() { 135 fakeCloudControllerClient.UpdateAppFeatureReturns( 136 ccv3.Warnings{"some-update-ssh-warning"}, 137 errors.New("some-update-ssh-error"), 138 ) 139 }) 140 141 It("returns the error and prints warnings", func() { 142 Expect(executeErr).To(MatchError("some-update-ssh-error")) 143 Expect(warnings).To(ConsistOf("some-update-ssh-warning")) 144 145 Expect(fakeCloudControllerClient.UpdateAppFeatureCallCount()).To(Equal(1)) 146 }) 147 }) 148 }) 149 }) 150 151 Describe("GetSSHEnabled", func() { 152 var ( 153 appGUID = "some-app-guid" 154 warnings Warnings 155 executeErr error 156 sshEnabled ccv3.SSHEnabled 157 ) 158 159 JustBeforeEach(func() { 160 sshEnabled, warnings, executeErr = actor.GetSSHEnabled(appGUID) 161 }) 162 163 When("it succeeds", func() { 164 BeforeEach(func() { 165 fakeCloudControllerClient.GetSSHEnabledReturns( 166 ccv3.SSHEnabled{Reason: "some-reason", Enabled: true}, 167 ccv3.Warnings{"some-ccv3-warning"}, 168 nil, 169 ) 170 }) 171 172 It("calls ccv3 to check current ssh ability", func() { 173 Expect(fakeCloudControllerClient.GetSSHEnabledCallCount()).To(Equal(1)) 174 appGuid := fakeCloudControllerClient.GetSSHEnabledArgsForCall(0) 175 Expect(appGuid).To(Equal(appGUID)) 176 }) 177 178 It("returns an sshEnabled", func() { 179 Expect(sshEnabled.Reason).To(Equal("some-reason")) 180 Expect(sshEnabled.Enabled).To(BeTrue()) 181 }) 182 183 It("returns a warning", func() { 184 Expect(warnings).To(ConsistOf("some-ccv3-warning")) 185 }) 186 187 When("when it's disabled", func() { 188 BeforeEach(func() { 189 fakeCloudControllerClient.GetSSHEnabledReturns( 190 ccv3.SSHEnabled{Reason: "another-reason", Enabled: false}, 191 ccv3.Warnings{"some-ccv3-warning"}, 192 nil, 193 ) 194 }) 195 196 It("returns an sshEnabled", func() { 197 Expect(sshEnabled.Reason).To(Equal("another-reason")) 198 Expect(sshEnabled.Enabled).To(BeFalse()) 199 }) 200 }) 201 }) 202 203 When("when the API layer call returns an error", func() { 204 BeforeEach(func() { 205 fakeCloudControllerClient.GetSSHEnabledReturns( 206 ccv3.SSHEnabled{Reason: "some-third-reason", Enabled: false}, 207 ccv3.Warnings{"some-get-ssh-warning"}, 208 errors.New("some-get-ssh-error"), 209 ) 210 }) 211 212 It("returns the error and prints warnings", func() { 213 Expect(executeErr).To(MatchError("some-get-ssh-error")) 214 Expect(warnings).To(ConsistOf("some-get-ssh-warning")) 215 216 Expect(fakeCloudControllerClient.GetSSHEnabledCallCount()).To(Equal(1)) 217 }) 218 }) 219 }) 220 221 Describe("GetSSHEnabledByAppName", func() { 222 var ( 223 appName = "some-app-name" 224 appGUID = "some-app-guid" 225 spaceGUID = "some-space-GUID" 226 warnings Warnings 227 executeErr error 228 sshEnabled ccv3.SSHEnabled 229 ) 230 231 JustBeforeEach(func() { 232 sshEnabled, warnings, executeErr = actor.GetSSHEnabledByAppName(appName, spaceGUID) 233 }) 234 235 BeforeEach(func() { 236 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns( 237 resources.Application{Name: appName, GUID: appGUID}, 238 ccv3.Warnings{"get-app-warning"}, 239 nil, 240 ) 241 242 fakeCloudControllerClient.GetSSHEnabledReturns( 243 ccv3.SSHEnabled{Enabled: false, Reason: "globally disabled"}, 244 ccv3.Warnings{"get-ssh-enabled-warning"}, 245 nil, 246 ) 247 }) 248 249 It("gets the app by name and space guid", func() { 250 Expect(executeErr).To(Not(HaveOccurred())) 251 Expect(sshEnabled).To(Equal(ccv3.SSHEnabled{Enabled: false, Reason: "globally disabled"})) 252 Expect(warnings).To(ConsistOf("get-app-warning", "get-ssh-enabled-warning")) 253 254 Expect(fakeCloudControllerClient.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 255 appNameArg, spaceGUIDArg := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(0) 256 Expect(appName).To(Equal(appNameArg)) 257 Expect(spaceGUID).To(Equal(spaceGUIDArg)) 258 }) 259 260 It("calls the API GetSSHEnabled with the expected arguments", func() { 261 Expect(executeErr).To(Not(HaveOccurred())) 262 Expect(sshEnabled).To(Equal(ccv3.SSHEnabled{Enabled: false, Reason: "globally disabled"})) 263 Expect(warnings).To(ConsistOf("get-app-warning", "get-ssh-enabled-warning")) 264 265 Expect(fakeCloudControllerClient.GetSSHEnabledCallCount()).To(Equal(1)) 266 appGUIDArg := fakeCloudControllerClient.GetSSHEnabledArgsForCall(0) 267 Expect(appGUID).To(Equal(appGUIDArg)) 268 }) 269 270 When("getting the app fails", func() { 271 BeforeEach(func() { 272 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns( 273 resources.Application{}, 274 ccv3.Warnings{"get-app-warning"}, 275 errors.New("get-app-error"), 276 ) 277 }) 278 279 It("returns the error and warnings", func() { 280 Expect(executeErr).To(MatchError("get-app-error")) 281 Expect(warnings).To(ConsistOf("get-app-warning")) 282 }) 283 }) 284 285 When("checking if SSH is enabled fails", func() { 286 BeforeEach(func() { 287 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns( 288 resources.Application{Name: appName, GUID: appGUID}, 289 ccv3.Warnings{"get-app-warning"}, 290 nil, 291 ) 292 293 fakeCloudControllerClient.GetSSHEnabledReturns( 294 ccv3.SSHEnabled{}, 295 ccv3.Warnings{"check-ssh-warning"}, 296 errors.New("check-ssh-error"), 297 ) 298 }) 299 300 It("returns the error and warnings", func() { 301 Expect(executeErr).To(MatchError("check-ssh-error")) 302 Expect(warnings).To(ConsistOf("get-app-warning", "check-ssh-warning")) 303 }) 304 }) 305 }) 306 })