github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/unbind_security_group_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/integration/helpers" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gbytes" 11 . "github.com/onsi/gomega/gexec" 12 . "github.com/onsi/gomega/ghttp" 13 ) 14 15 var _ = Describe("unbind-security-group command", func() { 16 var ( 17 orgName string 18 securityGroupName string 19 spaceName string 20 ) 21 22 BeforeEach(func() { 23 orgName = helpers.NewOrgName() 24 securityGroupName = helpers.NewSecurityGroupName() 25 spaceName = helpers.NewSpaceName() 26 27 helpers.LoginCF() 28 }) 29 30 Describe("help", func() { 31 Context("when --help flag is set", func() { 32 It("Displays command usage to output", func() { 33 session := helpers.CF("unbind-security-group", "--help") 34 Eventually(session.Out).Should(Say("NAME:")) 35 Eventually(session.Out).Should(Say("\\s+unbind-security-group - Unbind a security group from a space")) 36 Eventually(session.Out).Should(Say("USAGE:")) 37 Eventually(session.Out).Should(Say("\\s+cf unbind-security-group SECURITY_GROUP ORG SPACE \\[--lifecycle \\(running \\| staging\\)\\]")) 38 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 39 Eventually(session.Out).Should(Say("OPTIONS:")) 40 Eventually(session.Out).Should(Say("\\s+--lifecycle Lifecycle phase the group applies to \\(Default: running\\)")) 41 Eventually(session.Out).Should(Say("SEE ALSO:")) 42 Eventually(session.Out).Should(Say("\\s+apps, restart, security-groups")) 43 Eventually(session).Should(Exit(0)) 44 }) 45 }) 46 }) 47 48 Context("when the lifecycle flag is invalid", func() { 49 It("outputs a message and usage", func() { 50 session := helpers.CF("unbind-security-group", securityGroupName, "some-org", "--lifecycle", "invalid") 51 Eventually(session.Err).Should(Say("Incorrect Usage: Invalid value `invalid' for option `--lifecycle'. Allowed values are: running or staging")) 52 Eventually(session.Out).Should(Say("USAGE:")) 53 Eventually(session).Should(Exit(1)) 54 }) 55 }) 56 57 Context("when the lifecycle flag has no argument", func() { 58 It("outputs a message and usage", func() { 59 session := helpers.CF("unbind-security-group", securityGroupName, "some-org", "--lifecycle") 60 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `--lifecycle'")) 61 Eventually(session.Out).Should(Say("USAGE:")) 62 Eventually(session).Should(Exit(1)) 63 }) 64 }) 65 66 Context("when the environment is not setup correctly", func() { 67 Context("when no API endpoint is set", func() { 68 BeforeEach(func() { 69 helpers.UnsetAPI() 70 }) 71 72 It("fails with no API endpoint set message", func() { 73 session := helpers.CF("unbind-security-group", securityGroupName) 74 Eventually(session.Out).Should(Say("FAILED")) 75 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 76 Eventually(session).Should(Exit(1)) 77 }) 78 }) 79 80 Context("when not logged in", func() { 81 BeforeEach(func() { 82 helpers.LogoutCF() 83 }) 84 85 It("fails with not logged in message", func() { 86 session := helpers.CF("unbind-security-group", securityGroupName) 87 Eventually(session.Out).Should(Say("FAILED")) 88 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 89 Eventually(session).Should(Exit(1)) 90 }) 91 }) 92 93 Context("when no org is targeted and no org is specified on the command line", func() { 94 BeforeEach(func() { 95 helpers.ClearTarget() 96 }) 97 98 It("fails with no org targeted error", func() { 99 session := helpers.CF("unbind-security-group", securityGroupName) 100 Eventually(session.Out).Should(Say("FAILED")) 101 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 102 Eventually(session).Should(Exit(1)) 103 }) 104 }) 105 106 Context("when no space is targeted and no space is specified on the command line", func() { 107 BeforeEach(func() { 108 helpers.ClearTarget() 109 helpers.CreateOrg(orgName) 110 helpers.TargetOrg(orgName) 111 }) 112 113 AfterEach(func() { 114 helpers.QuickDeleteOrg(orgName) 115 }) 116 117 It("fails with no space targeted error", func() { 118 session := helpers.CF("unbind-security-group", securityGroupName) 119 Eventually(session.Out).Should(Say("FAILED")) 120 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space.")) 121 Eventually(session).Should(Exit(1)) 122 }) 123 }) 124 }) 125 126 Context("when the server's API version is too low", func() { 127 var server *Server 128 129 BeforeEach(func() { 130 server = NewTLSServer() 131 server.AppendHandlers( 132 CombineHandlers( 133 VerifyRequest(http.MethodGet, "/v2/info"), 134 RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`), 135 ), 136 CombineHandlers( 137 VerifyRequest(http.MethodGet, "/v2/info"), 138 RespondWith(http.StatusOK, fmt.Sprintf(`{"api_version":"2.34.0", "authorization_endpoint": "%s"}`, server.URL())), 139 ), 140 CombineHandlers( 141 VerifyRequest(http.MethodGet, "/login"), 142 RespondWith(http.StatusOK, `{}`), 143 ), 144 ) 145 Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0)) 146 }) 147 148 AfterEach(func() { 149 server.Close() 150 }) 151 152 It("reports an error with a minimum-version message", func() { 153 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging") 154 155 Eventually(session.Err).Should(Say("Lifecycle value 'staging' requires CF API version 2\\.68\\.0\\ or higher. Your target is 2\\.34\\.0\\.")) 156 Eventually(session).Should(Exit(1)) 157 }) 158 }) 159 160 Context("when the input is invalid", func() { 161 Context("when the security group is not provided", func() { 162 It("fails with an incorrect usage message and displays help", func() { 163 session := helpers.CF("unbind-security-group") 164 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SECURITY_GROUP` was not provided")) 165 Eventually(session.Out).Should(Say("USAGE:")) 166 Eventually(session).Should(Exit(1)) 167 }) 168 }) 169 170 Context("when the space is not provided", func() { 171 It("fails with an incorrect usage message and displays help", func() { 172 session := helpers.CF("unbind-security-group", securityGroupName, "some-org") 173 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP`, `ORG`, and `SPACE` were not provided")) 174 Eventually(session.Out).Should(Say("USAGE:")) 175 Eventually(session).Should(Exit(1)) 176 }) 177 }) 178 }) 179 180 Context("when the security group doesn't exist", func() { 181 BeforeEach(func() { 182 helpers.CreateOrgAndSpace(orgName, spaceName) 183 }) 184 185 AfterEach(func() { 186 helpers.QuickDeleteOrg(orgName) 187 }) 188 189 It("fails with a 'security group not found' message", func() { 190 session := helpers.CF("unbind-security-group", "some-other-security-group", orgName, spaceName) 191 Eventually(session.Out).Should(Say("FAILED")) 192 Eventually(session.Err).Should(Say("Security group 'some-other-security-group' not found\\.")) 193 Eventually(session).Should(Exit(1)) 194 }) 195 }) 196 197 Context("when the security group exists", func() { 198 BeforeEach(func() { 199 someSecurityGroup := helpers.NewSecurityGroup(securityGroupName, "tcp", "127.0.0.1", "8443", "some-description") 200 someSecurityGroup.Create() 201 }) 202 203 Context("when the org doesn't exist", func() { 204 It("fails with an 'org not found' message", func() { 205 session := helpers.CF("unbind-security-group", securityGroupName, "some-other-org", "some-other-space") 206 Eventually(session.Out).Should(Say("FAILED")) 207 Eventually(session.Err).Should(Say("Organization 'some-other-org' not found\\.")) 208 Eventually(session).Should(Exit(1)) 209 }) 210 }) 211 212 Context("when the org exists", func() { 213 var username string 214 215 BeforeEach(func() { 216 username, _ = helpers.GetCredentials() 217 218 helpers.CreateOrg(orgName) 219 helpers.TargetOrg(orgName) 220 }) 221 222 AfterEach(func() { 223 helpers.QuickDeleteOrg(orgName) 224 }) 225 226 Context("when the space doesn't exist", func() { 227 It("fails with a 'space not found' message", func() { 228 session := helpers.CF("unbind-security-group", securityGroupName, orgName, "some-other-space") 229 Eventually(session.Out).Should(Say("FAILED")) 230 Eventually(session.Err).Should(Say("Space 'some-other-space' not found\\.")) 231 Eventually(session).Should(Exit(1)) 232 }) 233 }) 234 235 Context("when the space exists", func() { 236 BeforeEach(func() { 237 helpers.CreateSpace(spaceName) 238 }) 239 240 Context("when the space isn't bound to the security group in any lifecycle", func() { 241 It("successfully runs the command", func() { 242 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName) 243 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 244 Eventually(session.Out).Should(Say("OK")) 245 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 246 Eventually(session).Should(Exit(0)) 247 }) 248 }) 249 250 Context("when a space is bound to a security group in the running lifecycle", func() { 251 BeforeEach(func() { 252 Eventually(helpers.CF("bind-security-group", securityGroupName, orgName, spaceName)).Should(Exit(0)) 253 }) 254 255 Context("when the lifecycle flag is not set", func() { 256 Context("when the org and space are not provided", func() { 257 BeforeEach(func() { 258 helpers.TargetOrgAndSpace(orgName, spaceName) 259 }) 260 261 It("successfully unbinds the space from the security group", func() { 262 session := helpers.CF("unbind-security-group", securityGroupName) 263 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 264 Eventually(session.Out).Should(Say("OK")) 265 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 266 Eventually(session).Should(Exit(0)) 267 }) 268 }) 269 270 Context("when the org and space are provided", func() { 271 BeforeEach(func() { 272 helpers.ClearTarget() 273 }) 274 275 It("successfully unbinds the space from the security group", func() { 276 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName) 277 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 278 Eventually(session.Out).Should(Say("OK")) 279 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 280 Eventually(session).Should(Exit(0)) 281 }) 282 }) 283 }) 284 285 Context("when the lifecycle flag is running", func() { 286 Context("when the org and space are not provided", func() { 287 BeforeEach(func() { 288 helpers.TargetOrgAndSpace(orgName, spaceName) 289 }) 290 291 It("successfully unbinds the space from the security group", func() { 292 session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "running") 293 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 294 Eventually(session.Out).Should(Say("OK")) 295 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 296 Eventually(session).Should(Exit(0)) 297 }) 298 }) 299 300 Context("when the org and space are provided", func() { 301 BeforeEach(func() { 302 helpers.ClearTarget() 303 }) 304 305 It("successfully unbinds the space from the security group", func() { 306 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "running") 307 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 308 Eventually(session.Out).Should(Say("OK")) 309 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 310 Eventually(session).Should(Exit(0)) 311 }) 312 }) 313 }) 314 315 Context("when the lifecycle flag is staging", func() { 316 Context("when the org and space are not provided", func() { 317 BeforeEach(func() { 318 helpers.TargetOrgAndSpace(orgName, spaceName) 319 }) 320 321 It("displays an error and exits 1", func() { 322 session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "staging") 323 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 324 Eventually(session.Out).Should(Say("OK")) 325 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'staging'\\.", securityGroupName)) 326 Eventually(session).Should(Exit(0)) 327 }) 328 }) 329 330 Context("when the org and space are provided", func() { 331 BeforeEach(func() { 332 helpers.ClearTarget() 333 }) 334 335 It("displays an error and exits 1", func() { 336 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging") 337 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 338 Eventually(session.Out).Should(Say("OK")) 339 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'staging'\\.", securityGroupName)) 340 Eventually(session).Should(Exit(0)) 341 }) 342 }) 343 }) 344 }) 345 346 Context("when a space is bound to a security group in the staging lifecycle", func() { 347 BeforeEach(func() { 348 Eventually(helpers.CF("bind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0)) 349 }) 350 351 Context("when the lifecycle flag is not set", func() { 352 Context("when the org and space are not provided", func() { 353 BeforeEach(func() { 354 helpers.TargetOrgAndSpace(orgName, spaceName) 355 }) 356 357 It("displays an error and exits 1", func() { 358 session := helpers.CF("unbind-security-group", securityGroupName) 359 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 360 Eventually(session.Out).Should(Say("OK")) 361 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName)) 362 Eventually(session).Should(Exit(0)) 363 }) 364 }) 365 366 Context("when the org and space are provided", func() { 367 BeforeEach(func() { 368 helpers.ClearTarget() 369 }) 370 371 It("displays an error and exits 1", func() { 372 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName) 373 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 374 Eventually(session.Out).Should(Say("OK")) 375 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName)) 376 Eventually(session).Should(Exit(0)) 377 }) 378 }) 379 }) 380 381 Context("when the lifecycle flag is running", func() { 382 Context("when the org and space are not provided", func() { 383 BeforeEach(func() { 384 helpers.TargetOrgAndSpace(orgName, spaceName) 385 }) 386 387 It("displays an error and exits 1", func() { 388 session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "running") 389 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 390 Eventually(session.Out).Should(Say("OK")) 391 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName)) 392 Eventually(session).Should(Exit(0)) 393 }) 394 }) 395 396 Context("when the org and space are provided", func() { 397 BeforeEach(func() { 398 helpers.ClearTarget() 399 }) 400 401 It("displays an error and exits 1", func() { 402 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "running") 403 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 404 Eventually(session.Out).Should(Say("OK")) 405 Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName)) 406 Eventually(session).Should(Exit(0)) 407 }) 408 }) 409 }) 410 411 Context("when the lifecycle flag is staging", func() { 412 Context("when the org and space are not provided", func() { 413 BeforeEach(func() { 414 helpers.TargetOrgAndSpace(orgName, spaceName) 415 }) 416 417 It("successfully unbinds the space from the security group", func() { 418 session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "staging") 419 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 420 Eventually(session.Out).Should(Say("OK")) 421 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 422 Eventually(session).Should(Exit(0)) 423 }) 424 }) 425 426 Context("when the org and space are provided", func() { 427 BeforeEach(func() { 428 helpers.ClearTarget() 429 }) 430 431 It("successfully unbinds the space from the security group", func() { 432 session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging") 433 Eventually(session.Out).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username)) 434 Eventually(session.Out).Should(Say("OK")) 435 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 436 Eventually(session).Should(Exit(0)) 437 }) 438 }) 439 }) 440 }) 441 }) 442 }) 443 }) 444 })