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