github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/v2action/service_broker_summary_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Service Broker Summary Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    24  	})
    25  
    26  	Describe("GetServiceBrokerSummaries", func() {
    27  		var (
    28  			broker       string
    29  			service      string
    30  			organization string
    31  
    32  			summaries  []ServiceBrokerSummary
    33  			warnings   Warnings
    34  			executeErr error
    35  		)
    36  
    37  		JustBeforeEach(func() {
    38  			summaries, warnings, executeErr = actor.GetServiceBrokerSummaries(broker, service, organization)
    39  		})
    40  
    41  		When("no broker, service, organization is specified", func() {
    42  			When("fetching the service broker is successful", func() {
    43  				BeforeEach(func() {
    44  					broker = ""
    45  					service = ""
    46  					organization = ""
    47  
    48  					fakeCloudControllerClient.GetServiceBrokersReturns(
    49  						[]ccv2.ServiceBroker{
    50  							{Name: "broker-1", GUID: "broker-guid-1"},
    51  							{Name: "broker-2", GUID: "broker-guid-2"},
    52  						},
    53  						ccv2.Warnings{"get-brokers-warning"},
    54  						nil)
    55  				})
    56  
    57  				When("there broker contains no services", func() {
    58  					It("returns expected Service Brokers", func() {
    59  						Expect(executeErr).ToNot(HaveOccurred())
    60  						Expect(warnings).To(ConsistOf("get-brokers-warning"))
    61  						Expect(summaries).To(ConsistOf(
    62  							ServiceBrokerSummary{
    63  								ServiceBroker: ServiceBroker{
    64  									Name: "broker-1",
    65  									GUID: "broker-guid-1",
    66  								},
    67  							},
    68  							ServiceBrokerSummary{
    69  								ServiceBroker: ServiceBroker{
    70  									Name: "broker-2",
    71  									GUID: "broker-guid-2",
    72  								},
    73  							},
    74  						))
    75  
    76  						Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
    77  						Expect(fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)).To(BeEmpty())
    78  					})
    79  				})
    80  
    81  				When("the brokers contain services", func() {
    82  					When("fetching the services is successful", func() {
    83  						BeforeEach(func() {
    84  							fakeCloudControllerClient.GetServicesReturnsOnCall(0,
    85  								[]ccv2.Service{
    86  									{Label: "service-1", GUID: "service-guid-1"},
    87  									{Label: "service-2", GUID: "service-guid-2"},
    88  								},
    89  								ccv2.Warnings{"service-warning-1"},
    90  								nil)
    91  							fakeCloudControllerClient.GetServicesReturnsOnCall(1,
    92  								[]ccv2.Service{
    93  									{Label: "service-3", GUID: "service-guid-3"},
    94  									{Label: "service-4", GUID: "service-guid-4"},
    95  								},
    96  								ccv2.Warnings{"service-warning-2"},
    97  								nil)
    98  						})
    99  
   100  						It("returns expected Services for their given brokers", func() {
   101  							Expect(executeErr).ToNot(HaveOccurred())
   102  							Expect(warnings).To(ConsistOf("get-brokers-warning", "service-warning-1", "service-warning-2"))
   103  							Expect(summaries[0].Services).To(ConsistOf(
   104  								[]ServiceSummary{
   105  									{
   106  										Service: Service{Label: "service-1", GUID: "service-guid-1"},
   107  									},
   108  									{
   109  										Service: Service{Label: "service-2", GUID: "service-guid-2"},
   110  									},
   111  								},
   112  							))
   113  							Expect(summaries[1].Services).To(ConsistOf(
   114  								[]ServiceSummary{
   115  									{
   116  										Service: Service{Label: "service-3", GUID: "service-guid-3"},
   117  									},
   118  									{
   119  										Service: Service{Label: "service-4", GUID: "service-guid-4"},
   120  									},
   121  								},
   122  							))
   123  
   124  							Expect(fakeCloudControllerClient.GetServicesCallCount()).To(Equal(2))
   125  							Expect(fakeCloudControllerClient.GetServicesArgsForCall(0)).To(ConsistOf(ccv2.Filter{
   126  								Type:     constant.ServiceBrokerGUIDFilter,
   127  								Operator: constant.EqualOperator,
   128  								Values:   []string{"broker-guid-1"},
   129  							}))
   130  							Expect(fakeCloudControllerClient.GetServicesArgsForCall(1)).To(ConsistOf(ccv2.Filter{
   131  								Type:     constant.ServiceBrokerGUIDFilter,
   132  								Operator: constant.EqualOperator,
   133  								Values:   []string{"broker-guid-2"},
   134  							}))
   135  						})
   136  
   137  						When("the services contain plans", func() {
   138  							When("fetching service plans is successful", func() {
   139  								When("all plans are public", func() {
   140  									BeforeEach(func() {
   141  										fakeCloudControllerClient.GetServicePlansReturnsOnCall(0,
   142  											[]ccv2.ServicePlan{
   143  												{GUID: "service-plan-guid-1", Name: "service-plan-1", Public: true},
   144  												{GUID: "service-plan-guid-2", Name: "service-plan-2", Public: true},
   145  											},
   146  											ccv2.Warnings{"service-plan-warning-1"},
   147  											nil)
   148  										fakeCloudControllerClient.GetServicePlansReturnsOnCall(2,
   149  											[]ccv2.ServicePlan{
   150  												{GUID: "service-plan-guid-3", Name: "service-plan-3", Public: true},
   151  												{GUID: "service-plan-guid-4", Name: "service-plan-4", Public: true},
   152  											},
   153  											ccv2.Warnings{"service-plan-warning-2"},
   154  											nil)
   155  									})
   156  
   157  									It("returns the expected Service Plans without visibilities", func() {
   158  										Expect(executeErr).ToNot(HaveOccurred())
   159  										Expect(warnings).To(ConsistOf(
   160  											"get-brokers-warning", "service-warning-1",
   161  											"service-warning-2", "service-plan-warning-1", "service-plan-warning-2"))
   162  
   163  										Expect(summaries[0].Services[0].Plans).To(ConsistOf(
   164  											[]ServicePlanSummary{
   165  												{
   166  													ServicePlan: ServicePlan{GUID: "service-plan-guid-1", Name: "service-plan-1", Public: true},
   167  												},
   168  												{
   169  													ServicePlan: ServicePlan{GUID: "service-plan-guid-2", Name: "service-plan-2", Public: true},
   170  												},
   171  											},
   172  										))
   173  										Expect(summaries[1].Services[0].Plans).To(ConsistOf(
   174  											[]ServicePlanSummary{
   175  												{
   176  													ServicePlan: ServicePlan{GUID: "service-plan-guid-3", Name: "service-plan-3", Public: true},
   177  												},
   178  												{
   179  													ServicePlan: ServicePlan{GUID: "service-plan-guid-4", Name: "service-plan-4", Public: true},
   180  												},
   181  											},
   182  										))
   183  
   184  										Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(4))
   185  										Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(ccv2.Filter{
   186  											Type:     constant.ServiceGUIDFilter,
   187  											Operator: constant.EqualOperator,
   188  											Values:   []string{"service-guid-1"},
   189  										}))
   190  										Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(1)).To(ConsistOf(ccv2.Filter{
   191  											Type:     constant.ServiceGUIDFilter,
   192  											Operator: constant.EqualOperator,
   193  											Values:   []string{"service-guid-2"},
   194  										}))
   195  
   196  										Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(0))
   197  									})
   198  								})
   199  
   200  								When("there are non-public services plans", func() {
   201  									BeforeEach(func() {
   202  										// Gets the service plans
   203  										fakeCloudControllerClient.GetServicePlansReturnsOnCall(0,
   204  											[]ccv2.ServicePlan{
   205  												{GUID: "service-plan-guid-1", Name: "service-plan-1"},
   206  												{GUID: "service-plan-guid-2", Name: "service-plan-2"},
   207  											},
   208  											ccv2.Warnings{"service-plan-warning-1"},
   209  											nil)
   210  									})
   211  
   212  									When("fetching orgs and service plan visibilities is successful", func() {
   213  										BeforeEach(func() {
   214  											// Gets the visibilities for the plans
   215  											fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(0,
   216  												[]ccv2.ServicePlanVisibility{
   217  													{OrganizationGUID: "org-guid-1"},
   218  													{OrganizationGUID: "org-guid-2"},
   219  												},
   220  												ccv2.Warnings{"service-plan-visibility-1"},
   221  												nil)
   222  											fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(1,
   223  												[]ccv2.ServicePlanVisibility{
   224  													{OrganizationGUID: "org-guid-3"},
   225  													{OrganizationGUID: "org-guid-4"},
   226  												},
   227  												ccv2.Warnings{"service-plan-visibility-2"},
   228  												nil)
   229  
   230  											// Gets the OrgNames for the visibilities
   231  											fakeCloudControllerClient.GetOrganizationReturnsOnCall(0,
   232  												ccv2.Organization{Name: "org-1"},
   233  												ccv2.Warnings{"org-warning-1"},
   234  												nil)
   235  											fakeCloudControllerClient.GetOrganizationReturnsOnCall(1,
   236  												ccv2.Organization{Name: "org-2"},
   237  												ccv2.Warnings{"org-warning-2"},
   238  												nil)
   239  											fakeCloudControllerClient.GetOrganizationReturnsOnCall(2,
   240  												ccv2.Organization{Name: "org-3"},
   241  												ccv2.Warnings{"org-warning-3"},
   242  												nil)
   243  											fakeCloudControllerClient.GetOrganizationReturnsOnCall(3,
   244  												ccv2.Organization{Name: "org-4"},
   245  												ccv2.Warnings{"org-warning-4"},
   246  												nil)
   247  										})
   248  
   249  										It("returns the expected Service Plans", func() {
   250  											Expect(executeErr).ToNot(HaveOccurred())
   251  											Expect(warnings).To(ConsistOf(
   252  												"get-brokers-warning",
   253  												"service-warning-1", "service-warning-2",
   254  												"service-plan-warning-1",
   255  												"service-plan-visibility-1", "service-plan-visibility-2",
   256  												"org-warning-1", "org-warning-2", "org-warning-3", "org-warning-4",
   257  											))
   258  
   259  											Expect(summaries[0].Services[0].Plans[0].VisibleTo).To(ConsistOf("org-1", "org-2"))
   260  											Expect(summaries[0].Services[0].Plans[1].VisibleTo).To(ConsistOf("org-3", "org-4"))
   261  
   262  											Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(4))
   263  											Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(0)).To(ConsistOf(ccv2.Filter{
   264  												Type:     constant.ServiceGUIDFilter,
   265  												Operator: constant.EqualOperator,
   266  												Values:   []string{"service-guid-1"},
   267  											}))
   268  											Expect(fakeCloudControllerClient.GetServicePlansArgsForCall(1)).To(ConsistOf(ccv2.Filter{
   269  												Type:     constant.ServiceGUIDFilter,
   270  												Operator: constant.EqualOperator,
   271  												Values:   []string{"service-guid-2"},
   272  											}))
   273  
   274  											Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesCallCount()).To(Equal(2))
   275  											Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(0)).To(ConsistOf(ccv2.Filter{
   276  												Type:     constant.ServicePlanGUIDFilter,
   277  												Operator: constant.EqualOperator,
   278  												Values:   []string{"service-plan-guid-1"},
   279  											}))
   280  											Expect(fakeCloudControllerClient.GetServicePlanVisibilitiesArgsForCall(1)).To(ConsistOf(ccv2.Filter{
   281  												Type:     constant.ServicePlanGUIDFilter,
   282  												Operator: constant.EqualOperator,
   283  												Values:   []string{"service-plan-guid-2"},
   284  											}))
   285  										})
   286  									})
   287  
   288  									When("fetching the service plan visibilities fails", func() {
   289  										BeforeEach(func() {
   290  											// Gets the visibilities for the plans
   291  											fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(0,
   292  												nil,
   293  												ccv2.Warnings{"service-plan-visibility-1"},
   294  												errors.New("boom"))
   295  										})
   296  
   297  										It("returns the error and warnings", func() {
   298  											Expect(executeErr).To(MatchError("boom"))
   299  											Expect(warnings).To(ConsistOf(
   300  												"get-brokers-warning",
   301  												"service-warning-1",
   302  												"service-plan-warning-1",
   303  												"service-plan-visibility-1",
   304  											))
   305  										})
   306  									})
   307  
   308  									When("fetching the organizations fails", func() {
   309  										BeforeEach(func() {
   310  											fakeCloudControllerClient.GetServicePlanVisibilitiesReturnsOnCall(0,
   311  												[]ccv2.ServicePlanVisibility{
   312  													{OrganizationGUID: "org-guid-1"},
   313  													{OrganizationGUID: "org-guid-2"},
   314  												},
   315  												ccv2.Warnings{"service-plan-visibility-1"},
   316  												nil)
   317  
   318  											fakeCloudControllerClient.GetOrganizationReturnsOnCall(0,
   319  												ccv2.Organization{},
   320  												ccv2.Warnings{"org-warning-1"},
   321  												errors.New("boom"))
   322  										})
   323  
   324  										It("returns the error and warnings", func() {
   325  											Expect(executeErr).To(MatchError("boom"))
   326  											Expect(warnings).To(ConsistOf(
   327  												"get-brokers-warning",
   328  												"service-warning-1",
   329  												"service-plan-warning-1",
   330  												"service-plan-visibility-1",
   331  												"org-warning-1",
   332  											))
   333  										})
   334  									})
   335  								})
   336  							})
   337  						})
   338  
   339  						When("fetching service plans fails", func() {
   340  							BeforeEach(func() {
   341  								fakeCloudControllerClient.GetServicePlansReturnsOnCall(0,
   342  									nil,
   343  									ccv2.Warnings{"service-plan-warning-1"},
   344  									errors.New("boom"))
   345  							})
   346  
   347  							It("returns the error and warnings", func() {
   348  								Expect(executeErr).To(MatchError("boom"))
   349  								Expect(warnings).To(ConsistOf("get-brokers-warning", "service-warning-1", "service-plan-warning-1"))
   350  							})
   351  						})
   352  					})
   353  
   354  					When("fetching services fails", func() {
   355  						BeforeEach(func() {
   356  							fakeCloudControllerClient.GetServicesReturnsOnCall(0,
   357  								nil,
   358  								ccv2.Warnings{"service-warning-1"},
   359  								errors.New("boom"))
   360  						})
   361  
   362  						It("returns the error and warnings", func() {
   363  							Expect(executeErr).To(MatchError("boom"))
   364  							Expect(warnings).To(ConsistOf("get-brokers-warning", "service-warning-1"))
   365  						})
   366  					})
   367  				})
   368  			})
   369  
   370  			When("fetching service brokers fails", func() {
   371  				BeforeEach(func() {
   372  					fakeCloudControllerClient.GetServiceBrokersReturns(nil, ccv2.Warnings{"test-warning"}, errors.New("explode"))
   373  				})
   374  
   375  				It("returns the warnings and propagates the error", func() {
   376  					Expect(warnings).To(ConsistOf("test-warning"))
   377  					Expect(executeErr).To(MatchError("explode"))
   378  				})
   379  			})
   380  		})
   381  
   382  		When("only a service broker is specified", func() {
   383  			BeforeEach(func() {
   384  				broker = "broker-1"
   385  				service = ""
   386  				organization = ""
   387  
   388  				fakeCloudControllerClient.GetServiceBrokersReturns(
   389  					[]ccv2.ServiceBroker{
   390  						{Name: "broker-1", GUID: "broker-guid-1"},
   391  					},
   392  					ccv2.Warnings{"get-brokers-warning"},
   393  					nil)
   394  			})
   395  
   396  			It("returns expected Service Brokers", func() {
   397  				Expect(executeErr).ToNot(HaveOccurred())
   398  				Expect(warnings).To(ConsistOf("get-brokers-warning"))
   399  				Expect(summaries).To(ConsistOf(
   400  					ServiceBrokerSummary{
   401  						ServiceBroker: ServiceBroker{
   402  							Name: "broker-1",
   403  							GUID: "broker-guid-1",
   404  						},
   405  					}))
   406  
   407  				Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1))
   408  				Expect(fakeCloudControllerClient.GetServiceBrokersArgsForCall(0)).To(ConsistOf(ccv2.Filter{
   409  					Type:     constant.NameFilter,
   410  					Operator: constant.EqualOperator,
   411  					Values:   []string{broker},
   412  				}))
   413  			})
   414  		})
   415  	})
   416  })