github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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(
   117  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{serviceBroker1Name}},
   118  					ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
   119  					ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
   120  				))
   121  
   122  				Expect(executeErr).ToNot(HaveOccurred())
   123  				Expect(warnings).To(ConsistOf("some-service-broker-warning"))
   124  				Expect(serviceBroker).To(Equal(
   125  					resources.ServiceBroker{Name: serviceBroker1Name, GUID: serviceBroker1Guid},
   126  				))
   127  			})
   128  		})
   129  
   130  		When("the service broker is not found", func() {
   131  			BeforeEach(func() {
   132  				fakeCloudControllerClient.GetServiceBrokersReturns(
   133  					[]resources.ServiceBroker{},
   134  					ccv3.Warnings{"some-other-service-broker-warning"},
   135  					nil,
   136  				)
   137  			})
   138  
   139  			It("returns an error and warnings", func() {
   140  				Expect(executeErr).To(MatchError(actionerror.ServiceBrokerNotFoundError{Name: serviceBroker1Name}))
   141  				Expect(warnings).To(ConsistOf("some-other-service-broker-warning"))
   142  				Expect(serviceBroker).To(Equal(resources.ServiceBroker{}))
   143  			})
   144  		})
   145  
   146  		When("when the API layer call returns an error", func() {
   147  			BeforeEach(func() {
   148  				fakeCloudControllerClient.GetServiceBrokersReturns(
   149  					[]resources.ServiceBroker{},
   150  					ccv3.Warnings{"some-service-broker-warning"},
   151  					errors.New("list-error"),
   152  				)
   153  			})
   154  
   155  			It("returns the error and prints warnings", func() {
   156  				Expect(executeErr).To(MatchError("list-error"))
   157  				Expect(warnings).To(ConsistOf("some-service-broker-warning"))
   158  				Expect(serviceBroker).To(Equal(resources.ServiceBroker{}))
   159  
   160  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
   161  			})
   162  		})
   163  	})
   164  
   165  	Describe("CreateServiceBroker", func() {
   166  		const (
   167  			name      = "name"
   168  			url       = "url"
   169  			username  = "username"
   170  			password  = "password"
   171  			spaceGUID = "space-guid"
   172  		)
   173  
   174  		var (
   175  			warnings       Warnings
   176  			executionError error
   177  		)
   178  
   179  		JustBeforeEach(func() {
   180  			warnings, executionError = actor.CreateServiceBroker(
   181  				resources.ServiceBroker{
   182  					Name:      name,
   183  					URL:       url,
   184  					Username:  username,
   185  					Password:  password,
   186  					SpaceGUID: spaceGUID,
   187  				},
   188  			)
   189  		})
   190  
   191  		When("the client request is successful", func() {
   192  			var (
   193  				expectedJobURL = ccv3.JobURL("some-job-url")
   194  			)
   195  
   196  			BeforeEach(func() {
   197  				fakeCloudControllerClient.CreateServiceBrokerReturns(
   198  					expectedJobURL, ccv3.Warnings{"some-creation-warning"}, nil,
   199  				)
   200  			})
   201  
   202  			It("passes the service broker credentials to the client", func() {
   203  				Expect(fakeCloudControllerClient.CreateServiceBrokerCallCount()).To(
   204  					Equal(1), "Expected client.CreateServiceBroker to be called once",
   205  				)
   206  
   207  				serviceBroker := fakeCloudControllerClient.CreateServiceBrokerArgsForCall(0)
   208  				Expect(serviceBroker.Name).To(Equal(name))
   209  				Expect(serviceBroker.Username).To(Equal(username))
   210  				Expect(serviceBroker.Password).To(Equal(password))
   211  				Expect(serviceBroker.URL).To(Equal(url))
   212  				Expect(serviceBroker.SpaceGUID).To(Equal(spaceGUID))
   213  			})
   214  
   215  			It("passes the job url to the client for polling", func() {
   216  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   217  					Equal(1), "Expected client.PollJob to be called once",
   218  				)
   219  
   220  				jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   221  				Expect(jobURL).To(Equal(expectedJobURL))
   222  			})
   223  
   224  			When("async job succeeds", func() {
   225  				BeforeEach(func() {
   226  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   227  				})
   228  
   229  				It("succeeds and returns warnings", func() {
   230  					Expect(executionError).NotTo(HaveOccurred())
   231  
   232  					Expect(warnings).To(ConsistOf("some-creation-warning", "some-poll-warning"))
   233  				})
   234  			})
   235  
   236  			When("async job fails", func() {
   237  				BeforeEach(func() {
   238  					fakeCloudControllerClient.PollJobReturns(nil, errors.New("oopsie"))
   239  				})
   240  
   241  				It("succeeds and returns warnings", func() {
   242  					Expect(executionError).To(MatchError("oopsie"))
   243  				})
   244  			})
   245  		})
   246  
   247  		When("the client returns an error", func() {
   248  			BeforeEach(func() {
   249  				fakeCloudControllerClient.CreateServiceBrokerReturns(
   250  					"", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"),
   251  				)
   252  			})
   253  
   254  			It("fails and returns warnings", func() {
   255  				Expect(executionError).To(MatchError("invalid broker"))
   256  
   257  				Expect(warnings).To(ConsistOf("some-other-warning"))
   258  			})
   259  		})
   260  	})
   261  
   262  	Describe("UpdateServiceBroker", func() {
   263  		const (
   264  			guid     = "broker-guid"
   265  			url      = "url"
   266  			username = "username"
   267  			password = "password"
   268  		)
   269  
   270  		var (
   271  			expectedJobURL = ccv3.JobURL("some-job-url")
   272  		)
   273  
   274  		It("passes the service broker creds and url to the client", func() {
   275  			_, executionError := actor.UpdateServiceBroker(
   276  				guid,
   277  				resources.ServiceBroker{
   278  					Username: username,
   279  					Password: password,
   280  					URL:      url,
   281  				},
   282  			)
   283  			Expect(executionError).ToNot(HaveOccurred())
   284  
   285  			Expect(fakeCloudControllerClient.UpdateServiceBrokerCallCount()).To(Equal(1))
   286  			guid, serviceBroker := fakeCloudControllerClient.UpdateServiceBrokerArgsForCall(0)
   287  			Expect(guid).To(Equal(guid))
   288  			Expect(serviceBroker.Name).To(BeEmpty())
   289  			Expect(serviceBroker.Username).To(Equal(username))
   290  			Expect(serviceBroker.Password).To(Equal(password))
   291  			Expect(serviceBroker.URL).To(Equal(url))
   292  		})
   293  
   294  		It("passes the job url to the client for polling", func() {
   295  			fakeCloudControllerClient.UpdateServiceBrokerReturns(
   296  				expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   297  			)
   298  
   299  			_, executionError := actor.UpdateServiceBroker(
   300  				guid,
   301  				resources.ServiceBroker{
   302  					Username: username,
   303  					Password: password,
   304  					URL:      url,
   305  				},
   306  			)
   307  			Expect(executionError).ToNot(HaveOccurred())
   308  
   309  			Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   310  				Equal(1), "Expected client.PollJob to be called once",
   311  			)
   312  
   313  			jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   314  			Expect(jobURL).To(Equal(expectedJobURL))
   315  		})
   316  
   317  		When("async job succeeds", func() {
   318  			BeforeEach(func() {
   319  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   320  					expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   321  				)
   322  				fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   323  			})
   324  
   325  			It("succeeds and returns warnings", func() {
   326  				warnings, executionError := actor.UpdateServiceBroker(
   327  					guid,
   328  					resources.ServiceBroker{
   329  						Username: username,
   330  						Password: password,
   331  						URL:      url,
   332  					},
   333  				)
   334  
   335  				Expect(executionError).NotTo(HaveOccurred())
   336  				Expect(warnings).To(ConsistOf("some-update-warning", "some-poll-warning"))
   337  			})
   338  		})
   339  
   340  		When("async job fails", func() {
   341  			BeforeEach(func() {
   342  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   343  					expectedJobURL, ccv3.Warnings{"some-update-warning"}, nil,
   344  				)
   345  				fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, errors.New("job-execution-failed"))
   346  			})
   347  
   348  			It("succeeds and returns warnings", func() {
   349  				warnings, executionError := actor.UpdateServiceBroker(
   350  					guid,
   351  					resources.ServiceBroker{
   352  						Username: username,
   353  						Password: password,
   354  						URL:      url,
   355  					},
   356  				)
   357  
   358  				Expect(executionError).To(MatchError("job-execution-failed"))
   359  				Expect(warnings).To(ConsistOf("some-update-warning", "some-poll-warning"))
   360  			})
   361  		})
   362  
   363  		When("the client returns an error", func() {
   364  			BeforeEach(func() {
   365  				fakeCloudControllerClient.UpdateServiceBrokerReturns(
   366  					"", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"),
   367  				)
   368  			})
   369  
   370  			It("fails and returns warnings", func() {
   371  				warnings, executionError := actor.UpdateServiceBroker(
   372  					guid,
   373  					resources.ServiceBroker{
   374  						Username: username,
   375  						Password: password,
   376  						URL:      url,
   377  					},
   378  				)
   379  
   380  				Expect(executionError).To(MatchError("invalid broker"))
   381  				Expect(warnings).To(ConsistOf("some-other-warning"))
   382  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   383  					Equal(0), "Expected client.PollJob to not have been called",
   384  				)
   385  			})
   386  		})
   387  	})
   388  
   389  	Describe("DeleteServiceBroker", func() {
   390  		var (
   391  			serviceBrokerGUID = "some-service-broker-guid"
   392  			warnings          Warnings
   393  			executionError    error
   394  			expectedJobURL    = ccv3.JobURL("some-job-URL")
   395  		)
   396  
   397  		JustBeforeEach(func() {
   398  			warnings, executionError = actor.DeleteServiceBroker(serviceBrokerGUID)
   399  		})
   400  
   401  		When("the client request is successful", func() {
   402  			BeforeEach(func() {
   403  				fakeCloudControllerClient.DeleteServiceBrokerReturns(expectedJobURL, ccv3.Warnings{"some-deletion-warning"}, nil)
   404  			})
   405  
   406  			It("passes the service broker credentials to the client", func() {
   407  				Expect(fakeCloudControllerClient.DeleteServiceBrokerCallCount()).To(Equal(1))
   408  				actualServiceBrokerGUID := fakeCloudControllerClient.DeleteServiceBrokerArgsForCall(0)
   409  				Expect(actualServiceBrokerGUID).To(Equal(serviceBrokerGUID))
   410  			})
   411  
   412  			It("passes the job url to the client for polling", func() {
   413  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(
   414  					Equal(1), "Expected client.PollJob to be called once",
   415  				)
   416  
   417  				jobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   418  				Expect(jobURL).To(Equal(expectedJobURL))
   419  			})
   420  
   421  			When("the delete service broker job completes successfully", func() {
   422  				BeforeEach(func() {
   423  					fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"some-poll-warning"}, nil)
   424  				})
   425  
   426  				It("succeeds and returns warnings", func() {
   427  					Expect(executionError).NotTo(HaveOccurred())
   428  
   429  					Expect(warnings).To(ConsistOf("some-deletion-warning", "some-poll-warning"))
   430  				})
   431  			})
   432  
   433  			When("the delete service broker job fails", func() {
   434  				BeforeEach(func() {
   435  					fakeCloudControllerClient.PollJobReturns(nil, errors.New("oopsie"))
   436  				})
   437  
   438  				It("succeeds and returns warnings", func() {
   439  					Expect(executionError).To(MatchError("oopsie"))
   440  				})
   441  			})
   442  		})
   443  
   444  		When("the client returns an error", func() {
   445  			BeforeEach(func() {
   446  				fakeCloudControllerClient.DeleteServiceBrokerReturns("", ccv3.Warnings{"some-other-warning"}, errors.New("invalid broker"))
   447  			})
   448  
   449  			It("fails and returns warnings", func() {
   450  				Expect(executionError).To(MatchError("invalid broker"))
   451  
   452  				Expect(warnings).To(ConsistOf("some-other-warning"))
   453  			})
   454  		})
   455  	})
   456  })