github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_summary_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	. "code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Service Summary Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    24  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    25  	})
    26  
    27  	Describe("GetServicesSummaries", func() {
    28  		var (
    29  			servicesSummaries []ServiceSummary
    30  			warnings          Warnings
    31  			err               error
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			servicesSummaries, warnings, err = actor.GetServicesSummaries()
    36  		})
    37  
    38  		When("there are no services", func() {
    39  			BeforeEach(func() {
    40  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, nil)
    41  			})
    42  
    43  			It("returns an empty list of summaries and all warnings", func() {
    44  				Expect(err).NotTo(HaveOccurred())
    45  				Expect(servicesSummaries).To(HaveLen(0))
    46  				Expect(warnings).To(ConsistOf("get-services-warning"))
    47  			})
    48  		})
    49  
    50  		When("fetching services returns an error", func() {
    51  			BeforeEach(func() {
    52  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, errors.New("oops"))
    53  			})
    54  
    55  			It("returns the error and all warnings", func() {
    56  				Expect(err).To(MatchError("oops"))
    57  				Expect(warnings).To(ConsistOf("get-services-warning"))
    58  			})
    59  		})
    60  
    61  		When("there are services with plans", func() {
    62  			BeforeEach(func() {
    63  				services := []ccv2.Service{
    64  					{
    65  						Label:       "service-a",
    66  						Description: "service-a-description",
    67  					},
    68  					{
    69  						Label:       "service-b",
    70  						Description: "service-b-description",
    71  					},
    72  				}
    73  
    74  				plans := []ccv2.ServicePlan{
    75  					{Name: "plan-a"},
    76  					{Name: "plan-b"},
    77  				}
    78  
    79  				fakeCloudControllerClient.GetServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
    80  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
    81  			})
    82  
    83  			It("returns summaries including plans and all warnings", func() {
    84  				Expect(err).NotTo(HaveOccurred())
    85  				Expect(servicesSummaries).To(ConsistOf(
    86  					ServiceSummary{
    87  						Service: Service{
    88  							Label:       "service-a",
    89  							Description: "service-a-description",
    90  						},
    91  						Plans: []ServicePlanSummary{
    92  							ServicePlanSummary{
    93  								ServicePlan: ServicePlan{
    94  									Name: "plan-a",
    95  								},
    96  							},
    97  							ServicePlanSummary{
    98  								ServicePlan: ServicePlan{
    99  									Name: "plan-b",
   100  								},
   101  							},
   102  						},
   103  					},
   104  					ServiceSummary{
   105  						Service: Service{
   106  							Label:       "service-b",
   107  							Description: "service-b-description",
   108  						},
   109  						Plans: []ServicePlanSummary{
   110  							ServicePlanSummary{
   111  								ServicePlan: ServicePlan{
   112  									Name: "plan-a",
   113  								},
   114  							},
   115  							ServicePlanSummary{
   116  								ServicePlan: ServicePlan{
   117  									Name: "plan-b",
   118  								},
   119  							},
   120  						},
   121  					},
   122  				))
   123  				Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning", "get-plans-warning"))
   124  			})
   125  
   126  			Context("and fetching plans returns an error", func() {
   127  				BeforeEach(func() {
   128  					fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, ccv2.Warnings{"get-plans-warning"}, errors.New("plan-oops"))
   129  				})
   130  
   131  				It("returns the error and all warnings", func() {
   132  					Expect(err).To(MatchError("plan-oops"))
   133  					Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   134  				})
   135  			})
   136  		})
   137  	})
   138  
   139  	Describe("GetServicesSummariesForSpace", func() {
   140  		var (
   141  			servicesSummaries []ServiceSummary
   142  			warnings          Warnings
   143  			err               error
   144  			spaceGUID         = "space-123"
   145  		)
   146  
   147  		JustBeforeEach(func() {
   148  			servicesSummaries, warnings, err = actor.GetServicesSummariesForSpace(spaceGUID)
   149  		})
   150  
   151  		When("there are no services", func() {
   152  			BeforeEach(func() {
   153  				fakeCloudControllerClient.GetSpaceServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, nil)
   154  			})
   155  
   156  			It("returns an empty list of summaries and all warnings", func() {
   157  				Expect(err).NotTo(HaveOccurred())
   158  				Expect(servicesSummaries).To(HaveLen(0))
   159  				Expect(warnings).To(ConsistOf("get-services-warning"))
   160  			})
   161  		})
   162  
   163  		When("fetching services returns an error", func() {
   164  			BeforeEach(func() {
   165  				fakeCloudControllerClient.GetSpaceServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, errors.New("oops"))
   166  			})
   167  
   168  			It("returns the error and all warnings", func() {
   169  				Expect(err).To(MatchError("oops"))
   170  				Expect(warnings).To(ConsistOf("get-services-warning"))
   171  			})
   172  		})
   173  
   174  		When("there are services with plans", func() {
   175  			BeforeEach(func() {
   176  				services := []ccv2.Service{
   177  					{
   178  						Label:       "service-a",
   179  						Description: "service-a-description",
   180  					},
   181  					{
   182  						Label:       "service-b",
   183  						Description: "service-b-description",
   184  					},
   185  				}
   186  
   187  				plans := []ccv2.ServicePlan{
   188  					{Name: "plan-a"},
   189  					{Name: "plan-b"},
   190  				}
   191  
   192  				fakeCloudControllerClient.GetSpaceServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
   193  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
   194  			})
   195  
   196  			It("returns summaries including plans and all warnings", func() {
   197  				Expect(err).NotTo(HaveOccurred())
   198  				Expect(servicesSummaries).To(ConsistOf(
   199  					ServiceSummary{
   200  						Service: Service{
   201  							Label:       "service-a",
   202  							Description: "service-a-description",
   203  						},
   204  						Plans: []ServicePlanSummary{
   205  							ServicePlanSummary{
   206  								ServicePlan: ServicePlan{
   207  									Name: "plan-a",
   208  								},
   209  							},
   210  							ServicePlanSummary{
   211  								ServicePlan: ServicePlan{
   212  									Name: "plan-b",
   213  								},
   214  							},
   215  						},
   216  					},
   217  					ServiceSummary{
   218  						Service: Service{
   219  							Label:       "service-b",
   220  							Description: "service-b-description",
   221  						},
   222  						Plans: []ServicePlanSummary{
   223  							ServicePlanSummary{
   224  								ServicePlan: ServicePlan{
   225  									Name: "plan-a",
   226  								},
   227  							},
   228  							ServicePlanSummary{
   229  								ServicePlan: ServicePlan{
   230  									Name: "plan-b",
   231  								},
   232  							},
   233  						},
   234  					},
   235  				))
   236  				Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning", "get-plans-warning"))
   237  			})
   238  
   239  			It("retrieves the services for the correct space", func() {
   240  				requestedSpaceGUID, _ := fakeCloudControllerClient.GetSpaceServicesArgsForCall(0)
   241  				Expect(requestedSpaceGUID).To(Equal(spaceGUID))
   242  			})
   243  
   244  			Context("and fetching plans returns an error", func() {
   245  				BeforeEach(func() {
   246  					fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, ccv2.Warnings{"get-plans-warning"}, errors.New("plan-oops"))
   247  				})
   248  
   249  				It("returns the error and all warnings", func() {
   250  					Expect(err).To(MatchError("plan-oops"))
   251  					Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   252  				})
   253  			})
   254  		})
   255  	})
   256  
   257  	Describe("GetServiceSummaryByName", func() {
   258  		var (
   259  			serviceName    string
   260  			serviceSummary ServiceSummary
   261  			warnings       Warnings
   262  			err            error
   263  		)
   264  
   265  		BeforeEach(func() {
   266  			serviceName = "service"
   267  		})
   268  
   269  		JustBeforeEach(func() {
   270  			serviceSummary, warnings, err = actor.GetServiceSummaryByName(serviceName)
   271  		})
   272  
   273  		When("there is no service matching the provided name", func() {
   274  			BeforeEach(func() {
   275  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, nil)
   276  			})
   277  
   278  			It("returns an error that the service with the provided name is missing", func() {
   279  				Expect(err).To(MatchError(actionerror.ServiceNotFoundError{Name: serviceName}))
   280  				Expect(warnings).To(ConsistOf("get-services-warning"))
   281  			})
   282  		})
   283  
   284  		When("fetching a service returns an error", func() {
   285  			BeforeEach(func() {
   286  				fakeCloudControllerClient.GetServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, errors.New("oops"))
   287  			})
   288  
   289  			It("returns the error and all warnings", func() {
   290  				Expect(err).To(MatchError("oops"))
   291  				Expect(warnings).To(ConsistOf("get-services-warning"))
   292  			})
   293  		})
   294  
   295  		When("the service exists", func() {
   296  			var services []ccv2.Service
   297  
   298  			BeforeEach(func() {
   299  				services = []ccv2.Service{
   300  					{
   301  						Label:       "service",
   302  						Description: "service-description",
   303  					},
   304  				}
   305  				plans := []ccv2.ServicePlan{
   306  					{Name: "plan-a"},
   307  					{Name: "plan-b"},
   308  				}
   309  
   310  				fakeCloudControllerClient.GetServicesStub = func(filters ...ccv2.Filter) ([]ccv2.Service, ccv2.Warnings, error) {
   311  					filterToMatch := ccv2.Filter{
   312  						Type:     constant.LabelFilter,
   313  						Operator: constant.EqualOperator,
   314  						Values:   []string{"service"},
   315  					}
   316  
   317  					if len(filters) == 1 && reflect.DeepEqual(filters[0], filterToMatch) {
   318  						return services, ccv2.Warnings{"get-services-warning"}, nil
   319  					}
   320  
   321  					return []ccv2.Service{}, nil, nil
   322  				}
   323  
   324  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
   325  			})
   326  
   327  			It("filters for the service, returning a service summary including plans and all warnings", func() {
   328  				Expect(err).NotTo(HaveOccurred())
   329  				Expect(serviceSummary).To(Equal(
   330  					ServiceSummary{
   331  						Service: Service{
   332  							Label:       "service",
   333  							Description: "service-description",
   334  						},
   335  						Plans: []ServicePlanSummary{
   336  							ServicePlanSummary{
   337  								ServicePlan: ServicePlan{
   338  									Name: "plan-a",
   339  								},
   340  							},
   341  							ServicePlanSummary{
   342  								ServicePlan: ServicePlan{
   343  									Name: "plan-b",
   344  								},
   345  							},
   346  						},
   347  					}))
   348  				Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   349  			})
   350  
   351  			Context("and fetching plans returns an error", func() {
   352  				BeforeEach(func() {
   353  					fakeCloudControllerClient.GetServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
   354  					fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, ccv2.Warnings{"get-plans-warning"}, errors.New("plan-oops"))
   355  				})
   356  
   357  				It("returns the error and all warnings", func() {
   358  					Expect(err).To(MatchError("plan-oops"))
   359  					Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   360  				})
   361  			})
   362  		})
   363  	})
   364  
   365  	Describe("GetServiceSummaryForSpaceByName", func() {
   366  		var (
   367  			serviceName    string
   368  			spaceGUID      string
   369  			serviceSummary ServiceSummary
   370  			warnings       Warnings
   371  			err            error
   372  		)
   373  
   374  		BeforeEach(func() {
   375  			serviceName = "service"
   376  			spaceGUID = "space-123"
   377  		})
   378  
   379  		JustBeforeEach(func() {
   380  			serviceSummary, warnings, err = actor.GetServiceSummaryForSpaceByName(spaceGUID, serviceName)
   381  		})
   382  
   383  		When("there is no service matching the provided name", func() {
   384  			BeforeEach(func() {
   385  				fakeCloudControllerClient.GetSpaceServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, nil)
   386  			})
   387  
   388  			It("returns an error that the service with the provided name is missing", func() {
   389  				Expect(err).To(MatchError(actionerror.ServiceNotFoundError{Name: serviceName}))
   390  				Expect(warnings).To(ConsistOf("get-services-warning"))
   391  			})
   392  		})
   393  
   394  		When("fetching a service returns an error", func() {
   395  			BeforeEach(func() {
   396  				fakeCloudControllerClient.GetSpaceServicesReturns([]ccv2.Service{}, ccv2.Warnings{"get-services-warning"}, errors.New("oops"))
   397  			})
   398  
   399  			It("returns the error and all warnings", func() {
   400  				Expect(err).To(MatchError("oops"))
   401  				Expect(warnings).To(ConsistOf("get-services-warning"))
   402  			})
   403  		})
   404  
   405  		When("the service exists", func() {
   406  			var services []ccv2.Service
   407  
   408  			BeforeEach(func() {
   409  				services = []ccv2.Service{
   410  					{
   411  						Label:       "service",
   412  						Description: "service-description",
   413  					},
   414  				}
   415  				plans := []ccv2.ServicePlan{
   416  					{Name: "plan-a"},
   417  					{Name: "plan-b"},
   418  				}
   419  
   420  				fakeCloudControllerClient.GetSpaceServicesStub = func(guid string, filters ...ccv2.Filter) ([]ccv2.Service, ccv2.Warnings, error) {
   421  					filterToMatch := ccv2.Filter{
   422  						Type:     constant.LabelFilter,
   423  						Operator: constant.EqualOperator,
   424  						Values:   []string{"service"},
   425  					}
   426  
   427  					if len(filters) == 1 && reflect.DeepEqual(filters[0], filterToMatch) && spaceGUID == guid {
   428  						return services, ccv2.Warnings{"get-services-warning"}, nil
   429  					}
   430  
   431  					return []ccv2.Service{}, nil, nil
   432  				}
   433  
   434  				fakeCloudControllerClient.GetServicePlansReturns(plans, ccv2.Warnings{"get-plans-warning"}, nil)
   435  			})
   436  
   437  			It("filters for the service within the space, returning a service summary including plans and all warnings", func() {
   438  				Expect(err).NotTo(HaveOccurred())
   439  				Expect(serviceSummary).To(Equal(
   440  					ServiceSummary{
   441  						Service: Service{
   442  							Label:       "service",
   443  							Description: "service-description",
   444  						},
   445  						Plans: []ServicePlanSummary{
   446  							ServicePlanSummary{
   447  								ServicePlan: ServicePlan{
   448  									Name: "plan-a",
   449  								},
   450  							},
   451  							ServicePlanSummary{
   452  								ServicePlan: ServicePlan{
   453  									Name: "plan-b",
   454  								},
   455  							},
   456  						},
   457  					}))
   458  				Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   459  			})
   460  
   461  			Context("and fetching plans returns an error", func() {
   462  				BeforeEach(func() {
   463  					fakeCloudControllerClient.GetSpaceServicesReturns(services, ccv2.Warnings{"get-services-warning"}, nil)
   464  					fakeCloudControllerClient.GetServicePlansReturns([]ccv2.ServicePlan{}, ccv2.Warnings{"get-plans-warning"}, errors.New("plan-oops"))
   465  				})
   466  
   467  				It("returns the error and all warnings", func() {
   468  					Expect(err).To(MatchError("plan-oops"))
   469  					Expect(warnings).To(ConsistOf("get-services-warning", "get-plans-warning"))
   470  				})
   471  			})
   472  		})
   473  	})
   474  })