github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/delete_orphaned_routes_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     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  	Context("Help", func() {
    16  		It("appears in cf help -a", func() {
    17  			session := helpers.CF("help", "-a")
    18  			Eventually(session).Should(Exit(0))
    19  			Expect(session).To(HaveCommandInCategoryWithDescription("delete-orphaned-routes", "ROUTES", "Delete all orphaned routes in the currently targeted space (i.e. those that are not mapped to an app or service instance)"))
    20  		})
    21  
    22  		It("displays the help information", func() {
    23  			session := helpers.CF("delete-orphaned-routes", "--help")
    24  			Eventually(session).Should(Say(`NAME:`))
    25  			Eventually(session).Should(Say(`delete-orphaned-routes - Delete all orphaned routes in the currently targeted space \(i\.e\. those that are not mapped to an app or service instance\)`))
    26  			Eventually(session).Should(Say(`\n`))
    27  
    28  			Eventually(session).Should(Say(`USAGE:`))
    29  			Eventually(session).Should(Say(`cf delete-orphaned-routes \[-f\]`))
    30  			Eventually(session).Should(Say(`\n`))
    31  
    32  			Eventually(session).Should(Say(`OPTIONS:`))
    33  			Eventually(session).Should(Say(`-f\s+Force deletion without confirmation`))
    34  			Eventually(session).Should(Say(`\n`))
    35  
    36  			Eventually(session).Should(Say(`SEE ALSO:`))
    37  			Eventually(session).Should(Say(`delete-routes, routes`))
    38  
    39  			Eventually(session).Should(Exit(0))
    40  		})
    41  	})
    42  
    43  	When("the environment is not setup correctly", func() {
    44  		It("fails with the appropriate errors", func() {
    45  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "delete-orphaned-routes")
    46  		})
    47  	})
    48  
    49  	When("the environment is set up correctly", func() {
    50  		var (
    51  			buffer     *Buffer
    52  			orgName    string
    53  			spaceName  string
    54  			domainName string
    55  			appName    string
    56  			hostName   string
    57  			userName   string
    58  		)
    59  
    60  		BeforeEach(func() {
    61  			buffer = NewBuffer()
    62  			orgName = helpers.NewOrgName()
    63  			spaceName = helpers.NewSpaceName()
    64  			appName = helpers.NewAppName()
    65  			hostName = helpers.NewHostName()
    66  			helpers.SetupCF(orgName, spaceName)
    67  			userName, _ = helpers.GetCredentials()
    68  			domainName = helpers.DefaultSharedDomain()
    69  
    70  			helpers.WithHelloWorldApp(func(appDir string) {
    71  				Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName, "--no-start")).Should(Exit(0))
    72  			})
    73  
    74  			Eventually(helpers.CF("create-route", domainName, "--hostname", hostName)).Should(Exit(0))
    75  		})
    76  
    77  		AfterEach(func() {
    78  			helpers.QuickDeleteOrg(orgName)
    79  		})
    80  
    81  		When("the -f flag is not given", func() {
    82  			When("the user enters 'y'", func() {
    83  				BeforeEach(func() {
    84  					_, err := buffer.Write([]byte("y\n"))
    85  					Expect(err).ToNot(HaveOccurred())
    86  				})
    87  				It("it asks for confirmation and deletes the domain", func() {
    88  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
    89  					Eventually(session).Should(Say(`Really delete orphaned routes?`))
    90  					Eventually(session).Should(Say(regexp.QuoteMeta(`Deleting orphaned routes as %s...`), userName))
    91  					Eventually(session).Should(Say("OK"))
    92  					Eventually(session).Should(Exit(0))
    93  
    94  					Expect(string(session.Out.Contents())).NotTo(ContainSubstring("Unable to delete"))
    95  					session = helpers.CF("routes")
    96  					Consistently(session).ShouldNot(Say(`%s\s+%s`, hostName, domainName))
    97  					Eventually(session).Should(Say(`%s\s+%s`, appName, domainName))
    98  					Eventually(session).Should(Exit(0))
    99  				})
   100  			})
   101  
   102  			When("the user enters 'n'", func() {
   103  				BeforeEach(func() {
   104  					_, err := buffer.Write([]byte("n\n"))
   105  					Expect(err).ToNot(HaveOccurred())
   106  				})
   107  
   108  				It("it asks for confirmation and does not delete the domain", func() {
   109  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   110  					Eventually(session).Should(Say(`Really delete orphaned routes?`))
   111  					Eventually(session).Should(Say(`Routes have not been deleted`))
   112  					Consistently(session).ShouldNot(Say("OK"))
   113  					Eventually(session).Should(Exit(0))
   114  				})
   115  			})
   116  
   117  			When("the user's inpiut is invalud", func() {
   118  				BeforeEach(func() {
   119  					_, err := buffer.Write([]byte("abc\n"))
   120  					Expect(err).ToNot(HaveOccurred())
   121  					_, err = buffer.Write([]byte("n\n"))
   122  					Expect(err).ToNot(HaveOccurred())
   123  				})
   124  
   125  				It("it asks for confirmation and does not delete the domain", func() {
   126  					session := helpers.CFWithStdin(buffer, "delete-orphaned-routes")
   127  					Eventually(session).Should(Say(`Really delete orphaned routes?`))
   128  					Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   129  					Eventually(session).Should(Say(`Really delete orphaned routes?`))
   130  					Eventually(session).Should(Say(`Routes have not been deleted`))
   131  					Consistently(session).ShouldNot(Say("OK"))
   132  					Eventually(session).Should(Exit(0))
   133  				})
   134  
   135  			})
   136  		})
   137  
   138  		When("the -f flag is given", func() {
   139  			It("deletes the orphaned routes", func() {
   140  				session := helpers.CF("delete-orphaned-routes", "-f")
   141  				Eventually(session).Should(Say(`Deleting orphaned routes as %s\.\.\.`, userName))
   142  				Eventually(session).Should(Say(`OK`))
   143  				Eventually(session).Should(Exit(0))
   144  
   145  				Expect(string(session.Out.Contents())).NotTo(ContainSubstring("Unable to delete"))
   146  
   147  				session = helpers.CF("routes")
   148  				Consistently(session).ShouldNot(Say(`%s\s+%s`, hostName, domainName))
   149  				Eventually(session).Should(Say(`%s\s+%s`, appName, domainName))
   150  				Eventually(session).Should(Exit(0))
   151  			})
   152  		})
   153  	})
   154  })