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

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	. "github.com/onsi/gomega/gbytes"
     8  	. "github.com/onsi/gomega/gexec"
     9  )
    10  
    11  var _ = Describe("delete-space command", func() {
    12  	Describe("help", func() {
    13  		It("shows usage", func() {
    14  			session := helpers.CF("help", "delete-space")
    15  			Eventually(session).Should(Say("NAME:"))
    16  			Eventually(session).Should(Say(`\s+delete-space - Delete a space`))
    17  			Eventually(session).Should(Say("USAGE:"))
    18  			Eventually(session).Should(Say(`delete-space SPACE \[-o ORG\] \[-f\]`))
    19  			Eventually(session).Should(Say("OPTIONS:"))
    20  			Eventually(session).Should(Say(`\s+-f\s+Force deletion without confirmation`))
    21  			Eventually(session).Should(Say(`\s+-o\s+Delete space within specified org`))
    22  			Eventually(session).Should(Exit(0))
    23  		})
    24  	})
    25  
    26  	When("the environment is not setup correctly", func() {
    27  		When("the org is provided", func() {
    28  			It("fails with the appropriate errors", func() {
    29  				helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "delete-space", "space-name", "-o", ReadOnlyOrg)
    30  			})
    31  		})
    32  
    33  		When("the org is not provided", func() {
    34  			It("fails with the appropriate errors", func() {
    35  				helpers.CheckEnvironmentTargetedCorrectly(true, false, ReadOnlyOrg, "delete-space", "space-name")
    36  			})
    37  		})
    38  	})
    39  
    40  	When("the space name it not provided", func() {
    41  		It("displays an error and help", func() {
    42  			session := helpers.CF("delete-space")
    43  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SPACE` was not provided"))
    44  			Eventually(session).Should(Say("USAGE"))
    45  			Eventually(session).Should(Exit(1))
    46  		})
    47  	})
    48  
    49  	When("the space does not exist", func() {
    50  		var orgName string
    51  
    52  		BeforeEach(func() {
    53  			helpers.LoginCF()
    54  
    55  			orgName = helpers.NewOrgName()
    56  			helpers.CreateOrg(orgName)
    57  		})
    58  
    59  		AfterEach(func() {
    60  			helpers.QuickDeleteOrg(orgName)
    61  		})
    62  
    63  		It("passes and displays space not found", func() {
    64  			username, _ := helpers.GetCredentials()
    65  			session := helpers.CF("delete-space", "-f", "-o", orgName, "please-do-not-exist-in-real-life")
    66  			Eventually(session).Should(Say("Deleting space please-do-not-exist-in-real-life in org %s as %s...", orgName, username))
    67  			Eventually(session).Should(Say("OK"))
    68  			Eventually(session.Err).Should(Say(`Space 'please-do-not-exist-in-real-life' does not exist.`))
    69  			Eventually(session).Should(Exit(0))
    70  		})
    71  	})
    72  
    73  	When("the space exists", func() {
    74  		var orgName string
    75  		var spaceName string
    76  
    77  		BeforeEach(func() {
    78  			helpers.LoginCF()
    79  
    80  			orgName = helpers.NewOrgName()
    81  			spaceName = helpers.NewSpaceName()
    82  			helpers.CreateOrgAndSpace(orgName, spaceName)
    83  		})
    84  
    85  		AfterEach(func() {
    86  			helpers.QuickDeleteOrg(orgName)
    87  		})
    88  
    89  		When("the -f flag not is provided", func() {
    90  			var buffer *Buffer
    91  
    92  			BeforeEach(func() {
    93  				buffer = NewBuffer()
    94  			})
    95  
    96  			When("the user enters 'y'", func() {
    97  				BeforeEach(func() {
    98  					_, err := buffer.Write([]byte("y\n"))
    99  					Expect(err).ToNot(HaveOccurred())
   100  				})
   101  
   102  				It("deletes the space", func() {
   103  					username, _ := helpers.GetCredentials()
   104  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   105  					Eventually(session).Should(Say(`This action impacts all resources scoped to this space, including apps, service instances, and space-scoped service brokers.`))
   106  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   107  					Eventually(session).Should(Say("Deleting space %s in org %s as %s...", spaceName, orgName, username))
   108  					Eventually(session).Should(Say("OK"))
   109  					Eventually(session).Should(Exit(0))
   110  					Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   111  				})
   112  			})
   113  
   114  			When("the user enters 'n'", func() {
   115  				BeforeEach(func() {
   116  					_, err := buffer.Write([]byte("n\n"))
   117  					Expect(err).ToNot(HaveOccurred())
   118  				})
   119  
   120  				It("does not delete the space", func() {
   121  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   122  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   123  					Eventually(session).Should(Say("'%s' has not been deleted.", spaceName))
   124  					Eventually(session).Should(Exit(0))
   125  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   126  				})
   127  			})
   128  
   129  			When("the user enters the default input (hits return)", func() {
   130  				BeforeEach(func() {
   131  					_, err := buffer.Write([]byte("\n"))
   132  					Expect(err).ToNot(HaveOccurred())
   133  				})
   134  
   135  				It("does not delete the org", func() {
   136  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   137  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   138  					Eventually(session).Should(Say("'%s' has not been deleted.", spaceName))
   139  					Eventually(session).Should(Exit(0))
   140  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   141  				})
   142  			})
   143  
   144  			When("the user enters an invalid answer", func() {
   145  				BeforeEach(func() {
   146  					// The second '\n' is intentional. Otherwise the buffer will be
   147  					// closed while the interaction is still waiting for input; it gets
   148  					// an EOF and causes an error.
   149  					_, err := buffer.Write([]byte("wat\n\n"))
   150  					Expect(err).ToNot(HaveOccurred())
   151  				})
   152  
   153  				It("asks again", func() {
   154  					session := helpers.CFWithStdin(buffer, "delete-space", spaceName)
   155  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   156  					Eventually(session).Should(Say(`invalid input \(not y, n, yes, or no\)`))
   157  					Eventually(session).Should(Say(`Really delete the space %s\? \[yN\]`, spaceName))
   158  					Eventually(session).Should(Exit(0))
   159  					Eventually(helpers.CF("space", spaceName)).Should(Exit(0))
   160  				})
   161  			})
   162  		})
   163  
   164  		When("the -f flag is provided", func() {
   165  			It("deletes the space", func() {
   166  				username, _ := helpers.GetCredentials()
   167  				session := helpers.CF("delete-space", spaceName, "-f")
   168  				Eventually(session).Should(Say(`Deleting space %s in org %s as %s\.\.\.`, spaceName, orgName, username))
   169  				Eventually(session).Should(Say("OK"))
   170  				Eventually(session).Should(Exit(0))
   171  				Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   172  			})
   173  
   174  			When("the space was targeted", func() {
   175  				BeforeEach(func() {
   176  					Eventually(helpers.CF("target", "-s", spaceName)).Should(Exit(0))
   177  				})
   178  
   179  				It("deletes the space and clears the target", func() {
   180  					username, _ := helpers.GetCredentials()
   181  					session := helpers.CF("delete-space", spaceName, "-f")
   182  					Eventually(session).Should(Say(`Deleting space %s in org %s as %s\.\.\.`, spaceName, orgName, username))
   183  					Eventually(session).Should(Say("OK"))
   184  					Eventually(session).Should(Say(`TIP: No space targeted, use 'cf target -s' to target a space\.`))
   185  					Eventually(session).Should(Exit(0))
   186  
   187  					Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   188  
   189  					session = helpers.CF("target")
   190  					Eventually(session).Should(Say("No space targeted, use 'cf target -s SPACE'"))
   191  					Eventually(session).Should(Exit(0))
   192  				})
   193  			})
   194  		})
   195  	})
   196  
   197  	When("the -o organzation does not exist", func() {
   198  		BeforeEach(func() {
   199  			helpers.LoginCF()
   200  		})
   201  
   202  		It("fails and displays org not found", func() {
   203  			username, _ := helpers.GetCredentials()
   204  			session := helpers.CF("delete-space", "-f", "-o", "please-do-not-exist-in-real-life", "please-do-not-exist-in-real-life")
   205  			Eventually(session).Should(Say("Deleting space please-do-not-exist-in-real-life in org please-do-not-exist-in-real-life as %s...", username))
   206  			Eventually(session.Err).Should(Say(`Organization 'please-do-not-exist-in-real-life' not found\.`))
   207  			Eventually(session).Should(Say("FAILED"))
   208  			Eventually(session).Should(Exit(1))
   209  		})
   210  	})
   211  })