github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/global/share_service_command_test.go (about)

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