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

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    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("Application Summary Actions", func() {
    17  	Describe("ApplicationSummary", func() {
    18  		Describe("StartingOrRunningInstanceCount", func() {
    19  			It("only counts the running and starting instances", func() {
    20  				app := ApplicationSummary{
    21  					RunningInstances: []ApplicationInstanceWithStats{
    22  						{State: ApplicationInstanceState(constant.ApplicationInstanceCrashed)},
    23  						{State: ApplicationInstanceState(constant.ApplicationInstanceDown)},
    24  						{State: ApplicationInstanceState(constant.ApplicationInstanceFlapping)},
    25  						{State: ApplicationInstanceState(constant.ApplicationInstanceRunning)},
    26  						{State: ApplicationInstanceState(constant.ApplicationInstanceStarting)},
    27  						{State: ApplicationInstanceState(constant.ApplicationInstanceUnknown)},
    28  					},
    29  				}
    30  				Expect(app.StartingOrRunningInstanceCount()).To(Equal(2))
    31  			})
    32  		})
    33  	})
    34  
    35  	Describe("GetApplicationSummaryByNameSpace", func() {
    36  		var (
    37  			actor                     *Actor
    38  			fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    39  			ccApp                     ccv2.Application
    40  		)
    41  
    42  		BeforeEach(func() {
    43  			fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    44  			actor = NewActor(fakeCloudControllerClient, nil, nil)
    45  			ccApp = ccv2.Application{
    46  				GUID: "some-app-guid",
    47  				Name: "some-app",
    48  			}
    49  		})
    50  
    51  		When("the application does not exist", func() {
    52  			BeforeEach(func() {
    53  				fakeCloudControllerClient.GetApplicationsReturns(
    54  					[]ccv2.Application{},
    55  					ccv2.Warnings{"app-warning"},
    56  					nil)
    57  			})
    58  
    59  			It("returns an ApplicationNotFoundError and all warnings", func() {
    60  				_, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
    61  				Expect(err).To(MatchError(actionerror.ApplicationNotFoundError{Name: "some-app"}))
    62  				Expect(warnings).To(ConsistOf("app-warning"))
    63  			})
    64  		})
    65  
    66  		When("the application exists", func() {
    67  			BeforeEach(func() {
    68  				fakeCloudControllerClient.GetApplicationsReturns(
    69  					[]ccv2.Application{ccApp},
    70  					ccv2.Warnings{"app-warning"},
    71  					nil)
    72  			})
    73  
    74  			When("the application is STARTED", func() {
    75  				BeforeEach(func() {
    76  					ccApp.State = constant.ApplicationStarted
    77  					fakeCloudControllerClient.GetApplicationsReturns(
    78  						[]ccv2.Application{ccApp},
    79  						ccv2.Warnings{"app-warning"},
    80  						nil)
    81  				})
    82  
    83  				When("instance information is available", func() {
    84  					BeforeEach(func() {
    85  						fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
    86  							map[int]ccv2.ApplicationInstanceStatus{
    87  								0: {ID: 0, IsolationSegment: "isolation-segment-1"},
    88  								1: {ID: 1, IsolationSegment: "isolation-segment-2"}, // should never happen; iso segs for 2 instances of the same app should match.
    89  							},
    90  							ccv2.Warnings{"stats-warning"},
    91  							nil)
    92  						fakeCloudControllerClient.GetApplicationApplicationInstancesReturns(
    93  							map[int]ccv2.ApplicationInstance{
    94  								0: {ID: 0},
    95  								1: {ID: 1},
    96  							},
    97  							ccv2.Warnings{"instance-warning"},
    98  							nil)
    99  					})
   100  
   101  					It("returns the application with instance information and warnings and populates isolation segment from the first instance", func() {
   102  						app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   103  						Expect(err).ToNot(HaveOccurred())
   104  						Expect(app).To(Equal(ApplicationSummary{
   105  							Application: Application{
   106  								GUID:  "some-app-guid",
   107  								Name:  "some-app",
   108  								State: constant.ApplicationStarted,
   109  							},
   110  							RunningInstances: []ApplicationInstanceWithStats{
   111  								{ID: 0, IsolationSegment: "isolation-segment-1"},
   112  								{ID: 1, IsolationSegment: "isolation-segment-2"},
   113  							},
   114  							IsolationSegment: "isolation-segment-1",
   115  						}))
   116  						Expect(warnings).To(ConsistOf("app-warning", "stats-warning", "instance-warning"))
   117  					})
   118  				})
   119  
   120  				When("instance information is not available", func() {
   121  					BeforeEach(func() {
   122  						fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   123  							nil,
   124  							ccv2.Warnings{"stats-warning"},
   125  							ccerror.ApplicationStoppedStatsError{})
   126  					})
   127  
   128  					It("returns the empty list of instances and all warnings", func() {
   129  						app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   130  						Expect(err).ToNot(HaveOccurred())
   131  						Expect(app.RunningInstances).To(BeEmpty())
   132  						Expect(warnings).To(ConsistOf("app-warning", "stats-warning"))
   133  					})
   134  				})
   135  			})
   136  
   137  			When("the application is not STARTED", func() {
   138  				BeforeEach(func() {
   139  					ccApp.State = constant.ApplicationStopped
   140  				})
   141  
   142  				It("does not try and get application instance information", func() {
   143  					app, _, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   144  					Expect(err).ToNot(HaveOccurred())
   145  					Expect(app.RunningInstances).To(BeEmpty())
   146  
   147  					Expect(fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesCallCount()).To(Equal(0))
   148  					Expect(fakeCloudControllerClient.GetApplicationApplicationInstancesCallCount()).To(Equal(0))
   149  				})
   150  			})
   151  
   152  			When("the app has routes", func() {
   153  				BeforeEach(func() {
   154  					fakeCloudControllerClient.GetApplicationRoutesReturns(
   155  						[]ccv2.Route{
   156  							{
   157  								GUID: "some-route-1-guid",
   158  								Host: "host-1",
   159  							},
   160  							{
   161  								GUID: "some-route-2-guid",
   162  								Host: "host-2",
   163  							},
   164  						},
   165  						ccv2.Warnings{"get-application-routes-warning"},
   166  						nil)
   167  				})
   168  
   169  				It("returns the routes and all warnings", func() {
   170  					app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   171  					Expect(err).ToNot(HaveOccurred())
   172  					Expect(warnings).To(ConsistOf("app-warning", "get-application-routes-warning"))
   173  					Expect(app.Routes).To(ConsistOf(
   174  						Route{
   175  							GUID: "some-route-1-guid",
   176  							Host: "host-1",
   177  						},
   178  						Route{
   179  							GUID: "some-route-2-guid",
   180  							Host: "host-2",
   181  						},
   182  					))
   183  				})
   184  
   185  				When("an error is encountered while getting routes", func() {
   186  					var expectedErr error
   187  
   188  					BeforeEach(func() {
   189  						expectedErr = errors.New("get routes error")
   190  						fakeCloudControllerClient.GetApplicationRoutesReturns(
   191  							nil,
   192  							ccv2.Warnings{"get-application-routes-warning"},
   193  							expectedErr,
   194  						)
   195  					})
   196  
   197  					It("returns the error and all warnings", func() {
   198  						app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   199  						Expect(err).To(MatchError(expectedErr))
   200  						Expect(app.Routes).To(BeEmpty())
   201  						Expect(warnings).To(ConsistOf("app-warning", "get-application-routes-warning"))
   202  					})
   203  				})
   204  			})
   205  
   206  			When("the app has stack information", func() {
   207  				BeforeEach(func() {
   208  					fakeCloudControllerClient.GetStackReturns(
   209  						ccv2.Stack{Name: "some-stack"},
   210  						ccv2.Warnings{"get-application-stack-warning"},
   211  						nil)
   212  				})
   213  
   214  				It("returns the stack information and all warnings", func() {
   215  					app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   216  					Expect(err).ToNot(HaveOccurred())
   217  					Expect(warnings).To(ConsistOf("app-warning", "get-application-stack-warning"))
   218  					Expect(app.Stack).To(Equal(Stack{Name: "some-stack"}))
   219  				})
   220  
   221  				When("an error is encountered while getting stack", func() {
   222  					var expectedErr error
   223  
   224  					BeforeEach(func() {
   225  						expectedErr = errors.New("get stack error")
   226  						fakeCloudControllerClient.GetStackReturns(
   227  							ccv2.Stack{},
   228  							ccv2.Warnings{"get-application-stack-warning"},
   229  							expectedErr,
   230  						)
   231  					})
   232  
   233  					It("returns the error and all warnings", func() {
   234  						app, warnings, err := actor.GetApplicationSummaryByNameAndSpace("some-app", "some-space-guid")
   235  						Expect(err).To(MatchError(expectedErr))
   236  						Expect(app.Stack).To(Equal(Stack{}))
   237  						Expect(warnings).To(ConsistOf("app-warning", "get-application-stack-warning"))
   238  					})
   239  				})
   240  			})
   241  		})
   242  	})
   243  })