github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/integration/shared/experimental/create_space_command_test.go (about) 1 package experimental 2 3 import ( 4 "code.cloudfoundry.org/cli/integration/helpers" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 . "github.com/onsi/gomega/gbytes" 8 . "github.com/onsi/gomega/gexec" 9 ) 10 11 func expectHelpText(session *Session) { 12 Eventually(session).Should(Say(`NAME:`)) 13 Eventually(session).Should(Say(`create-space - Create a space\n`)) 14 Eventually(session).Should(Say(`\n`)) 15 16 Eventually(session).Should(Say(`USAGE:`)) 17 Eventually(session).Should(Say(`cf create-space SPACE \[-o ORG\] \[-q SPACE_QUOTA\]\n`)) 18 Eventually(session).Should(Say(`\n`)) 19 20 Eventually(session).Should(Say(`OPTIONS:`)) 21 Eventually(session).Should(Say(`-o\s+Organization`)) 22 Eventually(session).Should(Say(`-q\s+Quota to assign to the newly created space`)) 23 Eventually(session).Should(Say(`\n`)) 24 25 Eventually(session).Should(Say(`SEE ALSO:`)) 26 Eventually(session).Should(Say(`set-space-isolation-segment, space-quotas, spaces, target`)) 27 } 28 29 func expectSuccessTextAndExitCode(session *Session, user, orgName, spaceName string) { 30 Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user)) 31 Eventually(session).Should(Say(`OK\n`)) 32 Eventually(session).Should(Say(`Assigning role SpaceManager to user %s in org %s / space %s as %s\.\.\.`, user, orgName, spaceName, user)) 33 Eventually(session).Should(Say(`OK\n`)) 34 Eventually(session).Should(Say(`Assigning role SpaceDeveloper to user %s in org %s / space %s as %s\.\.\.`, user, orgName, spaceName, user)) 35 Eventually(session).Should(Say(`OK\n\n`)) 36 Eventually(session).Should(Say(`TIP: Use 'cf target -o "%s" -s "%s"' to target new space`, orgName, spaceName)) 37 Eventually(session).Should(Exit(0)) 38 } 39 40 var _ = Describe("create-space", func() { 41 var spaceName string 42 43 When("invoked with --help", func() { 44 It("displays the help information", func() { 45 session := helpers.CF("create-space", "--help") 46 expectHelpText(session) 47 Eventually(session).Should(Exit(0)) 48 }) 49 }) 50 51 When("invoked with no arguments", func() { 52 It("shows an error and the help text", func() { 53 session := helpers.CF("create-space") 54 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SPACE` was not provided")) 55 expectHelpText(session) 56 Eventually(session).Should(Exit(1)) 57 }) 58 }) 59 60 When("the environment is not set up correctly", func() { 61 It("fails with the appropriate errors", func() { 62 helpers.CheckEnvironmentTargetedCorrectly(false, false, "", "create-space", "some-space") 63 }) 64 }) 65 66 BeforeEach(func() { 67 spaceName = helpers.NewSpaceName() 68 }) 69 70 PWhen("logged in as a client", func() { 71 var client, orgName string 72 73 BeforeEach(func() { 74 client = helpers.LoginCFWithClientCredentials() 75 orgName = helpers.CreateAndTargetOrg() 76 }) 77 78 It("successfully creates a space", func() { 79 session := helpers.CF("create-space", spaceName) 80 expectSuccessTextAndExitCode(session, client, orgName, spaceName) 81 }) 82 }) 83 84 When("logged in as a user", func() { 85 var user, orgName string 86 87 BeforeEach(func() { 88 user = helpers.LoginCF() 89 orgName = helpers.CreateAndTargetOrg() 90 }) 91 92 When("the space already exists", func() { 93 BeforeEach(func() { 94 session := helpers.CF("create-space", spaceName) 95 Eventually(session).Should(Exit(0)) 96 }) 97 98 It("warns the user that the space already exists", func() { 99 session := helpers.CF("create-space", spaceName) 100 Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user)) 101 Eventually(session.Err).Should(Say(`Space %s already exists`, spaceName)) 102 Eventually(session).Should(Say(`OK\n`)) 103 Eventually(session).Should(Exit(0)) 104 }) 105 }) 106 107 When("the space does not exist yet", func() { 108 When("a quota is not specified", func() { 109 It("creates the space in the targeted org", func() { 110 session := helpers.CF("create-space", spaceName) 111 expectSuccessTextAndExitCode(session, user, orgName, spaceName) 112 113 session = helpers.CF("space", spaceName) 114 Eventually(session).Should(Say(`name:\s+%s`, spaceName)) 115 Eventually(session).Should(Say(`org:\s+%s`, orgName)) 116 Eventually(session).Should(Exit(0)) 117 }) 118 119 It("makes the user a space manager and space developer", func() { 120 session := helpers.CF("create-space", spaceName) 121 expectSuccessTextAndExitCode(session, user, orgName, spaceName) 122 123 session = helpers.CF("space-users", orgName, spaceName) 124 Eventually(session).Should(Say(`SPACE MANAGER\n\s+%s`, user)) 125 Eventually(session).Should(Say(`SPACE DEVELOPER\n\s+%s`, user)) 126 Eventually(session).Should(Exit(0)) 127 }) 128 }) 129 130 When("quota is specified", func() { 131 var ( 132 quotaName string 133 session *Session 134 ) 135 136 When("the quota exists", func() { 137 BeforeEach(func() { 138 quotaName = helpers.QuotaName() 139 quotaSession := helpers.CF("create-space-quota", quotaName) 140 Eventually(quotaSession).Should(Exit(0)) 141 }) 142 143 It("creates the space with the provided quota", func() { 144 session = helpers.CF("create-space", spaceName, "-q", quotaName) 145 expectSuccessTextAndExitCode(session, user, orgName, spaceName) 146 session = helpers.CF("space", spaceName) 147 Eventually(session).Should(Say(`name:\s+%s`, spaceName)) 148 Eventually(session).Should(Say(`space quota:\s+%s`, quotaName)) 149 Eventually(session).Should(Exit(0)) 150 }) 151 }) 152 153 When("the quota does not exist", func() { 154 BeforeEach(func() { 155 quotaName = "no-such-quota" 156 session = helpers.CF("create-space", spaceName, "-q", quotaName) 157 }) 158 159 It("fails with an error", func() { 160 Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user)) 161 Eventually(session.Err).Should(Say(`Quota no-such-quota not found`)) 162 Eventually(session).Should(Say(`FAILED\n`)) 163 Eventually(session).Should(Exit(1)) 164 }) 165 166 It("does not create the space", func() { 167 Eventually(helpers.CF("space", spaceName)).Should(Exit(1)) 168 }) 169 }) 170 }) 171 172 When("org is specified", func() { 173 var session *Session 174 175 When("the org exists", func() { 176 BeforeEach(func() { 177 orgName = helpers.NewOrgName() 178 orgSession := helpers.CF("create-org", orgName) 179 Eventually(orgSession).Should(Exit(0)) 180 }) 181 182 It("creates the space in the specified org", func() { 183 session = helpers.CF("create-space", spaceName, "-o", orgName) 184 expectSuccessTextAndExitCode(session, user, orgName, spaceName) 185 186 helpers.TargetOrg(orgName) 187 session = helpers.CF("space", spaceName) 188 Eventually(session).Should(Say(`name:\s+%s`, spaceName)) 189 Eventually(session).Should(Say(`org:\s+%s`, orgName)) 190 Eventually(session).Should(Exit(0)) 191 }) 192 }) 193 194 When("the org does not exist", func() { 195 BeforeEach(func() { 196 orgName = "no-such-org" 197 }) 198 199 It("fails with an error and does not create the space", func() { 200 session = helpers.CF("create-space", spaceName, "-o", orgName) 201 Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user)) 202 Eventually(session.Err).Should(Say(`Organization '%s' not found`, orgName)) 203 Eventually(session).Should(Say(`FAILED\n`)) 204 Eventually(session).Should(Exit(1)) 205 206 Eventually(helpers.CF("space", spaceName)).Should(Exit(1)) 207 }) 208 }) 209 }) 210 211 When("the user is not authorized to create a space", func() { 212 var user string 213 214 BeforeEach(func() { 215 user = helpers.SwitchToOrgRole(orgName, "OrgAuditor") 216 }) 217 218 AfterEach(func() { 219 helpers.ClearTarget() 220 Expect(user).To(MatchRegexp(`^INTEGRATION-USER-[\da-f-]+$`)) 221 helpers.DeleteUser(user) 222 }) 223 224 It("fails with an error telling the user that they are not authorized", func() { 225 session := helpers.CF("create-space", spaceName, "-o", orgName) 226 Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user)) 227 Eventually(session.Err).Should(Say(`You are not authorized to perform the requested action`)) 228 Eventually(session).Should(Say(`FAILED\n`)) 229 Eventually(session).Should(Exit(1)) 230 }) 231 }) 232 }) 233 }) 234 })