Skip to content

Commit

Permalink
fix(update): properly update launcher version if only launcher is upd…
Browse files Browse the repository at this point in the history
…ated
  • Loading branch information
burgerni10 committed Jan 9, 2025
1 parent 8a4a3a2 commit 3547c77
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 45 deletions.
7 changes: 1 addition & 6 deletions backend/src/repository/config/config-database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,9 @@ describe('Repository with populated database', () => {
});

it('should update version', () => {
repository.updateVersion('9.9.99');
repository.updateVersion('9.9.99', '9.9.99');
expect(repository.get()!.version).toEqual('9.9.99');
});

it('should update launcher version', () => {
repository.updateLauncherVersion('9.9.999');
expect(repository.get()!.launcherVersion).toEqual('9.9.999');
});
});

describe('Certificate', () => {
Expand Down
11 changes: 3 additions & 8 deletions backend/src/repository/config/engine.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,9 @@ export default class EngineRepository {
);
}

updateVersion(version: string): void {
const query = `UPDATE ${ENGINES_TABLE} SET oibus_version = ? WHERE rowid=(SELECT MIN(rowid) FROM ${ENGINES_TABLE});`;
this.database.prepare(query).run(version);
}

updateLauncherVersion(version: string): void {
const query = `UPDATE ${ENGINES_TABLE} SET oibus_launcher_version = ? WHERE rowid=(SELECT MIN(rowid) FROM ${ENGINES_TABLE});`;
this.database.prepare(query).run(version);
updateVersion(version: string, launcherVersion: string): void {
const query = `UPDATE ${ENGINES_TABLE} SET oibus_version = ?, oibus_launcher_version = ? WHERE rowid=(SELECT MIN(rowid) FROM ${ENGINES_TABLE});`;
this.database.prepare(query).run(version, launcherVersion);
}

private createDefault(command: Omit<EngineSettings, 'id' | 'version' | 'launcherVersion'>, launcherVersion: string): void {
Expand Down
42 changes: 37 additions & 5 deletions backend/src/service/oia/oianalytics-command.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import OIAnalyticsMessageServiceMock from '../../tests/__mocks__/service/oia/oia
import crypto from 'node:crypto';
import OIAnalyticsRegistrationService from './oianalytics-registration.service';
import OIAnalyticsRegistrationServiceMock from '../../tests/__mocks__/service/oia/oianalytics-registration-service.mock';
import { EngineSettings } from '../../model/engine.model';

jest.mock('node:crypto');
jest.mock('node:fs/promises');
Expand Down Expand Up @@ -95,14 +96,14 @@ describe('OIAnalytics Command Service', () => {

it('should properly start and stop service', async () => {
expect(oIBusService.getEngineSettings).toHaveBeenCalledTimes(1);
expect(oIBusService.updateOIBusLauncherVersion).not.toHaveBeenCalled();
expect(oIBusService.updateOIBusVersion).toHaveBeenCalledWith(
(testData.oIAnalytics.commands.oIBusList[0] as OIBusUpdateVersionCommand).commandContent.version.slice(1)
(testData.oIAnalytics.commands.oIBusList[0] as OIBusUpdateVersionCommand).commandContent.version.slice(1),
testData.engine.settings.launcherVersion
);
expect(oIAnalyticsCommandRepository.markAsCompleted).toHaveBeenCalledWith(
testData.oIAnalytics.commands.oIBusList[0].id,
testData.constants.dates.FAKE_NOW,
`OIBus updated to version ${(testData.oIAnalytics.commands.oIBusList[0] as OIBusUpdateVersionCommand).commandContent.version.slice(1)}`
`OIBus updated to version ${(testData.oIAnalytics.commands.oIBusList[0] as OIBusUpdateVersionCommand).commandContent.version.slice(1)}, launcher updated to version ${testData.engine.settings.launcherVersion}`
);

const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
Expand Down Expand Up @@ -774,7 +775,6 @@ describe('OIAnalytics Command service with update error', () => {
it('should properly start and stop service', async () => {
expect(oIBusService.getEngineSettings).toHaveBeenCalledTimes(1);
expect(oIBusService.updateOIBusVersion).not.toHaveBeenCalled();
expect(oIBusService.updateOIBusLauncherVersion).not.toHaveBeenCalled();
expect(oIAnalyticsCommandRepository.markAsErrored).toHaveBeenCalledWith(
testData.oIAnalytics.commands.oIBusList[0].id,
`OIBus has not been updated. Rollback to version ${version}`
Expand Down Expand Up @@ -809,7 +809,7 @@ describe('OIAnalytics Command service with no commands', () => {
it('should properly start when not registered', () => {
expect(oIBusService.getEngineSettings).toHaveBeenCalled();
expect(oIAnalyticsCommandRepository.list).toHaveBeenCalled();
expect(oIBusService.updateOIBusVersion).toHaveBeenCalledWith(version);
expect(oIBusService.updateOIBusVersion).toHaveBeenCalledWith(version, '3.4.0');
expect(oIAnalyticsCommandRepository.markAsCompleted).not.toHaveBeenCalled();
});

Expand All @@ -829,3 +829,35 @@ describe('OIAnalytics Command service with no commands', () => {
expect(logger.error).toHaveBeenCalledWith('OIBus is not set up to execute remote');
});
});

describe('OIAnalytics Command service with no commands and without update', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers().setSystemTime(new Date(testData.constants.dates.FAKE_NOW));

(oIAnalyticsCommandRepository.list as jest.Mock).mockReturnValue([]);
const engineSettings: EngineSettings = JSON.parse(JSON.stringify(testData.engine.settings));
engineSettings.version = version;
(oIBusService.getEngineSettings as jest.Mock).mockReturnValue(engineSettings);
service = new OIAnalyticsCommandService(
oIAnalyticsCommandRepository,
oIAnalyticsRegistrationService,
oIAnalyticsMessageService,
encryptionService,
oIAnalyticsClient,
oIBusService,
scanModeService,
southService,
northService,
logger,
'binaryFolder',
true,
engineSettings.launcherVersion
);
});

it('should properly start when not registered', () => {
expect(oIBusService.updateOIBusVersion).not.toHaveBeenCalled();
expect(oIAnalyticsCommandRepository.markAsCompleted).not.toHaveBeenCalled();
});
});
18 changes: 8 additions & 10 deletions backend/src/service/oia/oianalytics-command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export default class OIAnalyticsCommandService {
launcherVersion: string
) {
const engineSettings = this.oIBusService.getEngineSettings();
if (launcherVersion !== engineSettings.launcherVersion) {
this.logger.info(`OIBus launcher updated to ${launcherVersion}`);
this.oIBusService.updateOIBusLauncherVersion(launcherVersion);
}
const currentUpgradeCommand = this.oIAnalyticsCommandRepository.list({
status: ['RUNNING'],
types: ['update-version']
Expand All @@ -74,21 +70,23 @@ export default class OIAnalyticsCommandService {
const updateVersion = (currentUpgradeCommand[0] as OIBusUpdateVersionCommand).commandContent.version.startsWith('v')
? (currentUpgradeCommand[0] as OIBusUpdateVersionCommand).commandContent.version.slice(1)
: (currentUpgradeCommand[0] as OIBusUpdateVersionCommand).commandContent.version;
if (engineSettings.version !== updateVersion) {
this.oIBusService.updateOIBusVersion(updateVersion);
if (engineSettings.version !== updateVersion || engineSettings.launcherVersion !== launcherVersion) {
this.oIBusService.updateOIBusVersion(updateVersion, launcherVersion);
this.oIAnalyticsCommandRepository.markAsCompleted(
currentUpgradeCommand[0].id,
DateTime.now().toUTC().toISO(),
`OIBus updated to version ${updateVersion}`
`OIBus updated to version ${updateVersion}, launcher updated to version ${launcherVersion}`
);
} else if (launcherVersion === engineSettings.launcherVersion) {
this.logger.info(`OIBus updated to version ${version}, launcher updated to version ${launcherVersion}`);
} else {
this.oIAnalyticsCommandRepository.markAsErrored(
currentUpgradeCommand[0].id,
`OIBus has not been updated. Rollback to version ${version}`
);
}
} else if (engineSettings.version !== version) {
this.oIBusService.updateOIBusVersion(version);
} else if (engineSettings.version !== version || engineSettings.launcherVersion !== launcherVersion) {
this.oIBusService.updateOIBusVersion(version, launcherVersion);
this.logger.info(`OIBus updated to version ${version}, launcher updated to version ${launcherVersion}`);
}
}

Expand Down
9 changes: 2 additions & 7 deletions backend/src/service/oibus.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,8 @@ describe('OIBus Service', () => {
});

it('should update OIBus version', () => {
service.updateOIBusVersion('3.4.0');
expect(engineRepository.updateVersion).toHaveBeenCalledWith('3.4.0');
});

it('should update OIBus launcher version', () => {
service.updateOIBusLauncherVersion('3.4.0');
expect(engineRepository.updateLauncherVersion).toHaveBeenCalledWith('3.4.0');
service.updateOIBusVersion('3.4.9', '3.4.5');
expect(engineRepository.updateVersion).toHaveBeenCalledWith('3.4.9', '3.4.5');
});

it('should reset North Connector Metrics', () => {
Expand Down
8 changes: 2 additions & 6 deletions backend/src/service/oibus.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,8 @@ export default class OIBusService {
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
}

updateOIBusVersion(version: string): void {
this.engineRepository.updateVersion(version);
}

updateOIBusLauncherVersion(version: string): void {
this.engineRepository.updateLauncherVersion(version);
updateOIBusVersion(version: string, launcherVersion: string): void {
this.engineRepository.updateVersion(version, launcherVersion);
}

async resetLogger(settings: EngineSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default jest.fn().mockImplementation(() => {
return {
get: jest.fn(),
update: jest.fn(),
updateVersion: jest.fn(),
updateLauncherVersion: jest.fn()
updateVersion: jest.fn()
};
});
1 change: 0 additions & 1 deletion backend/src/tests/__mocks__/service/oibus-service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default jest.fn().mockImplementation(() => ({
getProxyServer: jest.fn(),
updateEngineSettings: jest.fn(),
updateOIBusVersion: jest.fn(),
updateOIBusLauncherVersion: jest.fn(),
setWebServerChangeLogger: jest.fn(),
setWebServerChangePort: jest.fn(),
restartOIBus: jest.fn(),
Expand Down

0 comments on commit 3547c77

Please sign in to comment.