Skip to content

Commit

Permalink
test: removing global agent usage
Browse files Browse the repository at this point in the history
I've also used the key override where needed for `createPolykeyAgent`.

Related #404
  • Loading branch information
tegefaulkes committed Jul 19, 2022
1 parent c5b1752 commit 41ba9cc
Show file tree
Hide file tree
Showing 26 changed files with 362 additions and 478 deletions.
2 changes: 1 addition & 1 deletion tests/bin/agent/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('lock', () => {
await session.stop();
},
);
runTestIfPlatforms('linux')(
runTestIfPlatforms('linux', 'docker')(
'lock ensures re-authentication is required',
async () => {
const password = agentPassword;
Expand Down
70 changes: 37 additions & 33 deletions tests/bin/agent/lockall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Session from '@/sessions/Session';
import config from '@/config';
import * as errors from '@/errors';
import * as testBinUtils from '../utils';
import * as testUtils from '../../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

/**
* Mock prompts module which is used prompt for password
Expand All @@ -20,37 +20,41 @@ describe('lockall', () => {
const logger = new Logger('lockall test', LogLevel.WARN, [
new StreamHandler(),
]);
let globalAgentDir;
let globalAgentPassword;
let globalAgentClose;
beforeAll(async () => {
({ globalAgentDir, globalAgentPassword, globalAgentClose } =
await testUtils.setupGlobalAgent(logger));
}, globalThis.maxTimeout);
afterAll(async () => {
await globalAgentClose();
let agentDir;
let agentPassword;
let agentClose;
beforeEach(async () => {
({ agentDir, agentPassword, agentClose } =
await testBinUtils.setupTestAgent(
global.testCmd,
globalRootKeyPems[0],
logger,
));
});
afterEach(async () => {
await agentClose();
});
runTestIfPlatforms('linux', 'docker')(
'lockall deletes the session token',
async () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
const { exitCode } = await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
expect(exitCode).toBe(0);
const session = await Session.createSession({
sessionTokenPath: path.join(globalAgentDir, config.defaults.tokenBase),
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
Expand All @@ -61,21 +65,21 @@ describe('lockall', () => {
runTestIfPlatforms('linux', 'docker')(
'lockall ensures reauthentication is required',
async () => {
const password = globalAgentPassword;
const password = agentPassword;
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Token is deleted, reauthentication is required
mockedPrompts.mockClear();
Expand All @@ -85,9 +89,9 @@ describe('lockall', () => {
await testBinUtils.pkStdio(
['agent', 'status'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Prompted for password 1 time
expect(mockedPrompts.mock.calls.length).toBe(1);
Expand All @@ -100,13 +104,13 @@ describe('lockall', () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
const session = await Session.createSession({
sessionTokenPath: path.join(globalAgentDir, config.defaults.tokenBase),
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
fs,
logger,
});
Expand All @@ -115,21 +119,21 @@ describe('lockall', () => {
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'lockall'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
// Old token is invalid
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(
global.testCmd,
)(
['agent', 'status', '--format', 'json'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
PK_TOKEN: token,
},
globalAgentDir,
agentDir,
);
testBinUtils.expectProcessError(exitCode, stderr, [
new errors.ErrorClientAuthDenied(),
Expand Down
47 changes: 22 additions & 25 deletions tests/bin/agent/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Status from '@/status/Status';
import * as nodesUtils from '@/nodes/utils';
import config from '@/config';
import * as testBinUtils from '../utils';
import * as testUtils from '../../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('status', () => {
const logger = new Logger('status test', LogLevel.WARN, [
Expand Down Expand Up @@ -43,8 +43,6 @@ describe('status', () => {
[
'agent',
'start',
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -56,6 +54,7 @@ describe('status', () => {
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger,
Expand Down Expand Up @@ -123,23 +122,24 @@ describe('status', () => {
});
});
describe('status with global agent', () => {
let globalAgentDir;
let globalAgentPassword;
let globalAgentClose;
beforeAll(async () => {
({ globalAgentDir, globalAgentPassword, globalAgentClose } =
await testUtils.setupGlobalAgent(logger));
let agentDir;
let agentPassword;
let agentClose;
beforeEach(async () => {
({ agentDir, agentPassword, agentClose } =
await testBinUtils.setupTestAgent(
global.testCmd,
globalRootKeyPems[1],
logger,
));
}, globalThis.maxTimeout);
afterAll(async () => {
await globalAgentClose();
afterEach(async () => {
await agentClose();
});
runTestIfPlatforms('linux', 'docker')('status on LIVE agent', async () => {
const status = new Status({
statusPath: path.join(globalAgentDir, config.defaults.statusBase),
statusLockPath: path.join(
globalAgentDir,
config.defaults.statusLockBase,
),
statusPath: path.join(agentDir, config.defaults.statusBase),
statusLockPath: path.join(agentDir, config.defaults.statusLockBase),
fs,
logger,
});
Expand All @@ -149,10 +149,10 @@ describe('status', () => {
)(
['agent', 'status', '--format', 'json', '--verbose'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
expect(exitCode).toBe(0);
expect(JSON.parse(stdout)).toMatchObject({
Expand All @@ -175,13 +175,10 @@ describe('status', () => {
'status on remote LIVE agent',
async () => {
const passwordPath = path.join(dataDir, 'password');
await fs.promises.writeFile(passwordPath, globalAgentPassword);
await fs.promises.writeFile(passwordPath, agentPassword);
const status = new Status({
statusPath: path.join(globalAgentDir, config.defaults.statusBase),
statusLockPath: path.join(
globalAgentDir,
config.defaults.statusLockBase,
),
statusPath: path.join(agentDir, config.defaults.statusBase),
statusLockPath: path.join(agentDir, config.defaults.statusLockBase),
fs,
logger,
});
Expand Down
17 changes: 5 additions & 12 deletions tests/bin/agent/stop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as binErrors from '@/bin/errors';
import * as clientErrors from '@/client/errors';
import * as testBinUtils from '../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

describe('stop', () => {
const logger = new Logger('stop test', LogLevel.WARN, [new StreamHandler()]);
Expand All @@ -31,9 +32,6 @@ describe('stop', () => {
[
'agent',
'start',
// 1024 is the smallest size and is faster to start
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -44,6 +42,7 @@ describe('stop', () => {
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger,
Expand Down Expand Up @@ -93,9 +92,6 @@ describe('stop', () => {
[
'agent',
'start',
// 1024 is the smallest size and is faster to start
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -106,6 +102,7 @@ describe('stop', () => {
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger,
Expand Down Expand Up @@ -181,9 +178,6 @@ describe('stop', () => {
[
'agent',
'start',
// 1024 is the smallest size and is faster to start
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -195,6 +189,7 @@ describe('stop', () => {
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger,
Expand Down Expand Up @@ -234,9 +229,6 @@ describe('stop', () => {
[
'agent',
'start',
// 1024 is the smallest size and is faster to start
'--root-key-pair-bits',
'1024',
'--client-host',
'127.0.0.1',
'--proxy-host',
Expand All @@ -247,6 +239,7 @@ describe('stop', () => {
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
PK_PASSWORD: password,
PK_ROOT_KEY: globalRootKeyPems[0],
},
dataDir,
logger,
Expand Down
Loading

0 comments on commit 41ba9cc

Please sign in to comment.