From a422c5ac7b8f609eeab891230d9660f71bf225c5 Mon Sep 17 00:00:00 2001 From: Cornelius Suermann Date: Thu, 5 Sep 2024 16:42:22 +0200 Subject: [PATCH] fix(core): Allow license:clear command to be used for licenses that failed renewal (#10665) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- packages/cli/src/commands/license/clear.ts | 8 +++++-- .../integration/commands/license.cmd.test.ts | 24 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/license/clear.ts b/packages/cli/src/commands/license/clear.ts index 5ae2e9015b55f..8abafa97ad322 100644 --- a/packages/cli/src/commands/license/clear.ts +++ b/packages/cli/src/commands/license/clear.ts @@ -12,10 +12,14 @@ export class ClearLicenseCommand extends BaseCommand { async run() { this.logger.info('Clearing license from database.'); - // Invoke shutdown() to force any floating entitlements to be released + // Attempt to invoke shutdown() to force any floating entitlements to be released const license = Container.get(License); await license.init(); - await license.shutdown(); + try { + await license.shutdown(); + } catch { + this.logger.info('License shutdown failed. Continuing with clearing license from database.'); + } await Container.get(SettingsRepository).delete({ key: SETTINGS_LICENSE_CERT_KEY, diff --git a/packages/cli/test/integration/commands/license.cmd.test.ts b/packages/cli/test/integration/commands/license.cmd.test.ts index f7c05a7c774f3..4dba8d163f629 100644 --- a/packages/cli/test/integration/commands/license.cmd.test.ts +++ b/packages/cli/test/integration/commands/license.cmd.test.ts @@ -1,10 +1,14 @@ -import { License } from '@/license'; -import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials'; -import { ClearLicenseCommand } from '@/commands/license/clear'; +import { Container } from 'typedi'; import { setupTestCommand } from '@test-integration/utils/test-command'; import { mockInstance } from '../../shared/mocking'; +import { ClearLicenseCommand } from '@/commands/license/clear'; +import { SETTINGS_LICENSE_CERT_KEY } from '@/constants'; +import { SettingsRepository } from '@/databases/repositories/settings.repository'; +import { License } from '@/license'; +import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials'; + mockInstance(LoadNodesAndCredentials); const license = mockInstance(License); const command = setupTestCommand(ClearLicenseCommand); @@ -15,3 +19,17 @@ test('license:clear invokes shutdown() to release any floating entitlements', as expect(license.init).toHaveBeenCalledTimes(1); expect(license.shutdown).toHaveBeenCalledTimes(1); }); + +test('license:clear deletes the license from the DB even if shutdown() fails', async () => { + license.shutdown.mockRejectedValueOnce(new Error('shutdown failed')); + + const settingsRepository = Container.get(SettingsRepository); + + settingsRepository.delete = jest.fn(); + + await command.run(); + + expect(settingsRepository.delete).toHaveBeenCalledWith({ + key: SETTINGS_LICENSE_CERT_KEY, + }); +});