github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/global/share_service_command_test.go (about)

     1  package global
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("share-service command", func() {
    17  	var (
    18  		sourceOrgName     string
    19  		sourceSpaceName   string
    20  		sharedToOrgName   string
    21  		sharedToSpaceName string
    22  		serviceInstance   string
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		sourceOrgName = helpers.NewOrgName()
    27  		sourceSpaceName = helpers.NewSpaceName()
    28  		sharedToOrgName = helpers.NewOrgName()
    29  		sharedToSpaceName = helpers.NewSpaceName()
    30  		serviceInstance = helpers.PrefixedRandomName("svc-inst")
    31  
    32  		helpers.LoginCF()
    33  		session := helpers.CF("enable-feature-flag", "service_instance_sharing")
    34  		Eventually(session).Should(Exit(0))
    35  	})
    36  
    37  	Describe("help", func() {
    38  		When("--help flag is set", func() {
    39  			It("Displays command usage to output", func() {
    40  				session := helpers.CF("share-service", "--help")
    41  				Eventually(session).Should(Say("NAME:"))
    42  				Eventually(session).Should(Say("share-service - Share a service instance with another space"))
    43  				Eventually(session).Should(Say("USAGE:"))
    44  				Eventually(session).Should(Say(`cf share-service SERVICE_INSTANCE -s OTHER_SPACE \[-o OTHER_ORG\]`))
    45  				Eventually(session).Should(Say("OPTIONS:"))
    46  				Eventually(session).Should(Say(`-o\s+Org of the other space \(Default: targeted org\)`))
    47  				Eventually(session).Should(Say(`-s\s+Space to share the service instance into`))
    48  				Eventually(session).Should(Say("SEE ALSO:"))
    49  				Eventually(session).Should(Say("bind-service, service, services, unshare-service"))
    50  				Eventually(session).Should(Exit(0))
    51  			})
    52  		})
    53  	})
    54  
    55  	When("the service instance name is not provided", func() {
    56  		It("tells the user that the service instance name is required, prints help text, and exits 1", func() {
    57  			session := helpers.CF("share-service", "-s", sharedToSpaceName)
    58  
    59  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SERVICE_INSTANCE` was not provided"))
    60  			Eventually(session).Should(Say("NAME:"))
    61  			Eventually(session).Should(Exit(1))
    62  		})
    63  	})
    64  
    65  	When("the space name is not provided", func() {
    66  		It("tells the user that the space name is required, prints help text, and exits 1", func() {
    67  			session := helpers.CF("share-service")
    68  
    69  			Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `-s' was not specified"))
    70  			Eventually(session).Should(Say("NAME:"))
    71  			Eventually(session).Should(Exit(1))
    72  		})
    73  	})
    74  
    75  	When("the environment is not setup correctly", func() {
    76  		It("fails with the appropriate errors", func() {
    77  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "share-service", serviceInstance, "-s", sharedToSpaceName)
    78  		})
    79  
    80  		When("the v3 api version is lower than the minimum version", func() {
    81  			var server *Server
    82  
    83  			BeforeEach(func() {
    84  				server = helpers.StartAndTargetMockServerWithAPIVersions(helpers.DefaultV2Version, "3.0.0")
    85  			})
    86  
    87  			AfterEach(func() {
    88  				server.Close()
    89  			})
    90  
    91  			It("fails with error message that the minimum version is not met", func() {
    92  				session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
    93  				Eventually(session).Should(Say("FAILED"))
    94  				Eventually(session.Err).Should(Say(`This command requires CF API version 3\.63\.0 or higher\.`))
    95  				Eventually(session).Should(Exit(1))
    96  			})
    97  		})
    98  	})
    99  
   100  	When("the environment is set up correctly", func() {
   101  		var (
   102  			service     string
   103  			servicePlan string
   104  		)
   105  
   106  		BeforeEach(func() {
   107  			helpers.CreateOrgAndSpace(sharedToOrgName, sharedToSpaceName)
   108  			helpers.SetupCF(sourceOrgName, sourceSpaceName)
   109  		})
   110  
   111  		AfterEach(func() {
   112  			helpers.QuickDeleteOrg(sharedToOrgName)
   113  			helpers.QuickDeleteOrg(sourceOrgName)
   114  		})
   115  
   116  		When("there is a managed service instance in my current targeted space", func() {
   117  			var broker *servicebrokerstub.ServiceBrokerStub
   118  
   119  			BeforeEach(func() {
   120  				broker = servicebrokerstub.EnableServiceAccess()
   121  				service = broker.FirstServiceOfferingName()
   122  				servicePlan = broker.FirstServicePlanName()
   123  
   124  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   125  			})
   126  
   127  			AfterEach(func() {
   128  				broker.Forget()
   129  			})
   130  
   131  			When("I want to share my service instance to a space in another org", func() {
   132  				AfterEach(func() {
   133  					Eventually(helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")).Should(Exit(0))
   134  				})
   135  
   136  				It("shares the service instance from my targeted space with the share-to org/space", func() {
   137  					username, _ := helpers.GetCredentials()
   138  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   139  					Eventually(session.Out).Should(Say(`Sharing service instance %s into org %s / space %s as %s\.\.\.`, serviceInstance, sharedToOrgName, sharedToSpaceName, username))
   140  					Eventually(session.Out).Should(Say("OK"))
   141  					Eventually(session).Should(Exit(0))
   142  				})
   143  
   144  				When("the service instance is already shared with that space", func() {
   145  					BeforeEach(func() {
   146  						Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)).Should(Exit(0))
   147  					})
   148  
   149  					It("displays a warning and exits 0", func() {
   150  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   151  						Consistently(session.Out).ShouldNot(Say("FAILED"))
   152  						Eventually(session.Out).Should(Say(`Service instance %s is already shared with that space\.`, serviceInstance))
   153  						Eventually(session.Out).Should(Say("OK"))
   154  						Eventually(session).Should(Exit(0))
   155  					})
   156  				})
   157  			})
   158  
   159  			When("I want to share my service instance into another space in my targeted org", func() {
   160  				BeforeEach(func() {
   161  					helpers.CreateSpace(sharedToSpaceName)
   162  				})
   163  
   164  				AfterEach(func() {
   165  					Eventually(helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")).Should(Exit(0))
   166  				})
   167  
   168  				It("shares the service instance from my targeted space with the share-to space", func() {
   169  					username, _ := helpers.GetCredentials()
   170  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   171  					Eventually(session.Out).Should(Say(`Sharing service instance %s into org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   172  					Eventually(session.Out).Should(Say("OK"))
   173  					Eventually(session).Should(Exit(0))
   174  				})
   175  
   176  				When("the service instance is already shared with that space", func() {
   177  					BeforeEach(func() {
   178  						Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)).Should(Exit(0))
   179  					})
   180  
   181  					It("displays a warning and exits 0", func() {
   182  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   183  						Consistently(session.Out).ShouldNot(Say("FAILED"))
   184  						Eventually(session.Out).Should(Say(`Service instance %s is already shared with that space\.`, serviceInstance))
   185  						Eventually(session.Out).Should(Say("OK"))
   186  						Eventually(session).Should(Exit(0))
   187  					})
   188  				})
   189  			})
   190  
   191  			When("the org I want to share into does not exist", func() {
   192  				It("fails with an org not found error", func() {
   193  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", "missing-org")
   194  					Eventually(session).Should(Say("FAILED"))
   195  					Eventually(session.Err).Should(Say("Organization 'missing-org' not found"))
   196  					Eventually(session).Should(Exit(1))
   197  				})
   198  			})
   199  
   200  			When("the space I want to share into does not exist", func() {
   201  				It("fails with a space not found error", func() {
   202  					session := helpers.CF("share-service", serviceInstance, "-s", "missing-space")
   203  					Eventually(session).Should(Say("FAILED"))
   204  					Eventually(session.Err).Should(Say("Space 'missing-space' not found"))
   205  					Eventually(session).Should(Exit(1))
   206  				})
   207  			})
   208  
   209  			When("I am a SpaceAuditor in the space I want to share into", func() {
   210  				var sharedToSpaceGUID string
   211  				BeforeEach(func() {
   212  					user := helpers.NewUsername()
   213  					password := helpers.NewPassword()
   214  					Eventually(helpers.CF("create-user", user, password)).Should(Exit(0))
   215  					Eventually(helpers.CF("set-space-role", user, sourceOrgName, sourceSpaceName, "SpaceDeveloper")).Should(Exit(0))
   216  					Eventually(helpers.CF("set-space-role", user, sharedToOrgName, sharedToSpaceName, "SpaceAuditor")).Should(Exit(0))
   217  					env := map[string]string{
   218  						"CF_USERNAME": user,
   219  						"CF_PASSWORD": password,
   220  					}
   221  
   222  					helpers.LogoutCF()
   223  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   224  					helpers.TargetOrgAndSpace(sharedToOrgName, sharedToSpaceName)
   225  					sharedToSpaceGUID = helpers.GetSpaceGUID(sharedToSpaceName)
   226  					helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   227  				})
   228  
   229  				AfterEach(func() {
   230  					helpers.SetupCF(sourceOrgName, sourceSpaceName)
   231  				})
   232  
   233  				It("fails with an unauthorized error", func() {
   234  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   235  					Eventually(session).Should(Say("FAILED"))
   236  					Eventually(session.Err).Should(Say(`Unable to share service instance %s with spaces \['%s'\].`, serviceInstance, sharedToSpaceGUID))
   237  					Eventually(session.Err).Should(Say("Write permission is required in order to share a service instance with a space"))
   238  					Eventually(session).Should(Exit(1))
   239  				})
   240  			})
   241  
   242  			When("my targeted space is the same as my share-to space", func() {
   243  				It("fails with a cannot share to self error", func() {
   244  					session := helpers.CF("share-service", serviceInstance, "-s", sourceSpaceName)
   245  					Eventually(session).Should(Say("FAILED"))
   246  					Eventually(session.Err).Should(Say("Service instances cannot be shared into the space where they were created"))
   247  					Eventually(session).Should(Exit(1))
   248  				})
   249  			})
   250  
   251  			When("a service instance with the same name exists in the shared-to space", func() {
   252  				BeforeEach(func() {
   253  					helpers.CreateSpace(sharedToSpaceName)
   254  					helpers.TargetOrgAndSpace(sourceOrgName, sharedToSpaceName)
   255  					Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   256  					helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   257  				})
   258  
   259  				It("fails with a name clash error", func() {
   260  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   261  					Eventually(session).Should(Say("FAILED"))
   262  					Eventually(session.Err).Should(Say(fmt.Sprintf("A service instance called %s already exists in %s", serviceInstance, sharedToSpaceName)))
   263  					Eventually(session).Should(Exit(1))
   264  				})
   265  			})
   266  
   267  			When("the service instance is NOT shareable", func() {
   268  				Context("due to global settings", func() {
   269  					BeforeEach(func() {
   270  						helpers.DisableFeatureFlag("service_instance_sharing")
   271  					})
   272  
   273  					AfterEach(func() {
   274  						helpers.EnableFeatureFlag("service_instance_sharing")
   275  					})
   276  
   277  					It("should display that the service instance feature flag is disabled and exit 1", func() {
   278  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   279  						Eventually(session.Err).Should(Say(`The "service_instance_sharing" feature flag is disabled for this Cloud Foundry platform.`))
   280  						Eventually(session).Should(Exit(1))
   281  					})
   282  				})
   283  
   284  				Context("due to service broker settings", func() {
   285  					BeforeEach(func() {
   286  						broker.Services[0].Shareable = false
   287  						broker.Configure().Register()
   288  					})
   289  
   290  					It("should display that service instance sharing is disabled for this service and exit 1", func() {
   291  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   292  						Eventually(session.Err).Should(Say("Service instance sharing is disabled for this service."))
   293  						Eventually(session).Should(Exit(1))
   294  					})
   295  				})
   296  
   297  				Context("due to global settings AND service broker settings", func() {
   298  					BeforeEach(func() {
   299  						helpers.DisableFeatureFlag("service_instance_sharing")
   300  						broker.Services[0].Shareable = false
   301  						broker.Configure().Register()
   302  					})
   303  
   304  					AfterEach(func() {
   305  						helpers.EnableFeatureFlag("service_instance_sharing")
   306  					})
   307  
   308  					It("should display that service instance sharing is disabled for this service and exit 1", func() {
   309  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   310  						Eventually(session.Err).Should(Say(`The "service_instance_sharing" feature flag is disabled for this Cloud Foundry platform. Also, service instance sharing is disabled for this service.`))
   311  						Eventually(session).Should(Exit(1))
   312  					})
   313  				})
   314  			})
   315  		})
   316  
   317  		When("the service instance does not exist", func() {
   318  			It("fails with a service instance not found error", func() {
   319  				session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   320  				Eventually(session).Should(Say("FAILED"))
   321  				Eventually(session.Err).Should(Say("Specified instance not found or not a managed service instance. Sharing is not supported for user provided services."))
   322  				Eventually(session).Should(Exit(1))
   323  			})
   324  		})
   325  
   326  		When("I try to share a user-provided-service", func() {
   327  			BeforeEach(func() {
   328  				helpers.CF("create-user-provided-service", serviceInstance, "-p", `{"username":"admin","password":"pa55woRD"}`)
   329  			})
   330  
   331  			It("fails with only managed services can be shared", func() {
   332  				session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   333  				Eventually(session).Should(Say("FAILED"))
   334  				Eventually(session.Err).Should(Say("User-provided services cannot be shared"))
   335  				Eventually(session).Should(Exit(1))
   336  			})
   337  		})
   338  	})
   339  })