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

     1  package isolated
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("update-user-provided-service command", func() {
    14  	Describe("help", func() {
    15  		When("--help flag is set", func() {
    16  			It("displays command usage to output", func() {
    17  				session := helpers.CF("update-user-provided-service", "--help")
    18  				eventuallyExpectHelpMessage(session)
    19  				Eventually(session).Should(Exit(0))
    20  			})
    21  		})
    22  	})
    23  
    24  	When("the environment is not setup correctly", func() {
    25  		It("fails with the appropriate errors", func() {
    26  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "update-user-provided-service", "foo")
    27  		})
    28  	})
    29  
    30  	When("an api is targeted, the user is logged in, and an org and space are targeted", func() {
    31  		var (
    32  			userName  string
    33  			orgName   string
    34  			spaceName string
    35  		)
    36  
    37  		BeforeEach(func() {
    38  			orgName = helpers.NewOrgName()
    39  			spaceName = helpers.NewSpaceName()
    40  			helpers.SetupCF(orgName, spaceName)
    41  			userName, _ = helpers.GetCredentials()
    42  		})
    43  
    44  		AfterEach(func() {
    45  			helpers.QuickDeleteOrg(orgName)
    46  		})
    47  
    48  		When("the user-provided service instance name is not provided", func() {
    49  			It("displays the help message and exits 1", func() {
    50  				session := helpers.CF("update-user-provided-service")
    51  				eventuallyExpectHelpMessage(session)
    52  				Eventually(session).Should(Exit(1))
    53  			})
    54  		})
    55  
    56  		When("there are unknown additional arguments", func() {
    57  			It("displays the help message and exits 1", func() {
    58  				session := helpers.CF("update-user-provided-service", "service-name", "additional", "invalid", "arguments")
    59  				eventuallyExpectHelpMessage(session)
    60  				Eventually(session).Should(Exit(1))
    61  			})
    62  		})
    63  
    64  		When("the user-provided service instance does not exist", func() {
    65  			It("displays an informative error and exits 1", func() {
    66  				session := helpers.CF("update-user-provided-service", "non-existent-service")
    67  				Eventually(session.Err).Should(Say("Service instance non-existent-service not found"))
    68  				Eventually(session.Out).Should(Say("FAILED"))
    69  				Eventually(session).Should(Exit(1))
    70  			})
    71  		})
    72  
    73  		When("the user-provided service exists", func() {
    74  			var serviceName string
    75  
    76  			BeforeEach(func() {
    77  				serviceName = randomUserProvidedServiceName()
    78  				createUserProvidedService(serviceName)
    79  			})
    80  
    81  			AfterEach(func() {
    82  				deleteUserProvidedService(serviceName)
    83  			})
    84  
    85  			When("no flags are provided", func() {
    86  				It("displays an informative message and exits 0", func() {
    87  					session := helpers.CF("update-user-provided-service", serviceName)
    88  					Eventually(session.Out).Should(Say("No flags specified. No changes were made."))
    89  					Eventually(session).Should(Exit(0))
    90  				})
    91  			})
    92  
    93  			When("flags are provided", func() {
    94  				It("displays success message, exits 0, and updates the service", func() {
    95  					session := helpers.CF(
    96  						`update-user-provided-service`, serviceName,
    97  						`-l`, `syslog://example.com`,
    98  						`-p`, `{"some": "credentials"}`,
    99  						`-r`, `https://example.com`,
   100  						`-t`, `"tag1,tag2"`,
   101  					)
   102  
   103  					eventuallyExpectOKMessage(session, serviceName, orgName, spaceName, userName)
   104  					Eventually(session).Should(Exit(0))
   105  
   106  					session = helpers.CF("service", serviceName)
   107  					Eventually(session).Should(Say(`tags:\s+tag1,\s*tag2`))
   108  					Eventually(session).Should(Say(`route service url:\s+https://example.com`))
   109  				})
   110  			})
   111  
   112  			Context("when the user-provided service already has values", func() {
   113  				BeforeEach(func() {
   114  					session := helpers.CF(
   115  						`update-user-provided-service`, serviceName,
   116  						`-l`, `syslog://example.com`,
   117  						`-p`, `{"some": "credentials"}`,
   118  						`-r`, `https://example.com`,
   119  						`-t`, `"tag1,tag2"`,
   120  					)
   121  					Eventually(session).Should(Exit(0))
   122  
   123  					session = helpers.CF("service", serviceName)
   124  					Eventually(session).Should(Say(`tags:\s+tag1,\s*tag2`))
   125  					Eventually(session).Should(Say(`route service url:\s+https://example.com`))
   126  				})
   127  
   128  				It("can unset previous values provideding empty strings as flag values", func() {
   129  					session := helpers.CF(
   130  						`update-user-provided-service`, serviceName,
   131  						`-l`, `""`,
   132  						`-p`, `""`,
   133  						`-r`, `""`,
   134  						`-t`, `""`,
   135  					)
   136  					Eventually(session).Should(Exit(0))
   137  
   138  					session = helpers.CF("service", serviceName)
   139  					Consistently(session).ShouldNot(Say(`tags:\s+tag1,\s*tag2`))
   140  					Consistently(session).ShouldNot(Say(`route service url:\s+https://example.com`))
   141  				})
   142  
   143  				It("does not unset previous values for flags that are not provided", func() {
   144  					session := helpers.CF(
   145  						`update-user-provided-service`, serviceName,
   146  						`-l`, `""`,
   147  						`-p`, `""`,
   148  					)
   149  					Eventually(session).Should(Exit(0))
   150  
   151  					session = helpers.CF("service", serviceName)
   152  					Eventually(session).Should(Say(`tags:\s+tag1,\s*tag2`))
   153  					Eventually(session).Should(Say(`route service url:\s+https://example.com`))
   154  				})
   155  			})
   156  
   157  			When("requesting interactive credentials", func() {
   158  				var buffer *Buffer
   159  
   160  				BeforeEach(func() {
   161  					buffer = NewBuffer()
   162  					_, err := buffer.Write([]byte("fake-username\nfake-password\n"))
   163  					Expect(err).ToNot(HaveOccurred())
   164  				})
   165  
   166  				It("requests the credentials at a prompt", func() {
   167  					session := helpers.CFWithStdin(buffer, "update-user-provided-service", serviceName, "-p", `"username,password"`)
   168  
   169  					Eventually(session).Should(Say("username: "))
   170  					Eventually(session).Should(Say("password: "))
   171  					Consistently(session).ShouldNot(Say("fake-username"), "credentials should not be echoed to the user")
   172  					Consistently(session).ShouldNot(Say("fake-password"), "credentials should not be echoed to the user")
   173  					eventuallyExpectOKMessage(session, serviceName, orgName, spaceName, userName)
   174  					Eventually(session).Should(Exit(0))
   175  				})
   176  			})
   177  
   178  			When("reading JSON credentials from a file", func() {
   179  				var path string
   180  
   181  				BeforeEach(func() {
   182  					path = helpers.TempFileWithContent(`{"some": "credentials"}`)
   183  				})
   184  
   185  				AfterEach(func() {
   186  					Expect(os.Remove(path)).To(Succeed())
   187  				})
   188  
   189  				It("accepts a file path", func() {
   190  					session := helpers.CF("update-user-provided-service", serviceName, "-p", path)
   191  
   192  					By("checking that it does not interpret the file name as request for an interactive credential prompt")
   193  					Consistently(session.Out.Contents()).ShouldNot(ContainSubstring(path))
   194  
   195  					By("succeeding")
   196  					eventuallyExpectOKMessage(session, serviceName, orgName, spaceName, userName)
   197  					Eventually(session).Should(Exit(0))
   198  				})
   199  			})
   200  		})
   201  	})
   202  })
   203  
   204  func eventuallyExpectHelpMessage(session *Session) {
   205  	Eventually(session).Should(Say(`NAME:`))
   206  	Eventually(session).Should(Say(`\s+update-user-provided-service - Update user-provided service instance`))
   207  	Eventually(session).Should(Say(`USAGE:`))
   208  	Eventually(session).Should(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE \[-p CREDENTIALS\] \[-l SYSLOG_DRAIN_URL\] \[-r ROUTE_SERVICE_URL\] \[-t TAGS\]`))
   209  	Eventually(session).Should(Say(`\s+Pass comma separated credential parameter names to enable interactive mode:`))
   210  	Eventually(session).Should(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p "comma, separated, parameter, names"`))
   211  	Eventually(session).Should(Say(`\s+Pass credential parameters as JSON to create a service non-interactively:`))
   212  	Eventually(session).Should(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p '{"key1":"value1","key2":"value2"}'`))
   213  	Eventually(session).Should(Say(`\s+Specify a path to a file containing JSON:`))
   214  	Eventually(session).Should(Say(`\s+cf update-user-provided-service SERVICE_INSTANCE -p PATH_TO_FILE`))
   215  	Eventually(session).Should(Say(`EXAMPLES:`))
   216  	Eventually(session).Should(Say(`\s+cf update-user-provided-service my-db-mine -p '{"username":"admin", "password":"pa55woRD"}'`))
   217  	Eventually(session).Should(Say(`\s+cf update-user-provided-service my-db-mine -p /path/to/credentials.json`))
   218  	Eventually(session).Should(Say(`\s+cf create-user-provided-service my-db-mine -t "list, of, tags"`))
   219  	Eventually(session).Should(Say(`\s+cf update-user-provided-service my-drain-service -l syslog://example.com`))
   220  	Eventually(session).Should(Say(`\s+cf update-user-provided-service my-route-service -r https://example.com`))
   221  	Eventually(session).Should(Say(`ALIAS:`))
   222  	Eventually(session).Should(Say(`\s+uups`))
   223  	Eventually(session).Should(Say(`OPTIONS:`))
   224  	Eventually(session).Should(Say(`\s+-l\s+URL to which logs for bound applications will be streamed`))
   225  	Eventually(session).Should(Say(`\s+-p\s+Credentials, provided inline or in a file, to be exposed in the VCAP_SERVICES environment variable for bound applications. Provided credentials will override existing credentials.`))
   226  	Eventually(session).Should(Say(`\s+-r\s+URL to which requests for bound routes will be forwarded. Scheme for this URL must be https`))
   227  	Eventually(session).Should(Say(`\s+-t\s+User provided tags`))
   228  	Eventually(session).Should(Say(`SEE ALSO:`))
   229  	Eventually(session).Should(Say(`\s+rename-service, services, update-service`))
   230  }
   231  
   232  func eventuallyExpectOKMessage(session *Session, serviceName, orgName, spaceName, userName string) {
   233  	Eventually(session.Out).Should(Say("Updating user provided service %s in org %s / space %s as %s...", serviceName, orgName, spaceName, userName))
   234  	Eventually(session.Out).Should(Say("OK"))
   235  	Eventually(session.Out).Should(Say("TIP: Use 'cf restage' for any bound apps to ensure your env variable changes take effect"))
   236  }
   237  
   238  func randomUserProvidedServiceName() string {
   239  	return helpers.PrefixedRandomName("ups")
   240  }
   241  
   242  func createUserProvidedService(name string) {
   243  	Eventually(helpers.CF("create-user-provided-service", name)).Should(Exit(0))
   244  }
   245  
   246  func deleteUserProvidedService(name string) {
   247  	Eventually(helpers.CF("delete-service", name)).Should(Exit(0))
   248  }