github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/isolated/marketplace_command_test.go (about)

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