github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v2action/application_summary_test.go (about)

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