-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2180 from snyk/test/migrate-is-docker
test: migrate is-docker to jest
- Loading branch information
Showing
2 changed files
with
56 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
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); | ||
}); | ||
}); |