Skip to content

Commit 1eb9a81

Browse files
committed
Adding tests
1 parent e31dbd2 commit 1eb9a81

File tree

1 file changed

+147
-24
lines changed

1 file changed

+147
-24
lines changed

test/ContainerLogger.unit.spec.js

Lines changed: 147 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Container Logger tests', () => {
1515

1616
describe('positive', () => {
1717

18-
it('should handle a received message on data stream event in case tty is true', () => {
18+
it('should separate the stdout from stderr', () => {
1919
const containerInspect = {
2020
Config: {
2121
Tty: true
@@ -36,32 +36,91 @@ describe('Container Logger tests', () => {
3636
callback(null, containerInspect);
3737
},
3838
logs: (options, callback) => {
39+
expect(options.stdout ^ options.stderr).to.be.trusty; // jshint ignore:line
3940
callback(null, stream);
4041
}
4142
};
4243
const firebaseLogger = {};
4344
const firebaseLastUpdate = {};
4445
const loggerStrategy = LoggerStrategy.LOGS;
4546

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+
4694
const containerLogger = new ContainerLogger(containerId, containerInterface, firebaseLogger, firebaseLastUpdate, loggerStrategy);
4795
containerLogger._logMessageToFirebase = sinon.spy();
4896
return containerLogger.start()
4997
.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
52100
});
53101
});
54102

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', () => {
56104
const containerInspect = {
57105
Config: {
58-
Tty: false
106+
Tty: true
59107
}
60108
};
61-
const stream = {
109+
110+
let receivedStdoutEvent, receivedStderrEvent;
111+
const stdoutStream = {
62112
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');
65124
}
66125
}
67126
};
@@ -72,7 +131,11 @@ describe('Container Logger tests', () => {
72131
callback(null, containerInspect);
73132
},
74133
logs: (options, callback) => {
75-
callback(null, stream);
134+
if (options.stdout) {
135+
callback(null, stdoutStream);
136+
} else {
137+
callback(null, stderrStream);
138+
}
76139
}
77140
};
78141
const firebaseLogger = {};
@@ -81,9 +144,47 @@ describe('Container Logger tests', () => {
81144

82145
const containerLogger = new ContainerLogger(containerId, containerInterface, firebaseLogger, firebaseLastUpdate, loggerStrategy);
83146
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+
});
85153
});
86154

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+
87188
});
88189

89190
describe('negative', () => {
@@ -109,7 +210,9 @@ describe('Container Logger tests', () => {
109210
.then(() => {
110211
return Q.reject(new Error('should have failed'));
111212
}, (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');
113216
});
114217
});
115218

@@ -139,14 +242,14 @@ describe('Container Logger tests', () => {
139242
Tty: true
140243
}
141244
};
142-
let receivedAttachOptions;
245+
let receivedAttachOptions = [];
143246
const containerId = 'containerId';
144247
const containerInterface = {
145248
inspect: (callback) => {
146249
callback(null, containerInspect);
147250
},
148251
attach: (options, callback) => {
149-
receivedAttachOptions = options;
252+
receivedAttachOptions.push(options);
150253
callback(new Error('attach error'));
151254
}
152255
};
@@ -159,11 +262,10 @@ describe('Container Logger tests', () => {
159262
.then(() => {
160263
return Q.reject(new Error('should have failed'));
161264
}, (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);
167269
});
168270
expect(err.toString()).to.contain('attach error');
169271
});
@@ -175,14 +277,14 @@ describe('Container Logger tests', () => {
175277
Tty: true
176278
}
177279
};
178-
let receivedLogsOptions;
280+
let receivedLogsOptions = [];
179281
const containerId = 'containerId';
180282
const containerInterface = {
181283
inspect: (callback) => {
182284
callback(null, containerInspect);
183285
},
184286
logs: (options, callback) => {
185-
receivedLogsOptions = options;
287+
receivedLogsOptions.push(options);
186288
callback(new Error('logs error'));
187289
}
188290
};
@@ -195,10 +297,9 @@ describe('Container Logger tests', () => {
195297
.then(() => {
196298
return Q.reject(new Error('should have failed'));
197299
}, (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);
202303
});
203304
expect(err.toString()).to.contain('logs error');
204305
});
@@ -231,6 +332,28 @@ describe('Container Logger tests', () => {
231332
expect(pushSpy).to.have.been.calledWith('message'); // jshint ignore:line
232333
expect(setSpy).to.have.been.calledOnce; // jshint ignore:line
233334
});
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+
});
234357

235358
});
236359

0 commit comments

Comments
 (0)