github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/integration/experimental/create_route_command_test.go (about) 1 package experimental 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/liamawhite/cli-with-i18n/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("create-route command", func() { 16 Context("Help", func() { 17 It("displays the help information", func() { 18 session := helpers.CF("create-route", "--help") 19 Eventually(session.Out).Should(Say(`NAME:`)) 20 Eventually(session.Out).Should(Say(`create-route - Create a url route in a space for later use\n`)) 21 Eventually(session.Out).Should(Say(`\n`)) 22 23 Eventually(session.Out).Should(Say(`USAGE:`)) 24 Eventually(session.Out).Should(Say(`Create an HTTP route:`)) 25 Eventually(session.Out).Should(Say(`cf create-route SPACE DOMAIN \[--hostname HOSTNAME\] \[--path PATH\]\n`)) 26 Eventually(session.Out).Should(Say(`\n`)) 27 28 Eventually(session.Out).Should(Say(`Create a TCP route:`)) 29 Eventually(session.Out).Should(Say(`cf create-route SPACE DOMAIN \(--port PORT \| --random-port\)\n`)) 30 Eventually(session.Out).Should(Say(`\n`)) 31 32 Eventually(session.Out).Should(Say(`EXAMPLES:`)) 33 Eventually(session.Out).Should(Say(`cf create-route my-space example.com\s+# example.com`)) 34 Eventually(session.Out).Should(Say(`cf create-route my-space example.com --hostname myapp\s+# myapp.example.com`)) 35 Eventually(session.Out).Should(Say(`cf create-route my-space example.com --hostname myapp --path foo\s+# myapp.example.com/foo`)) 36 Eventually(session.Out).Should(Say(`cf create-route my-space example.com --port 5000\s+# example.com:5000\n`)) 37 Eventually(session.Out).Should(Say(`\n`)) 38 39 Eventually(session.Out).Should(Say(`OPTIONS:`)) 40 Eventually(session.Out).Should(Say(`--hostname, -n\s+Hostname for the HTTP route \(required for shared domains\)`)) 41 Eventually(session.Out).Should(Say(`--path\s+Path for the HTTP route`)) 42 Eventually(session.Out).Should(Say(`--port\s+Port for the TCP route`)) 43 Eventually(session.Out).Should(Say(`--random-port\s+Create a random port for the TCP route\n`)) 44 Eventually(session.Out).Should(Say(`\n`)) 45 46 Eventually(session.Out).Should(Say(`SEE ALSO:`)) 47 Eventually(session.Out).Should(Say(`check-route, domains, map-route`)) 48 49 Eventually(session).Should(Exit(0)) 50 }) 51 }) 52 53 Context("Flag Errors", func() { 54 Context("when --hostname and --port are provided", func() { 55 It("fails with a message about being unable to mix --port with the HTTP route options", func() { 56 session := helpers.CF("create-route", "some-space", "some-domain", "--hostname", "some-host", "--port", "1122") 57 Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --hostname, --port`)) 58 Eventually(session).Should(Exit(1)) 59 }) 60 }) 61 62 Context("when --hostname and --random-port are provided", func() { 63 It("fails with a message about being unable to mix --random-port with any other options", func() { 64 session := helpers.CF("create-route", "some-space", "some-domain", "--hostname", "some-host", "--random-port") 65 Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --hostname, --random-port`)) 66 Eventually(session).Should(Exit(1)) 67 }) 68 }) 69 70 Context("when --path and --port are provided", func() { 71 It("fails with a message about being unable to mix --port with the HTTP route options", func() { 72 session := helpers.CF("create-route", "some-space", "some-domain", "--path", "/some-path", "--port", "1111") 73 Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --path, --port`)) 74 Eventually(session).Should(Exit(1)) 75 }) 76 }) 77 78 Context("when --path and --random-port are provided", func() { 79 It("fails with a message about being unable to mix --random-port with any other options", func() { 80 session := helpers.CF("create-route", "some-space", "some-domain", "--path", "/some-path", "--random-port") 81 Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --path, --random-port`)) 82 Eventually(session).Should(Exit(1)) 83 }) 84 }) 85 86 Context("when both --port and --random-port are provided", func() { 87 It("fails with a message about being unable to mix --random-port with any other options", func() { 88 session := helpers.CF("create-route", "some-space", "some-domain", "--port", "1121", "--random-port") 89 Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --port, --random-port`)) 90 Eventually(session).Should(Exit(1)) 91 }) 92 }) 93 94 Context("when the provided port is not valid / parseable", func() { 95 It("fails with an appropriate error", func() { 96 session := helpers.CF("create-route", "some-space", "some-domain", "--port", "ABC") 97 Eventually(session.Err).Should(Say(`Incorrect Usage: invalid argument for flag '--port' \(expected int > 0\)`)) 98 Eventually(session).Should(Exit(1)) 99 }) 100 }) 101 }) 102 103 Context("when the environment is not setup correctly", func() { 104 Context("when no API endpoint is set", func() { 105 BeforeEach(func() { 106 helpers.UnsetAPI() 107 }) 108 109 It("fails with no API endpoint set message", func() { 110 session := helpers.CF("create-route", "some-space", "some-domain") 111 Eventually(session.Out).Should(Say(`FAILED`)) 112 Eventually(session.Err).Should(Say(`No API endpoint set\. Use 'cf login' or 'cf api' to target an endpoint\.`)) 113 Eventually(session).Should(Exit(1)) 114 }) 115 }) 116 117 Context("when not logged in", func() { 118 BeforeEach(func() { 119 helpers.LogoutCF() 120 }) 121 122 It("fails with not logged in message", func() { 123 session := helpers.CF("create-route", "some-space", "some-domain") 124 Eventually(session.Out).Should(Say(`FAILED`)) 125 Eventually(session.Err).Should(Say(`Not logged in\. Use 'cf login' to log in\.`)) 126 Eventually(session).Should(Exit(1)) 127 }) 128 }) 129 130 Context("when no organization is targeted", func() { 131 BeforeEach(func() { 132 helpers.ClearTarget() 133 }) 134 135 It("fails with 'no organization targeted' message and exits 1", func() { 136 session := helpers.CF("create-route", "some-space", "some-domain") 137 Eventually(session.Out).Should(Say(`FAILED`)) 138 Eventually(session.Err).Should(Say(`No org targeted, use 'cf target -o ORG' to target an org\.`)) 139 Eventually(session).Should(Exit(1)) 140 }) 141 }) 142 }) 143 144 Context("when the server's API version is too low", func() { 145 var server *Server 146 147 BeforeEach(func() { 148 server = NewTLSServer() 149 server.AppendHandlers( 150 CombineHandlers( 151 VerifyRequest(http.MethodGet, "/v2/info"), 152 RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`), 153 ), 154 CombineHandlers( 155 VerifyRequest(http.MethodGet, "/v2/info"), 156 RespondWith(http.StatusOK, fmt.Sprintf(`{"api_version":"2.34.0", "authorization_endpoint": "%s"}`, server.URL())), 157 ), 158 CombineHandlers( 159 VerifyRequest(http.MethodGet, "/login"), 160 RespondWith(http.StatusOK, `{}`), 161 ), 162 ) 163 Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0)) 164 }) 165 166 AfterEach(func() { 167 server.Close() 168 }) 169 170 Context("for HTTP routes", func() { 171 Context("when specifying --path", func() { 172 It("reports an error with a minimum-version message", func() { 173 session := helpers.CF("create-route", "some-space", "example.com", "--path", "/foo") 174 Eventually(session.Out).Should(Say(`FAILED`)) 175 Eventually(session.Err).Should(Say(`Option '--path' requires CF API version 2\.36\.0 or higher. Your target is 2\.34\.0\.`)) 176 Eventually(session).Should(Exit(1)) 177 }) 178 }) 179 }) 180 181 Context("for TCP routes", func() { 182 Context("when specifying --port", func() { 183 It("reports an error with a minimum-version message", func() { 184 session := helpers.CF("create-route", "some-space", "example.com", "--port", "1110") 185 Eventually(session.Out).Should(Say(`FAILED`)) 186 Eventually(session.Err).Should(Say(`Option '--port' requires CF API version 2\.53\.0 or higher\. Your target is 2\.34\.0\.`)) 187 Eventually(session).Should(Exit(1)) 188 }) 189 }) 190 191 Context("when specifying --random-port", func() { 192 It("reports an error with a minimum-version message", func() { 193 session := helpers.CF("create-route", "some-space", "example.com", "--random-port") 194 Eventually(session.Out).Should(Say(`FAILED`)) 195 Eventually(session.Err).Should(Say(`Option '--random-port' requires CF API version 2\.53\.0 or higher\. Your target is 2\.34\.0\.`)) 196 Eventually(session).Should(Exit(1)) 197 }) 198 }) 199 }) 200 }) 201 202 Context("when the environment is set up correctly", func() { 203 var ( 204 orgName string 205 spaceName string 206 ) 207 208 BeforeEach(func() { 209 orgName = helpers.NewOrgName() 210 spaceName = helpers.NewSpaceName() 211 212 setupCF(orgName, spaceName) 213 }) 214 215 AfterEach(func() { 216 helpers.QuickDeleteOrg(orgName) 217 }) 218 219 Context("when the space does not exist", func() { 220 It("displays 'space not found' and exits 1", func() { 221 badSpaceName := fmt.Sprintf("%s-1", spaceName) 222 session := helpers.CF("create-route", badSpaceName, "some-domain") 223 Eventually(session.Out).Should(Say(`FAILED`)) 224 Eventually(session.Err).Should(Say(`Space '%s' not found\.`, badSpaceName)) 225 Eventually(session).Should(Exit(1)) 226 }) 227 }) 228 229 Context("when the space is not specified", func() { 230 It("displays error and exits 1", func() { 231 session := helpers.CF("create-route") 232 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SPACE` and `DOMAIN` were not provided\n")) 233 Eventually(session.Err).Should(Say("\n")) 234 Eventually(session.Out).Should(Say("NAME:\n")) 235 Eventually(session).Should(Exit(1)) 236 }) 237 }) 238 239 Context("when the domain does not exist", func() { 240 It("displays error and exits 1", func() { 241 session := helpers.CF("create-route", spaceName, "some-domain") 242 Eventually(session.Out).Should(Say(`FAILED`)) 243 Eventually(session.Err).Should(Say(`Domain some-domain not found`)) 244 Eventually(session).Should(Exit(1)) 245 }) 246 }) 247 248 Context("when the domain is not specified", func() { 249 It("displays error and exits 1", func() { 250 session := helpers.CF("create-route", spaceName) 251 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `DOMAIN` was not provided\n")) 252 Eventually(session.Err).Should(Say("\n")) 253 Eventually(session.Out).Should(Say("NAME:\n")) 254 Eventually(session).Should(Exit(1)) 255 }) 256 }) 257 258 Context("when the space and domain exist", func() { 259 var ( 260 userName string 261 domainName string 262 ) 263 264 BeforeEach(func() { 265 domainName = helpers.DomainName() 266 userName, _ = helpers.GetCredentials() 267 }) 268 269 Context("when the route already exists", func() { 270 var domain helpers.Domain 271 272 BeforeEach(func() { 273 domain = helpers.NewDomain(orgName, domainName) 274 domain.Create() 275 Eventually(helpers.CF("create-route", spaceName, domainName)).Should(Exit(0)) 276 }) 277 278 AfterEach(func() { 279 domain.Delete() 280 }) 281 282 It("warns the user that it has already been created and runs to completion without failing", func() { 283 session := helpers.CF("create-route", spaceName, domainName) 284 Eventually(session.Out).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName)) 285 Eventually(session.Err).Should(Say(`Route %s already exists\.`, domainName)) 286 Eventually(session.Out).Should(Say(`OK`)) 287 Eventually(session).Should(Exit(0)) 288 }) 289 }) 290 291 Context("when the route already exists in a different space", func() { 292 var domain helpers.Domain 293 294 BeforeEach(func() { 295 domain = helpers.NewDomain(orgName, domainName) 296 domain.Create() 297 differentSpaceName := helpers.NewSpaceName() 298 helpers.CreateSpace(differentSpaceName) 299 Eventually(helpers.CF("create-route", differentSpaceName, domainName)).Should(Exit(0)) 300 }) 301 302 AfterEach(func() { 303 domain.Delete() 304 }) 305 306 It("warns the user that the route is already in use and then fails", func() { 307 session := helpers.CF("create-route", spaceName, domainName) 308 Eventually(session.Out).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName)) 309 Eventually(session.Err).Should(Say(`The route %s is already in use\.`, domainName)) 310 Eventually(session.Out).Should(Say(`FAILED`)) 311 Eventually(session).Should(Exit(1)) 312 }) 313 }) 314 315 Context("when the route does not already exist", func() { 316 Context("when the domain is private", func() { 317 var domain helpers.Domain 318 319 BeforeEach(func() { 320 domain = helpers.NewDomain(orgName, domainName) 321 domain.Create() 322 Eventually(helpers.CF("create-route", spaceName, domainName)).Should(Exit(0)) 323 }) 324 325 AfterEach(func() { 326 domain.Delete() 327 }) 328 329 Context("when no flags are used", func() { 330 It("creates the route", func() { 331 session := helpers.CF("create-route", spaceName, domainName) 332 Eventually(session.Out).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName)) 333 Eventually(session).Should(Exit(0)) 334 }) 335 }) 336 337 Context("when the path is provided but the hostname is not", func() { 338 var path string 339 340 BeforeEach(func() { 341 path = helpers.PrefixedRandomName("path") 342 }) 343 344 It("creates the route", func() { 345 session := helpers.CF("create-route", spaceName, domainName, "--path", path) 346 Eventually(session.Out).Should(Say(`Creating route %s/%s for org %s / space %s as %s\.\.\.`, domainName, path, orgName, spaceName, userName)) 347 Eventually(session).Should(Exit(0)) 348 }) 349 }) 350 }) 351 352 Context("when the domain is a shared HTTP domain", func() { 353 var domain helpers.Domain 354 355 BeforeEach(func() { 356 domain = helpers.NewDomain(orgName, domainName) 357 domain.CreateShared() 358 }) 359 360 AfterEach(func() { 361 domain.DeleteShared() 362 }) 363 364 Context("when no flags are used", func() { 365 It("fails with an error message and exits 1", func() { 366 session := helpers.CF("create-route", spaceName, domainName) 367 Eventually(session.Out).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName)) 368 Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`)) 369 Eventually(session).Should(Exit(1)) 370 }) 371 }) 372 373 Context("when TCP flag options are provided", func() { 374 It("fails with an error message and exits 1", func() { 375 port := "90230" 376 session := helpers.CF("create-route", spaceName, domainName, "--port", port) 377 Eventually(session.Out).Should(Say(`Creating route %s:%s for org %s / space %s as %s\.\.\.`, domainName, port, orgName, spaceName, userName)) 378 Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`)) 379 Eventually(session).Should(Exit(1)) 380 }) 381 }) 382 383 Context("when the hostname is provided", func() { 384 var hostName string 385 386 BeforeEach(func() { 387 hostName = helpers.PrefixedRandomName("my-host") 388 }) 389 390 Context("when no path is provided", func() { 391 It("creates the route", func() { 392 session := helpers.CF("create-route", spaceName, domainName, "--hostname", hostName) 393 Eventually(session.Out).Should(Say(`Creating route %s.%s for org %s / space %s as %s\.\.\.`, hostName, domainName, orgName, spaceName, userName)) 394 Eventually(session).Should(Exit(0)) 395 }) 396 }) 397 398 Context("when a path is provided", func() { 399 It("creates the route", func() { 400 path := fmt.Sprintf("/%s", helpers.PrefixedRandomName("path")) 401 session := helpers.CF("create-route", spaceName, domainName, "--hostname", hostName, "--path", path) 402 Eventually(session.Out).Should(Say(`Creating route %s.%s%s for org %s / space %s as %s\.\.\.`, hostName, domainName, path, orgName, spaceName, userName)) 403 Eventually(session).Should(Exit(0)) 404 }) 405 }) 406 }) 407 408 Context("when the hostname is not provided", func() { 409 var path string 410 411 BeforeEach(func() { 412 path = helpers.PrefixedRandomName("path") 413 }) 414 415 Context("when the path is provided", func() { 416 It("fails with an error message and exits 1", func() { 417 session := helpers.CF("create-route", spaceName, domainName, "-v", "--path", path) 418 Eventually(session.Out).Should(Say(`Creating route %s/%s for org %s / space %s as %s\.\.\.`, domainName, path, orgName, spaceName, userName)) 419 Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`)) 420 Eventually(session).Should(Exit(1)) 421 }) 422 }) 423 }) 424 }) 425 426 Context("when the domain is a shared TCP domain", func() { 427 var domain helpers.Domain 428 429 BeforeEach(func() { 430 domain = helpers.NewDomain(orgName, domainName) 431 domain.CreateWithRouterGroup("default-tcp") 432 }) 433 434 AfterEach(func() { 435 domain.DeleteShared() 436 }) 437 438 Context("when HTTP flag options are provided", func() { 439 It("fails with an error message and exits 1", func() { 440 hostName := helpers.PrefixedRandomName("host-") 441 path := helpers.PrefixedRandomName("path-") 442 session := helpers.CF("create-route", spaceName, domainName, "--hostname", hostName, "--path", path) 443 Eventually(session.Out).Should(Say(`Creating route %s.%s/%s for org %s / space %s as %s\.\.\.`, hostName, domainName, path, orgName, spaceName, userName)) 444 Eventually(session.Err).Should(Say(`For TCP routes you must specify a port or request a random one\.`)) 445 Eventually(session).Should(Exit(1)) 446 }) 447 }) 448 449 Context("when a port is provided", func() { 450 It("creates the route", func() { 451 port := "1110" 452 session := helpers.CF("create-route", spaceName, domainName, "--port", port) 453 Eventually(session.Out).Should(Say(`Creating route %s:%s for org %s / space %s as %s\.\.\.`, domainName, port, orgName, spaceName, userName)) 454 Eventually(session).Should(Exit(0)) 455 }) 456 }) 457 458 Context("when --random-port is provided", func() { 459 It("creates the route", func() { 460 session := helpers.CF("create-route", spaceName, domainName, "--random-port") 461 Eventually(session.Out).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName)) 462 Eventually(session.Out).Should(Say(`Route %s:\d+ has been created\.`, domainName)) 463 Eventually(session).Should(Exit(0)) 464 }) 465 }) 466 }) 467 }) 468 }) 469 }) 470 })