github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/push/http_routes_in_manifest_test.go (about)

     1  package push
     2  
     3  import (
     4  	"path/filepath"
     5  	"regexp"
     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  )
    13  
    14  var _ = Describe("HTTP routes in manifest", func() {
    15  	var (
    16  		app           string
    17  		domain        helpers.Domain
    18  		subdomain     helpers.Domain
    19  		route1        helpers.Route
    20  		route2        helpers.Route
    21  		routeWithPath helpers.Route
    22  		routeStar     helpers.Route
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		app = helpers.NewAppName()
    27  		domain = helpers.NewDomain(organization, helpers.NewDomainName())
    28  		subdomain = helpers.NewDomain(organization, "sub."+domain.Name)
    29  		route1 = helpers.NewRoute(space, domain.Name, helpers.PrefixedRandomName("r1"), "")
    30  		route2 = helpers.NewRoute(space, subdomain.Name, helpers.PrefixedRandomName("r2"), "")
    31  		routeWithPath = helpers.NewRoute(space, domain.Name, helpers.PrefixedRandomName("r3"), helpers.PrefixedRandomName("p1"))
    32  		routeStar = helpers.NewRoute(space, subdomain.Name, "*", "")
    33  	})
    34  
    35  	When("the domain exist", func() {
    36  		BeforeEach(func() {
    37  			domain.Create()
    38  			subdomain.Create()
    39  		})
    40  
    41  		When("the routes don't have a path", func() {
    42  			When("the routes are new", func() {
    43  				It("creates and maps the routes", func() {
    44  					helpers.WithHelloWorldApp(func(dir string) {
    45  						helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
    46  							"applications": []map[string]interface{}{
    47  								{
    48  									"name": app,
    49  									"routes": []map[string]string{
    50  										{"route": route1.String()},
    51  										{"route": route2.String()},
    52  										{"route": routeStar.String()},
    53  									},
    54  								},
    55  							},
    56  						})
    57  
    58  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
    59  						Eventually(session).Should(Say(`Getting app info\.\.\.`))
    60  
    61  						Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
    62  						Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
    63  						Eventually(session).Should(Say(`\s+routes:`))
    64  						Eventually(session).Should(Say(`(?i)\+\s+%s`, regexp.QuoteMeta(routeStar.String())))
    65  						Eventually(session).Should(Say(`(?i)\+\s+%s`, route1))
    66  						Eventually(session).Should(Say(`(?i)\+\s+%s`, route2))
    67  						Eventually(session).Should(Exit(0))
    68  					})
    69  
    70  					session := helpers.CF("app", app)
    71  					Eventually(session).Should(Say(`name:\s+%s`, app))
    72  					Eventually(session).Should(Say(`routes:\s+(%s, %s)|(%s, %s)`, route1, route2, route2, route1))
    73  					Eventually(session).Should(Exit(0))
    74  				})
    75  
    76  				When("--random-route is also specified", func() {
    77  					It("creates and maps the routes", func() {
    78  						helpers.WithHelloWorldApp(func(dir string) {
    79  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
    80  								"applications": []map[string]interface{}{
    81  									{
    82  										"name":         app,
    83  										"random-route": true,
    84  										"routes": []map[string]string{
    85  											{"route": route1.String()},
    86  											{"route": route2.String()},
    87  										},
    88  									},
    89  								},
    90  							})
    91  
    92  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
    93  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
    94  
    95  							Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
    96  							Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
    97  							Eventually(session).Should(Say(`\s+routes:`))
    98  							Eventually(session).Should(Say(`(?i)\+\s+%s`, route1))
    99  							Eventually(session).Should(Say(`(?i)\+\s+%s`, route2))
   100  							Eventually(session).Should(Exit(0))
   101  						})
   102  
   103  						session := helpers.CF("app", app)
   104  						Eventually(session).Should(Say(`name:\s+%s`, app))
   105  						Eventually(session).Should(Say(`routes:\s+(%s, %s)|(%s, %s)`, route1, route2, route2, route1))
   106  						Eventually(session).Should(Exit(0))
   107  					})
   108  				})
   109  			})
   110  
   111  			When("one of the routes exists", func() {
   112  				When("the route is in the current space", func() {
   113  					BeforeEach(func() {
   114  						route2.Create()
   115  					})
   116  
   117  					It("creates and maps the new route; maps the old route", func() {
   118  						helpers.WithHelloWorldApp(func(dir string) {
   119  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   120  								"applications": []map[string]interface{}{
   121  									{
   122  										"name": app,
   123  										"routes": []map[string]string{
   124  											{"route": route1.String()},
   125  											{"route": route2.String()},
   126  										},
   127  									},
   128  								},
   129  							})
   130  
   131  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   132  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
   133  
   134  							Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   135  							Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
   136  							Eventually(session).Should(Say(`\s+routes:`))
   137  							Eventually(session).Should(Say(`(?i)\+\s+%s`, route1))
   138  							Eventually(session).Should(Say(`(?i)\+\s+%s`, route2))
   139  							Eventually(session).Should(Exit(0))
   140  						})
   141  
   142  						session := helpers.CF("app", app)
   143  						Eventually(session).Should(Say(`name:\s+%s`, app))
   144  						Eventually(session).Should(Say(`routes:\s+(%s, %s)|(%s, %s)`, route1, route2, route2, route1))
   145  						Eventually(session).Should(Exit(0))
   146  					})
   147  				})
   148  
   149  				When("the route is in a different space", func() {
   150  					BeforeEach(func() {
   151  						otherSpace := helpers.NewSpaceName()
   152  						helpers.CreateSpace(otherSpace)
   153  						route2.Space = otherSpace
   154  						route2.Create()
   155  					})
   156  
   157  					It("returns an error", func() {
   158  						helpers.WithHelloWorldApp(func(dir string) {
   159  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   160  								"applications": []map[string]interface{}{
   161  									{
   162  										"name": app,
   163  										"routes": []map[string]string{
   164  											{"route": route1.String()},
   165  											{"route": route2.String()},
   166  										},
   167  									},
   168  								},
   169  							})
   170  
   171  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   172  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
   173  							Eventually(session.Err).Should(Say("The app cannot be mapped to route %s because the route exists in a different space.", route2))
   174  							Eventually(session).Should(Exit(1))
   175  						})
   176  					})
   177  				})
   178  			})
   179  
   180  			When("the route contains a port", func() {
   181  				BeforeEach(func() {
   182  					route1.Port = 1234
   183  				})
   184  
   185  				It("returns an error", func() {
   186  					helpers.WithHelloWorldApp(func(dir string) {
   187  						helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   188  							"applications": []map[string]interface{}{
   189  								{
   190  									"name": app,
   191  									"routes": []map[string]string{
   192  										{"route": route1.String()},
   193  										{"route": route2.String()},
   194  									},
   195  								},
   196  							},
   197  						})
   198  
   199  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   200  						Eventually(session).Should(Say(`Getting app info\.\.\.`))
   201  						Eventually(session.Err).Should(Say("Port not allowed in HTTP domain %s", domain.Name))
   202  						Eventually(session).Should(Exit(1))
   203  					})
   204  				})
   205  			})
   206  		})
   207  
   208  		When("the route has a path", func() {
   209  
   210  			When("the route is new", func() {
   211  				It("creates and maps the route", func() {
   212  					helpers.WithHelloWorldApp(func(dir string) {
   213  						helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   214  							"applications": []map[string]interface{}{
   215  								{
   216  									"name": app,
   217  									"routes": []map[string]string{
   218  										{"route": routeWithPath.String()},
   219  									},
   220  								},
   221  							},
   222  						})
   223  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   224  						Eventually(session).Should(Say(`Getting app info\.\.\.`))
   225  
   226  						Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   227  						Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
   228  						Eventually(session).Should(Say(`\s+routes:`))
   229  						Eventually(session).Should(Say(`(?i)\+\s+%s`, routeWithPath))
   230  						Eventually(session).Should(Exit(0))
   231  					})
   232  
   233  					session := helpers.CF("app", app)
   234  					Eventually(session).Should(Say(`name:\s+%s`, app))
   235  					Eventually(session).Should(Say(`routes:\s+%s`, routeWithPath))
   236  					Eventually(session).Should(Exit(0))
   237  				})
   238  
   239  				// This test refers to this dev consideration [https://www.pivotaltracker.com/story/show/126256733/comments/181122853]
   240  				When("a route with the same hostname and domain exists", func() {
   241  					var routeWithoutPath helpers.Route
   242  
   243  					BeforeEach(func() {
   244  						routeWithoutPath = routeWithPath
   245  						routeWithoutPath.Path = ""
   246  
   247  						routeWithoutPath.Create()
   248  					})
   249  
   250  					It("creates and maps the route", func() {
   251  						helpers.WithHelloWorldApp(func(dir string) {
   252  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   253  								"applications": []map[string]interface{}{
   254  									{
   255  										"name": app,
   256  										"routes": []map[string]string{
   257  											{"route": routeWithPath.String()},
   258  										},
   259  									},
   260  								},
   261  							})
   262  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   263  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
   264  
   265  							Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   266  							Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
   267  							Eventually(session).Should(Say(`\s+routes:`))
   268  							Eventually(session).Should(Say(`(?i)\+\s+%s`, routeWithPath))
   269  							Eventually(session).Should(Exit(0))
   270  						})
   271  
   272  						session := helpers.CF("app", app)
   273  						Eventually(session).Should(Say(`name:\s+%s`, app))
   274  						Eventually(session).Should(Say(`routes:\s+%s`, routeWithPath))
   275  						Eventually(session).Should(Exit(0))
   276  					})
   277  				})
   278  			})
   279  
   280  			When("the route exists", func() {
   281  				When("the route exists in the same space", func() {
   282  					BeforeEach(func() {
   283  						routeWithPath.Create()
   284  					})
   285  
   286  					It("creates and maps the new route; maps the old route", func() {
   287  						helpers.WithHelloWorldApp(func(dir string) {
   288  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   289  								"applications": []map[string]interface{}{
   290  									{
   291  										"name": app,
   292  										"routes": []map[string]string{
   293  											{"route": routeWithPath.String()},
   294  										},
   295  									},
   296  								},
   297  							})
   298  
   299  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   300  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
   301  
   302  							Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   303  							Eventually(session).Should(Say(`\+\s+name:\s+%s`, app))
   304  							Eventually(session).Should(Say(`\s+routes:`))
   305  							Eventually(session).Should(Say(`(?i)\+\s+%s`, routeWithPath))
   306  							Eventually(session).Should(Exit(0))
   307  						})
   308  
   309  						session := helpers.CF("app", app)
   310  						Eventually(session).Should(Say(`name:\s+%s`, app))
   311  						Eventually(session).Should(Say(`routes:\s+%s`, routeWithPath))
   312  						Eventually(session).Should(Exit(0))
   313  					})
   314  				})
   315  
   316  				When("the route exists in another space", func() {
   317  					BeforeEach(func() {
   318  						otherSpace := helpers.NewSpaceName()
   319  						helpers.CreateSpace(otherSpace)
   320  						routeWithPath.Space = otherSpace
   321  						routeWithPath.Create()
   322  					})
   323  
   324  					It("returns an error", func() {
   325  						helpers.WithHelloWorldApp(func(dir string) {
   326  							helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   327  								"applications": []map[string]interface{}{
   328  									{
   329  										"name": app,
   330  										"routes": []map[string]string{
   331  											{"route": routeWithPath.String()},
   332  										},
   333  									},
   334  								},
   335  							})
   336  
   337  							session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   338  							Eventually(session).Should(Say(`Getting app info\.\.\.`))
   339  							Eventually(session.Err).Should(Say("The app cannot be mapped to route %s because the route exists in a different space.", routeWithPath))
   340  							Eventually(session).Should(Exit(1))
   341  						})
   342  					})
   343  				})
   344  			})
   345  		})
   346  	})
   347  
   348  	When("the domain does not exist", func() {
   349  		It("returns an error", func() {
   350  			helpers.WithHelloWorldApp(func(dir string) {
   351  				helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
   352  					"applications": []map[string]interface{}{
   353  						{
   354  							"name": app,
   355  							"routes": []map[string]string{
   356  								{"route": route1.String()},
   357  								{"route": route2.String()},
   358  							},
   359  						},
   360  					},
   361  				})
   362  
   363  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
   364  				Eventually(session).Should(Say(`Getting app info\.\.\.`))
   365  				Eventually(session.Err).Should(Say("The route %s did not match any existing domains.", route1))
   366  				Eventually(session).Should(Exit(1))
   367  			})
   368  		})
   369  	})
   370  })