Skip to content

Commit

Permalink
wip: removing global agent usuage
Browse files Browse the repository at this point in the history
Related #404
  • Loading branch information
tegefaulkes committed Jul 18, 2022
1 parent 84a671a commit c5b1752
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
52 changes: 28 additions & 24 deletions tests/bin/agent/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,49 @@ import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import Session from '@/sessions/Session';
import config from '@/config';
import * as testBinUtils from '../utils';
import * as testUtils from '../../utils';
import { runTestIfPlatforms } from '../../utils';
import { globalRootKeyPems } from '../../globalRootKeyPems';

jest.mock('prompts');
const mockedPrompts = mocked(prompts);

describe('lock', () => {
const logger = new Logger('lock 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: string;
let agentPassword: string;
let agentClose: () => Promise<void>;
beforeEach(async () => {
({ agentDir, agentPassword, agentClose } =
await testBinUtils.setupTestAgent(
global.testCmd,
globalRootKeyPems[0],
logger,
));
});
afterEach(async () => {
await agentClose();
});
runTestIfPlatforms('linux', 'docker')(
'lock 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', 'lock'],
{
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 @@ -55,34 +59,34 @@ describe('lock', () => {
runTestIfPlatforms('linux')(
'lock ensures re-authentication is required',
async () => {
const password = globalAgentPassword;
const password = agentPassword;
mockedPrompts.mockClear();
mockedPrompts.mockImplementation(async (_opts: any) => {
return { password };
});
await testBinUtils.pkStdio(
['agent', 'unlock'],
{
PK_NODE_PATH: globalAgentDir,
PK_PASSWORD: globalAgentPassword,
PK_NODE_PATH: agentDir,
PK_PASSWORD: agentPassword,
},
globalAgentDir,
agentDir,
);
// Session token is deleted
await testBinUtils.pkStdio(
['agent', 'lock'],
{
PK_NODE_PATH: globalAgentDir,
PK_NODE_PATH: agentDir,
},
globalAgentDir,
agentDir,
);
// Will prompt to reauthenticate
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 Down
20 changes: 7 additions & 13 deletions tests/bin/agent/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,9 @@ describe('start', () => {
runDescribeIfPlatforms('linux').only('start with global agent', () => {
let agentDataDir;
let agent1Status: StatusLive;
let agent1Stop: () => void;
let agent1Close: () => Promise<void>;
let agent2Status: StatusLive;
let agent2Stop: () => void;
let agent2Close: () => Promise<void>;
let seedNodeId1: NodeId;
let seedNodeHost1: Host;
let seedNodePort1: Port;
Expand All @@ -846,21 +846,15 @@ describe('start', () => {
agentDataDir = await fs.promises.mkdtemp(
path.join(global.tmpDir, 'polykey-test-'),
);
const agent1Path = path.join(agentDataDir, 'agent1');
await fs.promises.mkdir(agent1Path);
({ agentStatus: agent1Status, agentStop: agent1Stop } =
({ agentStatus: agent1Status, agentClose: agent1Close } =
await testBinUtils.setupTestAgent(
undefined,
agent1Path,
globalRootKeyPems[0],
logger,
));
const agent2Path = path.join(agentDataDir, 'agent2');
await fs.promises.mkdir(agent2Path);
({ agentStatus: agent2Status, agentStop: agent2Stop } =
({ agentStatus: agent2Status, agentClose: agent2Close } =
await testBinUtils.setupTestAgent(
undefined,
agent2Path,
globalRootKeyPems[1],
logger,
));
Expand All @@ -870,10 +864,10 @@ describe('start', () => {
seedNodeId2 = agent2Status.data.nodeId;
seedNodeHost2 = agent2Status.data.proxyHost;
seedNodePort2 = agent2Status.data.proxyPort;
}, globalThis.maxTimeout);
});
afterEach(async () => {
agent1Stop();
agent2Stop();
await agent1Close();
await agent2Close();
await fs.promises.rm(agentDataDir, {
force: true,
recursive: true,
Expand Down
24 changes: 17 additions & 7 deletions tests/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,17 +574,18 @@ function expectProcessError(
/**
*
* @param cmd - Optional target command to run, usually `global.testCmd`
* @param agentDir - Directory to run the agent in, must exist
* @param privateKeyPem - Optional root key override to skip key generation
* @param logger
*/
async function setupTestAgent(
cmd: string | undefined,
agentDir: string,
privateKeyPem: PrivateKeyPem,
logger: Logger,
): Promise<{ agentStatus: StatusLive; agentStop: () => void }> {
const password = 'password';
) {
const agentDir = await fs.promises.mkdtemp(
path.join(global.tmpDir, 'polykey-test-'),
);
const agentPassword = 'password';
const agentProcess = await pkSpawnSwitch(cmd)(
[
'agent',
Expand All @@ -602,7 +603,7 @@ async function setupTestAgent(
'--verbose',
],
{
PK_PASSWORD: password,
PK_PASSWORD: agentPassword,
PK_ROOT_KEY: privateKeyPem,
},
agentDir,
Expand All @@ -618,9 +619,18 @@ async function setupTestAgent(
data: { ...data, nodeId: validationUtils.parseNodeId(data.nodeId) },
};
try {
return { agentStatus, agentStop: () => agentProcess.kill('SIGINT') };
return {
agentStatus,
agentClose: async () => {
agentProcess.kill();
await fs.promises.rm(agentDir, { recursive: true, force: true });
},
agentDir,
agentPassword,
};
} catch (e) {
agentProcess.kill('SIGINT');
agentProcess.kill();
await fs.promises.rm(agentDir, { recursive: true, force: true });
throw e;
}
}
Expand Down

0 comments on commit c5b1752

Please sign in to comment.