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

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     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 command", func() {
    15  	var (
    16  		orgName   string
    17  		spaceName string
    18  		appName   string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  		appName = helpers.PrefixedRandomName("app")
    25  	})
    26  
    27  	When("--help flag is set", func() {
    28  		It("appears in cf help -a", func() {
    29  			session := helpers.CF("help", "-a")
    30  			Eventually(session).Should(Exit(0))
    31  			Expect(session).To(HaveCommandInCategoryWithDescription("delete", "APPS", "Delete an app"))
    32  		})
    33  
    34  		It("Displays command usage to output", func() {
    35  			session := helpers.CF("delete", "--help")
    36  			Eventually(session).Should(Say("NAME:"))
    37  			Eventually(session).Should(Say("delete - Delete an app"))
    38  			Eventually(session).Should(Say("USAGE:"))
    39  			Eventually(session).Should(Say(`cf delete APP_NAME \[-r\] \[-f\]`))
    40  			Eventually(session).Should(Say("OPTIONS:"))
    41  			Eventually(session).Should(Say(`\s+-f\s+Force deletion without confirmation`))
    42  			Eventually(session).Should(Say(`\s+-r\s+Also delete any mapped routes`))
    43  			Eventually(session).Should(Exit(0))
    44  		})
    45  	})
    46  
    47  	When("the app name is not provided", func() {
    48  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    49  			session := helpers.CF("delete")
    50  
    51  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    52  			Eventually(session).Should(Say("NAME:"))
    53  			Eventually(session).Should(Exit(1))
    54  		})
    55  	})
    56  
    57  	When("the environment is not setup correctly", func() {
    58  		It("fails with the appropriate errors", func() {
    59  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "delete", "-f", appName)
    60  		})
    61  	})
    62  
    63  	When("the environment is setup correctly", func() {
    64  		BeforeEach(func() {
    65  			helpers.SetupCF(orgName, spaceName)
    66  		})
    67  
    68  		AfterEach(func() {
    69  			helpers.QuickDeleteOrg(orgName)
    70  		})
    71  
    72  		When("the app does not exist", func() {
    73  			When("the -f flag is provided", func() {
    74  				It("it displays the app does not exist", func() {
    75  					username, _ := helpers.GetCredentials()
    76  					session := helpers.CF("delete", appName, "-f")
    77  					Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
    78  					Eventually(session.Err).Should(Say(`App '%s' does not exist\.`, appName))
    79  					Eventually(session).Should(Say("OK"))
    80  					Eventually(session).Should(Exit(0))
    81  				})
    82  			})
    83  
    84  			When("the -f flag not is provided", func() {
    85  				var buffer *Buffer
    86  
    87  				BeforeEach(func() {
    88  					buffer = NewBuffer()
    89  				})
    90  
    91  				When("the -r flag is provided", func() {
    92  					BeforeEach(func() {
    93  						_, err := buffer.Write([]byte("y\n"))
    94  						Expect(err).ToNot(HaveOccurred())
    95  					})
    96  
    97  					It("shows more information when confiriming", func() {
    98  						username, _ := helpers.GetCredentials()
    99  						session := helpers.CFWithStdin(buffer, "delete", "-r", appName)
   100  						Eventually(session).Should(Say(
   101  							`Deleting the app and associated routes will make apps with this route, in any org, unreachable\.`,
   102  						))
   103  						Eventually(session).Should(Say(`Really delete the app %s and associated routes\? \[yN\]`, appName))
   104  						Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   105  						Eventually(session.Err).Should(Say(`App '%s' does not exist\.`, appName))
   106  						Eventually(session).Should(Say("OK"))
   107  						Eventually(session).Should(Exit(0))
   108  					})
   109  				})
   110  
   111  				When("the user enters 'y'", func() {
   112  					BeforeEach(func() {
   113  						_, err := buffer.Write([]byte("y\n"))
   114  						Expect(err).ToNot(HaveOccurred())
   115  					})
   116  
   117  					It("it displays the app does not exist", func() {
   118  						username, _ := helpers.GetCredentials()
   119  						session := helpers.CFWithStdin(buffer, "delete", appName)
   120  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   121  						Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   122  						Eventually(session.Err).Should(Say(`App '%s' does not exist\.`, appName))
   123  						Eventually(session).Should(Say("OK"))
   124  						Eventually(session).Should(Exit(0))
   125  					})
   126  				})
   127  
   128  				When("the user enters 'n'", func() {
   129  					BeforeEach(func() {
   130  						_, err := buffer.Write([]byte("n\n"))
   131  						Expect(err).ToNot(HaveOccurred())
   132  					})
   133  
   134  					It("does not delete the app", func() {
   135  						session := helpers.CFWithStdin(buffer, "delete", appName)
   136  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   137  						Eventually(session).Should(Say(`App '%s' has not been deleted\.`, appName))
   138  						Eventually(session).Should(Exit(0))
   139  					})
   140  				})
   141  
   142  				When("the user enters the default input (hits return)", func() {
   143  					BeforeEach(func() {
   144  						_, err := buffer.Write([]byte("\n"))
   145  						Expect(err).ToNot(HaveOccurred())
   146  					})
   147  
   148  					It("does not delete the app", func() {
   149  						session := helpers.CFWithStdin(buffer, "delete", appName)
   150  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   151  						Eventually(session).Should(Say(`App '%s' has not been deleted\.`, appName))
   152  						Eventually(session).Should(Exit(0))
   153  					})
   154  				})
   155  
   156  				When("the user enters an invalid answer", func() {
   157  					BeforeEach(func() {
   158  						// The second '\n' is intentional. Otherwise the buffer will be
   159  						// closed while the interaction is still waiting for input; it gets
   160  						// an EOF and causes an error.
   161  						_, err := buffer.Write([]byte("wat\n\n"))
   162  						Expect(err).ToNot(HaveOccurred())
   163  					})
   164  
   165  					It("asks again", func() {
   166  						session := helpers.CFWithStdin(buffer, "delete", appName)
   167  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   168  						Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   169  						Eventually(session).Should(Say(`Really delete the app %s\? \[yN\]`, appName))
   170  						Eventually(session).Should(Exit(0))
   171  					})
   172  				})
   173  			})
   174  		})
   175  
   176  		When("the app exists", func() {
   177  			BeforeEach(func() {
   178  				helpers.WithHelloWorldApp(func(appDir string) {
   179  					Eventually(
   180  						helpers.CustomCF(
   181  							helpers.CFEnv{WorkingDirectory: appDir},
   182  							"push",
   183  							appName,
   184  							"--no-start",
   185  						),
   186  					).Should(Exit(0))
   187  				})
   188  			})
   189  
   190  			It("deletes the app", func() {
   191  				session := helpers.CF("delete", appName, "-f")
   192  				username, _ := helpers.GetCredentials()
   193  				Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   194  				Eventually(session).Should(Say("OK"))
   195  				Eventually(session).Should(Exit(0))
   196  
   197  				Eventually(helpers.CF("app", appName)).Should(Exit(1))
   198  			})
   199  
   200  			When("the -r flag is provided", func() {
   201  				It("deletes the app and associated routes", func() {
   202  					session := helpers.CF("delete", appName, "-f", "-r")
   203  					username, _ := helpers.GetCredentials()
   204  					Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   205  					Eventually(session).Should(Say("OK"))
   206  					Eventually(session).Should(Exit(0))
   207  
   208  					Eventually(helpers.CF("app", appName)).Should(Exit(1))
   209  
   210  					session = helpers.CF("routes")
   211  					Eventually(session).Should(Exit(0))
   212  					Expect(session).NotTo(Say(appName))
   213  				})
   214  
   215  				When("app to delete has a route bound to another app", func() {
   216  					var boundRouteURL string
   217  
   218  					BeforeEach(func() {
   219  						var (
   220  							appNameSharingBoundRoute = helpers.PrefixedRandomName("another-app")
   221  							domain                   = helpers.DefaultSharedDomain()
   222  							host                     = appName
   223  							path                     string
   224  						)
   225  						boundRouteURL = fmt.Sprintf("%s.%s", host, domain)
   226  
   227  						helpers.WithHelloWorldApp(func(appDir string) {
   228  							Eventually(
   229  								helpers.CustomCF(
   230  									helpers.CFEnv{WorkingDirectory: appDir},
   231  									"push",
   232  									appNameSharingBoundRoute,
   233  									"--no-start",
   234  								),
   235  							).Should(Exit(0))
   236  						})
   237  
   238  						helpers.MapRouteToApplication(appNameSharingBoundRoute, domain, host, path)
   239  					})
   240  
   241  					It("does not delete the app or associated routes", func() {
   242  						session := helpers.CF("delete", appName, "-f", "-r")
   243  						username, _ := helpers.GetCredentials()
   244  						Eventually(session).Should(Say("Deleting app %s in org %s / space %s as %s...", appName, orgName, spaceName, username))
   245  						Eventually(session.Err).Should(Say("App '%s' was not deleted because route '%s' is mapped to more than one app.", appName, boundRouteURL))
   246  						Eventually(session).Should(Say("FAILED"))
   247  						Eventually(session).Should(Say("\n\nTIP: Run 'cf delete %s' to delete the app and 'cf delete-route' to delete the route.", appName))
   248  						Eventually(session).Should(Exit(1))
   249  					})
   250  				})
   251  			})
   252  		})
   253  	})
   254  })