github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/delete_orphaned_routes_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     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("delete-orphaned-routes command", func() {
    15  	Describe("help text", func() {
    16  		It("displays the help information", func() {
    17  			session := helpers.CF("delete-orphaned-routes", "--help")
    18  			Eventually(session).Should(Say("NAME:\n"))
    19  			Eventually(session).Should(Say(regexp.QuoteMeta("delete-orphaned-routes - Delete all orphaned routes in the currently targeted space (i.e. those that are not mapped to an app)\n")))
    20  			Eventually(session).Should(Say("USAGE:\n"))
    21  			Eventually(session).Should(Say(regexp.QuoteMeta("cf delete-orphaned-routes [-f]")))
    22  			Eventually(session).Should(Say("OPTIONS:\n"))
    23  			Eventually(session).Should(Say(`-f\s+Force deletion without confirmation`))
    24  			Eventually(session).Should(Say("SEE ALSO:\n"))
    25  			Eventually(session).Should(Say("delete-route, routes"))
    26  			Eventually(session).Should(Exit(0))
    27  		})
    28  	})
    29  	When("the environment is not setup correctly", func() {
    30  		It("fails with the appropriate errors", func() {
    31  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "delete-orphaned-routes")
    32  		})
    33  	})
    34  
    35  	When("the environment is setup correctly", func() {
    36  		var (
    37  			orgName    string
    38  			spaceName  string
    39  			domainName string
    40  			appName    string
    41  			domain     helpers.Domain
    42  		)
    43  
    44  		BeforeEach(func() {
    45  			orgName = helpers.NewOrgName()
    46  			spaceName = helpers.NewSpaceName()
    47  			domainName = helpers.NewDomainName()
    48  			appName = helpers.PrefixedRandomName("APP")
    49  
    50  			helpers.SetupCF(orgName, spaceName)
    51  			domain = helpers.NewDomain(orgName, domainName)
    52  			domain.Create()
    53  		})
    54  
    55  		AfterEach(func() {
    56  			helpers.QuickDeleteOrg(orgName)
    57  		})
    58  
    59  		When("there are orphaned routes", func() {
    60  			var (
    61  				orphanedRoute1 helpers.Route
    62  				orphanedRoute2 helpers.Route
    63  			)
    64  
    65  			BeforeEach(func() {
    66  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
    67  				orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2")
    68  				orphanedRoute1.Create()
    69  				orphanedRoute2.Create()
    70  			})
    71  
    72  			It("deletes all the orphaned routes", func() {
    73  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
    74  					Exit(0),
    75  					Say("Deleting routes as"),
    76  					Say("OK"),
    77  				))
    78  
    79  				Eventually(helpers.CF("routes")).Should(Say("No routes found"))
    80  			})
    81  		})
    82  
    83  		When("there are orphaned routes and bound routes", func() {
    84  			var (
    85  				orphanedRoute1 helpers.Route
    86  				orphanedRoute2 helpers.Route
    87  				boundRoute     helpers.Route
    88  			)
    89  
    90  			BeforeEach(func() {
    91  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
    92  				orphanedRoute2 = helpers.NewRoute(spaceName, domainName, "orphan-2", "path-2")
    93  				orphanedRoute1.Create()
    94  				orphanedRoute2.Create()
    95  
    96  				helpers.WithHelloWorldApp(func(appDir string) {
    97  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
    98  				})
    99  
   100  				boundRoute = helpers.NewRoute(spaceName, domainName, "bound-1", "path-3")
   101  				boundRoute.Create()
   102  				helpers.MapRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path)
   103  			})
   104  
   105  			It("deletes only the orphaned routes", func() {
   106  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   107  					Exit(0),
   108  					Say("Deleting routes as"),
   109  					Say("OK"),
   110  				))
   111  
   112  				Eventually(helpers.CF("routes")).Should(SatisfyAll(
   113  					Say("bound-1.*path-3"),
   114  					Not(Say("orphan-1.*path-1")),
   115  					Not(Say("orphan-2.*path-2")),
   116  				))
   117  			})
   118  		})
   119  
   120  		When("there are more than one page of routes", func() {
   121  			BeforeEach(func() {
   122  				var orphanedRoute helpers.Route
   123  				for i := 0; i < 51; i++ {
   124  					orphanedRoute = helpers.NewRoute(spaceName, domainName, fmt.Sprintf("orphan-multi-page-%d", i), "")
   125  					orphanedRoute.Create()
   126  				}
   127  			})
   128  			It("deletes all the orphaned routes", func() {
   129  				session := helpers.CF("delete-orphaned-routes", "-f")
   130  
   131  				Eventually(session).Should(SatisfyAll(
   132  					Exit(0),
   133  					Say("OK"),
   134  				))
   135  
   136  				Eventually(helpers.CF("routes")).Should(Say("No routes found"))
   137  			})
   138  		})
   139  
   140  		When("the force flag is not given", func() {
   141  			var buffer *Buffer
   142  			BeforeEach(func() {
   143  				orphanedRoute := helpers.NewRoute(spaceName, domainName, "orphan", "path")
   144  				orphanedRoute.Create()
   145  			})
   146  
   147  			When("the user inputs y", func() {
   148  				BeforeEach(func() {
   149  					buffer = NewBuffer()
   150  					_, err := buffer.Write([]byte("y\n"))
   151  					Expect(err).ToNot(HaveOccurred())
   152  				})
   153  
   154  				It("deletes the orphaned routes", func() {
   155  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   156  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   157  					Eventually(session).Should(SatisfyAll(
   158  						Exit(0),
   159  						Say("OK"),
   160  					))
   161  				})
   162  			})
   163  
   164  			When("the user inputs n", func() {
   165  				BeforeEach(func() {
   166  					buffer = NewBuffer()
   167  					_, err := buffer.Write([]byte("n\n"))
   168  					Expect(err).ToNot(HaveOccurred())
   169  				})
   170  
   171  				It("exits without deleting the orphaned routes", func() {
   172  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   173  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   174  					Eventually(session).Should(SatisfyAll(
   175  						Exit(0),
   176  						Not(Say("OK")),
   177  					))
   178  				})
   179  			})
   180  		})
   181  
   182  		When("there are no orphaned routes", func() {
   183  			var (
   184  				boundRoute helpers.Route
   185  			)
   186  
   187  			BeforeEach(func() {
   188  				helpers.WithHelloWorldApp(func(appDir string) {
   189  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   190  				})
   191  
   192  				boundRoute = helpers.NewRoute(spaceName, domainName, "bound-route", "bound-path")
   193  				boundRoute.Create()
   194  				helpers.MapRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path)
   195  			})
   196  
   197  			It("displays OK without deleting any routes", func() {
   198  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   199  					Exit(0),
   200  					Say("OK"),
   201  				))
   202  			})
   203  		})
   204  
   205  		When("the orphaned routes are attached to both shared and private domains", func() {
   206  			var (
   207  				orphanedRoute1   helpers.Route
   208  				orphanedRoute2   helpers.Route
   209  				sharedDomainName string
   210  			)
   211  
   212  			BeforeEach(func() {
   213  				sharedDomainName = helpers.NewDomainName()
   214  				sharedDomain := helpers.NewDomain(orgName, sharedDomainName)
   215  				sharedDomain.Create()
   216  
   217  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
   218  				orphanedRoute2 = helpers.NewRoute(spaceName, sharedDomainName, "orphan-2", "path-2")
   219  				orphanedRoute1.Create()
   220  				orphanedRoute2.Create()
   221  			})
   222  
   223  			It("deletes both the routes", func() {
   224  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   225  					Exit(0),
   226  					Say("OK"),
   227  				))
   228  			})
   229  		})
   230  	})
   231  })