-
Notifications
You must be signed in to change notification settings - Fork 534
/
is-docker.test.ts
45 lines (36 loc) · 1.35 KB
/
is-docker.test.ts
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
import { test } from 'tap';
import * as sinon from 'sinon';
import * as fs from 'fs';
import * as path from 'path';
import { isDocker } from '../src/lib/is-docker';
test('inside a Docker container (.dockerenv test)', async (t) => {
delete require.cache[path.join(__dirname, 'index.js')];
const statSyncStub = sinon.stub(fs, 'statSync').returns({} as any);
t.true(isDocker());
statSyncStub.restore();
});
test('inside a Docker container (cgroup test)', async (t) => {
delete require.cache[path.join(__dirname, 'index.js')];
const statSyncStub = sinon.stub(fs, 'statSync');
statSyncStub
.withArgs('/.dockerenv')
.throws("ENOENT, no such file or directory '/.dockerinit'");
const readFileSyncStub = sinon.stub(fs, 'readFileSync');
readFileSyncStub
.withArgs('/proc/self/cgroup', 'utf8')
.returns('xxx docker yyyy');
t.true(isDocker());
statSyncStub.restore();
readFileSyncStub.restore();
});
test('not inside a Docker container', async (t) => {
delete require.cache[path.join(__dirname, 'index.js')];
const statSyncStub = sinon.stub(fs, 'statSync');
statSyncStub
.withArgs('/.dockerenv')
.throws("ENOENT, no such file or directory '/.dockerinit'");
const readFileSyncStub = sinon.stub(fs, 'readFileSync');
readFileSyncStub.throws('ENOENT, no such file or directory');
t.false(isDocker());
statSyncStub.restore();
});