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