github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/shared/isolated/marketplace_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("marketplace command", func() {
    16  	Describe("help", func() {
    17  		When("the --help flag is set", func() {
    18  			It("displays command usage to output", func() {
    19  				session := helpers.CF("marketplace", "--help")
    20  				Eventually(session).Should(Say("NAME:"))
    21  				Eventually(session).Should(Say("marketplace - List available offerings in the marketplace"))
    22  				Eventually(session).Should(Say("USAGE:"))
    23  				Eventually(session).Should(Say("cf marketplace \\[-s SERVICE\\]"))
    24  				Eventually(session).Should(Say("ALIAS:"))
    25  				Eventually(session).Should(Say("m"))
    26  				Eventually(session).Should(Say("OPTIONS:"))
    27  				Eventually(session).Should(Say("-s\\s+Show plan details for a particular service offering"))
    28  				Eventually(session).Should(Say("create-service, services"))
    29  				Eventually(session).Should(Exit(0))
    30  			})
    31  		})
    32  	})
    33  
    34  	When("no flags are passed", func() {
    35  		When("an API target is not set", func() {
    36  			BeforeEach(func() {
    37  				helpers.UnsetAPI()
    38  			})
    39  
    40  			It("displays an error message that no API endpoint is set", func() {
    41  				session := helpers.CF("marketplace")
    42  				Eventually(session).Should(Say("FAILED"))
    43  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    44  				Eventually(session).Should(Exit(1))
    45  			})
    46  		})
    47  
    48  		When("an API endpoint is set", func() {
    49  			When("not logged in", func() {
    50  				When("there are accessible services", func() {
    51  					var (
    52  						broker1 helpers.ServiceBroker
    53  						broker2 helpers.ServiceBroker
    54  						org     string
    55  						space   string
    56  					)
    57  
    58  					BeforeEach(func() {
    59  						org = helpers.NewOrgName()
    60  						space = helpers.NewSpaceName()
    61  						helpers.SetupCF(org, space)
    62  						helpers.TargetOrgAndSpace(org, space)
    63  
    64  						domain := helpers.DefaultSharedDomain()
    65  
    66  						broker1 = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE-1"), "SERVICE-PLAN-1")
    67  						enableServiceAccess(broker1)
    68  						broker2 = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE-2"), "SERVICE-PLAN-2")
    69  						enableServiceAccess(broker2)
    70  
    71  						helpers.LogoutCF()
    72  					})
    73  
    74  					AfterEach(func() {
    75  						helpers.SetupCF(org, space)
    76  						broker1.Destroy()
    77  						broker2.Destroy()
    78  						helpers.QuickDeleteOrg(org)
    79  					})
    80  
    81  					It("displays a table of all available services and a tip", func() {
    82  						session := helpers.CF("marketplace")
    83  						Eventually(session).Should(Say("Getting all services from marketplace"))
    84  						Eventually(session).Should(Say("OK"))
    85  						Eventually(session).Should(Say("\n\n"))
    86  						Eventually(session).Should(Say("service\\s+plans\\s+description"))
    87  						Eventually(session).Should(Say("%s\\s+%s\\s+fake service", getServiceName(broker1), getBrokerPlanNames(broker1)))
    88  						Eventually(session).Should(Say("%s\\s+%s\\s+fake service", getServiceName(broker2), getBrokerPlanNames(broker2)))
    89  						Eventually(session).Should(Say("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."))
    90  						Eventually(session).Should(Exit(0))
    91  					})
    92  				})
    93  			})
    94  
    95  			When("logged in", func() {
    96  				var user string
    97  
    98  				BeforeEach(func() {
    99  					helpers.LoginCF()
   100  					user, _ = helpers.GetCredentials()
   101  				})
   102  
   103  				When("no space is targeted", func() {
   104  					BeforeEach(func() {
   105  						helpers.TargetOrg(ReadOnlyOrg)
   106  					})
   107  
   108  					It("displays an error that a space must be targeted", func() {
   109  						session := helpers.CF("marketplace")
   110  						Eventually(session).Should(Say("FAILED"))
   111  						Eventually(session.Err).Should(Say("Cannot list marketplace services without a targeted space"))
   112  						Eventually(session).Should(Exit(1))
   113  					})
   114  				})
   115  
   116  				When("a service is accessible but not in the currently targeted org", func() {
   117  					var (
   118  						broker1      helpers.ServiceBroker
   119  						org1, space1 string
   120  
   121  						broker2      helpers.ServiceBroker
   122  						org2, space2 string
   123  					)
   124  
   125  					BeforeEach(func() {
   126  						org1 = helpers.NewOrgName()
   127  						space1 = helpers.NewSpaceName()
   128  						helpers.SetupCF(org1, space1)
   129  						helpers.TargetOrgAndSpace(org1, space1)
   130  
   131  						domain := helpers.DefaultSharedDomain()
   132  
   133  						broker1 = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE-1"), "SERVICE-PLAN-1")
   134  						enableServiceAccessForOrg(broker1, org1)
   135  
   136  						org2 = helpers.NewOrgName()
   137  						space2 = helpers.NewSpaceName()
   138  						helpers.CreateOrgAndSpace(org2, space2)
   139  						helpers.TargetOrgAndSpace(org2, space2)
   140  
   141  						broker2 = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE-2"), "SERVICE-PLAN-2")
   142  						enableServiceAccess(broker2)
   143  					})
   144  
   145  					AfterEach(func() {
   146  						helpers.TargetOrgAndSpace(org2, space2)
   147  						broker2.Destroy()
   148  						helpers.QuickDeleteOrg(org2)
   149  
   150  						helpers.TargetOrgAndSpace(org1, space1)
   151  						broker1.Destroy()
   152  						helpers.QuickDeleteOrg(org1)
   153  					})
   154  
   155  					When("CC API does not return broker names in response", func() {
   156  						BeforeEach(func() {
   157  							helpers.SkipIfVersionAtLeast(ccversion.MinVersionMultiServiceRegistrationV2)
   158  						})
   159  
   160  						It("displays a table and tip that does not include that service", func() {
   161  							session := helpers.CF("marketplace")
   162  							Eventually(session).Should(Say("Getting services from marketplace in org %s / space %s as %s\\.\\.\\.", org2, space2, user))
   163  							Eventually(session).Should(Say("OK"))
   164  							Eventually(session).Should(Say("\n\n"))
   165  							Eventually(session).Should(Say("service\\s+plans\\s+description\\s+broker"))
   166  							Consistently(session).ShouldNot(Say(getServiceName(broker1)))
   167  							Consistently(session).ShouldNot(Say(getBrokerPlanNames(broker1)))
   168  							Eventually(session).Should(Say("%s\\s+%s\\s+fake service\\s*", getServiceName(broker2), getBrokerPlanNames(broker2)))
   169  							Eventually(session).Should(Say("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."))
   170  							Eventually(session).Should(Exit(0))
   171  						})
   172  					})
   173  
   174  					When("CC API returns broker names in response", func() {
   175  						BeforeEach(func() {
   176  							helpers.SkipIfVersionLessThan(ccversion.MinVersionMultiServiceRegistrationV2)
   177  						})
   178  
   179  						It("displays a table with broker name", func() {
   180  							session := helpers.CF("marketplace")
   181  							Eventually(session).Should(Say("Getting services from marketplace in org %s / space %s as %s\\.\\.\\.", org2, space2, user))
   182  							Eventually(session).Should(Say("OK"))
   183  							Eventually(session).Should(Say("\n\n"))
   184  							Eventually(session).Should(Say("service\\s+plans\\s+description\\s+broker"))
   185  							Consistently(session).ShouldNot(Say(getServiceName(broker1)))
   186  							Consistently(session).ShouldNot(Say(getBrokerPlanNames(broker1)))
   187  							Consistently(session).ShouldNot(Say(broker1.Name))
   188  							Eventually(session).Should(Say("%s\\s+%s\\s+fake service\\s+%s", getServiceName(broker2), getBrokerPlanNames(broker2), broker2.Name))
   189  							Eventually(session).Should(Say("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service."))
   190  							Eventually(session).Should(Exit(0))
   191  						})
   192  					})
   193  				})
   194  			})
   195  		})
   196  
   197  		When("the -s flag is passed", func() {
   198  			When("an api endpoint is not set", func() {
   199  				BeforeEach(func() {
   200  					helpers.UnsetAPI()
   201  				})
   202  
   203  				It("displays an error message that no API endpoint is set", func() {
   204  					session := helpers.CF("marketplace")
   205  					Eventually(session).Should(Say("FAILED"))
   206  					Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
   207  					Eventually(session).Should(Exit(1))
   208  				})
   209  			})
   210  
   211  			When("the api is set", func() {
   212  				When("not logged in", func() {
   213  					BeforeEach(func() {
   214  						helpers.LogoutCF()
   215  					})
   216  
   217  					When("the specified service does not exist", func() {
   218  						It("displays an error that the service doesn't exist", func() {
   219  							session := helpers.CF("marketplace", "-s", "not-found-service")
   220  							Eventually(session).Should(Say("Getting service plan information for service not-found-service\\.\\.\\."))
   221  							Eventually(session).Should(Say("FAILED"))
   222  							Eventually(session.Err).Should(Say("Service offering 'not-found-service' not found"))
   223  							Eventually(session).Should(Exit(1))
   224  						})
   225  					})
   226  
   227  					When("the specified service exists", func() {
   228  						var (
   229  							broker helpers.ServiceBroker
   230  							org    string
   231  							space  string
   232  						)
   233  
   234  						BeforeEach(func() {
   235  							org = helpers.NewOrgName()
   236  							space = helpers.NewSpaceName()
   237  							helpers.SetupCF(org, space)
   238  							helpers.TargetOrgAndSpace(org, space)
   239  
   240  							domain := helpers.DefaultSharedDomain()
   241  
   242  							broker = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE"), "SERVICE-PLAN")
   243  							enableServiceAccess(broker)
   244  
   245  							helpers.LogoutCF()
   246  						})
   247  
   248  						AfterEach(func() {
   249  							helpers.LoginCF()
   250  							helpers.TargetOrgAndSpace(org, space)
   251  							broker.Destroy()
   252  							helpers.QuickDeleteOrg(org)
   253  						})
   254  
   255  						It("displays extended information about the service", func() {
   256  							description := "Shared fake Server, 5tb persistent disk, 40 max concurrent connections"
   257  							session := helpers.CF("marketplace", "-s", getServiceName(broker))
   258  							Eventually(session).Should(Say("Getting service plan information for service %s\\.\\.\\.", getServiceName(broker)))
   259  							Eventually(session).Should(Say("OK"))
   260  							Eventually(session).Should(Say("\n\n"))
   261  							Eventually(session).Should(Say("service plan\\s+description\\s+free or paid"))
   262  							Eventually(session).Should(Say("%s\\s+%s\\s+%s", getPlanName(broker), description, "free"))
   263  							Eventually(session).Should(Exit(0))
   264  						})
   265  					})
   266  				})
   267  
   268  				When("logged in", func() {
   269  					var user string
   270  
   271  					BeforeEach(func() {
   272  						helpers.LoginCF()
   273  						user, _ = helpers.GetCredentials()
   274  					})
   275  
   276  					When("no space is targeted", func() {
   277  						BeforeEach(func() {
   278  							helpers.TargetOrg(ReadOnlyOrg)
   279  						})
   280  
   281  						It("displays an error that a space must be targeted", func() {
   282  							session := helpers.CF("marketplace", "-s", "service")
   283  							Eventually(session).Should(Say("FAILED"))
   284  							Eventually(session.Err).Should(Say("Cannot list plan information for service without a targeted space"))
   285  							Eventually(session).Should(Exit(1))
   286  						})
   287  					})
   288  
   289  					When("a space is targeted", func() {
   290  						BeforeEach(func() {
   291  							helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   292  						})
   293  
   294  						When("the specified service does not exist", func() {
   295  							It("displays an error that the service doesn't exist", func() {
   296  								session := helpers.CF("marketplace", "-s", "not-found-service")
   297  								Eventually(session).Should(Say("Getting service plan information for service not-found-service as %s\\.\\.\\.", user))
   298  								Eventually(session).Should(Say("FAILED"))
   299  								Eventually(session.Err).Should(Say("Service offering 'not-found-service' not found"))
   300  								Eventually(session).Should(Exit(1))
   301  							})
   302  						})
   303  
   304  						When("the specified service exists", func() {
   305  							var (
   306  								broker helpers.ServiceBroker
   307  								org    string
   308  								space  string
   309  							)
   310  
   311  							BeforeEach(func() {
   312  								org = helpers.NewOrgName()
   313  								space = helpers.NewSpaceName()
   314  								helpers.SetupCF(org, space)
   315  								helpers.TargetOrgAndSpace(org, space)
   316  
   317  								domain := helpers.DefaultSharedDomain()
   318  
   319  								broker = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE"), "SERVICE-PLAN")
   320  								enableServiceAccess(broker)
   321  							})
   322  
   323  							AfterEach(func() {
   324  								broker.Destroy()
   325  								helpers.QuickDeleteOrg(org)
   326  							})
   327  
   328  							It("displays extended information about the service", func() {
   329  								description := "Shared fake Server, 5tb persistent disk, 40 max concurrent connections"
   330  								session := helpers.CF("marketplace", "-s", getServiceName(broker))
   331  								Eventually(session).Should(Say("Getting service plan information for service %s as %s\\.\\.\\.", getServiceName(broker), user))
   332  								Eventually(session).Should(Say("OK"))
   333  								Eventually(session).Should(Say("\n\n"))
   334  								Eventually(session).Should(Say("service plan\\s+description\\s+free or paid"))
   335  								Eventually(session).Should(Say("%s\\s+%s\\s+%s", getPlanName(broker), description, "free"))
   336  								Eventually(session).Should(Exit(0))
   337  							})
   338  						})
   339  
   340  						When("the specified service is accessible but not in the targeted space", func() {
   341  							var (
   342  								broker helpers.ServiceBroker
   343  								org    string
   344  								space  string
   345  							)
   346  
   347  							BeforeEach(func() {
   348  								org = helpers.NewOrgName()
   349  								space = helpers.NewSpaceName()
   350  								helpers.SetupCF(org, space)
   351  								helpers.TargetOrgAndSpace(org, space)
   352  
   353  								domain := helpers.DefaultSharedDomain()
   354  
   355  								broker = helpers.CreateBroker(domain, helpers.PrefixedRandomName("SERVICE"), "SERVICE-PLAN")
   356  								enableServiceAccessForOrg(broker, org)
   357  
   358  								helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   359  							})
   360  
   361  							AfterEach(func() {
   362  								helpers.TargetOrgAndSpace(org, space)
   363  								broker.Destroy()
   364  								helpers.QuickDeleteOrg(org)
   365  							})
   366  
   367  							It("displays an error that the service doesn't exist", func() {
   368  								session := helpers.CF("marketplace", "-s", getServiceName(broker))
   369  								Eventually(session).Should(Say("Getting service plan information for service %s as %s\\.\\.\\.", getServiceName(broker), user))
   370  								Eventually(session).Should(Say("FAILED"))
   371  								Eventually(session.Err).Should(Say("Service offering '%s' not found", getServiceName(broker)))
   372  								Eventually(session).Should(Exit(1))
   373  							})
   374  						})
   375  					})
   376  				})
   377  			})
   378  		})
   379  	})
   380  })
   381  
   382  func enableServiceAccess(broker helpers.ServiceBroker) {
   383  	Eventually(helpers.CF("enable-service-access", getServiceName(broker))).Should(Exit(0))
   384  }
   385  
   386  func enableServiceAccessForOrg(broker helpers.ServiceBroker, orgName string) {
   387  	Eventually(helpers.CF("enable-service-access", getServiceName(broker), "-o", orgName)).Should(Exit(0))
   388  }
   389  
   390  func getServiceName(broker helpers.ServiceBroker) string {
   391  	return broker.Service.Name
   392  }
   393  
   394  func getPlanName(broker helpers.ServiceBroker) string {
   395  	return broker.SyncPlans[0].Name
   396  }
   397  
   398  func getBrokerPlanNames(broker helpers.ServiceBroker) string {
   399  	return strings.Join(plansToNames(append(broker.SyncPlans, broker.AsyncPlans...)), ", ")
   400  }
   401  
   402  func plansToNames(plans []helpers.Plan) []string {
   403  	planNames := []string{}
   404  	for _, plan := range plans {
   405  		planNames = append(planNames, plan.Name)
   406  	}
   407  	return planNames
   408  }