github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/bind_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("bind-security-group command", func() { 16 var ( 17 orgName string 18 secGroupName string 19 someOrgName string 20 spaceName1 string 21 spaceName2 string 22 ) 23 24 BeforeEach(func() { 25 orgName = helpers.NewOrgName() 26 secGroupName = helpers.NewSecurityGroupName() 27 someOrgName = helpers.NewOrgName() 28 spaceName1 = helpers.NewSpaceName() 29 spaceName2 = helpers.NewSpaceName() 30 31 helpers.LoginCF() 32 }) 33 34 Describe("help", func() { 35 Context("when --help flag is set", func() { 36 It("Displays command usage to output", func() { 37 session := helpers.CF("bind-security-group", "--help") 38 Eventually(session.Out).Should(Say("NAME:")) 39 Eventually(session.Out).Should(Say("\\s+bind-security-group - Bind a security group to a particular space, or all existing spaces of an org")) 40 Eventually(session.Out).Should(Say("USAGE:")) 41 Eventually(session.Out).Should(Say("\\s+cf bind-security-group SECURITY_GROUP ORG \\[SPACE\\] \\[--lifecycle \\(running \\| staging\\)\\]")) 42 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 43 Eventually(session.Out).Should(Say("OPTIONS:")) 44 Eventually(session.Out).Should(Say("\\s+--lifecycle Lifecycle phase the group applies to \\(Default: running\\)")) 45 Eventually(session.Out).Should(Say("SEE ALSO:")) 46 Eventually(session.Out).Should(Say("\\s+apps, bind-running-security-group, bind-staging-security-group, restart, security-groups")) 47 Eventually(session).Should(Exit(0)) 48 }) 49 }) 50 }) 51 52 Context("when the lifecycle flag is invalid", func() { 53 It("outputs a message and usage", func() { 54 session := helpers.CF("bind-security-group", secGroupName, someOrgName, "--lifecycle", "invalid") 55 Eventually(session.Err).Should(Say("Incorrect Usage: Invalid value `invalid' for option `--lifecycle'. Allowed values are: running or staging")) 56 Eventually(session.Out).Should(Say("USAGE:")) 57 Eventually(session).Should(Exit(1)) 58 }) 59 }) 60 61 Context("when the lifecycle flag has no argument", func() { 62 It("outputs a message and usage", func() { 63 session := helpers.CF("bind-security-group", secGroupName, someOrgName, "--lifecycle") 64 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `--lifecycle'")) 65 Eventually(session.Out).Should(Say("USAGE:")) 66 Eventually(session).Should(Exit(1)) 67 }) 68 }) 69 70 Context("when the environment is not setup correctly", func() { 71 Context("when no API endpoint is set", func() { 72 BeforeEach(func() { 73 helpers.UnsetAPI() 74 }) 75 76 It("fails with no API endpoint set message", func() { 77 session := helpers.CF("bind-security-group", secGroupName, someOrgName) 78 Eventually(session.Out).Should(Say("FAILED")) 79 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 80 Eventually(session).Should(Exit(1)) 81 }) 82 }) 83 84 Context("when not logged in", func() { 85 BeforeEach(func() { 86 helpers.LogoutCF() 87 }) 88 89 It("fails with not logged in message", func() { 90 session := helpers.CF("bind-security-group", secGroupName, someOrgName) 91 Eventually(session.Out).Should(Say("FAILED")) 92 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 93 Eventually(session).Should(Exit(1)) 94 }) 95 }) 96 }) 97 98 Context("when the server's API version is too low", func() { 99 var server *Server 100 101 BeforeEach(func() { 102 server = NewTLSServer() 103 server.AppendHandlers( 104 CombineHandlers( 105 VerifyRequest(http.MethodGet, "/v2/info"), 106 RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`), 107 ), 108 CombineHandlers( 109 VerifyRequest(http.MethodGet, "/v2/info"), 110 RespondWith(http.StatusOK, fmt.Sprintf(`{"api_version":"2.34.0", "authorization_endpoint": "%s"}`, server.URL())), 111 ), 112 CombineHandlers( 113 VerifyRequest(http.MethodGet, "/login"), 114 RespondWith(http.StatusOK, `{}`), 115 ), 116 ) 117 Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0)) 118 }) 119 120 AfterEach(func() { 121 server.Close() 122 }) 123 124 It("reports an error with a minimum-version message", func() { 125 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging") 126 127 Eventually(session.Err).Should(Say("Lifecycle value 'staging' requires CF API version 2\\.68\\.0\\ or higher. Your target is 2\\.34\\.0\\.")) 128 Eventually(session).Should(Exit(1)) 129 }) 130 }) 131 132 Context("when the input is invalid", func() { 133 Context("when the security group is not provided", func() { 134 It("fails with an incorrect usage message and displays help", func() { 135 session := helpers.CF("bind-security-group") 136 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP` and `ORG` were not provided")) 137 Eventually(session.Out).Should(Say("USAGE:")) 138 Eventually(session).Should(Exit(1)) 139 }) 140 }) 141 142 Context("when the org is not provided", func() { 143 It("fails with an incorrect usage message and displays help", func() { 144 session := helpers.CF("bind-security-group", secGroupName) 145 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ORG` was not provided")) 146 Eventually(session.Out).Should(Say("USAGE:")) 147 Eventually(session).Should(Exit(1)) 148 }) 149 }) 150 }) 151 152 Context("when the security group doesn't exist", func() { 153 It("fails with a security group not found message", func() { 154 session := helpers.CF("bind-security-group", "some-security-group-that-doesn't-exist", someOrgName) 155 Eventually(session.Err).Should(Say("Security group 'some-security-group-that-doesn't-exist' not found.")) 156 Eventually(session.Out).Should(Say("FAILED")) 157 Eventually(session).Should(Exit(1)) 158 }) 159 }) 160 161 Context("when the security group exists", func() { 162 var someSecurityGroup helpers.SecurityGroup 163 164 BeforeEach(func() { 165 someSecurityGroup = helpers.NewSecurityGroup(secGroupName, "tcp", "0.0.0.0/0", "53", "") 166 someSecurityGroup.Create() 167 }) 168 169 Context("when the org doesn't exist", func() { 170 It("fails with an org not found message", func() { 171 session := helpers.CF("bind-security-group", secGroupName, someOrgName) 172 Eventually(session.Err).Should(Say("Organization '%s' not found.", someOrgName)) 173 Eventually(session.Out).Should(Say("FAILED")) 174 Eventually(session).Should(Exit(1)) 175 }) 176 }) 177 178 Context("when the org exists", func() { 179 BeforeEach(func() { 180 helpers.CreateOrg(orgName) 181 helpers.TargetOrg(orgName) 182 }) 183 184 AfterEach(func() { 185 helpers.QuickDeleteOrg(orgName) 186 }) 187 188 Context("when the space doesn't exist", func() { 189 It("fails with a space not found message", func() { 190 session := helpers.CF("bind-security-group", secGroupName, orgName, "space-doesnt-exist") 191 Eventually(session.Err).Should(Say("Space 'space-doesnt-exist' not found.")) 192 Eventually(session.Out).Should(Say("FAILED")) 193 Eventually(session).Should(Exit(1)) 194 }) 195 }) 196 197 Context("when there are no spaces in this org", func() { 198 It("does not bind the security group to any space", func() { 199 session := helpers.CF("bind-security-group", secGroupName, orgName) 200 Consistently(session.Out).ShouldNot(Say("Assigning security group")) 201 Consistently(session.Out).ShouldNot(Say("OK")) 202 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 203 Eventually(session).Should(Exit(0)) 204 }) 205 }) 206 207 Context("when there are spaces in this org", func() { 208 BeforeEach(func() { 209 helpers.CreateSpace(spaceName1) 210 helpers.CreateSpace(spaceName2) 211 }) 212 213 Context("when the lifecycle flag is not set", func() { 214 Context("when binding to all spaces in an org", func() { 215 It("binds the security group to each space", func() { 216 session := helpers.CF("bind-security-group", secGroupName, orgName) 217 userName, _ := helpers.GetCredentials() 218 Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 219 Eventually(session.Out).Should(Say("OK")) 220 Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 221 Eventually(session.Out).Should(Say("OK")) 222 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 223 Eventually(session).Should(Exit(0)) 224 }) 225 }) 226 }) 227 228 Context("when binding to a particular space", func() { 229 It("binds the security group to the space", func() { 230 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1) 231 userName, _ := helpers.GetCredentials() 232 Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 233 Eventually(session.Out).Should(Say("OK")) 234 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 235 Eventually(session).Should(Exit(0)) 236 }) 237 }) 238 239 Context("when the lifecycle flag is running", func() { 240 Context("when binding to a particular space", func() { 241 It("binds the security group to the space", func() { 242 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "running") 243 userName, _ := helpers.GetCredentials() 244 Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 245 Eventually(session.Out).Should(Say("OK")) 246 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 247 Eventually(session).Should(Exit(0)) 248 }) 249 }) 250 }) 251 252 Context("when the lifecycle flag is staging", func() { 253 Context("when binding to all spaces in an org", func() { 254 It("binds the security group to each space", func() { 255 session := helpers.CF("bind-security-group", secGroupName, orgName, "--lifecycle", "staging") 256 userName, _ := helpers.GetCredentials() 257 Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 258 Eventually(session.Out).Should(Say("OK")) 259 Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 260 Eventually(session.Out).Should(Say("OK")) 261 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 262 Eventually(session).Should(Exit(0)) 263 }) 264 }) 265 266 Context("when binding to a particular space", func() { 267 It("binds the security group to the space", func() { 268 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging") 269 userName, _ := helpers.GetCredentials() 270 Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 271 Eventually(session.Out).Should(Say("OK")) 272 Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 273 Eventually(session).Should(Exit(0)) 274 }) 275 }) 276 }) 277 }) 278 }) 279 }) 280 })