github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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/fakeservicebroker"
     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 *fakeservicebroker.FakeServiceBroker
   120  
   121  			BeforeEach(func() {
   122  				broker = fakeservicebroker.New().EnsureBrokerIsAvailable()
   123  				service = broker.ServiceName()
   124  				servicePlan = broker.ServicePlanName()
   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.Services[0].Bindable = false
   212  					broker.Update()
   213  				})
   214  
   215  				It("displays FAILED and an informative error, and exits 1", func() {
   216  					session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   217  					Eventually(session).Should(Say("FAILED"))
   218  					Eventually(session.Err).Should(Say("This service doesn't support creation of keys."))
   219  					Eventually(session).Should(Exit(1))
   220  				})
   221  			})
   222  		})
   223  
   224  		When("provided with a user-provided service instance", func() {
   225  			BeforeEach(func() {
   226  				Eventually(helpers.CF("create-user-provided-service", serviceInstance)).Should(Exit(0))
   227  			})
   228  
   229  			It("Displays an informative error message and FAILED, and exits 1", func() {
   230  				session := helpers.CF("create-service-key", serviceInstance, serviceKeyName)
   231  				Eventually(session.Err).Should(Say("Service keys are not supported for user-provided service instances."))
   232  				Eventually(session).Should(Say("FAILED"))
   233  				Eventually(session).Should(Exit(1))
   234  			})
   235  		})
   236  	})
   237  })