github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/service_broker_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Service Broker Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    24  	})
    25  
    26  	Describe("GetServiceBrokers", func() {
    27  		var (
    28  			serviceBrokers []resources.ServiceBroker
    29  			warnings       Warnings
    30  			executionError error
    31  		)
    32  
    33  		JustBeforeEach(func() {
    34  			serviceBrokers, warnings, executionError = actor.GetServiceBrokers()
    35  		})
    36  
    37  		When("the cloud controller request is successful", func() {
    38  			When("the cloud controller returns service brokers", func() {
    39  				BeforeEach(func() {
    40  					fakeCloudControllerClient.GetServiceBrokersReturns([]resources.ServiceBroker{
    41  						{
    42  							GUID: "service-broker-guid-1",
    43  							Name: "service-broker-1",
    44  							URL:  "service-broker-url-1",
    45  						},
    46  						{
    47  							GUID: "service-broker-guid-2",
    48  							Name: "service-broker-2",
    49  							URL:  "service-broker-url-2",
    50  						},
    51  					}, ccv3.Warnings{"some-service-broker-warning"}, nil)
    52  				})
    53  
    54  				It("returns the service brokers and warnings", func() {
    55  					Expect(executionError).NotTo(HaveOccurred())
    56  
    57  					Expect(serviceBrokers).To(ConsistOf(
    58  						resources.ServiceBroker{Name: "service-broker-1", GUID: "service-broker-guid-1", URL: "service-broker-url-1"},
    59  						resources.ServiceBroker{Name: "service-broker-2", GUID: "service-broker-guid-2", URL: "service-broker-url-2"},
    60  					))
    61  					Expect(warnings).To(ConsistOf("some-service-broker-warning"))
    62  					Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
    63  				})
    64  			})
    65  		})
    66  
    67  		When("the cloud controller returns an error", func() {
    68  			BeforeEach(func() {
    69  				fakeCloudControllerClient.GetServiceBrokersReturns(
    70  					nil,
    71  					ccv3.Warnings{"some-service-broker-warning"},
    72  					errors.New("no service broker"))
    73  			})
    74  
    75  			It("returns an error and warnings", func() {
    76  				Expect(executionError).To(MatchError("no service broker"))
    77  				Expect(warnings).To(ConsistOf("some-service-broker-warning"))
    78  			})
    79  		})
    80  	})
    81  
    82  	Describe("GetServiceBrokerByName", func() {
    83  		const (
    84  			serviceBroker1Name = "broker-name"
    85  			serviceBroker1Guid = "broker-guid"
    86  		)
    87  
    88  		var (
    89  			ccv3ServiceBrokers []resources.ServiceBroker
    90  			serviceBroker      resources.ServiceBroker
    91  			warnings           Warnings
    92  			executeErr         error
    93  		)
    94  
    95  		BeforeEach(func() {
    96  			ccv3ServiceBrokers = []resources.ServiceBroker{
    97  				{Name: serviceBroker1Name, GUID: serviceBroker1Guid},
    98  			}
    99  		})
   100  
   101  		JustBeforeEach(func() {
   102  			serviceBroker, warnings, executeErr = actor.GetServiceBrokerByName(serviceBroker1Name)
   103  		})
   104  
   105  		When("the service broker is found", func() {
   106  			BeforeEach(func() {
   107  				fakeCloudControllerClient.GetServiceBrokersReturns(
   108  					ccv3ServiceBrokers,
   109  					ccv3.Warnings{"some-service-broker-warning"},
   110  					nil,
   111  				)
   112  			})
   113  
   114  			It("returns the service broker and warnings", func() {
   115  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
   116  				Expect(fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)).To(ConsistOf(ccv3.Query{
   117  					Key:    ccv3.NameFilter,
   118  					Values: []string{serviceBroker1Name},
   119  				}))
   120  
   121  				Expect(executeErr).ToNot(HaveOccurred())
   122  				Expect(warnings).To(ConsistOf("some-service-broker-warning"))
   123  				Expect(serviceBroker).To(Equal(
   124  					resources.ServiceBroker{Name: serviceBroker1Name, GUID: serviceBroker1Guid},
   125  				))
   126  			})
   127  		})
   128  
   129  		When("the service broker is not found", func() {
   130  			BeforeEach(func() {
   131  				fakeCloudControllerClient.GetServiceBrokersReturns(
   132  					[]resources.ServiceBroker{},
   133  					ccv3.Warnings{"some-other-service-broker-warning"},
   134  					nil,
   135  				)
   136  			})
   137  
   138  			It("returns an error and warnings", func() {
   139  				Expect(executeErr).To(MatchError(actionerror.ServiceBrokerNotFoundError{Name: serviceBroker1Name}))
   140  				Expect(warnings).To(ConsistOf("some-other-service-broker-warning"))
   141  				Expect(serviceBroker).To(Equal(resources.ServiceBroker{}))
   142  			})
   143  		})
   144  
   145  		When("when the API layer call returns an error", func() {
   146  			BeforeEach(func() {
   147  				fakeCloudControllerClient.GetServiceBrokersReturns(
   148  					[]resources.ServiceBroker{},
   149  					ccv3.Warnings{"some-service-broker-warning"},
   150  					errors.New("list-error"),
   151  				)
   152  			})
   153  
   154  			It("returns the error and prints warnings", func() {
   155  				Expect(executeErr).To(MatchError("list-error"))
   156  				Expect(warnings).To(ConsistOf("some-service-broker-warning"))
   157  				Expect(serviceBroker).To(Equal(resources.ServiceBroker{}))
   158  
   159  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
   160  			})
   161  		})
   162  	})
   163  
   164  	Describe("CreateServiceBroker", func() {
   165  		const (
   166  			name      = "name"
   167  			url       = "url"
   168  			username  = "username"
   169  			password  = "password"
   170  			spaceGUID = "space-guid"
   171  		)
   172  
   173  		var (
   174  			warnings       Warnings
   175  			executionError error
   176  		)
   177  
   178  		JustBeforeEach(func() {
   179  			warnings, executionError = actor.CreateServiceBroker(
   180  				resources.ServiceBroker{
   181  					Name:      name,
   182  					URL:       url,
   183  					Username:  username,
   184  					Password:  password,
   185  					SpaceGUID: spaceGUID,
   186  				},
   187  			)
   188  		})
   189  
   190  		When("the client request is successful", func() {
   191  			var (
   192  				expectedJobURL = ccv3.JobURL("some-job-url")
   193  			)
   194  
   195  			BeforeEach(func() {
   196  				fakeCloudControllerClient.CreateServiceBrokerReturns(
   197  					expectedJobURL, ccv3.Warnings{"some-creation-warning"}, nil,
   198  				)
   199  			})
   200  
   201  			It("passes the service broker credentials to the client", func() {
   202  				Expect(fakeCloudControllerClient.CreateServiceBrokerCallCount()).To(
   203  					Equal(1), "Expected client.CreateServiceBroker to be called once",
   204  				)
   205  
   206  				serviceBroker := fakeCloudControllerClient.CreateServiceBrokerArgsForCall(0)
   207  				Expect(serviceBroker.Name).To(Equal(name))
   208  				Expect(serviceBroker.Username).To(Equal(username))
   209  				Expect(serviceBroker.Password).To(Equal(password))
   210  				Expect(serviceBroker.URL).To(Equal(url))
   211  				Expect(serviceBroker.SpaceGUID).To(Equal(spaceGUID))
   212  			})
   213  
   214  			It("passes the job url to the client for polling", func() {
   215  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   216  					Equal(1), "Expected client.PollJob to be called once",
   217  				)
   218  
   219  				jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   220  				Expect(jobURL).To(Equal(expectedJobURL))
   221  			})
   222  
   223  			When("async job succeeds", func() {
   224  				BeforeEach(func() {
   225  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   226  				})
   227  
   228  				It("succeeds and returns warnings", func() {
   229  					Expect(executionError).NotTo(HaveOccurred())
   230  
   231  					Expect(warnings).To(ConsistOf("some-creation-warning", "some-poll-warning"))
   232  				})
   233  			})
   234  
   235  			When("async job fails", func() {
   236  				BeforeEach(func() {
   237  					fakeCloudControllerClient.PollJobReturns(nil, errors.New("oopsie"))
   238  				})
   239  
   240  				It("succeeds and returns warnings", func() {
   241  					Expect(executionError).To(MatchError("oopsie"))
   242  				})
   243  			})
   244  		})
   245  
   246  		When("the client returns an error", func() {
   247  			BeforeEach(func() {
   248  				fakeCloudControllerClient.CreateServiceBrokerReturns(
   249  					"", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"),
   250  				)
   251  			})
   252  
   253  			It("fails and returns warnings", func() {
   254  				Expect(executionError).To(MatchError("invalid broker"))
   255  
   256  				Expect(warnings).To(ConsistOf("some-other-warning"))
   257  			})
   258  		})
   259  	})
   260  
   261  	Describe("UpdateServiceBroker", func() {
   262  		const (
   263  			guid     = "broker-guid"
   264  			url      = "url"
   265  			username = "username"
   266  			password = "password"
   267  		)
   268  
   269  		var (
   270  			expectedJobURL = ccv3.JobURL("some-job-url")
   271  		)
   272  
   273  		It("passes the service broker creds and url to the client", func() {
   274  			_, executionError := actor.UpdateServiceBroker(
   275  				guid,
   276  				resources.ServiceBroker{
   277  					Username: username,
   278  					Password: password,
   279  					URL:      url,
   280  				},
   281  			)
   282  			Expect(executionError).ToNot(HaveOccurred())
   283  
   284  			Expect(fakeCloudControllerClient.UpdateServiceBrokerCallCount()).To(Equal(1))
   285  			guid, serviceBroker := fakeCloudControllerClient.UpdateServiceBrokerArgsForCall(0)
   286  			Expect(guid).To(Equal(guid))
   287  			Expect(serviceBroker.Name).To(BeEmpty())
   288  			Expect(serviceBroker.Username).To(Equal(username))
   289  			Expect(serviceBroker.Password).To(Equal(password))
   290  			Expect(serviceBroker.URL).To(Equal(url))
   291  		})
   292  
   293  		It("passes the job url to the client for polling", func() {
   294  			fakeCloudControllerClient.UpdateServiceBrokerReturns(
   295  				expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   296  			)
   297  
   298  			_, executionError := actor.UpdateServiceBroker(
   299  				guid,
   300  				resources.ServiceBroker{
   301  					Username: username,
   302  					Password: password,
   303  					URL:      url,
   304  				},
   305  			)
   306  			Expect(executionError).ToNot(HaveOccurred())
   307  
   308  			Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   309  				Equal(1), "Expected client.PollJob to be called once",
   310  			)
   311  
   312  			jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   313  			Expect(jobURL).To(Equal(expectedJobURL))
   314  		})
   315  
   316  		When("async job succeeds", func() {
   317  			BeforeEach(func() {
   318  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   319  					expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   320  				)
   321  				fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   322  			})
   323  
   324  			It("succeeds and returns warnings", func() {
   325  				warnings, executionError := actor.UpdateServiceBroker(
   326  					guid,
   327  					resources.ServiceBroker{
   328  						Username: username,
   329  						Password: password,
   330  						URL:      url,
   331  					},
   332  				)
   333  
   334  				Expect(executionError).NotTo(HaveOccurred())
   335  				Expect(warnings).To(ConsistOf("some-update-warning", "some-poll-warning"))
   336  			})
   337  		})
   338  
   339  		When("async job fails", func() {
   340  			BeforeEach(func() {
   341  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   342  					expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   343  				)
   344  				fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, errors.New("job-execution-failed"))
   345  			})
   346  
   347  			It("succeeds and returns warnings", func() {
   348  				warnings, executionError := actor.UpdateServiceBroker(
   349  					guid,
   350  					resources.ServiceBroker{
   351  						Username: username,
   352  						Password: password,
   353  						URL:      url,
   354  					},
   355  				)
   356  
   357  				Expect(executionError).To(MatchError("job-execution-failed"))
   358  				Expect(warnings).To(ConsistOf("some-update-warning", "some-poll-warning"))
   359  			})
   360  		})
   361  
   362  		When("the client returns an error", func() {
   363  			BeforeEach(func() {
   364  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   365  					"", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"),
   366  				)
   367  			})
   368  
   369  			It("fails and returns warnings", func() {
   370  				warnings, executionError := actor.UpdateServiceBroker(
   371  					guid,
   372  					resources.ServiceBroker{
   373  						Username: username,
   374  						Password: password,
   375  						URL:      url,
   376  					},
   377  				)
   378  
   379  				Expect(executionError).To(MatchError("invalid broker"))
   380  				Expect(warnings).To(ConsistOf("some-other-warning"))
   381  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   382  					Equal(0), "Expected client.PollJob to not have been called",
   383  				)
   384  			})
   385  		})
   386  	})
   387  
   388  	Describe("DeleteServiceBroker", func() {
   389  		var (
   390  			serviceBrokerGUID = "some-service-broker-guid"
   391  			warnings          Warnings
   392  			executionError    error
   393  			expectedJobURL    = ccv3.JobURL("some-job-URL")
   394  		)
   395  
   396  		JustBeforeEach(func() {
   397  			warnings, executionError = actor.DeleteServiceBroker(serviceBrokerGUID)
   398  		})
   399  
   400  		When("the client request is successful", func() {
   401  			BeforeEach(func() {
   402  				fakeCloudControllerClient.DeleteServiceBrokerReturns(expectedJobURL, ccv3.Warnings{"some-deletion-warning"}, nil)
   403  			})
   404  
   405  			It("passes the service broker credentials to the client", func() {
   406  				Expect(fakeCloudControllerClient.DeleteServiceBrokerCallCount()).To(Equal(1))
   407  				actualServiceBrokerGUID := fakeCloudControllerClient.DeleteServiceBrokerArgsForCall(0)
   408  				Expect(actualServiceBrokerGUID).To(Equal(serviceBrokerGUID))
   409  			})
   410  
   411  			It("passes the job url to the client for polling", func() {
   412  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   413  					Equal(1), "Expected client.PollJob to be called once",
   414  				)
   415  
   416  				jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   417  				Expect(jobURL).To(Equal(expectedJobURL))
   418  			})
   419  
   420  			When("the delete service broker job completes successfully", func() {
   421  				BeforeEach(func() {
   422  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   423  				})
   424  
   425  				It("succeeds and returns warnings", func() {
   426  					Expect(executionError).NotTo(HaveOccurred())
   427  
   428  					Expect(warnings).To(ConsistOf("some-deletion-warning", "some-poll-warning"))
   429  				})
   430  			})
   431  
   432  			When("the delete service broker job fails", func() {
   433  				BeforeEach(func() {
   434  					fakeCloudControllerClient.PollJobReturns(nil, errors.New("oopsie"))
   435  				})
   436  
   437  				It("succeeds and returns warnings", func() {
   438  					Expect(executionError).To(MatchError("oopsie"))
   439  				})
   440  			})
   441  		})
   442  
   443  		When("the client returns an error", func() {
   444  			BeforeEach(func() {
   445  				fakeCloudControllerClient.DeleteServiceBrokerReturns("", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"))
   446  			})
   447  
   448  			It("fails and returns warnings", func() {
   449  				Expect(executionError).To(MatchError("invalid broker"))
   450  
   451  				Expect(warnings).To(ConsistOf("some-other-warning"))
   452  			})
   453  		})
   454  	})
   455  })