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

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     5  	"code.cloudfoundry.org/cli/integration/assets/hydrabroker/config"
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     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("update-service command", func() {
    15  	Describe("help", func() {
    16  		When("--help flag is set", func() {
    17  			It("displays command usage to output", func() {
    18  				session := helpers.CF("update-service", "--help")
    19  
    20  				Eventually(session).Should(Exit(0))
    21  
    22  				Expect(session).Should(Say("NAME:"))
    23  				Expect(session).Should(Say(`\s+update-service - Update a service instance`))
    24  				Expect(session).Should(Say(`USAGE:`))
    25  				Expect(session).Should(Say(`\s+cf update-service SERVICE_INSTANCE \[-p NEW_PLAN\] \[-c PARAMETERS_AS_JSON\] \[-t TAGS\] \[--upgrade\]`))
    26  				Expect(session).Should(Say(`\s+Optionally provide service-specific configuration parameters in a valid JSON object in-line:`))
    27  				Expect(session).Should(Say(`\s+cf update-service SERVICE_INSTANCE -c '{\"name\":\"value\",\"name\":\"value\"}'`))
    28  				Expect(session).Should(Say(`\s+Optionally provide a file containing service-specific configuration parameters in a valid JSON object\.`))
    29  				Expect(session).Should(Say(`\s+The path to the parameters file can be an absolute or relative path to a file:`))
    30  				Expect(session).Should(Say(`\s+cf update-service SERVICE_INSTANCE -c PATH_TO_FILE`))
    31  				Expect(session).Should(Say(`\s+Example of valid JSON object:`))
    32  				Expect(session).Should(Say(`\s+{`))
    33  				Expect(session).Should(Say(`\s+\"cluster_nodes\": {`))
    34  				Expect(session).Should(Say(`\s+\"count\": 5,`))
    35  				Expect(session).Should(Say(`\s+\"memory_mb\": 1024`))
    36  				Expect(session).Should(Say(`\s+}`))
    37  				Expect(session).Should(Say(`\s+}`))
    38  				Expect(session).Should(Say(`\s+ Optionally provide a list of comma-delimited tags that will be written to the VCAP_SERVICES environment variable for any bound applications.`))
    39  				Expect(session).Should(Say(`EXAMPLES:`))
    40  				Expect(session).Should(Say(`\s+cf update-service mydb -p gold`))
    41  				Expect(session).Should(Say(`\s+cf update-service mydb -c '{\"ram_gb\":4}'`))
    42  				Expect(session).Should(Say(`\s+cf update-service mydb -c ~/workspace/tmp/instance_config.json`))
    43  				Expect(session).Should(Say(`\s+cf update-service mydb -t "list, of, tags"`))
    44  				Expect(session).Should(Say(`\s+cf update-service mydb --upgrade`))
    45  				Expect(session).Should(Say(`\s+cf update-service mydb --upgrade --force`))
    46  				Expect(session).Should(Say(`OPTIONS:`))
    47  				Expect(session).Should(Say(`\s+-c\s+Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file\. For a list of supported configuration parameters, see documentation for the particular service offering\.`))
    48  				Expect(session).Should(Say(`\s+-p\s+Change service plan for a service instance`))
    49  				Expect(session).Should(Say(`\s+-t\s+User provided tags`))
    50  				Expect(session).Should(Say(`\s+--upgrade, -u\s+Upgrade the service instance to the latest version of the service plan available. It cannot be combined with flags: -c, -p, -t.`))
    51  				Expect(session).Should(Say(`\s+--force, -f\s+Force the upgrade to the latest available version of the service plan. It can only be used with: -u, --upgrade.`))
    52  				Expect(session).Should(Say(`SEE ALSO:`))
    53  				Expect(session).Should(Say(`\s+rename-service, services, update-user-provided-service`))
    54  			})
    55  		})
    56  	})
    57  
    58  	When("the environment is not setup correctly", func() {
    59  		BeforeEach(func() {
    60  			helpers.SkipIfVersionLessThan(ccversion.MinVersionUpdateServiceInstanceMaintenanceInfoV2)
    61  		})
    62  
    63  		It("fails with the appropriate errors", func() {
    64  			// the upgrade flag is passed here to exercise a particular code path before refactoring
    65  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "update-service", "foo", "--upgrade")
    66  		})
    67  	})
    68  
    69  	When("an api is targeted, the user is logged in, and an org and space are targeted", func() {
    70  		var (
    71  			orgName string
    72  		)
    73  
    74  		BeforeEach(func() {
    75  			orgName = helpers.NewOrgName()
    76  			var spaceName = helpers.NewSpaceName()
    77  			helpers.SetupCF(orgName, spaceName)
    78  		})
    79  
    80  		AfterEach(func() {
    81  			helpers.QuickDeleteOrg(orgName)
    82  		})
    83  
    84  		When("there are no service instances", func() {
    85  			When("upgrading", func() {
    86  				BeforeEach(func() {
    87  					helpers.SkipIfVersionLessThan(ccversion.MinVersionUpdateServiceInstanceMaintenanceInfoV2)
    88  				})
    89  
    90  				It("displays an informative error before prompting and exits 1", func() {
    91  					session := helpers.CF("update-service", "non-existent-service", "--upgrade")
    92  
    93  					Eventually(session).Should(Exit(1))
    94  
    95  					Expect(session.Err).Should(Say("Service instance non-existent-service not found"))
    96  				})
    97  			})
    98  		})
    99  
   100  		When("providing other arguments while upgrading", func() {
   101  			It("displays an informative error message and exits 1", func() {
   102  				session := helpers.CF("update-service", "irrelevant", "--upgrade", "-c", "{\"hello\": \"world\"}")
   103  
   104  				Eventually(session).Should(Exit(1))
   105  				Expect(session.Err).Should(Say("Incorrect Usage: The following arguments cannot be used together: --upgrade, -t, -c, -p"))
   106  				Expect(session).Should(Say("FAILED"))
   107  				Expect(session).Should(Say("USAGE:"))
   108  				Expect(session).Should(Exit(1))
   109  			})
   110  		})
   111  
   112  		When("there is a service instance", func() {
   113  			var (
   114  				broker              *servicebrokerstub.ServiceBrokerStub
   115  				serviceInstanceName string
   116  				tags                string
   117  				username            string
   118  			)
   119  
   120  			BeforeEach(func() {
   121  				broker = servicebrokerstub.EnableServiceAccess()
   122  
   123  				serviceInstanceName = helpers.PrefixedRandomName("SI")
   124  				tags = "a tag"
   125  				Eventually(helpers.CF("create-service", broker.FirstServiceOfferingName(), broker.FirstServicePlanName(), serviceInstanceName, "-t", tags)).Should(Exit(0))
   126  
   127  				username, _ = helpers.GetCredentials()
   128  			})
   129  
   130  			AfterEach(func() {
   131  				Eventually(helpers.CF("delete-service", serviceInstanceName, "-f")).Should(Exit(0))
   132  				broker.Forget()
   133  			})
   134  
   135  			When("updating to a service plan that does not exist", func() {
   136  				It("displays an informative error message, exits 1", func() {
   137  					session := helpers.CF("update-service", serviceInstanceName, "-p", "non-existing-service-plan")
   138  					Eventually(session).Should(Exit(1))
   139  
   140  					Expect(session).Should(Say("Plan does not exist for the %s service", broker.FirstServiceOfferingName()))
   141  				})
   142  			})
   143  
   144  			When("updating to the same service plan (no-op)", func() {
   145  				It("displays an informative success message, exits 0", func() {
   146  					session := helpers.CF("update-service", serviceInstanceName, "-p", broker.FirstServicePlanName())
   147  
   148  					Eventually(session).Should(Exit(0))
   149  					Expect(session).Should(Say("Updating service instance %s as %s...", serviceInstanceName, username))
   150  
   151  					Expect(session).Should(Say("OK"))
   152  					Expect(session).Should(Say("No changes were made"))
   153  				})
   154  			})
   155  
   156  			When("upgrading", func() {
   157  				var buffer *Buffer
   158  
   159  				BeforeEach(func() {
   160  					buffer = NewBuffer()
   161  				})
   162  
   163  				When("the user provides --upgrade in an unsupported CAPI version", func() {
   164  					BeforeEach(func() {
   165  						helpers.SkipIfVersionAtLeast(ccversion.MinVersionUpdateServiceInstanceMaintenanceInfoV2)
   166  					})
   167  
   168  					It("should report that the version of CAPI is too low", func() {
   169  						session := helpers.CF("update-service", serviceInstanceName, "--upgrade")
   170  						Eventually(session.Err).Should(Say(`Option '--upgrade' requires CF API version %s or higher. Your target is 2\.\d+\.\d+`, ccversion.MinVersionUpdateServiceInstanceMaintenanceInfoV2))
   171  						Eventually(session).Should(Exit(1))
   172  					})
   173  				})
   174  
   175  				When("when CAPI supports service instance maintenance_info updates", func() {
   176  					BeforeEach(func() {
   177  						helpers.SkipIfVersionLessThan(ccversion.MinVersionUpdateServiceInstanceMaintenanceInfoV2)
   178  					})
   179  
   180  					When("cancelling the update", func() {
   181  						BeforeEach(func() {
   182  							_, err := buffer.Write([]byte("n\n"))
   183  							Expect(err).ToNot(HaveOccurred())
   184  						})
   185  
   186  						It("does not proceed", func() {
   187  							session := helpers.CFWithStdin(buffer, "update-service", serviceInstanceName, "--upgrade")
   188  
   189  							Eventually(session).Should(Exit(0))
   190  
   191  							Expect(session).Should(Say("You are about to update %s", serviceInstanceName))
   192  							Expect(session).Should(Say("Warning: This operation may be long running and will block further operations on the service until complete."))
   193  							Expect(session).Should(Say("Really update service %s\\? \\[yN\\]:", serviceInstanceName))
   194  							Expect(session).Should(Say("Update cancelled"))
   195  							Expect(session).Should(Exit(0))
   196  						})
   197  					})
   198  
   199  					When("proceeding with the update", func() {
   200  						BeforeEach(func() {
   201  							_, err := buffer.Write([]byte("y\n"))
   202  							Expect(err).ToNot(HaveOccurred())
   203  						})
   204  
   205  						When("updating a parameter", func() {
   206  							It("updates the service without removing the tags", func() {
   207  								session := helpers.CFWithStdin(buffer, "update-service", serviceInstanceName, "-c", "{\"tls\": true}")
   208  
   209  								Eventually(session).Should(Exit(0))
   210  
   211  								Expect(session).Should(Say("Updating service instance %s as %s...", serviceInstanceName, username))
   212  
   213  								session = helpers.CF("service", serviceInstanceName)
   214  
   215  								Eventually(session).Should(Exit(0))
   216  								Expect(session).Should(Say("a tag"))
   217  							})
   218  						})
   219  
   220  						When("upgrade is available", func() {
   221  							BeforeEach(func() {
   222  								broker.Services[0].Plans[0].MaintenanceInfo = &config.MaintenanceInfo{Version: "9.1.2"}
   223  								broker.Configure().Register()
   224  							})
   225  
   226  							It("updates the service", func() {
   227  
   228  								session := helpers.CFWithStdin(buffer, "update-service", serviceInstanceName, "--upgrade")
   229  								By("displaying an informative message")
   230  								Eventually(session).Should(Exit(0))
   231  
   232  								Expect(session).Should(Say("You are about to update %s", serviceInstanceName))
   233  								Expect(session).Should(Say("Warning: This operation may be long running and will block further operations on the service until complete."))
   234  								Expect(session).Should(Say("Really update service %s\\? \\[yN\\]:", serviceInstanceName))
   235  								Expect(session).Should(Say("Updating service instance %s as %s...", serviceInstanceName, username))
   236  
   237  								By("requesting an upgrade from the platform")
   238  								session = helpers.CF("service", serviceInstanceName)
   239  								Eventually(session).Should(Say("status:\\s+update succeeded"))
   240  							})
   241  						})
   242  
   243  						When("no upgrade is available", func() {
   244  							It("does not update the service and outputs informative message", func() {
   245  								session := helpers.CFWithStdin(buffer, "update-service", serviceInstanceName, "--upgrade")
   246  
   247  								Eventually(session).Should(Exit(1))
   248  
   249  								Expect(session).Should(Say("You are about to update %s", serviceInstanceName))
   250  								Expect(session).Should(Say("Warning: This operation may be long running and will block further operations on the service until complete."))
   251  								Expect(session).Should(Say("Really update service %s\\? \\[yN\\]:", serviceInstanceName))
   252  								Expect(session).Should(Say("Updating service instance %s as %s...", serviceInstanceName, username))
   253  								Expect(session.Err).Should(Say("No upgrade is available."))
   254  								Expect(session.Err).Should(Say("TIP: To find out if upgrade is available run `cf service %s`.", serviceInstanceName))
   255  							})
   256  						})
   257  					})
   258  
   259  					When("providing --force argument and upgrade is available", func() {
   260  						BeforeEach(func() {
   261  							broker.Services[0].Plans[0].MaintenanceInfo = &config.MaintenanceInfo{Version: "9.1.2"}
   262  							broker.Configure().Register()
   263  						})
   264  
   265  						It("updates the service without prompting", func() {
   266  							session := helpers.CFWithStdin(buffer, "update-service", serviceInstanceName, "--upgrade", "--force")
   267  
   268  							By("displaying an informative message")
   269  							Eventually(session).Should(Exit(0))
   270  							Expect(session).Should(Say("Updating service instance %s as %s...", serviceInstanceName, username))
   271  
   272  							By("requesting an upgrade from the platform")
   273  							session = helpers.CF("service", serviceInstanceName)
   274  							Eventually(session).Should(Say("status:\\s+update succeeded"))
   275  						})
   276  					})
   277  				})
   278  			})
   279  		})
   280  	})
   281  })