Repository navigation
Expand file tree
/
Copy pathdocker.spec.js
More file actions
263 lines (231 loc) · 8.11 KB
/
Copy pathdocker.spec.js
File metadata and controls
263 lines (231 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Tests for docker.
* @file docker.spec.js
*/
'use strict';
// Setup chai.
const chai = require('chai');
const expect = chai.expect;
const filesystem = require('mock-fs');
chai.should();
chai.use(require('chai-as-promised'));
const sinon = require('sinon');
const Landerode = require('./../lib/docker');
const landerode = new Landerode();
const Dockerode = require('dockerode');
const Promise = require('./../lib/promise');
const _ = require('lodash');
const dummyContainer = (overrides = {}) => {
return _.assign(
new Dockerode.Container(),
{
Id: '8675309',
app: 'Death Star',
Labels: {
'com.docker.compose.project': 'Death Star',
'com.docker.compose.service': 'Exhaust Port',
'com.docker.compose.container-number': 73,
'com.docker.compose.oneoff': 'no',
'io.lando.container': 'TRUE',
'io.lando.src': '/tmp/.lando.yml',
'io.lando.id': 'lando',
'io.lando.service-container': 'no',
},
},
overrides
);
};
describe('docker', () => {
describe('#Landerode', () => {
it('should inherit things from Dockerode', () => {
expect(Landerode.prototype).to.be.an.instanceOf(Dockerode);
});
});
describe('#createNetwork', () => {
it('should merge name correctly into opts', () => {
const stub = sinon.stub(landerode, 'createNetwork')
.usingPromise(Promise)
.resolves();
return landerode.createNet('elthree', {}).then(() => {
// for some reason should/expect doesn't work here...
stub.calledWith({Name: 'elthree'});
stub.restore();
});
});
it('should throw an error if can\'t create network', () => {
const stub = sinon.stub(landerode, 'createNetwork').rejects('Too Many Capes');
return landerode.createNet('hardtimes').should.be.rejectedWith('Too Many Capes')
.then(() => {
stub.restore();
});
});
});
describe('#inspect', () => {
it('should throw an error if inspect fails', () => {
// We need this chain of stubs to send our call down an explicit line that
// will eventually throw a solid exception.
let bogusContainer = dummyContainer();
const inspectStub = sinon.stub(bogusContainer, 'inspect').throws();
const stub = sinon.stub(landerode, 'getContainer').returns(bogusContainer);
// Throw needs a function to check rather than a function result
const cptPhasma = () => landerode.inspect('fn-2187');
expect(cptPhasma).to.throw();
inspectStub.restore();
stub.restore();
});
});
describe('#isRunning', () => {
it('should return false if State.Running inspect data is false', () => {
// Container to return from 'getContainer'
const bogusContainer = dummyContainer();
// Force the state of the container to not be running.
const inspectStub = sinon.stub(bogusContainer, 'inspect').resolves({
State: {
Running: false,
},
});
// Force the return of our container
const getStub = sinon.stub(landerode, 'getContainer').returns(bogusContainer);
return landerode.isRunning('YT-1300').should.eventually.be.false
.then(() => {
inspectStub.restore();
getStub.restore();
});
});
it('should return true if State.Running inspect data is true', () => {
// Container to return from 'getContainer';;l
const bogusContainer = new Dockerode.Container({}, 'YT-1300');
// Force the state of the container to be running.
const inspectStub = sinon.stub(bogusContainer, 'inspect').resolves({
State: {
Running: true,
},
});
// Force the return of our container
const getStub = sinon.stub(landerode, 'getContainer').returns(bogusContainer);
return landerode.isRunning('YT-1300').should.eventually.be.true
.then(() => {
inspectStub.restore();
getStub.restore();
});
});
it('should return false if container doesn\'t exist', () => {
// Our own little bad container.
const bogusContainer = new Dockerode.Container();
// Throw the proper error.
const inspectStub = sinon.stub(bogusContainer, 'inspect')
.rejects(new Error('No such container: foo'));
// Make sure we return the proper failing container.
const getStub = sinon.stub(landerode, 'getContainer')
.returns(bogusContainer);
return landerode.isRunning('foo').should.eventually.be.false.then(() => {
getStub.restore();
inspectStub.restore();
});
});
it('should throw an error on all other catches', () => {
// Our own little bad container.
const bogusContainer = new Dockerode.Container();
// Throw the proper error.
const inspectStub = sinon.stub(bogusContainer, 'inspect').rejects(new Error('It\'s a trap!'));
// Make sure we return the proper failing container.
const getStub = sinon.stub(landerode, 'getContainer').returns(bogusContainer);
return landerode.isRunning('foo').should.be
.rejectedWith('It\'s a trap!').then(() => {
getStub.restore();
inspectStub.restore();
});
});
});
describe('#list', () => {
beforeEach(() => {
filesystem({'/tmp/.lando.yml': 'CODEZ'});
});
it('should filter out any containers that are pending removal', () => {
const listStub = sinon.stub(landerode, 'listContainers')
.usingPromise(Promise)
.resolves([
dummyContainer({Status: 'Being Awesome'}),
dummyContainer({Status: 'Removal In Progress'}),
]);
return landerode.list()
.should.eventually.be.an('Array').with.a.lengthOf(1)
.then(() => {
listStub.restore();
});
});
it('should remove any "null" containers', () => {
const listStub = sinon.stub(landerode, 'listContainers')
.usingPromise(Promise)
.resolves([
null,
dummyContainer({identity: 'Solo'}),
]);
return landerode.list()
.should.eventually.be.an('Array').with.a.lengthOf(1)
.then(() => {
listStub.restore();
});
});
it('should filter out non-lando containers', () => {
const listStub = sinon.stub(landerode, 'listContainers')
.usingPromise(Promise)
.resolves([
dummyContainer({Labels: {'io.lando.container': 'FALSE'}}),
dummyContainer(),
]);
return landerode.list()
.should.eventually.be.an('Array').with.a.lengthOf(1)
.then(() => {
listStub.restore();
});
});
it('should filter by app name if given', () => {
const listStub = sinon.stub(landerode, 'listContainers')
.usingPromise(Promise)
.resolves([
dummyContainer({Labels: {
'com.docker.compose.project': 'alderaan',
'com.docker.compose.service': 'Rescue Mission',
'com.docker.compose.container-number': 73,
'com.docker.compose.oneoff': 'no',
'io.lando.container': 'TRUE',
'io.lando.id': 'lando',
'io.lando.service-container': 'no',
'io.lando.src': '/tmp/.lando.yml',
}}),
dummyContainer(),
]);
return landerode.list({app: 'alderaan'})
.should.eventually.be.an('Array').with.a.lengthOf(1)
.then(() => {
listStub.restore();
});
});
it('should throw an error on all other catches', () => {
let container = dummyContainer();
delete container.lando;
const listStub = sinon.stub(landerode, 'listContainers').resolves([container]);
const fail = () => landerode.list();
expect(fail).to.throw();
return listStub.restore();
});
afterEach(() => {
filesystem.restore();
});
});
describe('#remove', () => {
it('should throw an error if remove fails', () => {
const container = dummyContainer({Id: '1234'});
const getStub = sinon.stub(landerode, 'getContainer').returns(container);
const removeStub = sinon.stub(container, 'remove')
.usingPromise(Promise)
.rejects('Oh No!');
landerode.remove('1234').should.be.rejectedWith('Oh No!')
.then(() => {
getStub.restore();
removeStub.restore();
});
});
});
});