github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/integration/v7/isolated/delete_service_key_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/integration/helpers"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/integration/helpers/servicebrokerstub"
     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("delete-service-key command", func() {
    15  	const command = "delete-service-key"
    16  
    17  	Describe("help", func() {
    18  		matchHelpMessage := SatisfyAll(
    19  			Say(`NAME:\n`),
    20  			Say(`\s+delete-service-key - Delete a service key\n`),
    21  			Say(`\n`),
    22  			Say(`USAGE:\n`),
    23  			Say(`\s+cf delete-service-key SERVICE_INSTANCE SERVICE_KEY \[-f\] \[--wait\]\n`),
    24  			Say(`\n`),
    25  			Say(`EXAMPLES:\n`),
    26  			Say(`\s+cf delete-service-key mydb mykey\n`),
    27  			Say(`\n`),
    28  			Say(`ALIAS:\n`),
    29  			Say(`\s+dsk\n`),
    30  			Say(`\n`),
    31  			Say(`OPTIONS:\n`),
    32  			Say(`\s+-f\s+Force deletion without confirmation\n`),
    33  			Say(`\s+--wait, -w\s+Wait for the operation to complete\n`),
    34  			Say(`\n`),
    35  			Say(`SEE ALSO:\n`),
    36  			Say(`\s+service-keys\n`),
    37  		)
    38  
    39  		When("the -h flag is specified", func() {
    40  			It("succeeds and prints help", func() {
    41  				session := helpers.CF(command, "-h")
    42  				Eventually(session).Should(Exit(0))
    43  				Expect(session.Out).To(matchHelpMessage)
    44  			})
    45  		})
    46  
    47  		When("the --help flag is specified", func() {
    48  			It("succeeds and prints help", func() {
    49  				session := helpers.CF(command, "--help")
    50  				Eventually(session).Should(Exit(0))
    51  				Expect(session.Out).To(matchHelpMessage)
    52  			})
    53  		})
    54  
    55  		When("no arguments are provided", func() {
    56  			It("displays a warning, the help text, and exits 1", func() {
    57  				session := helpers.CF(command)
    58  				Eventually(session).Should(Exit(1))
    59  				Expect(session.Err).To(Say("Incorrect Usage: the required arguments `SERVICE_INSTANCE` and `SERVICE_KEY` were not provided"))
    60  				Expect(session.Out).To(matchHelpMessage)
    61  			})
    62  		})
    63  
    64  		When("insufficient arguments are provided", func() {
    65  			It("displays a warning, the help text, and exits 1", func() {
    66  				session := helpers.CF(command, "instance")
    67  				Eventually(session).Should(Exit(1))
    68  				Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_KEY` was not provided"))
    69  				Expect(session.Out).To(matchHelpMessage)
    70  			})
    71  		})
    72  
    73  		When("superfluous arguments are provided", func() {
    74  			It("displays a warning, the help text, and exits 1", func() {
    75  				session := helpers.CF(command, "instance", "key", "superfluous")
    76  				Eventually(session).Should(Exit(1))
    77  				Expect(session.Err).To(Say(`Incorrect Usage: unexpected argument "superfluous"`))
    78  				Expect(session.Out).To(matchHelpMessage)
    79  			})
    80  		})
    81  
    82  		When("unknown flag is passed", func() {
    83  			It("displays a warning, the help text, and exits 1", func() {
    84  				session := helpers.CF(command, "-u")
    85  				Eventually(session).Should(Exit(1))
    86  				Expect(session.Err).To(Say("Incorrect Usage: unknown flag `u"))
    87  				Expect(session.Out).To(matchHelpMessage)
    88  			})
    89  		})
    90  	})
    91  
    92  	When("the environment is not setup correctly", func() {
    93  		It("fails with the appropriate errors", func() {
    94  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, command, "foo", "bar")
    95  		})
    96  	})
    97  
    98  	When("the environment is correct", func() {
    99  		var (
   100  			orgName             string
   101  			spaceName           string
   102  			username            string
   103  			broker              *servicebrokerstub.ServiceBrokerStub
   104  			serviceInstanceName string
   105  			serviceKeyName      string
   106  		)
   107  
   108  		BeforeEach(func() {
   109  			orgName = helpers.NewOrgName()
   110  			spaceName = helpers.NewSpaceName()
   111  			helpers.SetupCF(orgName, spaceName)
   112  
   113  			username, _ = helpers.GetCredentials()
   114  
   115  			broker = servicebrokerstub.New().WithRouteService().EnableServiceAccess()
   116  			serviceInstanceName = helpers.NewServiceInstanceName()
   117  			serviceKeyName = helpers.PrefixedRandomName("KEY")
   118  			helpers.CreateManagedServiceInstance(broker.FirstServiceOfferingName(), broker.FirstServicePlanName(), serviceInstanceName)
   119  
   120  			Eventually(helpers.CF("create-service-key", serviceInstanceName, serviceKeyName)).Should(Exit(0))
   121  		})
   122  
   123  		AfterEach(func() {
   124  			helpers.QuickDeleteOrg(orgName)
   125  			broker.Forget()
   126  		})
   127  
   128  		It("deletes the service key", func() {
   129  			session := helpers.CF(command, "-f", serviceInstanceName, serviceKeyName)
   130  			Eventually(session).Should(Exit(0))
   131  
   132  			Expect(session.Out).To(SatisfyAll(
   133  				Say(`Deleting key %s for service instance %s as %s\.\.\.\n`, serviceKeyName, serviceInstanceName, username),
   134  				Say(`OK\n`),
   135  			))
   136  			Expect(string(session.Err.Contents())).To(BeEmpty())
   137  
   138  			Expect(helpers.CF("service-keys", serviceInstanceName).Wait().Out).NotTo(Say(serviceKeyName))
   139  		})
   140  
   141  		When("broker response is asynchronous", func() {
   142  			BeforeEach(func() {
   143  				broker.WithAsyncDelay(time.Second).Configure()
   144  			})
   145  
   146  			It("starts to delete the service key", func() {
   147  				session := helpers.CF(command, "-f", serviceInstanceName, serviceKeyName)
   148  				Eventually(session).Should(Exit(0))
   149  
   150  				Expect(session.Out).To(SatisfyAll(
   151  					Say(`Deleting key %s for service instance %s as %s\.\.\.\n`, serviceKeyName, serviceInstanceName, username),
   152  					Say(`OK\n`),
   153  					Say(`\n`),
   154  					Say(`Delete in progress\.\n`),
   155  				))
   156  				Expect(string(session.Err.Contents())).To(BeEmpty())
   157  
   158  				Expect(helpers.CF("service-keys", serviceInstanceName).Wait().Out).To(Say(serviceKeyName))
   159  			})
   160  
   161  			It("can wait for the service key to be deleted", func() {
   162  				session := helpers.CF(command, "-f", "--wait", serviceInstanceName, serviceKeyName)
   163  				Eventually(session).Should(Exit(0))
   164  
   165  				Expect(session.Out).To(SatisfyAll(
   166  					Say(`Deleting key %s for service instance %s as %s\.\.\.\n`, serviceKeyName, serviceInstanceName, username),
   167  					Say(`Waiting for the operation to complete\.+\n`),
   168  					Say(`\n`),
   169  					Say(`OK\n`),
   170  				))
   171  				Expect(string(session.Err.Contents())).To(BeEmpty())
   172  
   173  				Expect(helpers.CF("service-keys", serviceInstanceName).Wait().Out).NotTo(Say(serviceKeyName))
   174  			})
   175  		})
   176  
   177  		When("key does not exist", func() {
   178  			It("succeeds with a message", func() {
   179  				nonExistentKey := helpers.PrefixedRandomName("NO-SUCH-KEY")
   180  				session := helpers.CF(command, "-f", serviceInstanceName, nonExistentKey)
   181  				Eventually(session).Should(Exit(0))
   182  
   183  				Expect(session.Out).To(SatisfyAll(
   184  					Say(`Deleting key %s for service instance %s as %s\.\.\.\n`, nonExistentKey, serviceInstanceName, username),
   185  					Say(`\n`),
   186  					Say(`Service key %s does not exist for service instance %s\.\n`, nonExistentKey, serviceInstanceName),
   187  					Say(`OK\n`),
   188  				))
   189  				Expect(string(session.Err.Contents())).To(BeEmpty())
   190  			})
   191  		})
   192  	})
   193  })