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

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