-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add broker client version preflight check
- Loading branch information
Showing
4 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/unit/client/checks/config/brokerClientVersionCheck.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
); | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
test/unit/client/checks/config/brokerClientVersionSkipDevLocalVersionCheck.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
); | ||
}); | ||
}); | ||
}); |