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

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     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/ccerror"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Application Instance With Stats Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  	})
    27  
    28  	Describe("ApplicationInstanceWithStats", func() {
    29  		var instance ApplicationInstanceWithStats
    30  
    31  		BeforeEach(func() {
    32  			instance = ApplicationInstanceWithStats{}
    33  		})
    34  
    35  		Describe("TimeSinceCreation", func() {
    36  			It("returns the time the instance started", func() {
    37  				instance.Since = 1485985587.12345
    38  				Expect(instance.TimeSinceCreation()).To(Equal(time.Unix(1485985587, 0)))
    39  			})
    40  		})
    41  	})
    42  
    43  	Describe("GetApplicationInstancesWithStatsByApplication", func() {
    44  		When("the application exists", func() {
    45  			BeforeEach(func() {
    46  				fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
    47  					map[int]ccv2.ApplicationInstanceStatus{
    48  						0: {
    49  							ID:               0,
    50  							CPU:              100,
    51  							Memory:           100,
    52  							MemoryQuota:      200,
    53  							Disk:             50,
    54  							DiskQuota:        100,
    55  							IsolationSegment: "some-isolation-segment",
    56  						},
    57  						1: {ID: 1, CPU: 200},
    58  					},
    59  					ccv2.Warnings{"stats-warning-1", "stats-warning-2"},
    60  					nil)
    61  
    62  				fakeCloudControllerClient.GetApplicationApplicationInstancesReturns(
    63  					map[int]ccv2.ApplicationInstance{
    64  						0: {ID: 0, Details: "hello", Since: 1485985587.12345, State: constant.ApplicationInstanceRunning},
    65  						1: {ID: 1, Details: "hi", Since: 1485985587.567},
    66  					},
    67  					ccv2.Warnings{"instance-warning-1", "instance-warning-2"},
    68  					nil)
    69  			})
    70  
    71  			It("returns the application instances and all warnings", func() {
    72  				instances, warnings, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
    73  				Expect(err).ToNot(HaveOccurred())
    74  				Expect(instances).To(ConsistOf(
    75  					ApplicationInstanceWithStats{
    76  						ID:               0,
    77  						CPU:              100,
    78  						Memory:           100,
    79  						MemoryQuota:      200,
    80  						Disk:             50,
    81  						DiskQuota:        100,
    82  						Details:          "hello",
    83  						IsolationSegment: "some-isolation-segment",
    84  						Since:            1485985587.12345,
    85  						State:            ApplicationInstanceState(constant.ApplicationInstanceRunning),
    86  					},
    87  					ApplicationInstanceWithStats{ID: 1, CPU: 200, Details: "hi", Since: 1485985587.567}))
    88  				Expect(warnings).To(ConsistOf(
    89  					"stats-warning-1",
    90  					"stats-warning-2",
    91  					"instance-warning-1",
    92  					"instance-warning-2"))
    93  
    94  				Expect(fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesCallCount()).To(Equal(1))
    95  				Expect(fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesArgsForCall(0)).To(Equal("some-app-guid"))
    96  				Expect(fakeCloudControllerClient.GetApplicationApplicationInstancesCallCount()).To(Equal(1))
    97  				Expect(fakeCloudControllerClient.GetApplicationApplicationInstancesArgsForCall(0)).To(Equal("some-app-guid"))
    98  			})
    99  		})
   100  
   101  		When("an error is encountered", func() {
   102  			var expectedErr error
   103  
   104  			BeforeEach(func() {
   105  				expectedErr = errors.New("banana")
   106  				fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   107  					nil,
   108  					ccv2.Warnings{"stats-warning"},
   109  					nil)
   110  				fakeCloudControllerClient.GetApplicationApplicationInstancesReturns(
   111  					nil,
   112  					ccv2.Warnings{"instances-warning"},
   113  					expectedErr)
   114  			})
   115  
   116  			It("returns the error and all warnings", func() {
   117  				_, warnings, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   118  				Expect(err).To(MatchError(expectedErr))
   119  				Expect(warnings).To(ConsistOf("stats-warning", "instances-warning"))
   120  			})
   121  
   122  			When("the application does not exist", func() {
   123  				BeforeEach(func() {
   124  					fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   125  						nil,
   126  						nil,
   127  						ccerror.ResourceNotFoundError{})
   128  				})
   129  
   130  				It("returns an ApplicationInstancesNotFoundError", func() {
   131  					_, _, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   132  					Expect(err).To(MatchError(actionerror.ApplicationInstancesNotFoundError{ApplicationGUID: "some-app-guid"}))
   133  				})
   134  			})
   135  
   136  			When("the desired state of the app is STARTED", func() {
   137  				When("the app has not been staged yet", func() {
   138  					When("getting instance stats returns a CF-AppStoppedStatsError", func() {
   139  						BeforeEach(func() {
   140  							fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   141  								nil,
   142  								nil,
   143  								ccerror.ApplicationStoppedStatsError{})
   144  						})
   145  
   146  						It("returns an ApplicationInstancesNotFoundError", func() {
   147  							_, _, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   148  							Expect(err).To(MatchError(actionerror.ApplicationInstancesNotFoundError{ApplicationGUID: "some-app-guid"}))
   149  						})
   150  					})
   151  				})
   152  
   153  				When("the app is not yet running", func() {
   154  					When("getting instance stats returns a CF-AppStoppedStatsError", func() {
   155  						BeforeEach(func() {
   156  							fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   157  								nil,
   158  								nil,
   159  								ccerror.ApplicationStoppedStatsError{})
   160  						})
   161  
   162  						It("returns an ApplicationInstancesNotFoundError", func() {
   163  							_, _, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   164  							Expect(err).To(MatchError(actionerror.ApplicationInstancesNotFoundError{ApplicationGUID: "some-app-guid"}))
   165  						})
   166  					})
   167  				})
   168  			})
   169  		})
   170  
   171  		When("getting the stats and instances return different number of results", func() {
   172  			When("an instance is missing from stats", func() {
   173  				BeforeEach(func() {
   174  					fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   175  						map[int]ccv2.ApplicationInstanceStatus{
   176  							0: {ID: 0},
   177  						}, nil, nil)
   178  
   179  					fakeCloudControllerClient.GetApplicationApplicationInstancesReturns(
   180  						map[int]ccv2.ApplicationInstance{
   181  							0: {ID: 0},
   182  							1: {ID: 1, Details: "backend details"},
   183  						}, nil, nil)
   184  				})
   185  
   186  				It("sets the detail field to incomplete", func() {
   187  					instances, _, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   188  					Expect(err).ToNot(HaveOccurred())
   189  					Expect(instances).To(ConsistOf(
   190  						ApplicationInstanceWithStats{ID: 0},
   191  						ApplicationInstanceWithStats{ID: 1, Details: "backend details (Unable to retrieve information)"},
   192  					))
   193  				})
   194  			})
   195  
   196  			When("an instance is missing from instances", func() {
   197  				BeforeEach(func() {
   198  					fakeCloudControllerClient.GetApplicationApplicationInstanceStatusesReturns(
   199  						map[int]ccv2.ApplicationInstanceStatus{
   200  							0: {ID: 0},
   201  							1: {ID: 1},
   202  						}, nil, nil)
   203  
   204  					fakeCloudControllerClient.GetApplicationApplicationInstancesReturns(
   205  						map[int]ccv2.ApplicationInstance{
   206  							0: {ID: 0},
   207  						}, nil, nil)
   208  				})
   209  
   210  				It("sets the detail field to incomplete", func() {
   211  					instances, _, err := actor.GetApplicationInstancesWithStatsByApplication("some-app-guid")
   212  					Expect(err).ToNot(HaveOccurred())
   213  					Expect(instances).To(ConsistOf(
   214  						ApplicationInstanceWithStats{ID: 0},
   215  						ApplicationInstanceWithStats{ID: 1, Details: "(Unable to retrieve information)"},
   216  					))
   217  				})
   218  			})
   219  		})
   220  	})
   221  })