Skip to content

Commit

Permalink
feat: add broker client version preflight check
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed May 9, 2024
1 parent 1979a22 commit 82e27ff
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/client/checks/config/brokerClientVersionCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { makeSingleRawRequestToDownstream } from '../../../common/http/request';
import { PostFilterPreparedRequest } from '../../../common/relay/prepareRequest';
import version from '../../../common/utils/version';
import { CheckOptions, CheckResult } from '../types';

export const validateBrokerClientVersionAgainstServer = async (
checkOptions: CheckOptions,
config,
) => {
const clientVersion = version();
if (clientVersion != 'local') {
const req: PostFilterPreparedRequest = {
url: `${config.BROKER_SERVER_URL}/healthcheck`,
headers: {},
method: 'GET',
};
const brokerServerHealthcheckResponse =
await makeSingleRawRequestToDownstream(req);

const brokerServerVersion = JSON.parse(
brokerServerHealthcheckResponse.body,
).version;

const clientMinorVersion = parseInt(clientVersion.split('.')[1]);
const brokerServerMinorVersion = parseInt(
brokerServerVersion.split('.')[1],
);
if (
clientMinorVersion <= brokerServerMinorVersion &&
brokerServerMinorVersion - clientMinorVersion > 10
) {
return {
id: checkOptions.id,
name: checkOptions.name,
status: 'error',
output:
'Your broker client version is outdated. Please upgrade to latest version.',
} satisfies CheckResult;
} else {
return {
id: checkOptions.id,
name: checkOptions.name,
status: 'passing',
output: 'Running supported broker client version.',
} satisfies CheckResult;
}
} else {
return {
id: checkOptions.id,
name: checkOptions.name,
status: 'warning',
output: 'Caution! You are running a dev version.',
} satisfies CheckResult;
}
};
16 changes: 16 additions & 0 deletions lib/client/checks/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { validateBrokerClientUrl } from './brokerClientUrlCheck';
import { validateAcceptFlagsConfig } from './customAcceptFile';
import { validateCodeAgentDeprecation } from './codeAgentDeprecation';
import { validateUniversalConnectionsConfig } from './universalConnectionConfigCheck';
import { validateBrokerClientVersionAgainstServer } from './brokerClientVersionCheck';

export function getConfigChecks(config: Config): Check[] {
return [
brokerClientUrlCheck(config),
universalBrokerConnectionsCheck(config),
acceptFlagsConfigurationCheck(config),
codeAgentDeprecationCheck(config),
brokerClientVersionCheck(config),
];
}

Expand Down Expand Up @@ -70,3 +72,17 @@ const codeAgentDeprecationCheck = (config: Config): Check => {
},
} satisfies Check;
};

const brokerClientVersionCheck = (config: Config): Check => {
return {
id: 'client-version-validation',
name: 'Broker Client Version Check',
enabled: true,
check: async function (): Promise<CheckResult> {
return await validateBrokerClientVersionAgainstServer(
{ id: this.id, name: this.name },
config,
);
},
} satisfies Check;
};
106 changes: 106 additions & 0 deletions test/unit/client/checks/config/brokerClientVersionCheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { validateBrokerClientVersionAgainstServer } from '../../../../../lib/client/checks/config/brokerClientVersionCheck';
import { aConfig } from '../../../../helpers/test-factories';
import nock from 'nock';

const brokerServerUrl = 'https://brokerServer';
const brokerServerUrl2 = 'https://brokerServer2';
const brokerServerUrl3 = 'https://brokerServer3';

jest.mock('../../../../../lib/common/utils/version', () => {
const originalModule = jest.requireActual(
'../../../../../lib/common/utils/version',
);

return {
__esModule: true,
...originalModule,
default: () => {
return '4.180.0';
},
};
});

nock(brokerServerUrl)
.persist()
.get('/healthcheck')
.reply(() => {
return [200, { ok: true, version: '4.179.2' }];
});

nock(brokerServerUrl2)
.persist()
.get('/healthcheck')
.reply(() => {
return [200, { ok: true, version: '4.191.2' }];
});

nock(brokerServerUrl3)
.persist()
.get('/healthcheck')
.reply(() => {
return [200, { ok: true, version: '4.171.2' }];
});

describe('client/checks/config', () => {
describe('validateBrokerClientVersionAgainstServer()', () => {
it('should return passing if version delta is <10', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({});
config.BROKER_SERVER_URL = brokerServerUrl;

const checkResult = await validateBrokerClientVersionAgainstServer(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('passing');
expect(checkResult.output).toContain(
'Running supported broker client version.',
);
});

it('should return passing if version delta is >10', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({});
config.BROKER_SERVER_URL = brokerServerUrl2;

const checkResult = await validateBrokerClientVersionAgainstServer(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('error');
expect(checkResult.output).toContain(
'Your broker client version is outdated. Please upgrade to latest version.',
);
});

it('should return passing if version is more recent than server', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({});
config.BROKER_SERVER_URL = brokerServerUrl3;

const checkResult = await validateBrokerClientVersionAgainstServer(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('passing');
expect(checkResult.output).toContain(
'Running supported broker client version.',
);
});

it('should return passing if version delta is <10', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({});
config.BROKER_SERVER_URL = brokerServerUrl;

const checkResult = await validateBrokerClientVersionAgainstServer(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('passing');
expect(checkResult.output).toContain(
'Running supported broker client version.',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { validateBrokerClientVersionAgainstServer } from '../../../../../lib/client/checks/config/brokerClientVersionCheck';
import { aConfig } from '../../../../helpers/test-factories';
import nock from 'nock';

const brokerServerUrl = 'https://brokerServer';

jest.mock('../../../../../lib/common/utils/version', () => {
const originalModule = jest.requireActual(
'../../../../../lib/common/utils/version',
);

return {
__esModule: true,
...originalModule,
default: () => {
return 'local';
},
};
});

nock(brokerServerUrl)
.persist()
.get('/healthcheck')
.reply(() => {
return [200, { ok: true, version: '4.179.2' }];
});

describe('client/checks/config', () => {
describe('validateBrokerClientVersionAgainstServer()', () => {
it('should warn about dev version', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({});
config.BROKER_SERVER_URL = brokerServerUrl;

const checkResult = await validateBrokerClientVersionAgainstServer(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('warning');
expect(checkResult.output).toContain(
'Caution! You are running a dev version.',
);
});
});
});

0 comments on commit 82e27ff

Please sign in to comment.