github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/actor/v7action/logging_test.go (about) 1 package v7action_test 2 3 import ( 4 "context" 5 "errors" 6 "time" 7 8 "code.cloudfoundry.org/cli/actor/sharedaction" 9 "code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes" 10 . "code.cloudfoundry.org/cli/actor/v7action" 11 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 12 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 13 "code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2" 14 logcache "code.cloudfoundry.org/log-cache/pkg/client" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Logging Actions", func() { 20 var ( 21 actor *Actor 22 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 23 fakeConfig *v7actionfakes.FakeConfig 24 fakeLogCacheClient *sharedactionfakes.FakeLogCacheClient 25 ) 26 27 BeforeEach(func() { 28 actor, fakeCloudControllerClient, fakeConfig, _, _, _ = NewTestActor() 29 fakeLogCacheClient = new(sharedactionfakes.FakeLogCacheClient) 30 fakeConfig.AccessTokenReturns("AccessTokenForTest") 31 }) 32 33 Describe("GetRecentLogsForApplicationByNameAndSpace", func() { 34 When("the application can be found", func() { 35 BeforeEach(func() { 36 fakeCloudControllerClient.GetApplicationsReturns( 37 []ccv3.Application{ 38 { 39 Name: "some-app", 40 GUID: "some-app-guid", 41 }, 42 }, 43 ccv3.Warnings{"some-app-warnings"}, 44 nil, 45 ) 46 }) 47 48 When("Log Cache returns logs", func() { 49 BeforeEach(func() { 50 messages := []*loggregator_v2.Envelope{ 51 { 52 Timestamp: int64(20), 53 SourceId: "some-app-guid", 54 InstanceId: "some-source-instance", 55 Message: &loggregator_v2.Envelope_Log{ 56 Log: &loggregator_v2.Log{ 57 Payload: []byte("message-2"), 58 Type: loggregator_v2.Log_OUT, 59 }, 60 }, 61 Tags: map[string]string{ 62 "source_type": "some-source-type", 63 }, 64 }, 65 { 66 Timestamp: int64(10), 67 SourceId: "some-app-guid", 68 InstanceId: "some-source-instance", 69 Message: &loggregator_v2.Envelope_Log{ 70 Log: &loggregator_v2.Log{ 71 Payload: []byte("message-1"), 72 Type: loggregator_v2.Log_OUT, 73 }, 74 }, 75 Tags: map[string]string{ 76 "source_type": "some-source-type", 77 }, 78 }, 79 } 80 81 fakeLogCacheClient.ReadReturns(messages, nil) 82 }) 83 84 It("returns all the recent logs and warnings", func() { 85 messages, warnings, err := actor.GetRecentLogsForApplicationByNameAndSpace("some-app", "some-space-guid", fakeLogCacheClient) 86 Expect(err).ToNot(HaveOccurred()) 87 Expect(warnings).To(ConsistOf("some-app-warnings")) 88 89 Expect(messages[0].Message()).To(Equal("message-1")) 90 Expect(messages[0].Type()).To(Equal("OUT")) 91 Expect(messages[0].Timestamp()).To(Equal(time.Unix(0, 10))) 92 Expect(messages[0].SourceType()).To(Equal("some-source-type")) 93 Expect(messages[0].SourceInstance()).To(Equal("some-source-instance")) 94 95 Expect(messages[1].Message()).To(Equal("message-2")) 96 Expect(messages[1].Type()).To(Equal("OUT")) 97 Expect(messages[1].Timestamp()).To(Equal(time.Unix(0, 20))) 98 Expect(messages[1].SourceType()).To(Equal("some-source-type")) 99 Expect(messages[1].SourceInstance()).To(Equal("some-source-instance")) 100 }) 101 }) 102 103 When("Log Cache errors", func() { 104 var expectedErr error 105 106 BeforeEach(func() { 107 expectedErr = errors.New("ZOMG") 108 fakeLogCacheClient.ReadReturns(nil, expectedErr) 109 }) 110 111 It("returns error and warnings", func() { 112 _, warnings, err := actor.GetRecentLogsForApplicationByNameAndSpace("some-app", "some-space-guid", fakeLogCacheClient) 113 Expect(err).To(MatchError(expectedErr)) 114 Expect(warnings).To(ConsistOf("some-app-warnings")) 115 }) 116 }) 117 }) 118 119 When("finding the application errors", func() { 120 var expectedErr error 121 122 BeforeEach(func() { 123 expectedErr = errors.New("ZOMG") 124 fakeCloudControllerClient.GetApplicationsReturns( 125 nil, 126 ccv3.Warnings{"some-app-warnings"}, 127 expectedErr, 128 ) 129 }) 130 131 It("returns error and warnings", func() { 132 _, warnings, err := actor.GetRecentLogsForApplicationByNameAndSpace("some-app", "some-space-guid", fakeLogCacheClient) 133 Expect(err).To(MatchError(expectedErr)) 134 Expect(warnings).To(ConsistOf("some-app-warnings")) 135 }) 136 }) 137 }) 138 139 Describe("GetStreamingLogsForApplicationByNameAndSpace", func() { 140 When("the application can be found", func() { 141 var ( 142 expectedAppGUID string 143 144 messages <-chan sharedaction.LogMessage 145 logErrs <-chan error 146 stopStreaming context.CancelFunc 147 ) 148 149 AfterEach(func() { 150 Eventually(messages).Should(BeClosed()) 151 Eventually(logErrs).Should(BeClosed()) 152 }) 153 154 BeforeEach(func() { 155 expectedAppGUID = "some-app-guid" 156 157 fakeCloudControllerClient.GetApplicationsReturns( 158 []ccv3.Application{ 159 { 160 Name: "some-app", 161 GUID: expectedAppGUID, 162 }, 163 }, 164 ccv3.Warnings{"some-app-warnings"}, 165 nil, 166 ) 167 168 fakeConfig.DialTimeoutReturns(60 * time.Minute) 169 fakeLogCacheClient.ReadStub = func( 170 ctx context.Context, 171 sourceID string, 172 start time.Time, 173 opts ...logcache.ReadOption, 174 ) ([]*loggregator_v2.Envelope, error) { 175 if fakeLogCacheClient.ReadCallCount() > 2 { 176 stopStreaming() 177 } 178 179 return []*loggregator_v2.Envelope{{ 180 // 2 seconds in the past to get past Walk delay 181 Timestamp: time.Now().Add(-3 * time.Second).UnixNano(), 182 SourceId: expectedAppGUID, 183 InstanceId: "some-source-instance", 184 Message: &loggregator_v2.Envelope_Log{ 185 Log: &loggregator_v2.Log{ 186 Payload: []byte("message-1"), 187 Type: loggregator_v2.Log_OUT, 188 }, 189 }, 190 Tags: map[string]string{ 191 "source_type": "some-source-type", 192 }, 193 }, { 194 // 2 seconds in the past to get past Walk delay 195 Timestamp: time.Now().Add(-2 * time.Second).UnixNano(), 196 SourceId: expectedAppGUID, 197 InstanceId: "some-source-instance", 198 Message: &loggregator_v2.Envelope_Log{ 199 Log: &loggregator_v2.Log{ 200 Payload: []byte("message-2"), 201 Type: loggregator_v2.Log_OUT, 202 }, 203 }, 204 Tags: map[string]string{ 205 "source_type": "some-source-type", 206 }, 207 }}, ctx.Err() 208 } 209 }) 210 211 It("converts them to log messages and passes them through the messages channel", func() { 212 var err error 213 var warnings Warnings 214 var message sharedaction.LogMessage 215 216 messages, logErrs, stopStreaming, warnings, err = actor.GetStreamingLogsForApplicationByNameAndSpace("some-app", "some-space-guid", fakeLogCacheClient) 217 218 Expect(err).ToNot(HaveOccurred()) 219 Expect(warnings).To(ConsistOf("some-app-warnings")) 220 221 Eventually(messages).Should(Receive(&message)) 222 Expect(message.Message()).To(Equal("message-1")) 223 224 Eventually(messages).Should(Receive(&message)) 225 Expect(message.Message()).To(Equal("message-2")) 226 }) 227 }) 228 229 When("finding the application errors", func() { 230 var expectedErr error 231 232 BeforeEach(func() { 233 expectedErr = errors.New("ZOMG") 234 fakeCloudControllerClient.GetApplicationsReturns( 235 nil, 236 ccv3.Warnings{"some-app-warnings"}, 237 expectedErr, 238 ) 239 }) 240 241 It("returns error and warnings", func() { 242 _, _, _, warnings, err := actor.GetStreamingLogsForApplicationByNameAndSpace("some-app", "some-space-guid", fakeLogCacheClient) 243 Expect(err).To(MatchError(expectedErr)) 244 Expect(warnings).To(ConsistOf("some-app-warnings")) 245 246 Expect(fakeLogCacheClient.ReadCallCount()).To(Equal(0)) 247 }) 248 }) 249 }) 250 })