github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/unbind_service_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     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("unbind-service command", func() {
    16  	var (
    17  		serviceInstance string
    18  		appName         string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		serviceInstance = helpers.PrefixedRandomName("si")
    23  		appName = helpers.PrefixedRandomName("app")
    24  	})
    25  
    26  	When("the environment is not setup correctly", func() {
    27  		It("fails with the appropriate errors", func() {
    28  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "unbind-service", "app-name", "service-name")
    29  		})
    30  	})
    31  
    32  	When("the environment is setup correctly", func() {
    33  		var (
    34  			org   string
    35  			space string
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			org = helpers.NewOrgName()
    40  			space = helpers.NewSpaceName()
    41  			helpers.SetupCF(org, space)
    42  		})
    43  
    44  		AfterEach(func() {
    45  			helpers.QuickDeleteOrg(org)
    46  		})
    47  
    48  		When("the service is provided by a user", func() {
    49  			BeforeEach(func() {
    50  				Eventually(helpers.CF("create-user-provided-service", serviceInstance, "-p", "{}")).Should(Exit(0))
    51  			})
    52  
    53  			AfterEach(func() {
    54  				Eventually(helpers.CF("delete-service", serviceInstance, "-f")).Should(Exit(0))
    55  			})
    56  
    57  			When("the service is bound to an app", func() {
    58  				BeforeEach(func() {
    59  					helpers.WithHelloWorldApp(func(appDir string) {
    60  						Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
    61  					})
    62  					Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0))
    63  				})
    64  
    65  				It("unbinds the service", func() {
    66  					Eventually(helpers.CF("services")).Should(SatisfyAll(
    67  						Exit(0),
    68  						Say("%s.*%s", serviceInstance, appName)),
    69  					)
    70  					Eventually(helpers.CF("unbind-service", appName, serviceInstance)).Should(Exit(0))
    71  					Eventually(helpers.CF("services")).Should(SatisfyAll(
    72  						Exit(0),
    73  						Not(Say("%s.*%s", serviceInstance, appName)),
    74  					))
    75  				})
    76  			})
    77  
    78  			When("the service is not bound to an app", func() {
    79  				BeforeEach(func() {
    80  					helpers.WithHelloWorldApp(func(appDir string) {
    81  						Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "--no-route")).Should(Exit(0))
    82  					})
    83  				})
    84  
    85  				It("returns a warning and continues", func() {
    86  					session := helpers.CF("unbind-service", appName, serviceInstance)
    87  					Eventually(session).Should(Say("OK"))
    88  					Eventually(session.Err).Should(Say("Binding between %s and %s did not exist", serviceInstance, appName))
    89  					Eventually(session).Should(Exit(0))
    90  				})
    91  			})
    92  
    93  			When("the service does not exist", func() {
    94  				BeforeEach(func() {
    95  					helpers.WithHelloWorldApp(func(appDir string) {
    96  						Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
    97  					})
    98  				})
    99  
   100  				It("fails to unbind the service", func() {
   101  					session := helpers.CF("unbind-service", appName, "does-not-exist")
   102  					Eventually(session).Should(Say("FAILED"))
   103  					Eventually(session.Err).Should(Say("Service instance %s not found", "does-not-exist"))
   104  					Eventually(session).Should(Exit(1))
   105  				})
   106  			})
   107  
   108  			When("the app does not exist", func() {
   109  				It("fails to unbind the service", func() {
   110  					session := helpers.CF("unbind-service", "does-not-exist", serviceInstance)
   111  					Eventually(session).Should(Say("FAILED"))
   112  					Eventually(session.Err).Should(Say("App '%s' not found", "does-not-exist"))
   113  					Eventually(session).Should(Exit(1))
   114  				})
   115  			})
   116  		})
   117  
   118  		When("the service is provided by a broker", func() {
   119  			var (
   120  				service     string
   121  				servicePlan string
   122  				broker      *servicebrokerstub.ServiceBrokerStub
   123  			)
   124  
   125  			When("the unbinding is asynchronous", func() {
   126  				BeforeEach(func() {
   127  					broker = servicebrokerstub.New().WithAsyncDelay(time.Millisecond).EnableServiceAccess()
   128  					service = broker.FirstServiceOfferingName()
   129  					servicePlan = broker.FirstServicePlanName()
   130  
   131  					Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   132  					helpers.WithHelloWorldApp(func(appDir string) {
   133  						Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   134  					})
   135  
   136  					Eventually(func() *Session {
   137  						session := helpers.CF("service", serviceInstance)
   138  						return session.Wait()
   139  					}, time.Minute*5, time.Second*5).Should(Say("create succeeded"))
   140  
   141  					Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0))
   142  					client, err := helpers.CreateCCV2Client()
   143  					Expect(err).ToNot(HaveOccurred())
   144  					helpers.PollLastOperationUntilSuccess(client, appName, serviceInstance)
   145  				})
   146  
   147  				AfterEach(func() {
   148  					broker.Forget()
   149  				})
   150  
   151  				It("returns that the unbind is in progress", func() {
   152  					session := helpers.CF("unbind-service", appName, serviceInstance)
   153  					Eventually(session).Should(Say("OK"))
   154  					Eventually(session).Should(Say("Unbinding in progress. Use 'cf service %s' to check operation status.", serviceInstance))
   155  					Eventually(session).Should(Exit(0))
   156  				})
   157  			})
   158  
   159  			When("the unbinding is blocking", func() {
   160  				BeforeEach(func() {
   161  					broker = servicebrokerstub.EnableServiceAccess()
   162  					service = broker.FirstServiceOfferingName()
   163  					servicePlan = broker.FirstServicePlanName()
   164  				})
   165  
   166  				AfterEach(func() {
   167  					broker.Forget()
   168  				})
   169  
   170  				When("the service is bound to an app", func() {
   171  					BeforeEach(func() {
   172  						Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   173  						helpers.WithHelloWorldApp(func(appDir string) {
   174  							Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   175  						})
   176  						Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0))
   177  					})
   178  
   179  					It("unbinds the service", func() {
   180  						Eventually(helpers.CF("services")).Should(SatisfyAll(
   181  							Exit(0),
   182  							Say("%s.*%s", serviceInstance, appName)),
   183  						)
   184  						Eventually(helpers.CF("unbind-service", appName, serviceInstance)).Should(Exit(0))
   185  						Eventually(helpers.CF("services")).Should(SatisfyAll(
   186  							Exit(0),
   187  							Not(Say("%s.*%s", serviceInstance, appName)),
   188  						))
   189  					})
   190  				})
   191  
   192  				When("the service is not bound to an app", func() {
   193  					BeforeEach(func() {
   194  						Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   195  						helpers.WithHelloWorldApp(func(appDir string) {
   196  							Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "--no-route")).Should(Exit(0))
   197  						})
   198  					})
   199  
   200  					It("returns a warning and continues", func() {
   201  						session := helpers.CF("unbind-service", appName, serviceInstance)
   202  						Eventually(session).Should(Say("OK"))
   203  						Eventually(session.Err).Should(Say("Binding between %s and %s did not exist", serviceInstance, appName))
   204  						Eventually(session).Should(Exit(0))
   205  					})
   206  				})
   207  
   208  				When("the service does not exist", func() {
   209  					BeforeEach(func() {
   210  						helpers.WithHelloWorldApp(func(appDir string) {
   211  							Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   212  						})
   213  					})
   214  
   215  					It("fails to unbind the service", func() {
   216  						session := helpers.CF("unbind-service", appName, serviceInstance)
   217  						Eventually(session).Should(Say("FAILED"))
   218  						Eventually(session.Err).Should(Say("Service instance %s not found", serviceInstance))
   219  						Eventually(session).Should(Exit(1))
   220  					})
   221  				})
   222  
   223  				When("the app does not exist", func() {
   224  					BeforeEach(func() {
   225  						Eventually(helpers.CF("create-service", service, servicePlan, serviceInstance)).Should(Exit(0))
   226  					})
   227  
   228  					It("fails to unbind the service", func() {
   229  						session := helpers.CF("unbind-service", appName, serviceInstance)
   230  						Eventually(session).Should(Say("FAILED"))
   231  						Eventually(session.Err).Should(Say("App '%s' not found", appName))
   232  						Eventually(session).Should(Exit(1))
   233  					})
   234  				})
   235  			})
   236  		})
   237  	})
   238  })