-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mostly tests and some small changes (#16)
* Create docker.test.ts * Add context tests * test main
- Loading branch information
Bryan Clark
authored and
GitHub
committed
Oct 9, 2020
1 parent
34d5f75
commit 4b15841
Showing
5 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
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,16 @@ | ||
import osm = require('os'); | ||
|
||
import {getInputs} from '../src/context'; | ||
|
||
test('without password getInputs throws errors', async () => { | ||
expect(() => { | ||
getInputs(); | ||
}).toThrowError('Input required and not supplied: password'); | ||
}); | ||
|
||
test('with password getInputs does not error', async () => { | ||
process.env['INPUT_PASSWORD'] = 'groundcontrol'; | ||
expect(() => { | ||
getInputs(); | ||
}).not.toThrowError(); | ||
}); |
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,49 @@ | ||
import {loginStandard, logout} from '../src/docker'; | ||
|
||
import * as path from 'path'; | ||
|
||
import * as exec from '@actions/exec'; | ||
|
||
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner'); | ||
|
||
test('loginStandard calls exec', async () => { | ||
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec'); | ||
// don't let exec try to actually run the commands | ||
execSpy.mockImplementation(() => {}); | ||
|
||
const username: string = 'dbowie'; | ||
const password: string = 'groundcontrol'; | ||
const registry: string = 'https://ghcr.io'; | ||
|
||
await loginStandard(registry, username, password); | ||
|
||
expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], { | ||
input: Buffer.from(password), | ||
silent: true, | ||
ignoreReturnCode: true, | ||
listeners: expect.objectContaining({ | ||
stdout: expect.any(Function), | ||
stderr: expect.any(Function) | ||
}) | ||
}); | ||
}); | ||
|
||
test('logout calls exec', async () => { | ||
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec'); | ||
// don't let exec try to actually run the commands | ||
execSpy.mockImplementation(() => {}); | ||
|
||
const registry: string = 'https://ghcr.io'; | ||
|
||
await logout(registry); | ||
|
||
expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], { | ||
silent: false, | ||
ignoreReturnCode: true, | ||
input: Buffer.from(''), | ||
listeners: expect.objectContaining({ | ||
stdout: expect.any(Function), | ||
stderr: expect.any(Function) | ||
}) | ||
}); | ||
}); |
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,76 @@ | ||
import osm = require('os'); | ||
|
||
import {run} from '../src/main'; | ||
import * as docker from '../src/docker'; | ||
import * as stateHelper from '../src/state-helper'; | ||
|
||
import * as core from '@actions/core'; | ||
|
||
test('errors when not run on linux platform', async () => { | ||
const platSpy = jest.spyOn(osm, 'platform'); | ||
platSpy.mockImplementation(() => 'netbsd'); | ||
|
||
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); | ||
|
||
await run(); | ||
|
||
expect(coreSpy).toHaveBeenCalledWith('Only supported on linux platform'); | ||
}); | ||
|
||
test('errors without password', async () => { | ||
const platSpy = jest.spyOn(osm, 'platform'); | ||
platSpy.mockImplementation(() => 'linux'); | ||
|
||
const coreSpy: jest.SpyInstance = jest.spyOn(core, 'setFailed'); | ||
|
||
await run(); | ||
|
||
expect(coreSpy).toHaveBeenCalledWith('Input required and not supplied: password'); | ||
}); | ||
|
||
test('successful with only password', async () => { | ||
const platSpy = jest.spyOn(osm, 'platform'); | ||
platSpy.mockImplementation(() => 'linux'); | ||
|
||
const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); | ||
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); | ||
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); | ||
dockerSpy.mockImplementation(() => {}); | ||
|
||
const password: string = 'groundcontrol'; | ||
process.env[`INPUT_PASSWORD`] = password; | ||
|
||
await run(); | ||
|
||
expect(setRegistrySpy).toHaveBeenCalledWith(''); | ||
expect(setLogoutSpy).toHaveBeenCalledWith(''); | ||
expect(dockerSpy).toHaveBeenCalledWith('', '', password); | ||
}); | ||
|
||
test('calls docker login', async () => { | ||
const platSpy = jest.spyOn(osm, 'platform'); | ||
platSpy.mockImplementation(() => 'linux'); | ||
|
||
const setRegistrySpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setRegistry'); | ||
const setLogoutSpy: jest.SpyInstance = jest.spyOn(stateHelper, 'setLogout'); | ||
const dockerSpy: jest.SpyInstance = jest.spyOn(docker, 'login'); | ||
dockerSpy.mockImplementation(() => {}); | ||
|
||
const username: string = 'dbowie'; | ||
process.env[`INPUT_USERNAME`] = username; | ||
|
||
const password: string = 'groundcontrol'; | ||
process.env[`INPUT_PASSWORD`] = password; | ||
|
||
const registry: string = 'https://ghcr.io'; | ||
process.env[`INPUT_REGISTRY`] = registry; | ||
|
||
const logout: string = 'true'; | ||
process.env['INPUT_LOGOUT'] = logout | ||
|
||
await run(); | ||
|
||
expect(setRegistrySpy).toHaveBeenCalledWith(registry); | ||
expect(setLogoutSpy).toHaveBeenCalledWith(logout); | ||
expect(dockerSpy).toHaveBeenCalledWith(registry, username, password); | ||
}); |
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
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