github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/shared/isolated/unbind_service_command_test.go (about)

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