github.com/arunkumar7540/cli@v6.45.0+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.CreateBroker(domain, service, servicePlan)
   125  
   126  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   127  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   128  			})
   129  
   130  			AfterEach(func() {
   131  				Eventually(helpers.CF("delete-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   132  				broker.Destroy()
   133  			})
   134  
   135  			It("creates the service key and displays OK", func() {
   136  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   137  				Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   138  				Eventually(session).Should(Say("OK"))
   139  				Eventually(session).Should(Exit(0))
   140  			})
   141  
   142  			When("provided valid configuration parameters", func() {
   143  				It("creates the service key and displays OK", func() {
   144  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"wheres":"waldo"}`)
   145  					Eventually(session).Should(Say("Creating service key %s for service instance %s as %s...", serviceKeyName, serviceInstance, username))
   146  					Eventually(session).Should(Say("OK"))
   147  					Eventually(session).Should(Exit(0))
   148  				})
   149  			})
   150  
   151  			When("provided invalid configuration parameters", func() {
   152  				It("displays an invalid configuration error", func() {
   153  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `{"bad json"}`)
   154  					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."))
   155  					Eventually(session).Should(Exit(1))
   156  				})
   157  			})
   158  
   159  			When("configuration parameters are provided in a file", func() {
   160  
   161  				When("the file-path does not exist", func() {
   162  					It("displays an invalid configuration error, and exits 1", func() {
   163  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", `/this/is/not/valid`)
   164  						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."))
   165  						Eventually(session).Should(Exit(1))
   166  					})
   167  				})
   168  
   169  				When("the file contains invalid json", func() {
   170  					var configurationFile *os.File
   171  
   172  					BeforeEach(func() {
   173  						var err error
   174  						content := []byte("{i-am-very-bad-json")
   175  						configurationFile, err = ioutil.TempFile("", "CF_CLI")
   176  						Expect(err).NotTo(HaveOccurred())
   177  
   178  						_, err = configurationFile.Write(content)
   179  						Expect(err).NotTo(HaveOccurred())
   180  
   181  						Expect(configurationFile.Close()).To(Succeed())
   182  					})
   183  
   184  					AfterEach(func() {
   185  						Expect(os.RemoveAll(configurationFile.Name())).To(Succeed())
   186  					})
   187  
   188  					It("displays an invalid configuration error, and exits 1", func() {
   189  						session := helpers.CF("create-service-key", serviceInstance, serviceKeyName, "-c", configurationFile.Name())
   190  						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."))
   191  						Eventually(session).Should(Exit(1))
   192  					})
   193  				})
   194  			})
   195  
   196  			When("a service key with the provided name already exists", func() {
   197  				BeforeEach(func() {
   198  					Eventually(helpers.CF("create-service-key", serviceInstance, serviceKeyName)).Should(Exit(0))
   199  				})
   200  
   201  				It("displays OK and an informative error message", func() {
   202  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   203  					Eventually(session).Should(Say("OK"))
   204  					Eventually(session.Err).Should(Say("Service key %s already exists", serviceKeyName))
   205  					Eventually(session).Should(Exit(0))
   206  				})
   207  			})
   208  
   209  			When("the service is not bindable", func() {
   210  				BeforeEach(func() {
   211  					broker.Service.Bindable = false
   212  					broker.Configure(true)
   213  					broker.Update()
   214  				})
   215  
   216  				It("displays FAILED and an informative error, and exits 1", func() {
   217  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   218  					Eventually(session).Should(Say("FAILED"))
   219  					Eventually(session.Err).Should(Say("This service doesn't support creation of keys."))
   220  					Eventually(session).Should(Exit(1))
   221  				})
   222  			})
   223  		})
   224  
   225  		When("provided with a user-provided service instance", func() {
   226  			BeforeEach(func() {
   227  				Eventually(helpers.CF("create-user-provided-service", serviceInstance)).Should(Exit(0))
   228  			})
   229  
   230  			It("Displays an informative error message and FAILED, and exits 1", func() {
   231  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   232  				Eventually(session.Err).Should(Say("Service keys are not supported for user-provided service instances."))
   233  				Eventually(session).Should(Say("FAILED"))
   234  				Eventually(session).Should(Exit(1))
   235  			})
   236  		})
   237  	})
   238  })