UNPKG

8.52 kBJavaScriptView Raw
1import test from 'ava'
2import fs from 'fs'
3import { Container } from '../lib/container'
4import { Image } from '../lib/image'
5import {default as MemoryStream} from 'memorystream'
6import { Docker } from '../lib/docker'
7
8const socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock'
9const isSocket = fs.existsSync(socket) ? fs.statSync(socket).isSocket() : false
10const docker = isSocket
11 ? new Docker()
12 : new Docker({ socketPath: socket })
13
14const createContainer = (name, extra) =>
15 docker.container.create(Object.assign({ Image: 'ubuntu', name: containerNames.get(name) }, extra))
16const containerNames = new Map([
17 [ 'create', 'docker_api_test_create' ],
18 [ 'inspect', 'docker_api_test_inspect' ],
19 [ 'top', 'docker_api_test_top' ],
20 [ 'logs', 'docker_api_test_logs' ],
21 [ 'changes', 'docker_api_test_changes' ],
22 [ 'export', 'docker_api_test_export' ],
23 [ 'stats', 'docker_api_test_stats' ],
24 [ 'resize', 'docker_api_test_resize' ],
25 [ 'prune', 'docker_api_test_prune' ],
26 [ 'start', 'docker_api_test_start' ],
27 [ 'stop', 'docker_api_test_stop' ],
28 [ 'restart', 'docker_api_test_restart' ],
29 [ 'kill', 'docker_api_test_kill' ],
30 [ 'update', 'docker_api_test_update' ],
31 [ 'rename', 'docker_api_test_rename' ],
32 [ 'rename_prev', 'docker_api_test_rename_prev' ],
33 [ 'pause', 'docker_api_test_pause' ],
34 [ 'attach', 'docker_api_test_attach' ],
35 [ 'commit', 'docker_api_test_commit' ],
36 [ 'exec', 'docker_api_test_exec' ],
37 [ 'inspect_exec', 'docker_api_test_inspect_exec' ],
38 [ 'get_archive', 'docker_api_test_get_archive' ],
39 [ 'put_archive', 'docker_api_test_put_archive' ],
40 [ 'info_archive', 'docker_api_test_info_archive' ]
41])
42
43test('list', async t => {
44 const containers = await docker.container.list()
45 t.is(containers.constructor, Array)
46})
47
48test('should create a container', async t => {
49 const container = await createContainer('create')
50 t.is(container.constructor, Container)
51})
52
53test('inspect', async t => {
54 const container = await createContainer('inspect')
55 const containerStatus = await container.status()
56 t.is(containerStatus.constructor, Container)
57})
58
59test('top', async t => {
60 const container = await createContainer('top', {
61 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
62 })
63 await container.start()
64 const processes = await container.top()
65 t.is(processes.Processes.constructor, Array)
66})
67
68test('log', async t => {
69 const container = await createContainer('logs', {
70 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
71 })
72 await container.start()
73 const logs = await container.logs({ stdout: 1, follow: true })
74 t.truthy(logs.pipe)
75})
76
77test('changes', async t => {
78 const container = await createContainer('changes', {
79 Cmd: [ '/bin/bash', '-c', 'echo "xfoo" > foo.txt' ],
80 })
81 await container.start()
82 const changes = await container.changes()
83 t.is(changes.constructor, Array)
84})
85
86test('export', async t => {
87 const container = await createContainer('export')
88 const result = container.export({ stream: false })
89 t.truthy(result)
90})
91
92test('stats', async t => {
93 const container = await createContainer('stats', {
94 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
95 })
96 await container.start()
97 const stats = await container.stats()
98 t.truthy(stats.pipe)
99})
100
101test('resize', async t => {
102 const container = await createContainer('resize', {
103 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
104 })
105 await container.start()
106 const result = await container.stats({
107 h: process.stdout.rows,
108 w: process.stdout.columns
109 })
110 t.truthy(result.pipe)
111})
112
113test('prune', async t => {
114 const container = await createContainer('prune')
115 t.notThrows(docker.container.prune())
116})
117
118test('start', async t => {
119 const container = await createContainer('start', {
120 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
121 })
122 const result = await container.start()
123 t.is(result.constructor, Container)
124})
125
126test('stop', async t => {
127 const container = await createContainer('stop', {
128 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
129 })
130 await container.start()
131 await container.stop()
132 const containerStatus = await container.status()
133 t.is(containerStatus.data.State.Status, 'exited')
134})
135
136test('restart', async t => {
137 const container = await createContainer('restart', {
138 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
139 })
140 await container.start()
141 await container.restart()
142 const containerStatus = await container.status()
143 t.is(containerStatus.data.State.Status, 'running')
144})
145
146test('kill', async t => {
147 const container = await createContainer('kill', {
148 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
149 })
150 await container.start()
151 await container.kill()
152 const containerStatus = await container.status()
153 t.is(containerStatus.data.State.Status, 'exited')
154})
155
156test('update', async t => {
157 const container = await createContainer('update', {
158 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
159 })
160 await container.update({ 'CpuShares': 512 })
161 const containerStatus = await container.status()
162 t.is(containerStatus.data.HostConfig.CpuShares, 512)
163})
164
165test('rename', async t => {
166 const container = await createContainer('rename_prev', {
167 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
168 })
169 await container.rename({ 'name': containerNames.get('rename') })
170 const containerStatus = await container.status()
171 t.is(containerStatus.data.Name, '/' + containerNames.get('rename'))
172})
173
174test('pause', async t => {
175 const container = await createContainer('pause', {
176 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
177 })
178 await container.start()
179 await container.pause()
180 const containerStatus = await container.status()
181 t.is(containerStatus.data.State.Status, 'paused')
182 await container.unpause()
183 const containerStatus2 = await container.status()
184 t.is(containerStatus2.data.State.Status, 'running')
185})
186
187test('commit', async t => {
188 const container = await createContainer('commit', {
189 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
190 })
191 await container.start()
192 const image = await container.commit({ comment: 'commit test' })
193 t.is(image.constructor, Image)
194})
195
196test('exec', async t => {
197 const container = await createContainer('exec', {
198 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
199 })
200 await container.start()
201 const exec = await container.exec.create({
202 Cmd: [ "top" ]
203 })
204 const stream = await exec.start()
205 t.truthy(stream.pipe)
206})
207
208test('exec-status', async t => {
209 const container = await createContainer('inspect_exec', {
210 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
211 })
212 await container.start()
213 const exec = await container.exec.create({
214 Cmd: [ "top" ]
215 })
216 const execStatus = await exec.status()
217 t.is(exec.constructor, execStatus.constructor)
218})
219
220test('attach', async t => {
221 const container = await createContainer('attach', {
222 'AttachStdin': false,
223 'AttachStdout': true,
224 'AttachStderr': true,
225 'Tty': false,
226 'OpenStdin': false,
227 'StdinOnce': false,
228 'Env': null,
229 'Cmd': ['/bin/bash', '-c', 'uptime'],
230 'Dns': ['8.8.8.8', '8.8.4.4'],
231 'Image': 'ubuntu',
232 })
233 const result = await container.attach({
234 stream: true,
235 stdout: true,
236 stderr: true
237 })
238 const stream = result[0]
239 t.truthy(stream)
240
241 await container.start()
242 const code = await container.wait()
243 t.is(code.StatusCode.constructor, Number)
244})
245
246test('get-archive', async t => {
247 const container = await createContainer('get_archive', {
248 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
249 })
250 const data = await container.fs.get({ path: '/var/log/dmesg' })
251 t.truthy(data)
252})
253
254test('put-archive', async t => {
255 const container = await createContainer('put_archive', {
256 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
257 })
258 const data = await container.fs.put('./test/test.tar', {
259 path: '/root'
260 })
261 t.truthy(data)
262})
263
264test('inspect-archive', async t => {
265 const container = await createContainer('info_archive', {
266 Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
267 })
268 const data = await container.fs.info({ path: '/var/log/dmesg' })
269 t.truthy(data)
270})
271
272test.after.always('cleanup', async t => {
273 const promises = Array.from(containerNames.values()).map((name) =>
274 docker.container.get(name).stop()
275 .then((container) => {
276 return container.delete({ force: true })
277 })
278 .catch((err) => console.log(err))
279 )
280 t.notThrows(Promise.all(promises))
281})