@@ -15,7 +15,7 @@ describe('Container Logger tests', () => {
15
15
16
16
describe ( 'positive' , ( ) => {
17
17
18
- it ( 'should handle a received message on data stream event in case tty is true ' , ( ) => {
18
+ it ( 'should separate the stdout from stderr ' , ( ) => {
19
19
const containerInspect = {
20
20
Config : {
21
21
Tty : true
@@ -36,32 +36,91 @@ describe('Container Logger tests', () => {
36
36
callback ( null , containerInspect ) ;
37
37
} ,
38
38
logs : ( options , callback ) => {
39
+ expect ( options . stdout ^ options . stderr ) . to . be . trusty ; // jshint ignore:line
39
40
callback ( null , stream ) ;
40
41
}
41
42
} ;
42
43
const firebaseLogger = { } ;
43
44
const firebaseLastUpdate = { } ;
44
45
const loggerStrategy = LoggerStrategy . LOGS ;
45
46
47
+ const containerLogger = new ContainerLogger ( containerId , containerInterface , firebaseLogger , firebaseLastUpdate , loggerStrategy ) ;
48
+ containerLogger . _logMessageToFirebase = sinon . spy ( ) ;
49
+ return containerLogger . start ( ) ;
50
+ } ) ;
51
+
52
+ it ( 'should handle a send message from stderr as error' , ( ) => {
53
+ const containerInspect = {
54
+ Config : {
55
+ Tty : true
56
+ }
57
+ } ;
58
+
59
+ let receivedStdoutEvent , receivedStderrEvent ;
60
+ const stdoutStream = {
61
+ on : ( event , callback ) => {
62
+ if ( event !== 'end' ) {
63
+ receivedStdoutEvent = event ;
64
+ callback ( 'message' ) ;
65
+ }
66
+ }
67
+ } ;
68
+ const stderrStream = {
69
+ on : ( event , callback ) => {
70
+ if ( event !== 'end' ) {
71
+ receivedStderrEvent = event ;
72
+ callback ( 'error' ) ;
73
+ }
74
+ }
75
+ } ;
76
+
77
+ const containerId = 'containerId' ;
78
+ const containerInterface = {
79
+ inspect : ( callback ) => {
80
+ callback ( null , containerInspect ) ;
81
+ } ,
82
+ logs : ( options , callback ) => {
83
+ if ( options . stdout ) {
84
+ callback ( null , stdoutStream ) ;
85
+ } else {
86
+ callback ( null , stderrStream ) ;
87
+ }
88
+ }
89
+ } ;
90
+ const firebaseLogger = { } ;
91
+ const firebaseLastUpdate = { } ;
92
+ const loggerStrategy = LoggerStrategy . LOGS ;
93
+
46
94
const containerLogger = new ContainerLogger ( containerId , containerInterface , firebaseLogger , firebaseLastUpdate , loggerStrategy ) ;
47
95
containerLogger . _logMessageToFirebase = sinon . spy ( ) ;
48
96
return containerLogger . start ( )
49
97
. then ( ( ) => {
50
- expect ( receivedEvent ) . to . equal ( 'data' ) ;
51
- expect ( containerLogger . _logMessageToFirebase ) . to . have . been . calledOnce ; // jshint ignore:line
98
+ expect ( containerLogger . _logMessageToFirebase ) . to . have . been . calledWith ( 'message' , false ) ; // jshint ignore:line
99
+ expect ( containerLogger . _logMessageToFirebase ) . to . have . been . calledWith ( 'error' , true ) ; // jshint ignore:line
52
100
} ) ;
53
101
} ) ;
54
102
55
- it ( 'should handle a received message on readable stream event in case tty is false ' , ( ) => {
103
+ it ( 'should handle a received message on data stream event in case tty is true ' , ( ) => {
56
104
const containerInspect = {
57
105
Config : {
58
- Tty : false
106
+ Tty : true
59
107
}
60
108
} ;
61
- const stream = {
109
+
110
+ let receivedStdoutEvent , receivedStderrEvent ;
111
+ const stdoutStream = {
62
112
on : ( event , callback ) => {
63
- if ( event === 'end' ) {
64
- callback ( ) ;
113
+ if ( event !== 'end' ) {
114
+ receivedStdoutEvent = event ;
115
+ callback ( 'message' ) ;
116
+ }
117
+ }
118
+ } ;
119
+ const stderrStream = {
120
+ on : ( event , callback ) => {
121
+ if ( event !== 'end' ) {
122
+ receivedStderrEvent = event ;
123
+ callback ( 'message' ) ;
65
124
}
66
125
}
67
126
} ;
@@ -72,7 +131,11 @@ describe('Container Logger tests', () => {
72
131
callback ( null , containerInspect ) ;
73
132
} ,
74
133
logs : ( options , callback ) => {
75
- callback ( null , stream ) ;
134
+ if ( options . stdout ) {
135
+ callback ( null , stdoutStream ) ;
136
+ } else {
137
+ callback ( null , stderrStream ) ;
138
+ }
76
139
}
77
140
} ;
78
141
const firebaseLogger = { } ;
@@ -81,9 +144,47 @@ describe('Container Logger tests', () => {
81
144
82
145
const containerLogger = new ContainerLogger ( containerId , containerInterface , firebaseLogger , firebaseLastUpdate , loggerStrategy ) ;
83
146
containerLogger . _logMessageToFirebase = sinon . spy ( ) ;
84
- return containerLogger . start ( ) ;
147
+ return containerLogger . start ( )
148
+ . then ( ( ) => {
149
+ expect ( receivedStdoutEvent ) . to . equal ( 'data' ) ;
150
+ expect ( receivedStderrEvent ) . to . equal ( 'data' ) ;
151
+ expect ( containerLogger . _logMessageToFirebase ) . to . have . been . calledTwice ; // jshint ignore:line
152
+ } ) ;
85
153
} ) ;
86
154
155
+ it ( 'should handle a received message on readable stream event in case tty is false' ,
156
+ ( ) => {
157
+ const containerInspect = {
158
+ Config : {
159
+ Tty : false
160
+ }
161
+ } ;
162
+ const stream = {
163
+ on : ( event , callback ) => {
164
+ if ( event === 'end' ) {
165
+ callback ( ) ;
166
+ }
167
+ }
168
+ } ;
169
+
170
+ const containerId = 'containerId' ;
171
+ const containerInterface = {
172
+ inspect : ( callback ) => {
173
+ callback ( null , containerInspect ) ;
174
+ } ,
175
+ logs : ( options , callback ) => {
176
+ callback ( null , stream ) ;
177
+ }
178
+ } ;
179
+ const firebaseLogger = { } ;
180
+ const firebaseLastUpdate = { } ;
181
+ const loggerStrategy = LoggerStrategy . LOGS ;
182
+
183
+ const containerLogger = new ContainerLogger ( containerId , containerInterface , firebaseLogger , firebaseLastUpdate , loggerStrategy ) ;
184
+ containerLogger . _logMessageToFirebase = sinon . spy ( ) ;
185
+ return containerLogger . start ( ) ;
186
+ } ) ;
187
+
87
188
} ) ;
88
189
89
190
describe ( 'negative' , ( ) => {
@@ -109,7 +210,9 @@ describe('Container Logger tests', () => {
109
210
. then ( ( ) => {
110
211
return Q . reject ( new Error ( 'should have failed' ) ) ;
111
212
} , ( err ) => {
112
- expect ( err . toString ( ) ) . to . contain ( 'Strategy: non-existing-strategy is not supported' ) ;
213
+ expect ( err . toString ( ) )
214
+ . to
215
+ . contain ( 'Strategy: non-existing-strategy is not supported' ) ;
113
216
} ) ;
114
217
} ) ;
115
218
@@ -139,14 +242,14 @@ describe('Container Logger tests', () => {
139
242
Tty : true
140
243
}
141
244
} ;
142
- let receivedAttachOptions ;
245
+ let receivedAttachOptions = [ ] ;
143
246
const containerId = 'containerId' ;
144
247
const containerInterface = {
145
248
inspect : ( callback ) => {
146
249
callback ( null , containerInspect ) ;
147
250
} ,
148
251
attach : ( options , callback ) => {
149
- receivedAttachOptions = options ;
252
+ receivedAttachOptions . push ( options ) ;
150
253
callback ( new Error ( 'attach error' ) ) ;
151
254
}
152
255
} ;
@@ -159,11 +262,10 @@ describe('Container Logger tests', () => {
159
262
. then ( ( ) => {
160
263
return Q . reject ( new Error ( 'should have failed' ) ) ;
161
264
} , ( err ) => {
162
- expect ( receivedAttachOptions ) . to . deep . equal ( {
163
- 'stderr' : true ,
164
- 'stdout' : true ,
165
- 'stream' : true ,
166
- 'tty' : true
265
+ receivedAttachOptions . forEach ( ( options ) => {
266
+ expect ( options . stdout ^ options . stderr ) . to . equal ( 1 ) ;
267
+ expect ( options . stream ) . to . equal ( true ) ;
268
+ expect ( options . tty ) . to . equal ( true ) ;
167
269
} ) ;
168
270
expect ( err . toString ( ) ) . to . contain ( 'attach error' ) ;
169
271
} ) ;
@@ -175,14 +277,14 @@ describe('Container Logger tests', () => {
175
277
Tty : true
176
278
}
177
279
} ;
178
- let receivedLogsOptions ;
280
+ let receivedLogsOptions = [ ] ;
179
281
const containerId = 'containerId' ;
180
282
const containerInterface = {
181
283
inspect : ( callback ) => {
182
284
callback ( null , containerInspect ) ;
183
285
} ,
184
286
logs : ( options , callback ) => {
185
- receivedLogsOptions = options ;
287
+ receivedLogsOptions . push ( options ) ;
186
288
callback ( new Error ( 'logs error' ) ) ;
187
289
}
188
290
} ;
@@ -195,10 +297,9 @@ describe('Container Logger tests', () => {
195
297
. then ( ( ) => {
196
298
return Q . reject ( new Error ( 'should have failed' ) ) ;
197
299
} , ( err ) => {
198
- expect ( receivedLogsOptions ) . to . deep . equal ( {
199
- 'follow' : 1 ,
200
- 'stderr' : 1 ,
201
- 'stdout' : 1
300
+ receivedLogsOptions . forEach ( ( options ) => {
301
+ expect ( options . stdout ^ options . stderr ) . to . equal ( 1 ) ;
302
+ expect ( options . follow ) . to . equal ( 1 ) ;
202
303
} ) ;
203
304
expect ( err . toString ( ) ) . to . contain ( 'logs error' ) ;
204
305
} ) ;
@@ -231,6 +332,28 @@ describe('Container Logger tests', () => {
231
332
expect ( pushSpy ) . to . have . been . calledWith ( 'message' ) ; // jshint ignore:line
232
333
expect ( setSpy ) . to . have . been . calledOnce ; // jshint ignore:line
233
334
} ) ;
335
+
336
+ it ( 'should log error to firebase with red decoration' , ( ) => {
337
+
338
+ const containerId = 'containerId' ;
339
+ const containerInterface = { } ;
340
+
341
+ const pushSpy = sinon . spy ( ) ;
342
+ const setSpy = sinon . spy ( ) ;
343
+ const firebaseLogger = {
344
+ push : pushSpy
345
+ } ;
346
+ const firebaseLastUpdate = {
347
+ set : setSpy
348
+ } ;
349
+ const loggerStrategy = LoggerStrategy . LOGS ;
350
+
351
+ const containerLogger = new ContainerLogger ( containerId , containerInterface , firebaseLogger , firebaseLastUpdate , loggerStrategy ) ;
352
+ containerLogger . _logMessageToFirebase ( 'message' , true ) ;
353
+ expect ( pushSpy ) . to . have . been . calledOnce ; // jshint ignore:line
354
+ expect ( pushSpy ) . to . have . been . calledWith ( '\x1B[31mmessage\x1B[0m' ) ; // jshint ignore:line
355
+ expect ( setSpy ) . to . have . been . calledOnce ; // jshint ignore:line
356
+ } ) ;
234
357
235
358
} ) ;
236
359
0 commit comments