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