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

     1  package isolated
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("create-service-key command", func() {
    17  	Describe("help", func() {
    18  		When("--help flag is set", func() {
    19  			It("Displays command usage to output", func() {
    20  				session := helpers.CF("create-service-key", "--help")
    21  				Eventually(session).Should(Say("NAME:"))
    22  				Eventually(session).Should(Say(`\s+create-service-key - Create key for a service instance`))
    23  
    24  				Eventually(session).Should(Say("USAGE:"))
    25  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY \[-c PARAMETERS_AS_JSON\]`))
    26  				Eventually(session).Should(Say(`\s+Optionally provide service-specific configuration parameters in a valid JSON object in-line.`))
    27  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{\"name\":\"value\",\"name\":\"value\"}'`))
    28  				Eventually(session).Should(Say(`\s+Optionally provide a file containing service-specific configuration parameters in a valid JSON object. The path to the parameters file can be an absolute or relative path to a file.`))
    29  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE`))
    30  				Eventually(session).Should(Say(`\s+Example of valid JSON object:`))
    31  				Eventually(session).Should(Say(`\s+{`))
    32  				Eventually(session).Should(Say(`\s+\"permissions\": \"read-only\"`))
    33  				Eventually(session).Should(Say(`\s+}`))
    34  
    35  				Eventually(session).Should(Say("EXAMPLES:"))
    36  				Eventually(session).Should(Say(`\s+cf create-service-key mydb mykey -c '{\"permissions\":\"read-only\"}'`))
    37  				Eventually(session).Should(Say(`\s+cf create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json`))
    38  				Eventually(session).Should(Say("ALIAS:"))
    39  				Eventually(session).Should(Say(`\s+csk`))
    40  
    41  				Eventually(session).Should(Say("OPTIONS:"))
    42  				Eventually(session).Should(Say(`\s+-c      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.`))
    43  
    44  				Eventually(session).Should(Say("SEE ALSO:"))
    45  				Eventually(session).Should(Say(`\s+service-key`))
    46  
    47  				Eventually(session).Should(Exit(0))
    48  			})
    49  		})
    50  	})
    51  
    52  	var (
    53  		serviceInstance string
    54  		serviceKeyName  string
    55  		service         string
    56  		servicePlan     string
    57  	)
    58  
    59  	BeforeEach(func() {
    60  		serviceInstance = helpers.PrefixedRandomName("si")
    61  		serviceKeyName = "service-key"
    62  	})
    63  
    64  	When("the environment is not setup correctly", func() {
    65  		It("fails with the appropriate errors", func() {
    66  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "create-service-key", "service-name", "key-name")
    67  		})
    68  	})
    69  
    70  	When("the environment is setup correctly", func() {
    71  		var (
    72  			org      string
    73  			space    string
    74  			username string
    75  		)
    76  
    77  		BeforeEach(func() {
    78  			org = helpers.NewOrgName()
    79  			space = helpers.NewSpaceName()
    80  			username, _ = helpers.GetCredentials()
    81  
    82  			helpers.SetupCF(org, space)
    83  		})
    84  
    85  		AfterEach(func() {
    86  			helpers.QuickDeleteOrg(org)
    87  		})
    88  
    89  		When("not providing any arguments", func() {
    90  			It("displays an invalid usage error and the help text, and exits 1", func() {
    91  				session := helpers.CF("create-service-key")
    92  				Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SERVICE_INSTANCE` and `SERVICE_KEY` were not provided"))
    93  
    94  				Eventually(session).Should(Say("NAME:"))
    95  				Eventually(session).Should(Say(`\s+create-service-key - Create key for a service instance`))
    96  				Eventually(session).Should(Exit(1))
    97  			})
    98  		})
    99  
   100  		When("provided with service instance name but no service key name", func() {
   101  			It("displays an invalid usage error and the help text, and exits 1", func() {
   102  				session := helpers.CF("create-service-key", serviceInstance)
   103  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SERVICE_KEY` was not provided"))
   104  				Eventually(session).Should(Say("NAME:"))
   105  				Eventually(session).Should(Exit(1))
   106  			})
   107  		})
   108  
   109  		When("provided with a service instance name that doesn't exist", func() {
   110  			It("displays FAILED and an informative error, and exits 1", func() {
   111  				session := helpers.CF("create-service-key", "missing-service-instance", serviceKeyName)
   112  				Eventually(session.Out).Should(Say("FAILED"))
   113  				Eventually(session.Err).Should(Say("Service instance missing-service-instance not found"))
   114  				Eventually(session).Should(Exit(1))
   115  			})
   116  		})
   117  
   118  		When("provided with a brokered service instance", func() {
   119  			var broker *servicebrokerstub.ServiceBrokerStub
   120  
   121  			BeforeEach(func() {
   122  				broker = servicebrokerstub.EnableServiceAccess()
   123  				service = broker.FirstServiceOfferingName()
   124  				servicePlan = broker.FirstServicePlanName()
   125  
   126  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   127  			})
   128  
   129  			AfterEach(func() {
   130  				Eventually(helpers.CF("delete-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   131  				broker.Forget()
   132  			})
   133  
   134  			It("creates the service key and displays OK", func() {
   135  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   136  				Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   137  				Eventually(session).Should(Say("OK"))
   138  				Eventually(session).Should(Exit(0))
   139  			})
   140  
   141  			When("provided valid configuration parameters", func() {
   142  				It("creates the service key and displays OK", func() {
   143  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"wheres":"waldo"}`)
   144  					Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   145  					Eventually(session).Should(Say("OK"))
   146  					Eventually(session).Should(Exit(0))
   147  				})
   148  			})
   149  
   150  			When("provided invalid configuration parameters", func() {
   151  				It("displays an invalid configuration error", func() {
   152  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"bad json"}`)
   153  					Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."))
   154  					Eventually(session).Should(Exit(1))
   155  				})
   156  			})
   157  
   158  			When("configuration parameters are provided in a file", func() {
   159  
   160  				When("the file-path does not exist", func() {
   161  					It("displays an invalid configuration error, and exits 1", func() {
   162  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `/this/is/not/valid`)
   163  						Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."))
   164  						Eventually(session).Should(Exit(1))
   165  					})
   166  				})
   167  
   168  				When("the file contains invalid json", func() {
   169  					var configurationFile *os.File
   170  
   171  					BeforeEach(func() {
   172  						var err error
   173  						content := []byte("{i-am-very-bad-json")
   174  						configurationFile, err = ioutil.TempFile("", "CF_CLI")
   175  						Expect(err).NotTo(HaveOccurred())
   176  
   177  						_, err = configurationFile.Write(content)
   178  						Expect(err).NotTo(HaveOccurred())
   179  
   180  						Expect(configurationFile.Close()).To(Succeed())
   181  					})
   182  
   183  					AfterEach(func() {
   184  						Expect(os.RemoveAll(configurationFile.Name())).To(Succeed())
   185  					})
   186  
   187  					It("displays an invalid configuration error, and exits 1", func() {
   188  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", configurationFile.Name())
   189  						Eventually(session.Err).Should(Say("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."))
   190  						Eventually(session).Should(Exit(1))
   191  					})
   192  				})
   193  			})
   194  
   195  			When("a service key with the provided name already exists", func() {
   196  				BeforeEach(func() {
   197  					Eventually(helpers.CF("create-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   198  				})
   199  
   200  				It("displays OK and an informative error message", func() {
   201  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   202  					Eventually(session).Should(Say("OK"))
   203  					Eventually(session.Err).Should(Say("Service key %s already exists", serviceKeyName))
   204  					Eventually(session).Should(Exit(0))
   205  				})
   206  			})
   207  
   208  			When("the service is not bindable", func() {
   209  				BeforeEach(func() {
   210  					broker.Services[0].Bindable = false
   211  					broker.Configure().Register()
   212  				})
   213  
   214  				It("displays FAILED and an informative error, and exits 1", func() {
   215  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   216  					Eventually(session).Should(Say("FAILED"))
   217  					Eventually(session.Err).Should(Say("This service doesn't support creation of keys."))
   218  					Eventually(session).Should(Exit(1))
   219  				})
   220  			})
   221  		})
   222  
   223  		When("provided with a user-provided service instance", func() {
   224  			BeforeEach(func() {
   225  				Eventually(helpers.CF("create-user-provided-service", serviceInstance)).Should(Exit(0))
   226  			})
   227  
   228  			It("Displays an informative error message and FAILED, and exits 1", func() {
   229  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   230  				Eventually(session.Err).Should(Say("Service keys are not supported for user-provided service instances."))
   231  				Eventually(session).Should(Say("FAILED"))
   232  				Eventually(session).Should(Exit(1))
   233  			})
   234  		})
   235  	})
   236  })