diff --git a/test/is-docker.test.ts b/test/is-docker.test.ts deleted file mode 100644 index 62dc374c29..0000000000 --- a/test/is-docker.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -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(); -}); diff --git a/test/jest/unit/is-docker.spec.ts b/test/jest/unit/is-docker.spec.ts new file mode 100644 index 0000000000..15d7b72d66 --- /dev/null +++ b/test/jest/unit/is-docker.spec.ts @@ -0,0 +1,56 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +import { isDocker } from '../../../src/lib/is-docker'; + +describe('isDocker', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('inside a Docker container (.dockerenv test)', async () => { + delete require.cache[path.join(__dirname, 'index.js')]; + const statSyncSpy = jest.spyOn(fs, 'statSync').mockReturnValue({} as any); + expect(isDocker()).toBeTruthy(); + expect(statSyncSpy).toHaveBeenCalledTimes(1); + expect(statSyncSpy).toHaveBeenLastCalledWith('/.dockerenv'); + }); + + it('inside a Docker container (cgroup test)', async () => { + delete require.cache[path.join(__dirname, 'index.js')]; + + const statSyncSpy = jest.spyOn(fs, 'statSync'); + const readFileSyncSpy = jest.spyOn(fs, 'readFileSync'); + + statSyncSpy.mockImplementationOnce((path): any => { + if (path === '/.dockerenv') { + throw new Error("ENOENT, no such file or directory '/.dockerinit'"); + } + }); + + readFileSyncSpy.mockImplementationOnce((path, options): any => { + if (path === '/proc/self/cgroup' && options === 'utf8') { + return 'xxx docker yyyy'; + } + }); + + expect(isDocker()).toEqual(true); + }); + + it('not inside a Docker container', async () => { + const statSyncSpy = jest.spyOn(fs, 'statSync'); + const readFileSync = jest.spyOn(fs, 'readFileSync'); + + statSyncSpy.mockImplementationOnce((path): any => { + if (path === '/.dockerenv') { + throw new Error("ENOENT, no such file or directory '/.dockerinit'"); + } + }); + + readFileSync.mockImplementationOnce((): any => { + throw new Error("ENOENT, no such file or directory '/.dockerinit'"); + }); + + expect(isDocker()).toEqual(false); + }); +});