github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/integration/isolated/delete_orphaned_routes_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("delete-orphaned-routes command", func() {
    14  	Context("when the environment is not setup correctly", func() {
    15  		Context("when no API endpoint is set", func() {
    16  			BeforeEach(func() {
    17  				helpers.UnsetAPI()
    18  			})
    19  
    20  			It("fails with no API endpoint set message", func() {
    21  				session := helpers.CF("delete-orphaned-routes", "-f")
    22  				Eventually(session).Should(Exit(1))
    23  				Expect(session.Out).To(Say("FAILED"))
    24  				Expect(session.Err).To(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    25  			})
    26  		})
    27  
    28  		Context("when not logged in", func() {
    29  			BeforeEach(func() {
    30  				helpers.LogoutCF()
    31  			})
    32  
    33  			It("fails with not logged in message", func() {
    34  				session := helpers.CF("delete-orphaned-routes", "-f")
    35  				Eventually(session).Should(Exit(1))
    36  				Expect(session.Out).To(Say("FAILED"))
    37  				Expect(session.Err).To(Say("Not logged in. Use 'cf login' to log in."))
    38  			})
    39  		})
    40  
    41  		Context("when there no org set", func() {
    42  			BeforeEach(func() {
    43  				helpers.LogoutCF()
    44  				helpers.LoginCF()
    45  			})
    46  
    47  			It("fails with no targeted org error message", func() {
    48  				session := helpers.CF("delete-orphaned-routes", "-f")
    49  				Eventually(session).Should(Exit(1))
    50  				Expect(session.Out).To(Say("FAILED"))
    51  				Expect(session.Err).To(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    52  			})
    53  		})
    54  
    55  		Context("when there no space set", func() {
    56  			BeforeEach(func() {
    57  				helpers.LogoutCF()
    58  				helpers.LoginCF()
    59  				helpers.TargetOrg(ReadOnlyOrg)
    60  			})
    61  
    62  			It("fails with no space targeted error message", func() {
    63  				session := helpers.CF("delete-orphaned-routes", "-f")
    64  				Eventually(session).Should(Exit(1))
    65  				Expect(session.Out).To(Say("FAILED"))
    66  				Expect(session.Err).To(Say("No space targeted, use 'cf target -s SPACE' to target a space"))
    67  			})
    68  		})
    69  	})
    70  
    71  	Context("when the environment is setup correctly", func() {
    72  		var (
    73  			orgName    string
    74  			spaceName  string
    75  			domainName string
    76  			appName    string
    77  			domain     helpers.Domain
    78  		)
    79  
    80  		BeforeEach(func() {
    81  			orgName = helpers.NewOrgName()
    82  			spaceName = helpers.PrefixedRandomName("SPACE")
    83  			domainName = helpers.DomainName()
    84  			appName = helpers.PrefixedRandomName("APP")
    85  
    86  			setupCF(orgName, spaceName)
    87  			domain = helpers.NewDomain(orgName, domainName)
    88  			domain.Create()
    89  		})
    90  
    91  		Context("when there are orphaned routes", func() {
    92  			var (
    93  				orphanedRoute1 helpers.Route
    94  				orphanedRoute2 helpers.Route
    95  			)
    96  
    97  			BeforeEach(func() {
    98  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
    99  				orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2")
   100  				orphanedRoute1.Create()
   101  				orphanedRoute2.Create()
   102  			})
   103  
   104  			It("deletes all the orphaned routes", func() {
   105  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   106  					Exit(0),
   107  					Say("Getting routes as"),
   108  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
   109  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)),
   110  					Say("OK"),
   111  				))
   112  			})
   113  		})
   114  
   115  		Context("when there are orphaned routes and bound routes", func() {
   116  			var (
   117  				orphanedRoute1 helpers.Route
   118  				orphanedRoute2 helpers.Route
   119  				boundRoute     helpers.Route
   120  			)
   121  
   122  			BeforeEach(func() {
   123  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
   124  				orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2")
   125  				orphanedRoute1.Create()
   126  				orphanedRoute2.Create()
   127  
   128  				helpers.WithHelloWorldApp(func(appDir string) {
   129  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   130  				})
   131  				Eventually(helpers.CF("apps")).Should(And(Exit(0), Say(fmt.Sprintf("%s\\s+stopped\\s+0/1\\s+%s\\s+%s", appName, helpers.DefaultMemoryLimit, helpers.DefaultDiskLimit))))
   132  
   133  				boundRoute = helpers.NewRoute(spaceName, domainName, "bound-1", "path-3")
   134  				boundRoute.Create()
   135  				helpers.BindRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path)
   136  			})
   137  
   138  			It("deletes only the orphaned routes", func() {
   139  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   140  					Exit(0),
   141  					Say("Getting routes as"),
   142  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
   143  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)),
   144  					Not(Say(fmt.Sprintf("Deleting route bound-1.%s/path-3...", domainName))),
   145  					Say("OK"),
   146  				))
   147  			})
   148  		})
   149  
   150  		Context("when there are more than one page of routes", func() {
   151  			BeforeEach(func() {
   152  				var orphanedRoute helpers.Route
   153  				for i := 0; i < 51; i++ {
   154  					orphanedRoute = helpers.NewRoute(spaceName, domainName, fmt.Sprintf("orphan-multi-page-%d", i), "")
   155  					orphanedRoute.Create()
   156  				}
   157  			})
   158  			It("deletes all the orphaned routes", func() {
   159  				session := helpers.CF("delete-orphaned-routes", "-f")
   160  				Eventually(session).Should(Exit(0))
   161  
   162  				for i := 0; i < 51; i++ {
   163  					Expect(session.Out).To(Say(fmt.Sprintf("Deleting route orphan-multi-page-%d.%s...", i, domainName)))
   164  				}
   165  			})
   166  		})
   167  
   168  		Context("when the force flag is not given", func() {
   169  			var buffer *Buffer
   170  			BeforeEach(func() {
   171  				orphanedRoute := helpers.NewRoute(spaceName, domainName, "orphan", "path")
   172  				orphanedRoute.Create()
   173  			})
   174  
   175  			Context("when the user inputs y", func() {
   176  				BeforeEach(func() {
   177  					buffer = NewBuffer()
   178  					buffer.Write([]byte("y\n"))
   179  				})
   180  
   181  				It("deletes the orphaned routes", func() {
   182  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   183  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   184  					Eventually(session).Should(SatisfyAll(
   185  						Exit(0),
   186  						Say("Getting routes as"),
   187  						Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName)),
   188  						Say("OK"),
   189  					))
   190  				})
   191  			})
   192  
   193  			Context("when the user inputs n", func() {
   194  				BeforeEach(func() {
   195  					buffer = NewBuffer()
   196  					buffer.Write([]byte("n\n"))
   197  				})
   198  
   199  				It("exits without deleting the orphaned routes", func() {
   200  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   201  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   202  					Eventually(session).Should(SatisfyAll(
   203  						Exit(0),
   204  						Not(Say("Getting routes as")),
   205  						Not(Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName))),
   206  						Not(Say("OK")),
   207  					))
   208  				})
   209  			})
   210  		})
   211  
   212  		Context("when there are no orphaned routes", func() {
   213  			var (
   214  				boundRoute helpers.Route
   215  			)
   216  
   217  			BeforeEach(func() {
   218  				helpers.WithHelloWorldApp(func(appDir string) {
   219  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   220  				})
   221  				Eventually(helpers.CF("apps")).Should(And(Exit(0), Say(fmt.Sprintf("%s\\s+stopped\\s+0/1\\s+%s\\s+%s", appName, helpers.DefaultMemoryLimit, helpers.DefaultDiskLimit))))
   222  
   223  				boundRoute = helpers.NewRoute(spaceName, domainName, "bound-route", "bound-path")
   224  				boundRoute.Create()
   225  				helpers.BindRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path)
   226  			})
   227  
   228  			It("displays OK without deleting any routes", func() {
   229  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   230  					Exit(0),
   231  					Say("Getting routes as"),
   232  					Not(Say(fmt.Sprintf("Deleting route bound-route.%s/bound-path...", domainName))),
   233  					Say("OK"),
   234  				))
   235  			})
   236  		})
   237  
   238  		Context("when the orphaned routes are attached to both shared and private domains", func() {
   239  			var (
   240  				orphanedRoute1   helpers.Route
   241  				orphanedRoute2   helpers.Route
   242  				sharedDomainName string
   243  			)
   244  
   245  			BeforeEach(func() {
   246  				sharedDomainName = helpers.DomainName()
   247  				sharedDomain := helpers.NewDomain(orgName, sharedDomainName)
   248  				sharedDomain.Create()
   249  				sharedDomain.Share()
   250  
   251  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
   252  				orphanedRoute2 = helpers.NewRoute(spaceName, sharedDomainName, "orphan-2", "path-2")
   253  				orphanedRoute1.Create()
   254  				orphanedRoute2.Create()
   255  			})
   256  
   257  			It("deletes both the routes", func() {
   258  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   259  					Exit(0),
   260  					Say("Getting routes as"),
   261  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
   262  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", sharedDomainName)),
   263  					Say("OK"),
   264  				))
   265  			})
   266  		})
   267  	})
   268  })