github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/purge_service_instance_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gbytes"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("purge-service-instance command", func() {
    13  	const command = "purge-service-instance"
    14  
    15  	Describe("help", func() {
    16  		matchHelpMessage := SatisfyAll(
    17  			Say(`NAME:\n`),
    18  			Say(`\s+%s - Recursively remove a service instance and child objects from Cloud Foundry database without making requests to a service broker\n`, command),
    19  			Say(`\n`),
    20  			Say(`USAGE:\n`),
    21  			Say(`\s+cf purge-service-instance SERVICE_INSTANCE \[-f\]\n`),
    22  			Say(`\n`),
    23  			Say(`WARNING: This operation assumes that the service broker responsible for this service instance is no longer available or is not responding with a 200 or 410, and the service instance has been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service instance will be removed from Cloud Foundry, including service bindings and service keys.\n`),
    24  			Say(`\n`),
    25  			Say(`OPTIONS:\n`),
    26  			Say(`\s+--force, -f\s+Force deletion without confirmation\n`),
    27  			Say(`\n`),
    28  			Say(`SEE ALSO:\n`),
    29  			Say(`\s+delete-service, service-brokers, services\n`),
    30  		)
    31  
    32  		When("--help is specified", func() {
    33  			It("exits successfully and print the help message", func() {
    34  				session := helpers.CF(command, "--help")
    35  				Eventually(session).Should(Exit(0))
    36  
    37  				Expect(session.Out).To(matchHelpMessage)
    38  				Expect(string(session.Err.Contents())).To(BeEmpty())
    39  			})
    40  		})
    41  
    42  		When("the service instance name is omitted", func() {
    43  			It("fails and prints the help message", func() {
    44  				session := helpers.CF(command)
    45  
    46  				Eventually(session).Should(Exit(1))
    47  				Expect(session.Out).To(matchHelpMessage)
    48  				Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided\n"))
    49  			})
    50  		})
    51  
    52  		When("an extra parameter is provided", func() {
    53  			It("fails and prints the help message", func() {
    54  				session := helpers.CF(command, "service-instance-name", "invalid-extra-parameter")
    55  
    56  				Eventually(session).Should(Exit(1))
    57  				Expect(session.Out).To(matchHelpMessage)
    58  				Expect(session.Err).To(Say(`Incorrect Usage: unexpected argument "invalid-extra-parameter"`))
    59  			})
    60  		})
    61  
    62  		When("an extra flag is provided", func() {
    63  			It("fails and prints the help message", func() {
    64  				session := helpers.CF(command, "service-instance-name", "--invalid")
    65  
    66  				Eventually(session).Should(Exit(1))
    67  				Expect(session.Out).To(matchHelpMessage)
    68  				Expect(session.Err).To(Say("Incorrect Usage: unknown flag `invalid'"))
    69  			})
    70  		})
    71  	})
    72  
    73  	When("the environment is not setup correctly", func() {
    74  		It("fails with the appropriate errors", func() {
    75  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, command, "service-instance-name")
    76  		})
    77  	})
    78  
    79  	When("targeting a space", func() {
    80  		var (
    81  			serviceInstanceName string
    82  			orgName             string
    83  			spaceName           string
    84  			username            string
    85  			appName             string
    86  		)
    87  
    88  		bindToApp := func() {
    89  			appName = helpers.NewAppName()
    90  			helpers.WithHelloWorldApp(func(appDir string) {
    91  				Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
    92  			})
    93  
    94  			Eventually(helpers.CF("bind-service", appName, serviceInstanceName)).Should(Exit(0))
    95  		}
    96  
    97  		BeforeEach(func() {
    98  			orgName = helpers.NewOrgName()
    99  			spaceName = helpers.NewSpaceName()
   100  			helpers.SetupCF(orgName, spaceName)
   101  
   102  			username, _ = helpers.GetCredentials()
   103  			serviceInstanceName = helpers.NewServiceInstanceName()
   104  		})
   105  
   106  		AfterEach(func() {
   107  			helpers.QuickDeleteOrg(orgName)
   108  		})
   109  
   110  		When("the service instance does not exist", func() {
   111  			It("prints a message and exits successfully", func() {
   112  				session := helpers.CF(command, "-f", serviceInstanceName)
   113  				Eventually(session).Should(Exit(0))
   114  
   115  				Expect(session.Out).To(SatisfyAll(
   116  					Say("Purging service instance %s in org %s / space %s as %s...", serviceInstanceName, orgName, spaceName, username),
   117  					Say("\n"),
   118  					Say("Service instance %s did not exist.\n", serviceInstanceName),
   119  				))
   120  
   121  				Expect(string(session.Err.Contents())).To(BeEmpty())
   122  			})
   123  		})
   124  
   125  		When("the service instance is user-provided", func() {
   126  			BeforeEach(func() {
   127  				session := helpers.CF("cups", serviceInstanceName)
   128  				Eventually(session).Should(Exit(0))
   129  
   130  				bindToApp()
   131  			})
   132  
   133  			It("prints a message and exits successfully", func() {
   134  				session := helpers.CF(command, "-f", serviceInstanceName)
   135  				Eventually(session).Should(Exit(0))
   136  
   137  				Expect(session.Out).To(SatisfyAll(
   138  					Say("Purging service instance %s in org %s / space %s as %s...", serviceInstanceName, orgName, spaceName, username),
   139  					Say("\n"),
   140  					Say("Service instance %s purged.\n", serviceInstanceName),
   141  				))
   142  
   143  				Expect(string(session.Err.Contents())).To(BeEmpty())
   144  
   145  				session = helpers.CF("services").Wait()
   146  				Expect(session.Out).NotTo(Say(serviceInstanceName))
   147  			})
   148  		})
   149  
   150  		When("the service instance is managed", func() {
   151  			var broker *servicebrokerstub.ServiceBrokerStub
   152  
   153  			BeforeEach(func() {
   154  				broker = servicebrokerstub.EnableServiceAccess()
   155  				helpers.CreateManagedServiceInstance(broker.FirstServiceOfferingName(), broker.FirstServicePlanName(), serviceInstanceName)
   156  
   157  				bindToApp()
   158  			})
   159  
   160  			AfterEach(func() {
   161  				broker.Forget()
   162  			})
   163  
   164  			It("prints a message and exits successfully", func() {
   165  				session := helpers.CF(command, "-f", serviceInstanceName)
   166  				Eventually(session).Should(Exit(0))
   167  
   168  				Expect(session.Out).To(SatisfyAll(
   169  					Say("Purging service instance %s in org %s / space %s as %s...", serviceInstanceName, orgName, spaceName, username),
   170  					Say("\n"),
   171  					Say("Service instance %s purged.\n", serviceInstanceName),
   172  				))
   173  
   174  				Expect(string(session.Err.Contents())).To(BeEmpty())
   175  			})
   176  		})
   177  	})
   178  })