github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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("Getting routes as"),
    61  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
    62  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)),
    63  					Say("OK"),
    64  				))
    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("Getting routes as"),
    94  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
    95  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", domainName)),
    96  					Not(Say(fmt.Sprintf("Deleting route bound-1.%s/path-3...", domainName))),
    97  					Say("OK"),
    98  				))
    99  			})
   100  		})
   101  
   102  		When("there are more than one page of routes", func() {
   103  			BeforeEach(func() {
   104  				var orphanedRoute helpers.Route
   105  				for i := 0; i < 51; i++ {
   106  					orphanedRoute = helpers.NewRoute(spaceName, domainName, fmt.Sprintf("orphan-multi-page-%d", i), "")
   107  					orphanedRoute.Create()
   108  				}
   109  			})
   110  			It("deletes all the orphaned routes", func() {
   111  				session := helpers.CF("delete-orphaned-routes", "-f")
   112  
   113  				for i := 0; i < 51; i++ {
   114  					Eventually(session).Should(Say(fmt.Sprintf("Deleting route orphan-multi-page-%d.%s...", i, domainName)))
   115  				}
   116  				Eventually(session).Should(Exit(0))
   117  			})
   118  		})
   119  
   120  		When("the force flag is not given", func() {
   121  			var buffer *Buffer
   122  			BeforeEach(func() {
   123  				orphanedRoute := helpers.NewRoute(spaceName, domainName, "orphan", "path")
   124  				orphanedRoute.Create()
   125  			})
   126  
   127  			When("the user inputs y", func() {
   128  				BeforeEach(func() {
   129  					buffer = NewBuffer()
   130  					buffer.Write([]byte("y\n"))
   131  				})
   132  
   133  				It("deletes the orphaned routes", func() {
   134  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   135  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   136  					Eventually(session).Should(SatisfyAll(
   137  						Exit(0),
   138  						Say("Getting routes as"),
   139  						Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName)),
   140  						Say("OK"),
   141  					))
   142  				})
   143  			})
   144  
   145  			When("the user inputs n", func() {
   146  				BeforeEach(func() {
   147  					buffer = NewBuffer()
   148  					buffer.Write([]byte("n\n"))
   149  				})
   150  
   151  				It("exits without deleting the orphaned routes", func() {
   152  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   153  					Eventually(session).Should(Say("Really delete orphaned routes?"))
   154  					Eventually(session).Should(SatisfyAll(
   155  						Exit(0),
   156  						Not(Say("Getting routes as")),
   157  						Not(Say(fmt.Sprintf("Deleting route orphan.%s/path...", domainName))),
   158  						Not(Say("OK")),
   159  					))
   160  				})
   161  			})
   162  		})
   163  
   164  		When("there are no orphaned routes", func() {
   165  			var (
   166  				boundRoute helpers.Route
   167  			)
   168  
   169  			BeforeEach(func() {
   170  				helpers.WithHelloWorldApp(func(appDir string) {
   171  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   172  				})
   173  
   174  				boundRoute = helpers.NewRoute(spaceName, domainName, "bound-route", "bound-path")
   175  				boundRoute.Create()
   176  				helpers.MapRouteToApplication(appName, boundRoute.Domain, boundRoute.Host, boundRoute.Path)
   177  			})
   178  
   179  			It("displays OK without deleting any routes", func() {
   180  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   181  					Exit(0),
   182  					Say("Getting routes as"),
   183  					Not(Say(fmt.Sprintf("Deleting route bound-route.%s/bound-path...", domainName))),
   184  					Say("OK"),
   185  				))
   186  			})
   187  		})
   188  
   189  		When("the orphaned routes are attached to both shared and private domains", func() {
   190  			var (
   191  				orphanedRoute1   helpers.Route
   192  				orphanedRoute2   helpers.Route
   193  				sharedDomainName string
   194  			)
   195  
   196  			BeforeEach(func() {
   197  				sharedDomainName = helpers.NewDomainName()
   198  				sharedDomain := helpers.NewDomain(orgName, sharedDomainName)
   199  				sharedDomain.Create()
   200  				sharedDomain.Share()
   201  
   202  				orphanedRoute1 = helpers.NewRoute(spaceName, domainName, "orphan-1", "path-1")
   203  				orphanedRoute2 = helpers.NewRoute(spaceName, sharedDomainName, "orphan-2", "path-2")
   204  				orphanedRoute1.Create()
   205  				orphanedRoute2.Create()
   206  			})
   207  
   208  			It("deletes both the routes", func() {
   209  				Eventually(helpers.CF("delete-orphaned-routes", "-f")).Should(SatisfyAll(
   210  					Exit(0),
   211  					Say("Getting routes as"),
   212  					Say(fmt.Sprintf("Deleting route orphan-1.%s/path-1...", domainName)),
   213  					Say(fmt.Sprintf("Deleting route orphan-2.%s/path-2...", sharedDomainName)),
   214  					Say("OK"),
   215  				))
   216  			})
   217  		})
   218  	})
   219  })