github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/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"
     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("create-service-key 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("create-service-key", "--help")
    19  				Eventually(session).Should(Say("NAME:"))
    20  				Eventually(session).Should(Say(`\s+create-service-key - Create key for a service instance`))
    21  
    22  				Eventually(session).Should(Say("USAGE:"))
    23  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY \[-c PARAMETERS_AS_JSON\]`))
    24  				Eventually(session).Should(Say(`\s+Optionally provide service-specific configuration parameters in a valid JSON object in-line.`))
    25  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{\"name\":\"value\",\"name\":\"value\"}'`))
    26  				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.`))
    27  				Eventually(session).Should(Say(`\s+cf create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE`))
    28  				Eventually(session).Should(Say(`\s+Example of valid JSON object:`))
    29  				Eventually(session).Should(Say(`\s+{`))
    30  				Eventually(session).Should(Say(`\s+\"permissions\": \"read-only\"`))
    31  				Eventually(session).Should(Say(`\s+}`))
    32  
    33  				Eventually(session).Should(Say("EXAMPLES:"))
    34  				Eventually(session).Should(Say(`\s+cf create-service-key mydb mykey -c '{\"permissions\":\"read-only\"}'`))
    35  				Eventually(session).Should(Say(`\s+cf create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json`))
    36  				Eventually(session).Should(Say("ALIAS:"))
    37  				Eventually(session).Should(Say(`\s+csk`))
    38  
    39  				Eventually(session).Should(Say("OPTIONS:"))
    40  				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.`))
    41  
    42  				Eventually(session).Should(Say("SEE ALSO:"))
    43  				Eventually(session).Should(Say(`\s+service-key`))
    44  
    45  				Eventually(session).Should(Exit(0))
    46  			})
    47  		})
    48  	})
    49  
    50  	var (
    51  		serviceInstance string
    52  		serviceKeyName  string
    53  		service         string
    54  		servicePlan     string
    55  	)
    56  
    57  	BeforeEach(func() {
    58  		serviceInstance = helpers.PrefixedRandomName("si")
    59  		serviceKeyName = "service-key"
    60  	})
    61  
    62  	When("the environment is not setup correctly", func() {
    63  		It("fails with the appropriate errors", func() {
    64  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "create-service-key", "service-name", "key-name")
    65  		})
    66  	})
    67  
    68  	When("the environment is setup correctly", func() {
    69  		var (
    70  			org      string
    71  			space    string
    72  			domain   string
    73  			username string
    74  		)
    75  
    76  		BeforeEach(func() {
    77  			org = helpers.NewOrgName()
    78  			space = helpers.NewSpaceName()
    79  			username, _ = helpers.GetCredentials()
    80  
    81  			helpers.SetupCF(org, space)
    82  			domain = helpers.DefaultSharedDomain()
    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 helpers.ServiceBroker
   120  
   121  			BeforeEach(func() {
   122  				service = helpers.PrefixedRandomName("SERVICE")
   123  				servicePlan = helpers.PrefixedRandomName("SERVICE-PLAN")
   124  				broker = helpers.NewServiceBroker(helpers.NewServiceBrokerName(), helpers.NewAssets().ServiceBroker, domain, service, servicePlan)
   125  				broker.Push()
   126  				broker.Configure(true)
   127  				broker.Create()
   128  
   129  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   130  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   131  			})
   132  
   133  			AfterEach(func() {
   134  				Eventually(helpers.CF("delete-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   135  				broker.Destroy()
   136  			})
   137  
   138  			It("creates the service key and displays OK", func() {
   139  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   140  				Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   141  				Eventually(session).Should(Say("OK"))
   142  				Eventually(session).Should(Exit(0))
   143  			})
   144  
   145  			When("provided valid configuration parameters", func() {
   146  				It("creates the service key and displays OK", func() {
   147  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"wheres":"waldo"}`)
   148  					Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   149  					Eventually(session).Should(Say("OK"))
   150  					Eventually(session).Should(Exit(0))
   151  				})
   152  			})
   153  
   154  			When("provided invalid configuration parameters", func() {
   155  				It("displays an invalid configuration error", func() {
   156  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"bad json"}`)
   157  					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."))
   158  					Eventually(session).Should(Exit(1))
   159  				})
   160  			})
   161  
   162  			When("configuration parameters are provided in a file", func() {
   163  
   164  				When("the file-path does not exist", func() {
   165  					It("displays an invalid configuration error, and exits 1", func() {
   166  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `/this/is/not/valid`)
   167  						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."))
   168  						Eventually(session).Should(Exit(1))
   169  					})
   170  				})
   171  
   172  				When("the file contains invalid json", func() {
   173  					var configurationFile *os.File
   174  
   175  					BeforeEach(func() {
   176  						var err error
   177  						content := []byte("{i-am-very-bad-json")
   178  						configurationFile, err = ioutil.TempFile("", "CF_CLI")
   179  						Expect(err).NotTo(HaveOccurred())
   180  
   181  						_, err = configurationFile.Write(content)
   182  						Expect(err).NotTo(HaveOccurred())
   183  
   184  						Expect(configurationFile.Close()).To(Succeed())
   185  					})
   186  
   187  					AfterEach(func() {
   188  						Expect(os.RemoveAll(configurationFile.Name())).To(Succeed())
   189  					})
   190  
   191  					It("displays an invalid configuration error, and exits 1", func() {
   192  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", configurationFile.Name())
   193  						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."))
   194  						Eventually(session).Should(Exit(1))
   195  					})
   196  				})
   197  			})
   198  
   199  			When("a service key with the provided name already exists", func() {
   200  				BeforeEach(func() {
   201  					Eventually(helpers.CF("create-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   202  				})
   203  
   204  				It("displays OK and an informative error message", func() {
   205  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   206  					Eventually(session).Should(Say("OK"))
   207  					Eventually(session.Err).Should(Say("Service key %s already exists", serviceKeyName))
   208  					Eventually(session).Should(Exit(0))
   209  				})
   210  			})
   211  
   212  			When("the service is not bindable", func() {
   213  				BeforeEach(func() {
   214  					broker.Service.Bindable = false
   215  					broker.Configure(true)
   216  					broker.Update()
   217  				})
   218  
   219  				It("displays FAILED and an informative error, and exits 1", func() {
   220  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   221  					Eventually(session).Should(Say("FAILED"))
   222  					Eventually(session.Err).Should(Say("This service doesn't support creation of keys."))
   223  					Eventually(session).Should(Exit(1))
   224  				})
   225  			})
   226  		})
   227  
   228  		When("provided with a user-provided service instance", func() {
   229  			BeforeEach(func() {
   230  				Eventually(helpers.CF("create-user-provided-service", serviceInstance)).Should(Exit(0))
   231  			})
   232  
   233  			It("Displays an informative error message and FAILED, and exits 1", func() {
   234  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   235  				Eventually(session.Err).Should(Say("Service keys are not supported for user-provided service instances."))
   236  				Eventually(session).Should(Say("FAILED"))
   237  				Eventually(session).Should(Exit(1))
   238  			})
   239  		})
   240  	})
   241  })