github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/space_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 var _ = Describe("space command", func() { 16 var ( 17 orgName string 18 spaceName string 19 ) 20 21 BeforeEach(func() { 22 orgName = helpers.NewOrgName() 23 spaceName = helpers.NewSpaceName() 24 }) 25 26 Describe("help", func() { 27 Context("when --help flag is set", func() { 28 It("Displays command usage to output", func() { 29 session := helpers.CF("space", "--help") 30 Eventually(session).Should(Say("NAME:")) 31 Eventually(session).Should(Say("space - Show space info")) 32 Eventually(session).Should(Say("USAGE:")) 33 Eventually(session).Should(Say("cf space SPACE \\[--guid\\] \\[--security-group-rules\\]")) 34 Eventually(session).Should(Say("OPTIONS:")) 35 Eventually(session).Should(Say("--guid\\s+Retrieve and display the given space's guid\\. All other output for the space is suppressed\\.")) 36 Eventually(session).Should(Say("--security-group-rules\\s+Retrieve the rules for all the security groups associated with the space\\.")) 37 Eventually(session).Should(Say("SEE ALSO:")) 38 Eventually(session).Should(Say("set-space-isolation-segment, space-quota, space-users")) 39 Eventually(session).Should(Exit(0)) 40 }) 41 }) 42 }) 43 44 Context("when the environment is not setup correctly", func() { 45 Context("when no API endpoint is set", func() { 46 BeforeEach(func() { 47 helpers.UnsetAPI() 48 }) 49 50 It("fails with no API endpoint set message", func() { 51 session := helpers.CF("space", "some-space") 52 Eventually(session).Should(Say("FAILED")) 53 Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 54 Eventually(session).Should(Exit(1)) 55 }) 56 }) 57 58 Context("when not logged in", func() { 59 BeforeEach(func() { 60 helpers.LogoutCF() 61 }) 62 63 It("fails with not logged in message", func() { 64 session := helpers.CF("space", "some-space") 65 Eventually(session).Should(Say("FAILED")) 66 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in.")) 67 Eventually(session).Should(Exit(1)) 68 }) 69 }) 70 71 Context("when no organization is targeted", func() { 72 BeforeEach(func() { 73 helpers.LoginCF() 74 }) 75 76 AfterEach(func() { 77 helpers.LogoutCF() 78 }) 79 80 It("fails with no organization targeted message and exits 1", func() { 81 session := helpers.CF("space", spaceName) 82 _, _ = helpers.GetCredentials() 83 Eventually(session).Should(Say("FAILED")) 84 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org.")) 85 Eventually(session).Should(Exit(1)) 86 }) 87 }) 88 }) 89 90 Context("when the environment is set up correctly", func() { 91 BeforeEach(func() { 92 helpers.LoginCF() 93 setupCF(orgName, spaceName) 94 }) 95 96 AfterEach(func() { 97 helpers.QuickDeleteOrg(orgName) 98 helpers.ClearTarget() 99 }) 100 101 Context("when the space does not exist", func() { 102 It("displays not found and exits 1", func() { 103 badSpaceName := fmt.Sprintf("%s-1", spaceName) 104 session := helpers.CF("space", badSpaceName) 105 userName, _ := helpers.GetCredentials() 106 Eventually(session).Should(Say("Getting info for space %s in org %s as %s\\.\\.\\.", badSpaceName, orgName, userName)) 107 Eventually(session).Should(Say("FAILED")) 108 Eventually(session.Err).Should(Say("Space '%s' not found.", badSpaceName)) 109 Eventually(session).Should(Exit(1)) 110 }) 111 }) 112 113 Context("when the space exists", func() { 114 Context("when the --guid flag is used", func() { 115 It("displays the space guid", func() { 116 session := helpers.CF("space", "--guid", spaceName) 117 Eventually(session).Should(Say("[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}")) 118 Eventually(session).Should(Exit(0)) 119 }) 120 }) 121 122 Context("when the --guid flag is not used", func() { 123 var ( 124 securityGroup0 helpers.SecurityGroup 125 securityGroup1 helpers.SecurityGroup 126 securityGroupName2 string 127 securityGroupRules2 *os.File 128 err error 129 ) 130 131 BeforeEach(func() { 132 securityGroup0 = helpers.NewSecurityGroup(helpers.NewSecurityGroupName("0"), "tcp", "4.3.2.1/24", "80,443", "foo security group") 133 securityGroup0.Create() 134 Eventually(helpers.CF("bind-security-group", securityGroup0.Name, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0)) 135 136 securityGroup1 = helpers.NewSecurityGroup(helpers.NewSecurityGroupName("1"), "tcp", "1.2.3.4/24", "80,443", "some security group") 137 securityGroup1.Create() 138 Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0)) 139 140 securityGroupName2 = helpers.NewSecurityGroupName("2") 141 securityGroupRules2, err = ioutil.TempFile("", "security-group-rules") 142 Expect(err).ToNot(HaveOccurred()) 143 144 securityGroupRules2.Write([]byte(` 145 [ 146 { 147 "protocol": "udp", 148 "destination": "92.0.0.1/24", 149 "ports": "80,443", 150 "description": "some other other security group" 151 }, 152 { 153 "protocol": "tcp", 154 "destination": "5.7.9.11/24", 155 "ports": "80,443", 156 "description": "some other security group" 157 } 158 ] 159 `)) 160 161 Eventually(helpers.CF("create-security-group", securityGroupName2, securityGroupRules2.Name())).Should(Exit(0)) 162 os.Remove(securityGroupRules2.Name()) 163 164 Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName)).Should(Exit(0)) 165 Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0)) 166 }) 167 168 Context("when no flags are used", func() { 169 var ( 170 appName string 171 spaceQuotaName string 172 serviceInstance string 173 isolationSegmentName string 174 ) 175 176 BeforeEach(func() { 177 appName = helpers.PrefixedRandomName("app") 178 helpers.WithHelloWorldApp(func(appDir string) { 179 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0)) 180 }) 181 serviceInstance = helpers.PrefixedRandomName("si") 182 Eventually(helpers.CF("create-user-provided-service", serviceInstance, "-p", "{}")).Should(Exit(0)) 183 Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0)) 184 spaceQuotaName = helpers.PrefixedRandomName("space-quota") 185 Eventually(helpers.CF("create-space-quota", spaceQuotaName)).Should(Exit(0)) 186 Eventually(helpers.CF("set-space-quota", spaceName, spaceQuotaName)).Should(Exit(0)) 187 isolationSegmentName = helpers.NewIsolationSegmentName() 188 Eventually(helpers.CF("create-isolation-segment", isolationSegmentName)).Should(Exit(0)) 189 Eventually(helpers.CF("enable-org-isolation", orgName, isolationSegmentName)).Should(Exit(0)) 190 Eventually(helpers.CF("set-space-isolation-segment", spaceName, isolationSegmentName)).Should(Exit(0)) 191 192 Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0)) 193 }) 194 195 It("displays a table with space name, org, apps, services, isolation segment, space quota and security groups", func() { 196 session := helpers.CF("space", spaceName) 197 userName, _ := helpers.GetCredentials() 198 Eventually(session).Should(Say("Getting info for space %s in org %s as %s\\.\\.\\.", spaceName, orgName, userName)) 199 200 Eventually(session).Should(Say("name:\\s+%s", spaceName)) 201 Eventually(session).Should(Say("org:\\s+%s", orgName)) 202 Eventually(session).Should(Say("apps:\\s+%s", appName)) 203 Eventually(session).Should(Say("services:\\s+%s", serviceInstance)) 204 Eventually(session).Should(Say("isolation segment:\\s+%s", isolationSegmentName)) 205 Eventually(session).Should(Say("space quota:\\s+%s", spaceQuotaName)) 206 Eventually(session).Should(Say("running security groups:\\s+.*%s,.* %s", securityGroup1.Name, securityGroupName2)) 207 Eventually(session).Should(Say("staging security groups:\\s+.*%s,.* %s", securityGroup0.Name, securityGroupName2)) 208 Eventually(session).Should(Exit(0)) 209 }) 210 }) 211 212 Context("when the space does not have an isolation segment and its org has a default isolation segment", func() { 213 var orgIsolationSegmentName string 214 215 BeforeEach(func() { 216 orgIsolationSegmentName = helpers.NewIsolationSegmentName() 217 Eventually(helpers.CF("create-isolation-segment", orgIsolationSegmentName)).Should(Exit(0)) 218 Eventually(helpers.CF("enable-org-isolation", orgName, orgIsolationSegmentName)).Should(Exit(0)) 219 Eventually(helpers.CF("set-org-default-isolation-segment", orgName, orgIsolationSegmentName)).Should(Exit(0)) 220 }) 221 222 It("shows the org default isolation segment", func() { 223 session := helpers.CF("space", spaceName) 224 Eventually(session).Should(Say("isolation segment:\\s+%s", orgIsolationSegmentName)) 225 Eventually(session).Should(Exit(0)) 226 }) 227 }) 228 229 Context("when the security group rules flag is used", func() { 230 It("displays the space information as well as all security group rules", func() { 231 session := helpers.CF("space", "--security-group-rules", spaceName) 232 userName, _ := helpers.GetCredentials() 233 Eventually(session).Should(Say("Getting info for space %s in org %s as %s\\.\\.\\.", spaceName, orgName, userName)) 234 235 Eventually(session).Should(Say("name:")) 236 Eventually(session).Should(Say("org:")) 237 Eventually(session).Should(Say("apps:")) 238 Eventually(session).Should(Say("services:")) 239 Eventually(session).Should(Say("isolation segment:")) 240 Eventually(session).Should(Say("space quota:")) 241 Eventually(session).Should(Say("running security groups:")) 242 Eventually(session).Should(Say("staging security groups:")) 243 Eventually(session).Should(Say("\n\n")) 244 245 Eventually(session).Should(Say("security group\\s+destination\\s+ports\\s+protocol\\s+lifecycle\\s+description")) 246 Eventually(session).Should(Say("#\\d+\\s+%s\\s+4.3.2.1/24\\s+80,443\\s+tcp\\s+staging\\s+foo security group", securityGroup0.Name)) 247 Eventually(session).Should(Say("#\\d+\\s+%s\\s+1.2.3.4/24\\s+80,443\\s+tcp\\s+running\\s+some security group", securityGroup1.Name)) 248 Eventually(session).Should(Say("#\\d+\\s+%s\\s+5.7.9.11/24\\s+80,443\\s+tcp\\s+running\\s+some other security group", securityGroupName2)) 249 Eventually(session).Should(Say("\\s+%s\\s+5.7.9.11/24\\s+80,443\\s+tcp\\s+staging\\s+some other security group", securityGroupName2)) 250 Eventually(session).Should(Say("\\s+%s\\s+92.0.0.1/24\\s+80,443\\s+udp\\s+running\\s+some other other security group", securityGroupName2)) 251 Eventually(session).Should(Say("\\s+%s\\s+92.0.0.1/24\\s+80,443\\s+udp\\s+staging\\s+some other other security group", securityGroupName2)) 252 Eventually(session).Should(Exit(0)) 253 }) 254 }) 255 }) 256 }) 257 }) 258 })