github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/unshare_service_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gbytes"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("unshare-service command", func() {
    13  	const unshareServiceCommand = "unshare-service"
    14  
    15  	var (
    16  		serviceInstanceName  string
    17  		unshareFromSpaceName string
    18  		unshareFromOrgName   string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		unshareFromSpaceName = "fake-space-name"
    23  		unshareFromOrgName = "fake-org-name"
    24  		serviceInstanceName = "fake-service-instance-name"
    25  	})
    26  
    27  	Describe("help", func() {
    28  		const serviceInstanceName = "fake-service-instance-name"
    29  
    30  		matchHelpMessage := SatisfyAll(
    31  			Say("NAME:"),
    32  			Say("unshare-service - Unshare a shared service instance from a space"),
    33  			Say("USAGE:"),
    34  			Say(`cf unshare-service SERVICE_INSTANCE -s OTHER_SPACE \[-o OTHER_ORG\] \[-f\]`),
    35  			Say("OPTIONS:"),
    36  			Say(`-s\s+Space to unshare the service instance from`),
    37  			Say(`-o\s+Org of the other space \(Default: targeted org\)`),
    38  			Say(`-f\s+Force unshare without confirmation`),
    39  			Say("SEE ALSO:"),
    40  			Say("delete-service, service, services, share-service, unbind-service"),
    41  		)
    42  
    43  		When("the -h flag is specified", func() {
    44  			It("succeeds and prints help", func() {
    45  				session := helpers.CF(unshareServiceCommand, "-h")
    46  				Eventually(session).Should(Exit(0))
    47  				Expect(session.Out).To(matchHelpMessage)
    48  
    49  			})
    50  		})
    51  
    52  		When("the service instance name is missing", func() {
    53  			It("fails with an error and prints help", func() {
    54  				session := helpers.CF(unshareServiceCommand, "-s", unshareFromSpaceName)
    55  				Eventually(session).Should(Exit(1))
    56  				Expect(session.Err).To(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided"))
    57  				Expect(session.Out).To(matchHelpMessage)
    58  			})
    59  		})
    60  
    61  		When("the space name is missing", func() {
    62  			It("fails with an error and prints help", func() {
    63  				session := helpers.CF(unshareServiceCommand, serviceInstanceName)
    64  				Eventually(session).Should(Exit(1))
    65  				Expect(session.Err).To(Say("Incorrect Usage: the required flag `-s' was not specified"))
    66  				Expect(session.Out).To(matchHelpMessage)
    67  			})
    68  		})
    69  
    70  		When("the org name is missing for the flag", func() {
    71  			It("fails with an error and prints help", func() {
    72  				session := helpers.CF(unshareServiceCommand, serviceInstanceName, "-s", unshareFromSpaceName, "-o")
    73  				Eventually(session).Should(Exit(1))
    74  				Expect(session.Err).To(Say("Incorrect Usage: expected argument for flag `-o"))
    75  				Expect(session.Out).To(matchHelpMessage)
    76  			})
    77  		})
    78  
    79  		When("an extra parameter is specified", func() {
    80  			It("fails with an error and prints help", func() {
    81  				session := helpers.CF(unshareServiceCommand, serviceInstanceName, "-s", unshareFromSpaceName, "anotherRandomParameter")
    82  				Eventually(session).Should(Exit(1))
    83  				Expect(session.Err).To(Say(`Incorrect Usage: unexpected argument "anotherRandomParameter"`))
    84  				Expect(session.Out).To(SatisfyAll(
    85  					Say(`FAILED\n\n`),
    86  					matchHelpMessage,
    87  				))
    88  			})
    89  		})
    90  
    91  		When("an extra flag is specified", func() {
    92  			It("fails with an error and prints help", func() {
    93  				session := helpers.CF(unshareServiceCommand, serviceInstanceName, "-s", unshareFromSpaceName, "--anotherRandomFlag")
    94  				Eventually(session).Should(Exit(1))
    95  				Expect(session.Err).To(Say("Incorrect Usage: unknown flag `anotherRandomFlag'"))
    96  				Expect(session.Out).To(matchHelpMessage)
    97  			})
    98  		})
    99  	})
   100  
   101  	When("the environment is not setup correctly", func() {
   102  
   103  		It("fails with the appropriate errors", func() {
   104  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, unshareServiceCommand, serviceInstanceName, "-s", unshareFromSpaceName)
   105  		})
   106  	})
   107  
   108  	Context("unshare-service command is valid", func() {
   109  		var (
   110  			orgName  string
   111  			username string
   112  		)
   113  
   114  		BeforeEach(func() {
   115  			orgName = helpers.NewOrgName()
   116  			spaceName := helpers.NewSpaceName()
   117  			helpers.SetupCF(orgName, spaceName)
   118  
   119  			username, _ = helpers.GetCredentials()
   120  		})
   121  
   122  		AfterEach(func() {
   123  			helpers.QuickDeleteOrg(orgName)
   124  		})
   125  
   126  		Describe("command parameters are invalid", func() {
   127  			Context("service instance cannot be retrieved", func() {
   128  				It("fails with an error", func() {
   129  					session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName)
   130  					Eventually(session).Should(Exit(1))
   131  					Expect(session.Out).To(SatisfyAll(
   132  						Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, orgName, unshareFromSpaceName, username),
   133  						Say("FAILED"),
   134  					))
   135  					Expect(session.Err).To(Say("Service instance '%s' not found", serviceInstanceName))
   136  				})
   137  			})
   138  
   139  			Context("service instance exists", func() {
   140  				var broker *servicebrokerstub.ServiceBrokerStub
   141  
   142  				BeforeEach(func() {
   143  					broker = servicebrokerstub.New().Create().EnableServiceAccess()
   144  
   145  					serviceInstanceName = helpers.NewServiceInstanceName()
   146  					helpers.CreateManagedServiceInstance(
   147  						broker.FirstServiceOfferingName(),
   148  						broker.FirstServicePlanName(),
   149  						serviceInstanceName,
   150  					)
   151  
   152  					unshareFromSpaceName = helpers.NewSpaceName()
   153  					unshareFromOrgName = helpers.NewOrgName()
   154  				})
   155  
   156  				AfterEach(func() {
   157  					broker.Forget()
   158  				})
   159  
   160  				Context("space cannot be retrieved in targeted org", func() {
   161  					It("fails with an error", func() {
   162  						session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName)
   163  						Eventually(session).Should(Exit(1))
   164  						Expect(session.Out).To(SatisfyAll(
   165  							Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, orgName, unshareFromSpaceName, username),
   166  							Say("FAILED"),
   167  						))
   168  						Eventually(session.Err).Should(Say("Space '%s' not found.", unshareFromSpaceName))
   169  					})
   170  				})
   171  
   172  				Context("space cannot be retrieved in specified org", func() {
   173  					BeforeEach(func() {
   174  						helpers.CreateOrg(unshareFromOrgName)
   175  					})
   176  
   177  					AfterEach(func() {
   178  						helpers.QuickDeleteOrg(unshareFromOrgName)
   179  					})
   180  
   181  					It("fails with an error", func() {
   182  						session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName, "-o", unshareFromOrgName)
   183  						Eventually(session).Should(Exit(1))
   184  						Expect(session.Out).To(SatisfyAll(
   185  							Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, unshareFromOrgName, unshareFromSpaceName, username),
   186  							Say("FAILED"),
   187  						))
   188  						Eventually(session.Err).Should(Say("Space '%s' not found.", unshareFromSpaceName))
   189  					})
   190  				})
   191  
   192  				Context("specified organization cannot be retrieved", func() {
   193  					It("fails with an error", func() {
   194  						session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName, "-o", unshareFromOrgName)
   195  						Eventually(session).Should(Exit(1))
   196  						Expect(session.Out).To(SatisfyAll(
   197  							Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, unshareFromOrgName, unshareFromSpaceName, username),
   198  							Say("FAILED"),
   199  						))
   200  						Eventually(session.Err).Should(Say("Organization '%s' not found.", unshareFromOrgName))
   201  					})
   202  				})
   203  			})
   204  		})
   205  
   206  		Describe("successful unsharing of the service instance", func() {
   207  			var broker *servicebrokerstub.ServiceBrokerStub
   208  
   209  			BeforeEach(func() {
   210  				broker = servicebrokerstub.New().Create().EnableServiceAccess()
   211  
   212  				serviceInstanceName = helpers.NewServiceInstanceName()
   213  				helpers.CreateManagedServiceInstance(
   214  					broker.FirstServiceOfferingName(),
   215  					broker.FirstServicePlanName(),
   216  					serviceInstanceName,
   217  				)
   218  			})
   219  
   220  			AfterEach(func() {
   221  				broker.Forget()
   222  			})
   223  
   224  			When("space is in targeted org", func() {
   225  				BeforeEach(func() {
   226  					unshareFromSpaceName = helpers.NewSpaceName()
   227  					helpers.CreateSpace(unshareFromSpaceName)
   228  
   229  					session := helpers.CF("share-service", serviceInstanceName, "-s", unshareFromSpaceName)
   230  					Eventually(session).Should(Exit(0))
   231  				})
   232  
   233  				AfterEach(func() {
   234  					helpers.QuickDeleteSpace(unshareFromSpaceName)
   235  				})
   236  
   237  				It("shares the service to space in targeted org", func() {
   238  					session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName)
   239  
   240  					Eventually(session).Should(Exit(0))
   241  					Expect(session.Out).To(SatisfyAll(
   242  						Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, orgName, unshareFromSpaceName, username),
   243  						Say("OK"),
   244  					))
   245  
   246  					By("validating the service is unshared", func() {
   247  						session = helpers.CF("service", serviceInstanceName)
   248  						Eventually(session).Should(Exit(0))
   249  						Expect(session.Out).To(Say(`This service instance is not currently being shared.`))
   250  					})
   251  				})
   252  			})
   253  
   254  			When("the space to share is in specified org", func() {
   255  				BeforeEach(func() {
   256  					unshareFromOrgName = helpers.NewOrgName()
   257  					helpers.CreateOrg(unshareFromOrgName)
   258  
   259  					unshareFromSpaceName = helpers.NewSpaceName()
   260  					helpers.CreateSpaceInOrg(unshareFromSpaceName, unshareFromOrgName)
   261  
   262  					session := helpers.CF("share-service", serviceInstanceName, "-s", unshareFromSpaceName, "-o", unshareFromOrgName)
   263  					Eventually(session).Should(Exit(0))
   264  				})
   265  
   266  				AfterEach(func() {
   267  					helpers.QuickDeleteOrg(unshareFromOrgName)
   268  				})
   269  
   270  				It("shares the service to space in specified org", func() {
   271  					session := helpers.CF(unshareServiceCommand, "-f", serviceInstanceName, "-s", unshareFromSpaceName, "-o", unshareFromOrgName)
   272  
   273  					Eventually(session).Should(Exit(0))
   274  					Expect(session.Out).To(SatisfyAll(
   275  						Say("Unsharing service instance %s from org %s / space %s as %s...", serviceInstanceName, unshareFromOrgName, unshareFromSpaceName, username),
   276  						Say("OK"),
   277  					))
   278  
   279  					By("validating the service is unshared", func() {
   280  						session = helpers.CF("service", serviceInstanceName)
   281  						Eventually(session).Should(Exit(0))
   282  						Expect(session.Out).To(Say(`This service instance is not currently being shared.`))
   283  					})
   284  				})
   285  			})
   286  		})
   287  	})
   288  })