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

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("create-service-broker command", func() {
    14  	var (
    15  		brokerName string
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		brokerName = helpers.NewServiceBrokerName()
    20  
    21  		helpers.LoginCF()
    22  	})
    23  
    24  	Describe("help", func() {
    25  		When("--help flag is set", func() {
    26  			It("displays command usage to output", func() {
    27  				session := helpers.CF("create-service-broker", "--help")
    28  				Eventually(session).Should(Say("NAME:"))
    29  				Eventually(session).Should(Say("\\s+create-service-broker - Create a service broker"))
    30  
    31  				Eventually(session).Should(Say("USAGE:"))
    32  				Eventually(session).Should(Say("\\s+cf create-service-broker SERVICE_BROKER USERNAME PASSWORD URL \\[--space-scoped\\]"))
    33  
    34  				Eventually(session).Should(Say("ALIAS:"))
    35  				Eventually(session).Should(Say("\\s+csb"))
    36  
    37  				Eventually(session).Should(Say("OPTIONS:"))
    38  				Eventually(session).Should(Say("\\s+--space-scoped      Make the broker's service plans only visible within the targeted space"))
    39  
    40  				Eventually(session).Should(Say("SEE ALSO:"))
    41  				Eventually(session).Should(Say("\\s+enable-service-access, service-brokers, target"))
    42  
    43  				Eventually(session).Should(Exit(0))
    44  			})
    45  		})
    46  	})
    47  
    48  	When("not logged in", func() {
    49  		BeforeEach(func() {
    50  			helpers.LogoutCF()
    51  		})
    52  
    53  		It("displays an informative error that the user must be logged in", func() {
    54  			session := helpers.CF("create-service-broker", brokerName, "user", "pass", "http://example.com")
    55  			Eventually(session).Should(Say("FAILED"))
    56  			Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    57  			Eventually(session).Should(Exit(1))
    58  		})
    59  	})
    60  
    61  	When("logged in", func() {
    62  		When("all arguments are provided", func() {
    63  			var (
    64  				brokerURI string
    65  				orgName   string
    66  				spaceName string
    67  			)
    68  
    69  			BeforeEach(func() {
    70  				orgName = helpers.NewOrgName()
    71  				spaceName = helpers.NewSpaceName()
    72  
    73  				brokerURI, _ = pushServiceBroker(orgName, spaceName)
    74  			})
    75  
    76  			When("no org or space is targeted", func() {
    77  				BeforeEach(func() {
    78  					helpers.ClearTarget()
    79  				})
    80  
    81  				AfterEach(func() {
    82  					Eventually(helpers.CF("delete-service-broker", brokerName, "-f")).Should(Exit(0))
    83  					helpers.QuickDeleteOrg(orgName)
    84  				})
    85  
    86  				It("registers the broker", func() {
    87  					session := helpers.CF("create-service-broker", brokerName, "username", "password", brokerURI)
    88  					Eventually(session).Should(Say("Creating service broker %s as admin...", brokerName))
    89  					Eventually(session).Should(Say("OK"))
    90  					Eventually(session).Should(Exit(0))
    91  
    92  					session = helpers.CF("service-brokers")
    93  					Eventually(session).Should(Say(brokerName))
    94  				})
    95  			})
    96  
    97  			When("the --space-scoped flag is passed", func() {
    98  				When("no org or space is targeted", func() {
    99  					BeforeEach(func() {
   100  						helpers.ClearTarget()
   101  					})
   102  
   103  					It("displays an informative error that a space must be targeted", func() {
   104  						session := helpers.CF("create-service-broker", "space-scoped-broker", "username", "password", "http://example.com", "--space-scoped")
   105  						Eventually(session).Should(Say("FAILED"))
   106  						Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
   107  						Eventually(session).Should(Exit(1))
   108  					})
   109  				})
   110  
   111  				When("both org and space are targeted", func() {
   112  					var (
   113  						brokerURI       string
   114  						orgName         string
   115  						spaceName       string
   116  						servicePlanName string
   117  					)
   118  
   119  					BeforeEach(func() {
   120  						orgName = helpers.NewOrgName()
   121  						spaceName = helpers.NewSpaceName()
   122  						brokerURI, servicePlanName = pushServiceBroker(orgName, spaceName)
   123  
   124  						helpers.TargetOrgAndSpace(orgName, spaceName)
   125  					})
   126  
   127  					AfterEach(func() {
   128  						Eventually(helpers.CF("delete-service-broker", brokerName, "-f")).Should(Exit(0))
   129  						helpers.QuickDeleteOrg(orgName)
   130  					})
   131  
   132  					It("registers the broker and exposes its services only to the targeted space", func() {
   133  						session := helpers.CF("create-service-broker", brokerName, "username", "password", brokerURI, "--space-scoped")
   134  						Eventually(session).Should(Say("Creating service broker " + brokerName + " in org " + orgName + " / space " + spaceName + " as admin..."))
   135  						Eventually(session).Should(Say("OK"))
   136  						Eventually(session).Should(Exit(0))
   137  
   138  						session = helpers.CF("service-brokers")
   139  						Eventually(session).Should(Say(brokerName))
   140  
   141  						session = helpers.CF("marketplace")
   142  						Eventually(session).Should(Say(servicePlanName))
   143  
   144  						helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   145  						session = helpers.CF("marketplace")
   146  						Eventually(session).ShouldNot(Say(servicePlanName))
   147  					})
   148  				})
   149  			})
   150  
   151  			When("no arguments are provided", func() {
   152  				It("displays an error, naming each of the missing args and the help text", func() {
   153  					session := helpers.CF("create-service-broker")
   154  					Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SERVICE_BROKER`, `USERNAME`, `PASSWORD` and `URL` were not provided"))
   155  
   156  					Eventually(session).Should(Say("NAME:"))
   157  					Eventually(session).Should(Say("\\s+create-service-broker - Create a service broker"))
   158  
   159  					Eventually(session).Should(Say("USAGE:"))
   160  					Eventually(session).Should(Say("\\s+cf create-service-broker SERVICE_BROKER USERNAME PASSWORD URL \\[--space-scoped\\]"))
   161  
   162  					Eventually(session).Should(Say("ALIAS:"))
   163  					Eventually(session).Should(Say("\\s+csb"))
   164  
   165  					Eventually(session).Should(Say("OPTIONS:"))
   166  					Eventually(session).Should(Say("\\s+--space-scoped      Make the broker's service plans only visible within the targeted space"))
   167  
   168  					Eventually(session).Should(Say("SEE ALSO:"))
   169  					Eventually(session).Should(Say("\\s+enable-service-access, service-brokers, target"))
   170  
   171  					Eventually(session).Should(Exit(1))
   172  				})
   173  			})
   174  
   175  			When("the broker URL is invalid", func() {
   176  				It("displays a relevant error", func() {
   177  					session := helpers.CF("create-service-broker", brokerName, "user", "pass", "not-a-valid-url")
   178  					Eventually(session).Should(Say("FAILED"))
   179  					Eventually(session.Err).Should(Say("not-a-valid-url is not a valid URL"))
   180  					Eventually(session).Should(Exit(1))
   181  				})
   182  			})
   183  		})
   184  	})
   185  })
   186  
   187  func pushServiceBroker(org, space string) (string, string) {
   188  	helpers.SetupCF(org, space)
   189  
   190  	servicePlanName := helpers.NewPlanName()
   191  	broker := helpers.NewServiceBroker(
   192  		helpers.NewServiceBrokerName(),
   193  		helpers.NewAssets().ServiceBroker,
   194  		helpers.DefaultSharedDomain(),
   195  		helpers.PrefixedRandomName("service"),
   196  		servicePlanName,
   197  	)
   198  	broker.Push()
   199  	broker.Configure(true)
   200  
   201  	return fmt.Sprintf("http://%s.%s", broker.Name, broker.AppsDomain), servicePlanName
   202  }