From 4b15841c41054ac7dc0a654170c80391dfe4eca9 Mon Sep 17 00:00:00 2001 From: Bryan Clark Date: Fri, 9 Oct 2020 03:30:45 -0700 Subject: [PATCH] Mostly tests and some small changes (#16) * Create docker.test.ts * Add context tests * test main --- __tests__/context.test.ts | 16 +++++++++ __tests__/docker.test.ts | 49 +++++++++++++++++++++++++ __tests__/main.test.ts | 76 +++++++++++++++++++++++++++++++++++++++ src/context.ts | 2 +- src/main.ts | 13 ++++--- 5 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 __tests__/context.test.ts create mode 100644 __tests__/docker.test.ts create mode 100644 __tests__/main.test.ts diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts new file mode 100644 index 0000000..a77f44b --- /dev/null +++ b/__tests__/context.test.ts @@ -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(); +}); diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts new file mode 100644 index 0000000..5c3ceed --- /dev/null +++ b/__tests__/docker.test.ts @@ -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) + }) + }); +}); diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts new file mode 100644 index 0000000..44c8f4e --- /dev/null +++ b/__tests__/main.test.ts @@ -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); +}); diff --git a/src/context.ts b/src/context.ts index b284b90..3d24754 100644 --- a/src/context.ts +++ b/src/context.ts @@ -7,7 +7,7 @@ export interface Inputs { logout: string; } -export async function getInputs(): Promise { +export function getInputs(): Inputs { return { registry: core.getInput('registry'), username: core.getInput('username'), diff --git a/src/main.ts b/src/main.ts index 4f377b4..06ff25c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,17 +4,16 @@ import {getInputs, Inputs} from './context'; import * as docker from './docker'; import * as stateHelper from './state-helper'; -async function run(): Promise { +export async function run(): Promise { try { if (os.platform() !== 'linux') { - core.setFailed('Only supported on linux platform'); - return; + throw new Error('Only supported on linux platform'); } - let inputs: Inputs = await getInputs(); - stateHelper.setRegistry(inputs.registry); - stateHelper.setLogout(inputs.logout); - await docker.login(inputs.registry, inputs.username, inputs.password); + const {registry, username, password, logout} = getInputs(); + stateHelper.setRegistry(registry); + stateHelper.setLogout(logout); + await docker.login(registry, username, password); } catch (error) { core.setFailed(error.message); }