Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate is-docker to jest #2180

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions test/is-docker.test.ts

This file was deleted.

56 changes: 56 additions & 0 deletions test/jest/unit/is-docker.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Avishagp marked this conversation as resolved.
Show resolved Hide resolved
delete require.cache[path.join(__dirname, 'index.js')];
Avishagp marked this conversation as resolved.
Show resolved Hide resolved
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'");
Avishagp marked this conversation as resolved.
Show resolved Hide resolved
});

expect(isDocker()).toEqual(false);
});
});