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