github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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).Should(Say("NAME:")) 39 Eventually(session).Should(Say("\\s+bind-security-group - Bind a security group to a particular space, or all existing spaces of an org")) 40 Eventually(session).Should(Say("USAGE:")) 41 Eventually(session).Should(Say("\\s+cf bind-security-group SECURITY_GROUP ORG \\[SPACE\\] \\[--lifecycle \\(running \\| staging\\)\\]")) 42 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 43 Eventually(session).Should(Say("OPTIONS:")) 44 Eventually(session).Should(Say("\\s+--lifecycle Lifecycle phase the group applies to \\(Default: running\\)")) 45 Eventually(session).Should(Say("SEE ALSO:")) 46 Eventually(session).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).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).Should(Say("USAGE:")) 66 Eventually(session).Should(Exit(1)) 67 }) 68 }) 69 70 Context("when the environment is not setup correctly", func() { 71 It("fails with the appropriate errors", func() { 72 helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "bind-security-group", "security-group-name", "org-name", "space-name") 73 }) 74 }) 75 76 Context("when the server's API version is too low", func() { 77 var server *Server 78 79 BeforeEach(func() { 80 server = NewTLSServer() 81 server.AppendHandlers( 82 CombineHandlers( 83 VerifyRequest(http.MethodGet, "/v2/info"), 84 RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`), 85 ), 86 CombineHandlers( 87 VerifyRequest(http.MethodGet, "/v2/info"), 88 RespondWith(http.StatusOK, fmt.Sprintf(`{"api_version":"2.34.0", "authorization_endpoint": "%s"}`, server.URL())), 89 ), 90 CombineHandlers( 91 VerifyRequest(http.MethodGet, "/login"), 92 RespondWith(http.StatusOK, `{}`), 93 ), 94 ) 95 Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0)) 96 }) 97 98 AfterEach(func() { 99 server.Close() 100 }) 101 102 It("reports an error with a minimum-version message", func() { 103 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging") 104 105 Eventually(session.Err).Should(Say("Lifecycle value 'staging' requires CF API version 2\\.68\\.0\\ or higher. Your target is 2\\.34\\.0\\.")) 106 Eventually(session).Should(Exit(1)) 107 }) 108 }) 109 110 Context("when the input is invalid", func() { 111 Context("when the security group is not provided", func() { 112 It("fails with an incorrect usage message and displays help", func() { 113 session := helpers.CF("bind-security-group") 114 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP` and `ORG` were not provided")) 115 Eventually(session).Should(Say("USAGE:")) 116 Eventually(session).Should(Exit(1)) 117 }) 118 }) 119 120 Context("when the org is not provided", func() { 121 It("fails with an incorrect usage message and displays help", func() { 122 session := helpers.CF("bind-security-group", secGroupName) 123 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ORG` was not provided")) 124 Eventually(session).Should(Say("USAGE:")) 125 Eventually(session).Should(Exit(1)) 126 }) 127 }) 128 }) 129 130 Context("when the security group doesn't exist", func() { 131 It("fails with a security group not found message", func() { 132 session := helpers.CF("bind-security-group", "some-security-group-that-doesn't-exist", someOrgName) 133 Eventually(session.Err).Should(Say("Security group 'some-security-group-that-doesn't-exist' not found.")) 134 Eventually(session).Should(Say("FAILED")) 135 Eventually(session).Should(Exit(1)) 136 }) 137 }) 138 139 Context("when the security group exists", func() { 140 var someSecurityGroup helpers.SecurityGroup 141 142 BeforeEach(func() { 143 someSecurityGroup = helpers.NewSecurityGroup(secGroupName, "tcp", "0.0.0.0/0", "53", "") 144 someSecurityGroup.Create() 145 }) 146 147 Context("when the org doesn't exist", func() { 148 It("fails with an org not found message", func() { 149 session := helpers.CF("bind-security-group", secGroupName, someOrgName) 150 Eventually(session.Err).Should(Say("Organization '%s' not found.", someOrgName)) 151 Eventually(session).Should(Say("FAILED")) 152 Eventually(session).Should(Exit(1)) 153 }) 154 }) 155 156 Context("when the org exists", func() { 157 BeforeEach(func() { 158 helpers.CreateOrg(orgName) 159 helpers.TargetOrg(orgName) 160 }) 161 162 AfterEach(func() { 163 helpers.QuickDeleteOrg(orgName) 164 }) 165 166 Context("when the space doesn't exist", func() { 167 It("fails with a space not found message", func() { 168 session := helpers.CF("bind-security-group", secGroupName, orgName, "space-doesnt-exist") 169 Eventually(session.Err).Should(Say("Space 'space-doesnt-exist' not found.")) 170 Eventually(session).Should(Say("FAILED")) 171 Eventually(session).Should(Exit(1)) 172 }) 173 }) 174 175 Context("when there are no spaces in this org", func() { 176 It("does not bind the security group to any space", func() { 177 session := helpers.CF("bind-security-group", secGroupName, orgName) 178 Consistently(session).ShouldNot(Say("Assigning security group")) 179 Consistently(session).ShouldNot(Say("OK")) 180 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 181 Eventually(session).Should(Exit(0)) 182 }) 183 }) 184 185 Context("when there are spaces in this org", func() { 186 BeforeEach(func() { 187 helpers.CreateSpace(spaceName1) 188 helpers.CreateSpace(spaceName2) 189 }) 190 191 Context("when the lifecycle flag is not set", func() { 192 Context("when binding to all spaces in an org", func() { 193 It("binds the security group to each space", func() { 194 session := helpers.CF("bind-security-group", secGroupName, orgName) 195 userName, _ := helpers.GetCredentials() 196 Eventually(session).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 197 Eventually(session).Should(Say("OK")) 198 Eventually(session).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 199 Eventually(session).Should(Say("OK")) 200 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 201 Eventually(session).Should(Exit(0)) 202 }) 203 }) 204 }) 205 206 Context("when binding to a particular space", func() { 207 It("binds the security group to the space", func() { 208 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1) 209 userName, _ := helpers.GetCredentials() 210 Eventually(session).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 211 Eventually(session).Should(Say("OK")) 212 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 213 Eventually(session).Should(Exit(0)) 214 }) 215 }) 216 217 Context("when the lifecycle flag is running", func() { 218 Context("when binding to a particular space", func() { 219 It("binds the security group to the space", func() { 220 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "running") 221 userName, _ := helpers.GetCredentials() 222 Eventually(session).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 223 Eventually(session).Should(Say("OK")) 224 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 225 Eventually(session).Should(Exit(0)) 226 }) 227 }) 228 }) 229 230 Context("when the lifecycle flag is staging", func() { 231 Context("when binding to all spaces in an org", func() { 232 It("binds the security group to each space", func() { 233 session := helpers.CF("bind-security-group", secGroupName, orgName, "--lifecycle", "staging") 234 userName, _ := helpers.GetCredentials() 235 Eventually(session).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 236 Eventually(session).Should(Say("OK")) 237 Eventually(session).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName)) 238 Eventually(session).Should(Say("OK")) 239 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 240 Eventually(session).Should(Exit(0)) 241 }) 242 }) 243 244 Context("when binding to a particular space", func() { 245 It("binds the security group to the space", func() { 246 session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging") 247 userName, _ := helpers.GetCredentials() 248 Eventually(session).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName)) 249 Eventually(session).Should(Say("OK")) 250 Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\.")) 251 Eventually(session).Should(Exit(0)) 252 }) 253 }) 254 }) 255 }) 256 }) 257 }) 258 })