Skip to content

Commit

Permalink
fix: fixed keys renew and reset tests
Browse files Browse the repository at this point in the history
It's a race condition with propagating keys changes. We needed to wait and then attempt calls with the new nodeId.

I think there's a separate issue when verifying with the old nodeId. That needs to be checked inside `Polykey` though.
  • Loading branch information
tegefaulkes authored and CMCDragonkai committed Oct 17, 2023
1 parent 342279e commit f3f0ce5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
18 changes: 8 additions & 10 deletions tests/keys/renew.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { NodeId } from 'polykey/dist/ids';
import path from 'path';
import fs from 'fs';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import * as keysUtils from 'polykey/dist/keys/utils';
import * as nodesUtils from 'polykey/dist/nodes/utils';
import { sleep } from 'polykey/dist/utils';
import * as testUtils from '../utils';

describe('renew', () => {
const logger = new Logger('renew test', LogLevel.WARN, [new StreamHandler()]);
const password = 'helloworld';
const password = 'helloWorld';
let dataDir: string;
let nodePath: string;
let pkAgent: PolykeyAgent;
let oldNodeId: NodeId;
beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(globalThis.tmpDir, 'polykey-test-'),
Expand All @@ -33,7 +32,6 @@ describe('renew', () => {
},
logger,
});
oldNodeId = pkAgent.keyRing.getNodeId();
}, globalThis.defaultTimeout * 2);
afterEach(async () => {
await pkAgent.stop();
Expand Down Expand Up @@ -87,6 +85,10 @@ describe('renew', () => {
},
));
expect(exitCode).toBe(0);
await sleep(1000);
const nodeIdEncodedNew = nodesUtils.encodeNodeId(
pkAgent.keyRing.getNodeId(),
);
// Get new keypair and nodeId and compare against old
({ exitCode, stdout } = await testUtils.pkStdio(
['keys', 'keypair', '--format', 'json'],
Expand All @@ -95,9 +97,7 @@ describe('renew', () => {
PK_NODE_PATH: nodePath,
PK_PASSWORD: 'password-new',
PK_PASSWORD_NEW: 'some-password',
// Client server still using old nodeId, this should be removed if
// this is fixed.
PK_NODE_ID: nodesUtils.encodeNodeId(oldNodeId),
PK_NODE_ID: nodeIdEncodedNew,
PK_CLIENT_HOST: '127.0.0.1',
PK_CLIENT_PORT: `${pkAgent.clientServicePort}`,
},
Expand All @@ -113,9 +113,7 @@ describe('renew', () => {
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: 'password-new',
// Client server still using old nodeId, this should be removed if
// this is fixed.
PK_NODE_ID: nodesUtils.encodeNodeId(oldNodeId),
PK_NODE_ID: nodeIdEncodedNew,
PK_CLIENT_HOST: '127.0.0.1',
PK_CLIENT_PORT: `${pkAgent.clientServicePort}`,
},
Expand Down
19 changes: 9 additions & 10 deletions tests/keys/reset.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { NodeId } from 'polykey/dist/ids';
import path from 'path';
import fs from 'fs';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import * as keysUtils from 'polykey/dist/keys/utils';
import * as nodesUtils from 'polykey/dist/nodes/utils';
import { sleep } from 'polykey/dist/utils';
import * as testUtils from '../utils';

describe('reset', () => {
const logger = new Logger('reset test', LogLevel.WARN, [new StreamHandler()]);
const password = 'helloworld';
const password = 'helloWorld';
let dataDir: string;
let nodePath: string;
let pkAgent: PolykeyAgent;
let oldNodeId: NodeId;
beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(globalThis.tmpDir, 'polykey-test-'),
Expand All @@ -33,7 +32,6 @@ describe('reset', () => {
},
logger,
});
oldNodeId = pkAgent.keyRing.getNodeId();
}, globalThis.defaultTimeout * 2);
afterEach(async () => {
await pkAgent.stop();
Expand Down Expand Up @@ -87,6 +85,11 @@ describe('reset', () => {
},
));
expect(exitCode).toBe(0);
// Wait for keys changes to propagate to the network
await sleep(1000);
const nodeIdEncodedNew = nodesUtils.encodeNodeId(
pkAgent.keyRing.getNodeId(),
);
// Get new keypair and nodeId and compare against old
({ exitCode, stdout } = await testUtils.pkStdio(
['keys', 'keypair', '--format', 'json'],
Expand All @@ -95,9 +98,7 @@ describe('reset', () => {
PK_NODE_PATH: nodePath,
PK_PASSWORD: 'password-new',
PK_PASSWORD_NEW: 'some-password',
// Client server still using old nodeId, this should be removed if
// this is fixed.
PK_NODE_ID: nodesUtils.encodeNodeId(oldNodeId),
PK_NODE_ID: nodeIdEncodedNew,
PK_CLIENT_HOST: '127.0.0.1',
PK_CLIENT_PORT: `${pkAgent.clientServicePort}`,
},
Expand All @@ -113,9 +114,7 @@ describe('reset', () => {
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: 'password-new',
// Client server still using old nodeId, this should be removed if
// this is fixed.
PK_NODE_ID: nodesUtils.encodeNodeId(oldNodeId),
PK_NODE_ID: nodeIdEncodedNew,
PK_CLIENT_HOST: '127.0.0.1',
PK_CLIENT_PORT: `${pkAgent.clientServicePort}`,
},
Expand Down

0 comments on commit f3f0ce5

Please sign in to comment.