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