github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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.MinSupportedV3ClientVersion)
    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.CreateBroker(domain, service, servicePlan)
   128  
   129  				Eventually(helpers.CF("enable-service-access", service)).Should(Exit(0))
   130  				Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   131  			})
   132  
   133  			AfterEach(func() {
   134  				broker.Destroy()
   135  			})
   136  
   137  			When("I want to share my service instance to a space in another org", func() {
   138  				AfterEach(func() {
   139  					Eventually(helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName, "-f")).Should(Exit(0))
   140  				})
   141  
   142  				It("shares the service instance from my targeted space with the share-to org/space", func() {
   143  					username, _ := helpers.GetCredentials()
   144  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   145  					Eventually(session.Out).Should(Say(`Sharing service instance %s into org %s / space %s as %s\.\.\.`, serviceInstance, sharedToOrgName, sharedToSpaceName, username))
   146  					Eventually(session.Out).Should(Say("OK"))
   147  					Eventually(session).Should(Exit(0))
   148  				})
   149  
   150  				When("the service instance is already shared with that space", func() {
   151  					BeforeEach(func() {
   152  						Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)).Should(Exit(0))
   153  					})
   154  
   155  					It("displays a warning and exits 0", func() {
   156  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   157  						Consistently(session.Out).ShouldNot(Say("FAILED"))
   158  						Eventually(session.Out).Should(Say(`Service instance %s is already shared with that space\.`, serviceInstance))
   159  						Eventually(session.Out).Should(Say("OK"))
   160  						Eventually(session).Should(Exit(0))
   161  					})
   162  				})
   163  			})
   164  
   165  			When("I want to share my service instance into another space in my targeted org", func() {
   166  				BeforeEach(func() {
   167  					helpers.CreateSpace(sharedToSpaceName)
   168  				})
   169  
   170  				AfterEach(func() {
   171  					Eventually(helpers.CF("unshare-service", serviceInstance, "-s", sharedToSpaceName, "-f")).Should(Exit(0))
   172  				})
   173  
   174  				It("shares the service instance from my targeted space with the share-to space", func() {
   175  					username, _ := helpers.GetCredentials()
   176  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   177  					Eventually(session.Out).Should(Say(`Sharing service instance %s into org %s / space %s as %s\.\.\.`, serviceInstance, sourceOrgName, sharedToSpaceName, username))
   178  					Eventually(session.Out).Should(Say("OK"))
   179  					Eventually(session).Should(Exit(0))
   180  				})
   181  
   182  				When("the service instance is already shared with that space", func() {
   183  					BeforeEach(func() {
   184  						Eventually(helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)).Should(Exit(0))
   185  					})
   186  
   187  					It("displays a warning and exits 0", func() {
   188  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   189  						Consistently(session.Out).ShouldNot(Say("FAILED"))
   190  						Eventually(session.Out).Should(Say(`Service instance %s is already shared with that space\.`, serviceInstance))
   191  						Eventually(session.Out).Should(Say("OK"))
   192  						Eventually(session).Should(Exit(0))
   193  					})
   194  				})
   195  			})
   196  
   197  			When("the org I want to share into does not exist", func() {
   198  				It("fails with an org not found error", func() {
   199  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", "missing-org")
   200  					Eventually(session).Should(Say("FAILED"))
   201  					Eventually(session.Err).Should(Say("Organization 'missing-org' not found"))
   202  					Eventually(session).Should(Exit(1))
   203  				})
   204  			})
   205  
   206  			When("the space I want to share into does not exist", func() {
   207  				It("fails with a space not found error", func() {
   208  					session := helpers.CF("share-service", serviceInstance, "-s", "missing-space")
   209  					Eventually(session).Should(Say("FAILED"))
   210  					Eventually(session.Err).Should(Say("Space 'missing-space' not found"))
   211  					Eventually(session).Should(Exit(1))
   212  				})
   213  			})
   214  
   215  			When("I am a SpaceAuditor in the space I want to share into", func() {
   216  				var sharedToSpaceGUID string
   217  				BeforeEach(func() {
   218  					user := helpers.NewUsername()
   219  					password := helpers.NewPassword()
   220  					Eventually(helpers.CF("create-user", user, password)).Should(Exit(0))
   221  					Eventually(helpers.CF("set-space-role", user, sourceOrgName, sourceSpaceName, "SpaceDeveloper")).Should(Exit(0))
   222  					Eventually(helpers.CF("set-space-role", user, sharedToOrgName, sharedToSpaceName, "SpaceAuditor")).Should(Exit(0))
   223  					env := map[string]string{
   224  						"CF_USERNAME": user,
   225  						"CF_PASSWORD": password,
   226  					}
   227  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   228  					helpers.TargetOrgAndSpace(sharedToOrgName, sharedToSpaceName)
   229  					sharedToSpaceGUID = helpers.GetSpaceGUID(sharedToSpaceName)
   230  					helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   231  				})
   232  
   233  				AfterEach(func() {
   234  					helpers.SetupCF(sourceOrgName, sourceSpaceName)
   235  				})
   236  
   237  				It("fails with an unauthorized error", func() {
   238  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   239  					Eventually(session).Should(Say("FAILED"))
   240  					Eventually(session.Err).Should(Say(`Unable to share service instance %s with spaces \['%s'\].`, serviceInstance, sharedToSpaceGUID))
   241  					Eventually(session.Err).Should(Say("Write permission is required in order to share a service instance with a space"))
   242  					Eventually(session).Should(Exit(1))
   243  				})
   244  			})
   245  
   246  			When("my targeted space is the same as my share-to space", func() {
   247  				It("fails with a cannot share to self error", func() {
   248  					session := helpers.CF("share-service", serviceInstance, "-s", sourceSpaceName)
   249  					Eventually(session).Should(Say("FAILED"))
   250  					Eventually(session.Err).Should(Say("Service instances cannot be shared into the space where they were created"))
   251  					Eventually(session).Should(Exit(1))
   252  				})
   253  			})
   254  
   255  			When("a service instance with the same name exists in the shared-to space", func() {
   256  				BeforeEach(func() {
   257  					helpers.CreateSpace(sharedToSpaceName)
   258  					helpers.TargetOrgAndSpace(sourceOrgName, sharedToSpaceName)
   259  					Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   260  					helpers.TargetOrgAndSpace(sourceOrgName, sourceSpaceName)
   261  				})
   262  
   263  				It("fails with a name clash error", func() {
   264  					session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   265  					Eventually(session).Should(Say("FAILED"))
   266  					Eventually(session.Err).Should(Say(fmt.Sprintf("A service instance called %s already exists in %s", serviceInstance, sharedToSpaceName)))
   267  					Eventually(session).Should(Exit(1))
   268  				})
   269  			})
   270  
   271  			When("the service instance is NOT shareable", func() {
   272  				Context("due to global settings", func() {
   273  					BeforeEach(func() {
   274  						helpers.DisableFeatureFlag("service_instance_sharing")
   275  					})
   276  
   277  					AfterEach(func() {
   278  						helpers.EnableFeatureFlag("service_instance_sharing")
   279  					})
   280  
   281  					It("should display that the service instance feature flag is disabled and exit 1", func() {
   282  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   283  						Eventually(session.Err).Should(Say(`The "service_instance_sharing" feature flag is disabled for this Cloud Foundry platform.`))
   284  						Eventually(session).Should(Exit(1))
   285  					})
   286  				})
   287  
   288  				Context("due to service broker settings", func() {
   289  					BeforeEach(func() {
   290  						broker.Configure(false)
   291  						broker.Update()
   292  					})
   293  
   294  					It("should display that service instance sharing is disabled for this service and exit 1", func() {
   295  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   296  						Eventually(session.Err).Should(Say("Service instance sharing is disabled for this service."))
   297  						Eventually(session).Should(Exit(1))
   298  					})
   299  				})
   300  
   301  				Context("due to global settings AND service broker settings", func() {
   302  					BeforeEach(func() {
   303  						helpers.DisableFeatureFlag("service_instance_sharing")
   304  						broker.Configure(false)
   305  						broker.Update()
   306  					})
   307  
   308  					AfterEach(func() {
   309  						helpers.EnableFeatureFlag("service_instance_sharing")
   310  					})
   311  
   312  					It("should display that service instance sharing is disabled for this service and exit 1", func() {
   313  						session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   314  						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.`))
   315  						Eventually(session).Should(Exit(1))
   316  					})
   317  				})
   318  			})
   319  		})
   320  
   321  		When("the service instance does not exist", func() {
   322  			It("fails with a service instance not found error", func() {
   323  				session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName)
   324  				Eventually(session).Should(Say("FAILED"))
   325  				Eventually(session.Err).Should(Say("Specified instance not found or not a managed service instance. Sharing is not supported for user provided services."))
   326  				Eventually(session).Should(Exit(1))
   327  			})
   328  		})
   329  
   330  		When("I try to share a user-provided-service", func() {
   331  			BeforeEach(func() {
   332  				helpers.CF("create-user-provided-service", serviceInstance, "-p", `{"username":"admin","password":"pa55woRD"}`)
   333  			})
   334  
   335  			It("fails with only managed services can be shared", func() {
   336  				session := helpers.CF("share-service", serviceInstance, "-s", sharedToSpaceName, "-o", sharedToOrgName)
   337  				Eventually(session).Should(Say("FAILED"))
   338  				Eventually(session.Err).Should(Say("User-provided services cannot be shared"))
   339  				Eventually(session).Should(Exit(1))
   340  			})
   341  		})
   342  	})
   343  })