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

     1  package isolated
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/integration/assets/hydrabroker/config"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("upgrade-service command", func() {
    16  	const command = "upgrade-service"
    17  
    18  	Describe("help", func() {
    19  		When("--help flag is set", func() {
    20  			helpMessage := SatisfyAll(
    21  				Say("NAME:"),
    22  				Say(`\s+upgrade-service - Upgrade a service instance to the latest available version of its current service plan`),
    23  				Say(`USAGE:`),
    24  				Say(`\s+cf upgrade-service SERVICE_INSTANCE`),
    25  				Say(`OPTIONS:`),
    26  				Say(`\s+--force, -f\s+Force upgrade without asking for confirmation`),
    27  				Say(`\s+--wait, -w\s+Wait for the operation to complete\n`),
    28  				Say(`SEE ALSO:`),
    29  				Say(`\s+services, update-service, update-user-provided-service`),
    30  			)
    31  
    32  			It("exits successfully and prints the help message", func() {
    33  				session := helpers.CF(command, "--help")
    34  
    35  				Eventually(session).Should(Exit(0))
    36  				Expect(session.Out).To(helpMessage)
    37  				Expect(session.Err.Contents()).To(BeEmpty())
    38  			})
    39  
    40  			When("the service instance name is omitted", func() {
    41  				It("fails and prints the help message", func() {
    42  					session := helpers.CF(command)
    43  
    44  					Eventually(session).Should(Exit(1))
    45  					Expect(session.Out).To(helpMessage)
    46  					Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided\n"))
    47  				})
    48  			})
    49  
    50  			When("an extra parameter is provided", func() {
    51  				It("fails and prints the help message", func() {
    52  					session := helpers.CF(command, "service-instance-name", "invalid-extra-parameter")
    53  
    54  					Eventually(session).Should(Exit(1))
    55  					Expect(session.Out).To(helpMessage)
    56  					Expect(session.Err).To(Say(`Incorrect Usage: unexpected argument "invalid-extra-parameter"`))
    57  				})
    58  			})
    59  
    60  			When("an extra flag is provided", func() {
    61  				It("fails and prints the help message", func() {
    62  					session := helpers.CF(command, "service-instance-name", "--invalid")
    63  
    64  					Eventually(session).Should(Exit(1))
    65  					Expect(session.Out).To(helpMessage)
    66  					Expect(session.Err).To(Say("Incorrect Usage: unknown flag `invalid'"))
    67  				})
    68  			})
    69  		})
    70  	})
    71  
    72  	When("the environment is not setup correctly", func() {
    73  		It("fails with the appropriate errors", func() {
    74  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, command, "service-instance-name")
    75  		})
    76  	})
    77  
    78  	When("logged in and targeting a space", func() {
    79  		var orgName, spaceName, serviceInstanceName, username string
    80  
    81  		BeforeEach(func() {
    82  			orgName = helpers.NewOrgName()
    83  			spaceName = helpers.NewSpaceName()
    84  			helpers.SetupCF(orgName, spaceName)
    85  			username, _ = helpers.GetCredentials()
    86  			serviceInstanceName = helpers.NewServiceInstanceName()
    87  		})
    88  
    89  		AfterEach(func() {
    90  			helpers.QuickDeleteOrg(orgName)
    91  		})
    92  
    93  		When("the service instance does not exist", func() {
    94  			It("prints a message and exits with error", func() {
    95  				session := helpers.CF(command, "-f", serviceInstanceName)
    96  				Eventually(session).Should(Exit(1))
    97  
    98  				Expect(session.Out).To(SatisfyAll(
    99  					Say("Upgrading service instance %s in org %s / space %s as %s...", serviceInstanceName, orgName, spaceName, username),
   100  					Say("\n"),
   101  					Say("FAILED"),
   102  				))
   103  
   104  				Expect(session.Err).To(
   105  					Say("Service instance '%s' not found\n", serviceInstanceName),
   106  				)
   107  			})
   108  		})
   109  
   110  		When("the service instance exists", func() {
   111  			var broker *servicebrokerstub.ServiceBrokerStub
   112  
   113  			BeforeEach(func() {
   114  				broker = servicebrokerstub.New().WithPlans(2).WithAsyncDelay(time.Microsecond).EnableServiceAccess()
   115  				helpers.CreateManagedServiceInstance(
   116  					broker.FirstServiceOfferingName(),
   117  					broker.FirstServicePlanName(),
   118  					serviceInstanceName,
   119  				)
   120  			})
   121  
   122  			AfterEach(func() {
   123  				broker.Forget()
   124  			})
   125  
   126  			Context("but there is no upgrade available", func() {
   127  				It("prints a message and exits successfully", func() {
   128  					session := helpers.CF(command, "-f", serviceInstanceName)
   129  					Eventually(session).Should(Exit(0))
   130  
   131  					Expect(session.Out).To(SatisfyAll(
   132  						Say("Upgrading service instance %s in org %s / space %s as %s...", serviceInstanceName, orgName, spaceName, username),
   133  						Say("\n"),
   134  						Say("No upgrade is available."),
   135  						Say("\n"),
   136  						Say("OK"),
   137  					))
   138  				})
   139  			})
   140  
   141  			Context("and there's an upgrade available", func() {
   142  				BeforeEach(func() {
   143  					broker.Services[0].Plans[0].MaintenanceInfo = &config.MaintenanceInfo{Version: "9.1.2"}
   144  					broker.Configure().Register()
   145  				})
   146  
   147  				It("upgrades the service instance", func() {
   148  					session := helpers.CF(command, "-f", serviceInstanceName, "--wait")
   149  
   150  					Eventually(session).Should(Exit(0))
   151  					Expect(session.Out).To(SatisfyAll(
   152  						Say(`Upgrading service instance %s in org %s / space %s as %s\.\.\.\n`, serviceInstanceName, orgName, spaceName, username),
   153  						Say(`\n`),
   154  						Say(`Waiting for the operation to complete\.+\n`),
   155  						Say(`\n`),
   156  						Say(`Upgrade of service instance %s complete\.\n`, serviceInstanceName),
   157  						Say(`OK\n`),
   158  					))
   159  				})
   160  			})
   161  		})
   162  	})
   163  })