From 24269fea9e4ead36cd2881e0bf176308e5e9d6ef Mon Sep 17 00:00:00 2001 From: Ivo Yankov Date: Fri, 20 Dec 2024 11:03:26 +0200 Subject: [PATCH] wip: attempting to fix tests Signed-off-by: Ivo Yankov --- src/core/config_manager.ts | 3 +- src/index.ts | 3 -- test/setup.ts | 4 ++ test/test_container.ts | 52 +++++++++++++++++++++++ test/test_util.ts | 6 ++- test/unit/commands/base.test.ts | 36 +++++++++------- test/unit/commands/network.test.ts | 5 ++- test/unit/core/platform_installer.test.ts | 15 ++++--- test/unit/core/profile_manager.test.ts | 33 ++++++++------ 9 files changed, 117 insertions(+), 40 deletions(-) create mode 100644 test/test_container.ts diff --git a/src/core/config_manager.ts b/src/core/config_manager.ts index 4fe075db9..7cbed15cc 100644 --- a/src/core/config_manager.ts +++ b/src/core/config_manager.ts @@ -22,7 +22,7 @@ import * as helpers from './helpers.js'; import type * as yargs from 'yargs'; import {type CommandFlag} from '../types/flag_types.js'; import {type ListrTaskWrapper} from 'listr2'; -import {autoInjectable} from 'tsyringe-neo'; +import {autoInjectable, container} from 'tsyringe-neo'; /** * ConfigManager cache command flag values so that user doesn't need to enter the same values repeatedly. @@ -35,6 +35,7 @@ export class ConfigManager { config!: Record; constructor(private readonly logger?: SoloLogger) { + const logger2 = container.resolve(SoloLogger) this.reset(); } diff --git a/src/index.ts b/src/index.ts index efca8f117..4fcdd1dc2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,6 @@ import * as commands from './commands/index.js'; import {HelmDependencyManager, DependencyManager} from './core/dependency_managers/index.js'; import * as constants from './core/constants.js'; import {PackageDownloader} from './core/package_downloader.js'; -import {Zippy} from './core/zippy.js'; import {Helm} from './core/helm.js'; import {ChartManager} from './core/chart_manager.js'; import {ConfigManager} from './core/config_manager.js'; @@ -44,8 +43,6 @@ import * as helpers from './core/helpers.js'; import {K8} from './core/k8.js'; import {CustomProcessOutput} from './core/process_output.js'; import {type Opts} from './types/command_types.js'; -import {IntervalLeaseRenewalService} from './core/lease/interval_lease_renewal.js'; -import {type LeaseRenewalService} from './core/lease/lease.js'; import {container} from 'tsyringe-neo'; import {SoloLogger} from './core/logging.js'; diff --git a/test/setup.ts b/test/setup.ts index c4c4ddb84..32e6dbded 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -17,6 +17,10 @@ import * as chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import sinonChai from 'sinon-chai'; +import 'reflect-metadata'; +import {resetTestContainer} from './test_container.js'; + +resetTestContainer(); chai.use(chaiAsPromised); chai.use(sinonChai); diff --git a/test/test_container.ts b/test/test_container.ts new file mode 100644 index 000000000..1e5872d2f --- /dev/null +++ b/test/test_container.ts @@ -0,0 +1,52 @@ +import {container} from "tsyringe-neo"; +import {SoloLogger} from "../src/core/logging.js"; +import {PackageDownloader} from "../src/core/package_downloader.js"; +import {Zippy} from "../src/core/zippy.js"; +import {DependencyManager, HelmDependencyManager} from "../src/core/dependency_managers/index.js"; +import {Helm} from "../src/core/helm.js"; +import {ChartManager} from "../src/core/chart_manager.js"; +import {ConfigManager} from "../src/core/config_manager.js"; +import {K8} from "../src/core/k8.js"; +import {AccountManager} from "../src/core/account_manager.js"; +import {PlatformInstaller} from "../src/core/platform_installer.js"; +import {KeyManager} from "../src/core/key_manager.js"; +import {ProfileManager} from "../src/core/profile_manager.js"; +import {IntervalLeaseRenewalService} from "../src/core/lease/interval_lease_renewal.js"; +import {LeaseManager} from "../src/core/lease/lease_manager.js"; +import {CertificateManager} from "../src/core/certificate_manager.js"; +import path from "path"; +import * as constants from "../src/core/constants.js"; +import {LocalConfig} from "../src/core/config/local_config.js"; +import {RemoteConfigManager} from "../src/core/config/remote/remote_config_manager.js"; + + +let cacheDir = path.join('test', 'data', 'tmp'); +// if (!cacheDir) cacheDir = path.join('test', 'data', 'tmp'); +// container.reset(); +container.register(SoloLogger, {useValue: new SoloLogger('debug', true)}); +container.register(PackageDownloader, {useValue: new PackageDownloader()}); +container.register(Zippy, {useValue: new Zippy()}); +container.register(HelmDependencyManager, {useValue: new HelmDependencyManager()}); +container.register(DependencyManager, {useValue: new DependencyManager()}); +container.register(Helm, {useValue: new Helm()}); +container.register(ChartManager, {useValue: new ChartManager()}); +container.register(ConfigManager, {useValue: new ConfigManager()}); +container.register(K8, {useValue: new K8()}); +container.register(AccountManager, {useValue: new AccountManager()}); +container.register(PlatformInstaller, {useValue: new PlatformInstaller()}); +container.register(KeyManager, {useValue: new KeyManager()}); +container.register(ProfileManager, {useValue: new ProfileManager()}); +container.register(IntervalLeaseRenewalService, { + useValue: new IntervalLeaseRenewalService(), +}); +container.register(LeaseManager, { + useValue: new LeaseManager(container.resolve(IntervalLeaseRenewalService)), +}); +container.register(CertificateManager, {useValue: new CertificateManager()}); +const localConfigPath = path.join(cacheDir, constants.DEFAULT_LOCAL_CONFIG_FILE); +container.register(LocalConfig, {useValue: new LocalConfig(localConfigPath)}); +container.register(RemoteConfigManager, {useValue: new RemoteConfigManager()}); + +export function resetTestContainer(cacheDir?: string) { + +} \ No newline at end of file diff --git a/test/test_util.ts b/test/test_util.ts index 24db4d826..32a53d4e2 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -36,7 +36,7 @@ import {AccountCommand} from '../src/commands/account.js'; import {SoloError} from '../src/core/errors.js'; import {execSync} from 'child_process'; import * as NodeCommandConfigs from '../src/commands/node/configs.js'; -import type {SoloLogger} from '../src/core/logging.js'; +import {SoloLogger} from '../src/core/logging.js'; import type {BaseCommand} from '../src/commands/base.js'; import type {NodeAlias} from '../src/types/aliases.js'; import type {NetworkNodeServices} from '../src/core/network_node_services.js'; @@ -61,6 +61,7 @@ import {HEDERA_PLATFORM_VERSION} from '../version.js'; import {IntervalLeaseRenewalService} from '../src/core/lease/interval_lease_renewal.js'; import {Duration} from '../src/core/time/duration.js'; import {container} from 'tsyringe-neo'; +import {resetTestContainer} from "./test_container.js"; export const testLogger = logging.NewLogger('debug', true); export const TEST_CLUSTER = 'solo-e2e'; @@ -138,6 +139,9 @@ export function bootstrapTestVariables( ): BootstrapResponse { const namespace: string = argv[flags.namespace.name] || 'bootstrap-ns'; const cacheDir: string = argv[flags.cacheDir.name] || getTestCacheDir(testName); + + // resetTestContainer(cacheDir) + const configManager = container.resolve(ConfigManager); configManager.update(argv); diff --git a/test/unit/commands/base.test.ts b/test/unit/commands/base.test.ts index 1514411fe..589154483 100644 --- a/test/unit/commands/base.test.ts +++ b/test/unit/commands/base.test.ts @@ -27,28 +27,34 @@ import * as logging from '../../../src/core/logging.js'; import {BaseCommand} from '../../../src/commands/base.js'; import {Flags as flags} from '../../../src/commands/flags.js'; import sinon from 'sinon'; -import path from 'path'; -import {BASE_TEST_DIR} from '../../test_util.js'; import {container} from 'tsyringe-neo'; - -const testLogger = logging.NewLogger('debug', true); - -describe('BaseCommand', () => { - const helm = container.resolve(Helm); - const chartManager = container.resolve(ChartManager); - const configManager = container.resolve(ConfigManager); - - const depManager = container.resolve(DependencyManager); - const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml')); - const remoteConfigManager = container.resolve(RemoteConfigManager); - +import {SoloLogger} from "../../../src/core/logging.js"; +import {resetTestContainer} from "../../test_container.js"; + +describe.only('BaseCommand', () => { + let helm: Helm; + let chartManager: ChartManager; + let configManager: ConfigManager; + let depManager: DependencyManager; + let localConfig: LocalConfig; + let remoteConfigManager: RemoteConfigManager; let sandbox = sinon.createSandbox(); + let testLogger: SoloLogger; let baseCmd: BaseCommand; describe('runShell', () => { before(() => { - sandbox = sinon.createSandbox(); + // resetTestContainer(); + testLogger = container.resolve(SoloLogger); + helm = container.resolve(Helm); + chartManager = container.resolve(ChartManager); + configManager = container.resolve(ConfigManager); + depManager = container.resolve(DependencyManager); + localConfig = container.resolve(LocalConfig); + remoteConfigManager = container.resolve(RemoteConfigManager); + + sandbox = sinon.createSandbox(); sandbox.stub(K8.prototype, 'init').callsFake(() => this); const k8 = container.resolve(K8); diff --git a/test/unit/commands/network.test.ts b/test/unit/commands/network.test.ts index ead8d3f25..3cffc6722 100644 --- a/test/unit/commands/network.test.ts +++ b/test/unit/commands/network.test.ts @@ -70,8 +70,11 @@ argv[flags.chartDirectory.name] = undefined; describe('NetworkCommand unit tests', () => { describe('Chart Install Function is called correctly', () => { let opts: any; + let bootstrapResp: any; - const bootstrapResp = bootstrapTestVariables(testName, argv); + before(() => { + bootstrapResp = bootstrapTestVariables(testName, argv); + }) beforeEach(() => { opts = getBaseCommandOpts(); diff --git a/test/unit/core/platform_installer.test.ts b/test/unit/core/platform_installer.test.ts index c52f5c104..720116b48 100644 --- a/test/unit/core/platform_installer.test.ts +++ b/test/unit/core/platform_installer.test.ts @@ -28,14 +28,17 @@ import {IllegalArgumentError, MissingArgumentError} from '../../../src/core/erro import {getK8Instance} from '../../test_util.js'; import type {PodName} from '../../../src/types/aliases.js'; import {container} from 'tsyringe-neo'; +import {K8} from "../../../src/core/k8.js"; +import {SoloLogger} from "../../../src/core/logging.js"; describe('PackageInstaller', () => { - const testLogger = logging.NewLogger('debug', true); - const configManager = container.resolve(ConfigManager); - - const k8 = getK8Instance(configManager); - - const installer = container.resolve(PlatformInstaller); + let testLogger: SoloLogger, configManager: ConfigManager, k8: K8, installer: PlatformInstaller; + before(() => { + testLogger = logging.NewLogger('debug', true); + configManager = container.resolve(ConfigManager); + k8 = getK8Instance(configManager); + installer = container.resolve(PlatformInstaller); + }) describe('validatePlatformReleaseDir', () => { it('should fail for missing path', () => { diff --git a/test/unit/core/profile_manager.test.ts b/test/unit/core/profile_manager.test.ts index ea88b6adf..961836487 100644 --- a/test/unit/core/profile_manager.test.ts +++ b/test/unit/core/profile_manager.test.ts @@ -29,21 +29,28 @@ import * as version from '../../../version.js'; import type {NodeAlias} from '../../../src/types/aliases.js'; import {container} from 'tsyringe-neo'; -const tmpDir = getTmpDir(); -const configManager = container.resolve(ConfigManager); -const profileManager = new ProfileManager(tmpDir); -configManager.setFlag(flags.nodeAliasesUnparsed, 'node1,node2,node4'); -const testProfileFile = path.join('test', 'data', 'test-profiles.yaml'); -configManager.setFlag(flags.cacheDir, getTestCacheDir('ProfileManager')); -configManager.setFlag(flags.releaseTag, version.HEDERA_PLATFORM_VERSION); -const cacheDir = configManager.getFlag(flags.cacheDir) as string; -configManager.setFlag(flags.apiPermissionProperties, path.join(cacheDir, 'templates', 'api-permission.properties')); -configManager.setFlag(flags.applicationProperties, path.join(cacheDir, 'templates', 'application.properties')); -configManager.setFlag(flags.bootstrapProperties, path.join(cacheDir, 'templates', 'bootstrap.properties')); -configManager.setFlag(flags.log4j2Xml, path.join(cacheDir, 'templates', 'log4j2.xml')); -configManager.setFlag(flags.settingTxt, path.join(cacheDir, 'templates', 'settings.txt')); + describe('ProfileManager', () => { + let tmpDir: string, configManager: ConfigManager, profileManager: ProfileManager, testProfileFile: string, + cacheDir: string; + + before(() => { + tmpDir = getTmpDir(); + configManager = container.resolve(ConfigManager); + profileManager = new ProfileManager(tmpDir); + configManager.setFlag(flags.nodeAliasesUnparsed, 'node1,node2,node4'); + testProfileFile = path.join('test', 'data', 'test-profiles.yaml'); + configManager.setFlag(flags.cacheDir, getTestCacheDir('ProfileManager')); + configManager.setFlag(flags.releaseTag, version.HEDERA_PLATFORM_VERSION); + cacheDir = configManager.getFlag(flags.cacheDir) as string; + configManager.setFlag(flags.apiPermissionProperties, path.join(cacheDir, 'templates', 'api-permission.properties')); + configManager.setFlag(flags.applicationProperties, path.join(cacheDir, 'templates', 'application.properties')); + configManager.setFlag(flags.bootstrapProperties, path.join(cacheDir, 'templates', 'bootstrap.properties')); + configManager.setFlag(flags.log4j2Xml, path.join(cacheDir, 'templates', 'log4j2.xml')); + configManager.setFlag(flags.settingTxt, path.join(cacheDir, 'templates', 'settings.txt')); + }); + after(() => { fs.rmSync(tmpDir, {recursive: true}); });