From 507a0817fe6686fe5405d0ba3b5f9ed9072d1032 Mon Sep 17 00:00:00 2001 From: Ivo Yankov Date: Thu, 19 Dec 2024 14:53:36 +0200 Subject: [PATCH] feat: implement IOC in core classes Signed-off-by: Ivo Yankov --- src/core/account_manager.ts | 13 ++++--- src/core/certificate_manager.ts | 20 +++++------ src/core/chart_manager.ts | 15 ++++---- src/core/config/local_config.ts | 11 +++--- .../config/remote/remote_config_manager.ts | 18 +++++----- src/core/config_manager.ts | 9 +++-- src/core/container_init.ts | 26 +++++++------- src/core/k8.ts | 15 ++++---- src/core/key_manager.ts | 8 ++--- src/core/lease/interval_lease_renewal.ts | 2 ++ src/core/lease/lease_manager.ts | 35 ++++++------------- src/core/platform_installer.ts | 22 ++++-------- src/core/profile_manager.ts | 15 ++++---- src/index.ts | 25 +++++++------ test/e2e/e2e_node_util.ts | 3 +- test/e2e/integration/commands/init.test.ts | 15 ++++---- .../integration/core/chart_manager.test.ts | 3 +- test/e2e/integration/core/k8_e2e.test.ts | 5 +-- test/e2e/integration/core/lease.test.ts | 5 +-- .../integration/core/lease_renewal.test.ts | 7 ++-- .../core/remote_config_manager.test.ts | 7 ++-- .../core/remote_config_validator.test.ts | 5 +-- test/test_util.ts | 35 +++++++------------ test/unit/commands/base.test.ts | 10 +++--- test/unit/commands/cluster.test.ts | 5 +-- test/unit/commands/context.test.ts | 16 ++++----- test/unit/commands/network.test.ts | 16 +++++---- test/unit/core/certificate_manager.test.ts | 7 ++-- test/unit/core/config_manager.test.ts | 11 +++--- test/unit/core/k8.test.ts | 5 +-- test/unit/core/key_manager.test.ts | 3 +- test/unit/core/local_config.test.ts | 12 +++---- test/unit/core/platform_installer.test.ts | 5 +-- test/unit/core/profile_manager.test.ts | 5 +-- 34 files changed, 195 insertions(+), 219 deletions(-) diff --git a/src/core/account_manager.ts b/src/core/account_manager.ts index d00b3fa2f..5fd6a14d9 100644 --- a/src/core/account_manager.ts +++ b/src/core/account_manager.ts @@ -42,11 +42,12 @@ import type {NetworkNodeServices} from './network_node_services.js'; import {NetworkNodeServicesBuilder} from './network_node_services.js'; import path from 'path'; -import {type SoloLogger} from './logging.js'; -import {type K8} from './k8.js'; +import {SoloLogger} from './logging.js'; +import {K8} from './k8.js'; import {type AccountIdWithKeyPairObject, type ExtendedNetServer} from '../types/index.js'; import {type NodeAlias, type PodName} from '../types/aliases.js'; import {IGNORED_NODE_ACCOUNT_ID} from './constants.js'; +import {autoInjectable} from "tsyringe-neo"; const REASON_FAILED_TO_GET_KEYS = 'failed to get keys for accountId'; const REASON_SKIPPED = 'skipped since it does not have a genesis key'; @@ -55,17 +56,15 @@ const REASON_FAILED_TO_CREATE_K8S_S_KEY = 'failed to create k8s scrt key'; const FULFILLED = 'fulfilled'; const REJECTED = 'rejected'; +@autoInjectable() export class AccountManager { private _portForwards: ExtendedNetServer[]; public _nodeClient: Client | null; constructor( - private readonly logger: SoloLogger, - private readonly k8: K8, + private readonly logger?: SoloLogger, + private readonly k8?: K8, ) { - if (!logger) throw new Error('An instance of core/SoloLogger is required'); - if (!k8) throw new Error('An instance of core/K8 is required'); - this._portForwards = []; this._nodeClient = null; } diff --git a/src/core/certificate_manager.ts b/src/core/certificate_manager.ts index d1e3876a5..773cd9cc9 100644 --- a/src/core/certificate_manager.ts +++ b/src/core/certificate_manager.ts @@ -20,25 +20,23 @@ import fs from 'fs'; import {Templates} from './templates.js'; import {GrpcProxyTlsEnums} from './enumerations.js'; -import type {ConfigManager} from './config_manager.js'; -import type {K8} from './k8.js'; -import type {SoloLogger} from './logging.js'; +import {ConfigManager} from './config_manager.js'; +import {K8} from './k8.js'; +import {SoloLogger} from './logging.js'; import type {ListrTaskWrapper} from 'listr2'; import type {NodeAlias} from '../types/aliases.js'; +import {autoInjectable} from "tsyringe-neo"; /** * Used to handle interactions with certificates data and inject it into the K8s cluster secrets */ +@autoInjectable() export class CertificateManager { constructor( - private readonly k8: K8, - private readonly logger: SoloLogger, - private readonly configManager: ConfigManager, - ) { - if (!k8) throw new MissingArgumentError('an instance of core/K8 is required'); - if (!logger) throw new MissingArgumentError('an instance of core/SoloLogger is required'); - if (!configManager) throw new MissingArgumentError('an instance of core/ConfigManager is required'); - } + private readonly k8?: K8, + private readonly logger?: SoloLogger, + private readonly configManager?: ConfigManager, + ) {} /** * Reads the certificate and key and build the secret with the appropriate structure diff --git a/src/core/chart_manager.ts b/src/core/chart_manager.ts index cab158fa9..42e6616be 100644 --- a/src/core/chart_manager.ts +++ b/src/core/chart_manager.ts @@ -15,19 +15,18 @@ * */ import * as constants from './constants.js'; -import {type Helm} from './helm.js'; +import {Helm} from './helm.js'; import chalk from 'chalk'; import {SoloError} from './errors.js'; -import {type SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; +import {autoInjectable} from "tsyringe-neo"; +@autoInjectable() export class ChartManager { constructor( - private readonly helm: Helm, - private readonly logger: SoloLogger, - ) { - if (!logger) throw new Error('An instance of core/SoloLogger is required'); - if (!helm) throw new Error('An instance of core/Helm is required'); - } + private readonly helm?: Helm, + private readonly logger?: SoloLogger, + ) {} /** * Setup chart repositories diff --git a/src/core/config/local_config.ts b/src/core/config/local_config.ts index 57ff22b2c..f9d263c63 100644 --- a/src/core/config/local_config.ts +++ b/src/core/config/local_config.ts @@ -21,13 +21,15 @@ import * as yaml from 'yaml'; import {Flags as flags} from '../../commands/flags.js'; import {type Deployments, type DeploymentStructure, type LocalConfigData} from './local_config_data.js'; import {MissingArgumentError, SoloError} from '../errors.js'; -import {type SoloLogger} from '../logging.js'; +import {SoloLogger} from '../logging.js'; import {IsDeployments} from '../validator_decorators.js'; -import type {ConfigManager} from '../config_manager.js'; +import {ConfigManager} from '../config_manager.js'; import type {EmailAddress, Namespace} from './remote/types.js'; import {Templates} from '../templates.js'; import {ErrorMessages} from '../error_messages.js'; +import {autoInjectable} from "tsyringe-neo"; +@autoInjectable() export class LocalConfig implements LocalConfigData { @IsEmail( {}, @@ -58,11 +60,10 @@ export class LocalConfig implements LocalConfigData { public constructor( private readonly filePath: string, - private readonly logger: SoloLogger, - private readonly configManager: ConfigManager, + private readonly logger?: SoloLogger, + private readonly configManager?: ConfigManager, ) { if (!filePath || filePath === '') throw new MissingArgumentError('a valid filePath is required'); - if (!logger) throw new Error('An instance of core/SoloLogger is required'); const allowedKeys = ['userEmailAddress', 'deployments', 'currentDeploymentName']; if (this.configFileExists()) { diff --git a/src/core/config/remote/remote_config_manager.ts b/src/core/config/remote/remote_config_manager.ts index 6da2cb402..6633ef35d 100644 --- a/src/core/config/remote/remote_config_manager.ts +++ b/src/core/config/remote/remote_config_manager.ts @@ -23,16 +23,17 @@ import {Flags as flags} from '../../../commands/flags.js'; import * as yaml from 'yaml'; import {ComponentsDataWrapper} from './components_data_wrapper.js'; import {RemoteConfigValidator} from './remote_config_validator.js'; -import type {K8} from '../../k8.js'; +import {K8} from '../../k8.js'; import type {Cluster, Namespace} from './types.js'; -import type {SoloLogger} from '../../logging.js'; -import type {ConfigManager} from '../../config_manager.js'; -import type {LocalConfig} from '../local_config.js'; +import {SoloLogger} from '../../logging.js'; +import {ConfigManager} from '../../config_manager.js'; +import {LocalConfig} from '../local_config.js'; import type {DeploymentStructure} from '../local_config_data.js'; import {type ContextClusterStructure} from '../../../types/config_types.js'; import {type EmptyContextConfig, type Optional, type SoloListrTask} from '../../../types/index.js'; import type * as k8s from '@kubernetes/client-node'; import {StatusCodes} from 'http-status-codes'; +import {autoInjectable} from "tsyringe-neo"; interface ListrContext { config: {contextCluster: ContextClusterStructure}; @@ -42,6 +43,7 @@ interface ListrContext { * Uses Kubernetes ConfigMaps to manage the remote configuration data by creating, loading, modifying, * and saving the configuration data to and from a Kubernetes cluster. */ +@autoInjectable() export class RemoteConfigManager { /** Stores the loaded remote configuration data. */ private remoteConfig: Optional; @@ -53,10 +55,10 @@ export class RemoteConfigManager { * @param configManager - Manager to retrieve application flags and settings. */ public constructor( - private readonly k8: K8, - private readonly logger: SoloLogger, - private readonly localConfig: LocalConfig, - private readonly configManager: ConfigManager, + private readonly k8?: K8, + private readonly logger?: SoloLogger, + private readonly localConfig?: LocalConfig, + private readonly configManager?: ConfigManager, ) {} /* ---------- Getters ---------- */ diff --git a/src/core/config_manager.ts b/src/core/config_manager.ts index 1f6789d93..a0c33bd11 100644 --- a/src/core/config_manager.ts +++ b/src/core/config_manager.ts @@ -11,7 +11,7 @@ * distributed under the License is distributed on an ""AS IS"" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and - * limitations under the License. + * limitations under the Licen}se. * */ import {SoloError, MissingArgumentError, IllegalArgumentError} from './errors.js'; @@ -22,6 +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"; /** * ConfigManager cache command flag values so that user doesn't need to enter the same values repeatedly. @@ -29,13 +30,11 @@ import {type ListrTaskWrapper} from 'listr2'; * For example, 'namespace' is usually remains the same across commands once it is entered, and therefore user * doesn't need to enter it repeatedly. However, user should still be able to specify the flag explicitly for any command. */ +@autoInjectable() export class ConfigManager { config!: Record; - constructor(private readonly logger: SoloLogger) { - if (!logger || !(logger instanceof SoloLogger)) - throw new MissingArgumentError('An instance of core/SoloLogger is required'); - + constructor(private readonly logger?: SoloLogger) { this.reset(); } diff --git a/src/core/container_init.ts b/src/core/container_init.ts index c4c41c36c..3bdb65a40 100644 --- a/src/core/container_init.ts +++ b/src/core/container_init.ts @@ -26,18 +26,18 @@ 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(constants.SOLO_CACHE_DIR, constants.DEFAULT_LOCAL_CONFIG_FILE); +container.register(LocalConfig, {useValue: new LocalConfig(localConfigPath)}); -// const chartManager = new ChartManager(helm, logger); -// const configManager = new ConfigManager(logger); -// const k8 = new K8(configManager, logger); -// const accountManager = new AccountManager(logger, k8); -// const platformInstaller = new PlatformInstaller(logger, k8, configManager); -// const keyManager = new KeyManager(logger); -// const profileManager = new ProfileManager(logger, configManager); -// const leaseRenewalService: LeaseRenewalService = new IntervalLeaseRenewalService(); -// const leaseManager = new LeaseManager(k8, configManager, logger, leaseRenewalService); -// const certificateManager = new CertificateManager(k8, logger, configManager); -// const localConfigPath = path.join(constants.SOLO_CACHE_DIR, constants.DEFAULT_LOCAL_CONFIG_FILE); -// const localConfig = new LocalConfig(localConfigPath, logger, configManager); -// const remoteConfigManager = new RemoteConfigManager(k8, logger, localConfig, configManager); +const remoteConfigManager = container.resolve(RemoteConfigManager); diff --git a/src/core/k8.ts b/src/core/k8.ts index 2da693d6f..3676fa558 100644 --- a/src/core/k8.ts +++ b/src/core/k8.ts @@ -31,12 +31,13 @@ import {getReasonPhrase, StatusCodes} from 'http-status-codes'; import {sleep} from './helpers.js'; import * as constants from './constants.js'; -import {type ConfigManager} from './config_manager.js'; -import {type SoloLogger} from './logging.js'; +import {ConfigManager} from './config_manager.js'; +import {SoloLogger} from './logging.js'; import {type PodName, type TarCreateFilter} from '../types/aliases.js'; import type {ExtendedNetServer, LocalContextObject} from '../types/index.js'; import {HEDERA_HAPI_PATH, ROOT_CONTAINER, SOLO_LOGS_DIR} from './constants.js'; import {Duration} from './time/duration.js'; +import {autoInjectable, container} from "tsyringe-neo"; interface TDirectoryData { directory: boolean; @@ -53,6 +54,7 @@ interface TDirectoryData { * Note: Take care if the same instance is used for parallel execution, as the behaviour may be unpredictable. * For parallel execution, create separate instances by invoking clone() */ +@autoInjectable() export class K8 { private _cachedContexts: Context[]; @@ -65,12 +67,9 @@ export class K8 { private coordinationApiClient: k8s.CoordinationV1Api; constructor( - private readonly configManager: ConfigManager, - public readonly logger: SoloLogger, + private readonly configManager?: ConfigManager, + public readonly logger?: SoloLogger, ) { - if (!configManager) throw new MissingArgumentError('An instance of core/ConfigManager is required'); - if (!logger) throw new MissingArgumentError('An instance of core/SoloLogger is required'); - this.init(); } @@ -79,7 +78,7 @@ export class K8 { * Internally it instantiates a new kube API client */ clone() { - const c = new K8(this.configManager, this.logger); + const c = container.resolve(K8); return c.init(); } diff --git a/src/core/key_manager.ts b/src/core/key_manager.ts index b55500bf6..e0561e0fa 100644 --- a/src/core/key_manager.ts +++ b/src/core/key_manager.ts @@ -27,10 +27,12 @@ import chalk from 'chalk'; import {type NodeAlias, type NodeAliases} from '../types/aliases.js'; import {type NodeKeyObject, type PrivateKeyAndCertificateObject} from '../types/index.js'; import type {ListrTask} from 'listr2'; +import {autoInjectable} from "tsyringe-neo"; // @ts-ignore x509.cryptoProvider.set(crypto); +@autoInjectable() export class KeyManager { static SigningKeyAlgo = { name: 'RSASSA-PKCS1-v1_5', @@ -60,11 +62,7 @@ export class KeyManager { hash: 'SHA-384', }; - constructor(private readonly logger: SoloLogger) { - if (!logger || !(logger instanceof SoloLogger)) - throw new MissingArgumentError('An instance of core/SoloLogger is required'); - this.logger = logger; - } + constructor(private readonly logger?: SoloLogger) {} /** Convert CryptoKey into PEM string */ async convertPrivateKeyToPem(privateKey: CryptoKey) { diff --git a/src/core/lease/interval_lease_renewal.ts b/src/core/lease/interval_lease_renewal.ts index caf6c039c..9dc2d01f2 100644 --- a/src/core/lease/interval_lease_renewal.ts +++ b/src/core/lease/interval_lease_renewal.ts @@ -16,11 +16,13 @@ */ import {type Lease, type LeaseRenewalService} from './lease.js'; import {Duration} from '../time/duration.js'; +import {autoInjectable} from "tsyringe-neo"; /** * Implements a lease renewal service which utilizes a setInterval() based approach to renew leases at regular intervals. * The renewal delay is calculated as half the duration of the lease in seconds. */ +@autoInjectable() export class IntervalLeaseRenewalService implements LeaseRenewalService { /** The internal registry used to track all non-cancelled lease renewals. */ private readonly _scheduledLeases: Map; diff --git a/src/core/lease/lease_manager.ts b/src/core/lease/lease_manager.ts index 7d80f867c..d2def5a8b 100644 --- a/src/core/lease/lease_manager.ts +++ b/src/core/lease/lease_manager.ts @@ -14,26 +14,21 @@ * limitations under the License. * */ -import {MissingArgumentError} from '../errors.js'; import {Flags as flags} from '../../commands/flags.js'; -import type {ConfigManager} from '../config_manager.js'; -import type {K8} from '../k8.js'; -import type {SoloLogger} from '../logging.js'; -import {type Lease, type LeaseRenewalService} from './lease.js'; +import {ConfigManager} from '../config_manager.js'; +import {K8} from '../k8.js'; +import {SoloLogger} from '../logging.js'; +import {type Lease, LeaseRenewalService} from './lease.js'; import {IntervalLease} from './interval_lease.js'; import {LeaseHolder} from './lease_holder.js'; import {LeaseAcquisitionError} from './lease_errors.js'; +import {autoInjectable} from "tsyringe-neo"; /** * Manages the acquisition and renewal of leases. */ +@autoInjectable() export class LeaseManager { - /** The injected logger instance. */ - private readonly _logger: SoloLogger; - - /** The injected lease renewal service instance. */ - private readonly _renewalService: LeaseRenewalService; - /** * Creates a new lease manager. * @@ -43,19 +38,11 @@ export class LeaseManager { * @param renewalService - the lease renewal service. */ constructor( - private readonly k8: K8, - private readonly configManager: ConfigManager, - logger: SoloLogger, - renewalService: LeaseRenewalService, - ) { - if (!k8) throw new MissingArgumentError('an instance of core/K8 is required'); - if (!logger) throw new MissingArgumentError('an instance of core/SoloLogger is required'); - if (!configManager) throw new MissingArgumentError('an instance of core/ConfigManager is required'); - if (!renewalService) throw new MissingArgumentError('an instance of core/LeaseRenewalService is required'); - - this._logger = logger; - this._renewalService = renewalService; - } + private readonly _renewalService?: LeaseRenewalService, + private readonly _logger?: SoloLogger, + private readonly k8?: K8, + private readonly configManager?: ConfigManager + ) {} /** * Creates a new lease. This lease is not acquired until the `acquire` method is called. diff --git a/src/core/platform_installer.ts b/src/core/platform_installer.ts index d8be49e00..4e1168334 100644 --- a/src/core/platform_installer.ts +++ b/src/core/platform_installer.ts @@ -19,33 +19,23 @@ import {Listr} from 'listr2'; import * as path from 'path'; import {SoloError, IllegalArgumentError, MissingArgumentError} from './errors.js'; import * as constants from './constants.js'; -import {type ConfigManager} from './config_manager.js'; -import {type K8} from './k8.js'; +import {ConfigManager} from './config_manager.js'; +import {K8} from './k8.js'; import {Templates} from './templates.js'; import {Flags as flags} from '../commands/flags.js'; import * as Base64 from 'js-base64'; import chalk from 'chalk'; -import {type SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; import type {NodeAlias, NodeAliases, PodName} from '../types/aliases.js'; import {Duration} from './time/duration.js'; import {sleep} from './helpers.js'; +import {autoInjectable} from "tsyringe-neo"; /** PlatformInstaller install platform code in the root-container of a network pod */ +@autoInjectable() export class PlatformInstaller { - private logger: SoloLogger; - private k8: K8; - private configManager: ConfigManager; - - constructor(logger: SoloLogger, k8: K8, configManager: ConfigManager) { - if (!logger) throw new MissingArgumentError('an instance of core/SoloLogger is required'); - if (!k8) throw new MissingArgumentError('an instance of core/K8 is required'); - if (!configManager) throw new MissingArgumentError('an instance of core/ConfigManager is required'); - - this.logger = logger; - this.k8 = k8; - this.configManager = configManager; - } + constructor(private logger?: SoloLogger, private k8?: K8, private configManager?: ConfigManager) {} private _getNamespace(): string { const ns = this.configManager.getFlag(flags.namespace) as string; diff --git a/src/core/profile_manager.ts b/src/core/profile_manager.ts index a64618a36..72c80e71d 100644 --- a/src/core/profile_manager.ts +++ b/src/core/profile_manager.ts @@ -26,11 +26,12 @@ import {readFile, writeFile} from 'fs/promises'; import {Flags as flags} from '../commands/flags.js'; import {Templates} from './templates.js'; import * as constants from './constants.js'; -import {type ConfigManager} from './config_manager.js'; +import {ConfigManager} from './config_manager.js'; import * as helpers from './helpers.js'; import {getNodeAccountMap} from './helpers.js'; -import type {SoloLogger} from './logging.js'; +import {SoloLogger} from './logging.js'; import type {NodeAlias, NodeAliases} from '../types/aliases.js'; +import {container, singleton} from "tsyringe-neo"; const consensusSidecars = [ 'recordStreamUploader', @@ -40,6 +41,7 @@ const consensusSidecars = [ 'otelCollector', ]; +@singleton() export class ProfileManager { private readonly logger: SoloLogger; private readonly configManager: ConfigManager; @@ -48,12 +50,9 @@ export class ProfileManager { private profiles: Map; private profileFile: string | undefined; - constructor(logger: SoloLogger, configManager: ConfigManager, cacheDir: string = constants.SOLO_VALUES_DIR) { - if (!logger) throw new MissingArgumentError('An instance of core/SoloLogger is required'); - if (!configManager) throw new MissingArgumentError('An instance of core/ConfigManager is required'); - - this.logger = logger; - this.configManager = configManager; + constructor(cacheDir: string = constants.SOLO_VALUES_DIR) { + this.logger = container.resolve(SoloLogger); + this.configManager = container.resolve(ConfigManager); this.profiles = new Map(); diff --git a/src/index.ts b/src/index.ts index 618d9e0d8..6257bcb09 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,20 +66,19 @@ export function main(argv: any) { const helmDepManager = container.resolve(HelmDependencyManager) const depManager = container.resolve(DependencyManager) const helm = container.resolve(Helm) - - const chartManager = new ChartManager(helm, logger); - const configManager = new ConfigManager(logger); - const k8 = new K8(configManager, logger); - const accountManager = new AccountManager(logger, k8); - const platformInstaller = new PlatformInstaller(logger, k8, configManager); - const keyManager = new KeyManager(logger); - const profileManager = new ProfileManager(logger, configManager); - const leaseRenewalService: LeaseRenewalService = new IntervalLeaseRenewalService(); - const leaseManager = new LeaseManager(k8, configManager, logger, leaseRenewalService); - const certificateManager = new CertificateManager(k8, logger, configManager); + const chartManager = container.resolve(ChartManager); + const configManager = container.resolve(ConfigManager) + const k8 = container.resolve(K8); + const accountManager = container.resolve(AccountManager); + const platformInstaller = container.resolve(PlatformInstaller); + const keyManager = container.resolve(KeyManager); + const profileManager = container.resolve(ProfileManager); + const leaseRenewalService: LeaseRenewalService = container.resolve(IntervalLeaseRenewalService); + const leaseManager = container.resolve(LeaseManager); + const certificateManager = container.resolve(CertificateManager); const localConfigPath = path.join(constants.SOLO_CACHE_DIR, constants.DEFAULT_LOCAL_CONFIG_FILE); - const localConfig = new LocalConfig(localConfigPath, logger, configManager); - const remoteConfigManager = new RemoteConfigManager(k8, logger, localConfig, configManager); + const localConfig = new LocalConfig(localConfigPath); + const remoteConfigManager = container.resolve(RemoteConfigManager); // set cluster and namespace in the global configManager from kubernetes context // so that we don't need to prompt the user diff --git a/test/e2e/e2e_node_util.ts b/test/e2e/e2e_node_util.ts index 19d135891..b5b17aedb 100644 --- a/test/e2e/e2e_node_util.ts +++ b/test/e2e/e2e_node_util.ts @@ -36,6 +36,7 @@ import {type K8} from '../../src/core/k8.js'; import {type NodeCommand} from '../../src/commands/node/index.js'; import {Duration} from '../../src/core/time/duration.js'; import {StatusCodes} from 'http-status-codes'; +import {container} from "tsyringe-neo"; export function e2eNodeKeyRefreshTest(testName: string, mode: string, releaseTag = HEDERA_PLATFORM_VERSION_TAG) { const namespace = testName; @@ -191,7 +192,7 @@ export function e2eNodeKeyRefreshTest(testName: string, mode: string, releaseTag async function nodeRefreshTestSetup(argv: Record, testName: string, k8: K8, nodeAliases: string) { argv[flags.nodeAliasesUnparsed.name] = nodeAliases; - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) configManager.update(argv); const podArray = await k8.getPodsByLabel([`app=network-${nodeAliases}`, 'solo.hedera.com/type=network-node']); diff --git a/test/e2e/integration/commands/init.test.ts b/test/e2e/integration/commands/init.test.ts index 5ce3506ca..72fdcd8d5 100644 --- a/test/e2e/integration/commands/init.test.ts +++ b/test/e2e/integration/commands/init.test.ts @@ -48,13 +48,13 @@ describe('InitCommand', () => { // const depManager = new DependencyManager(helmDepManager); const depManager = container.resolve(DependencyManager); const helm = container.resolve(Helm); + const chartManager = container.resolve(ChartManager); - const chartManager = new ChartManager(helm, testLogger); - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) let k8: K8; let localConfig: LocalConfig; - const keyManager = new KeyManager(testLogger); + const keyManager = container.resolve(KeyManager); let leaseManager: LeaseManager; let remoteConfigManager: RemoteConfigManager; @@ -65,10 +65,11 @@ describe('InitCommand', () => { before(() => { sandbox = sinon.createSandbox(); sandbox.stub(K8.prototype, 'init').callsFake(() => this); - k8 = new K8(configManager, testLogger); - localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml'), testLogger, configManager); - remoteConfigManager = new RemoteConfigManager(k8, testLogger, localConfig, configManager); - leaseManager = new LeaseManager(k8, configManager, testLogger, new IntervalLeaseRenewalService()); + k8 = container.resolve(K8); + localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml')); + remoteConfigManager = container.resolve(RemoteConfigManager); + leaseManager = container.resolve(LeaseManager); + // @ts-ignore initCmd = new InitCommand({ logger: testLogger, diff --git a/test/e2e/integration/core/chart_manager.test.ts b/test/e2e/integration/core/chart_manager.test.ts index f74e3ea94..2c47a235c 100644 --- a/test/e2e/integration/core/chart_manager.test.ts +++ b/test/e2e/integration/core/chart_manager.test.ts @@ -24,8 +24,7 @@ import {testLogger} from '../../../test_util.js'; import {container} from "tsyringe-neo"; describe('ChartManager', () => { - const helm = container.resolve(Helm); - const chartManager = new ChartManager(helm, testLogger); + const chartManager = container.resolve(ChartManager); it('should be able to list installed charts', async () => { const ns = constants.SOLO_SETUP_NAMESPACE; diff --git a/test/e2e/integration/core/k8_e2e.test.ts b/test/e2e/integration/core/k8_e2e.test.ts index f4642039a..7cfe4dadc 100644 --- a/test/e2e/integration/core/k8_e2e.test.ts +++ b/test/e2e/integration/core/k8_e2e.test.ts @@ -47,6 +47,7 @@ import { import crypto from 'crypto'; import type {PodName} from '../../../../src/types/aliases.js'; import {Duration} from '../../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; const defaultTimeout = Duration.ofMinutes(2).toMillis(); @@ -80,8 +81,8 @@ async function createPod( describe('K8', () => { const testLogger = logging.NewLogger('debug', true); - const configManager = new ConfigManager(testLogger); - const k8 = new K8(configManager, testLogger); + const configManager = container.resolve(ConfigManager) + const k8 = container.resolve(K8); const testNamespace = 'k8-e2e'; const argv = []; const podName = `test-pod-${uuid4()}` as PodName; diff --git a/test/e2e/integration/core/lease.test.ts b/test/e2e/integration/core/lease.test.ts index 27ef8cbde..d0e3b4ee2 100644 --- a/test/e2e/integration/core/lease.test.ts +++ b/test/e2e/integration/core/lease.test.ts @@ -25,14 +25,15 @@ import {sleep} from '../../../../src/core/helpers.js'; import {LeaseRelinquishmentError} from '../../../../src/core/lease/lease_errors.js'; import {NoopLeaseRenewalService} from './noop_lease_renewal_service.test.js'; import {Duration} from '../../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; const defaultTimeout = Duration.ofMinutes(2).toMillis(); const leaseDuration = 4; describe('Lease', async () => { const testLogger = logging.NewLogger('debug', true); - const configManager = new ConfigManager(testLogger); - const k8 = new K8(configManager, testLogger); + const configManager = container.resolve(ConfigManager) + const k8 = container.resolve(K8); const testNamespace = 'lease-e2e'; const renewalService = new NoopLeaseRenewalService(); diff --git a/test/e2e/integration/core/lease_renewal.test.ts b/test/e2e/integration/core/lease_renewal.test.ts index 27cebceff..d55460186 100644 --- a/test/e2e/integration/core/lease_renewal.test.ts +++ b/test/e2e/integration/core/lease_renewal.test.ts @@ -25,16 +25,17 @@ import {sleep} from '../../../../src/core/helpers.js'; import {IntervalLeaseRenewalService} from '../../../../src/core/lease/interval_lease_renewal.js'; import {type V1Lease} from '@kubernetes/client-node'; import {Duration} from '../../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; const defaultTimeout = Duration.ofMinutes(2).toMillis(); const leaseDuration = 4; describe('LeaseRenewalService', async () => { const testLogger = logging.NewLogger('debug', true); - const configManager = new ConfigManager(testLogger); - const k8 = new K8(configManager, testLogger); + const configManager = container.resolve(ConfigManager) + const k8 = container.resolve(K8); + const renewalService = container.resolve(IntervalLeaseRenewalService); const testNamespace = 'lease-renewal-e2e'; - const renewalService = new IntervalLeaseRenewalService(); before(async function () { this.timeout(defaultTimeout); diff --git a/test/e2e/integration/core/remote_config_manager.test.ts b/test/e2e/integration/core/remote_config_manager.test.ts index 8098bb2c1..0f2607e83 100644 --- a/test/e2e/integration/core/remote_config_manager.test.ts +++ b/test/e2e/integration/core/remote_config_manager.test.ts @@ -29,6 +29,7 @@ import path from 'path'; import {SoloError} from '../../../../src/core/errors.js'; import {RemoteConfigDataWrapper} from '../../../../src/core/config/remote/remote_config_data_wrapper.js'; import {Duration} from '../../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; const defaultTimeout = Duration.ofSeconds(20).toMillis(); @@ -58,12 +59,10 @@ e2eTestSuite( bootstrapResp => { describe('RemoteConfigManager', async () => { const k8 = bootstrapResp.opts.k8; - const logger = bootstrapResp.opts.logger; - const configManager = bootstrapResp.opts.configManager; const filePath = path.join(constants.SOLO_CACHE_DIR, constants.DEFAULT_LOCAL_CONFIG_FILE); - const localConfig = new LocalConfig(filePath, logger, configManager); - const remoteConfigManager = new RemoteConfigManager(k8, logger, localConfig, configManager); + const localConfig = new LocalConfig(filePath); + const remoteConfigManager = container.resolve(RemoteConfigManager); const email = 'john@gmail.com'; diff --git a/test/e2e/integration/core/remote_config_validator.test.ts b/test/e2e/integration/core/remote_config_validator.test.ts index fc3cf49b4..ae7485f29 100644 --- a/test/e2e/integration/core/remote_config_validator.test.ts +++ b/test/e2e/integration/core/remote_config_validator.test.ts @@ -36,13 +36,14 @@ import {MirrorNodeExplorerComponent} from '../../../../src/core/config/remote/co import {EnvoyProxyComponent} from '../../../../src/core/config/remote/components/envoy_proxy_component.js'; import type {NodeAlias, NodeAliases} from '../../../../src/types/aliases.js'; +import {container} from "tsyringe-neo"; describe('RemoteConfigValidator', () => { const namespace = 'remote-config-validator'; - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) configManager.update({[flags.namespace.name]: namespace}); - const k8 = new K8(configManager, testLogger); + const k8 = container.resolve(K8); before(async () => { await k8.createNamespace(namespace); diff --git a/test/test_util.ts b/test/test_util.ts index 9ee7c1555..a3a51db9b 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -138,31 +138,22 @@ export function bootstrapTestVariables( ): BootstrapResponse { const namespace: string = argv[flags.namespace.name] || 'bootstrap-ns'; const cacheDir: string = argv[flags.cacheDir.name] || getTestCacheDir(testName); - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) configManager.update(argv); - // const downloader = new PackageDownloader(testLogger); - // const zippy = new Zippy(testLogger); - const downloader = container.resolve(PackageDownloader); - - // const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger); - // const helmDepManager = new HelmDependencyManager(); - // const depManagerMap = new Map().set(constants.HELM, helmDepManager); - // const depManager = new DependencyManager(testLogger, depManagerMap); const depManager = container.resolve(DependencyManager); const helm = container.resolve(Helm); - - const keyManager = new KeyManager(testLogger); - const chartManager = new ChartManager(helm, testLogger); - const k8 = k8Arg || new K8(configManager, testLogger); - const accountManager = new AccountManager(testLogger, k8); - const platformInstaller = new PlatformInstaller(testLogger, k8, configManager); - const profileManager = new ProfileManager(testLogger, configManager); - const leaseManager = new LeaseManager(k8, configManager, testLogger, new IntervalLeaseRenewalService()); - const certificateManager = new CertificateManager(k8, testLogger, configManager); - const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml'), testLogger, configManager); - const remoteConfigManager = new RemoteConfigManager(k8, testLogger, localConfig, configManager); + const chartManager = container.resolve(ChartManager); + const keyManager = container.resolve(KeyManager); + const k8 = k8Arg || container.resolve(K8); + const accountManager = container.resolve(AccountManager); + const platformInstaller = container.resolve(PlatformInstaller); + const profileManager = container.resolve(ProfileManager); + const leaseManager = container.resolve(LeaseManager); + const certificateManager = container.resolve(CertificateManager); + const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml')); + const remoteConfigManager = container.resolve(RemoteConfigManager); const opts: TestOpts = { logger: testLogger, @@ -441,7 +432,7 @@ async function addKeyHashToMap( export function getK8Instance(configManager: ConfigManager) { try { - return new K8(configManager, testLogger); + return container.resolve(K8); // TODO: return a mock without running the init within constructor after we convert to Mocha, Jest ESModule mocks are broke. } catch (e) { if (!(e instanceof SoloError)) { @@ -455,7 +446,7 @@ export function getK8Instance(configManager: ConfigManager) { // Create cluster execSync(`kind create cluster --name "${process.env.SOLO_CLUSTER_NAME}"`, {stdio: 'inherit'}); - return new K8(configManager, testLogger); + return container.resolve(K8); } } diff --git a/test/unit/commands/base.test.ts b/test/unit/commands/base.test.ts index 19aa8161a..2918b3883 100644 --- a/test/unit/commands/base.test.ts +++ b/test/unit/commands/base.test.ts @@ -38,12 +38,12 @@ const testLogger = logging.NewLogger('debug', true); describe('BaseCommand', () => { const helm = container.resolve(Helm); - const chartManager = new ChartManager(helm, testLogger); - const configManager = new ConfigManager(testLogger); + 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'), testLogger, configManager); - const remoteConfigManager = new RemoteConfigManager({} as any, testLogger, localConfig, configManager); + const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml')); + const remoteConfigManager = container.resolve(RemoteConfigManager); let sandbox = sinon.createSandbox(); @@ -53,7 +53,7 @@ describe('BaseCommand', () => { before(() => { sandbox = sinon.createSandbox(); sandbox.stub(K8.prototype, 'init').callsFake(() => this); - const k8 = new K8(configManager, testLogger); + const k8 = container.resolve(K8); // @ts-ignore baseCmd = new BaseCommand({ diff --git a/test/unit/commands/cluster.test.ts b/test/unit/commands/cluster.test.ts index 0e2e52cef..f0a824b5a 100644 --- a/test/unit/commands/cluster.test.ts +++ b/test/unit/commands/cluster.test.ts @@ -63,12 +63,13 @@ describe('ClusterCommand unit tests', () => { opts = getBaseCommandOpts(); opts.logger = new SoloLogger(); opts.helm = container.resolve(Helm); + opts.chartManager = container.resolve(ChartManager); opts.helm.dependency = sinon.stub(); - opts.chartManager = new ChartManager(opts.helm, opts.logger); + opts.chartManager.isChartInstalled = sinon.stub().returns(false); opts.chartManager.install = sinon.stub().returns(true); - opts.configManager = new ConfigManager(opts.logger); + opts.configManager = container.resolve(ConfigManager); opts.remoteConfigManager = sinon.stub(); }); diff --git a/test/unit/commands/context.test.ts b/test/unit/commands/context.test.ts index cfc181ce0..75cf67588 100644 --- a/test/unit/commands/context.test.ts +++ b/test/unit/commands/context.test.ts @@ -78,7 +78,7 @@ describe('ContextCommandTasks unit tests', () => { chartManager: sandbox.createStubInstance(ChartManager), configManager, depManager: sandbox.createStubInstance(DependencyManager), - localConfig: new LocalConfig(filePath, loggerStub, configManager), + localConfig: new LocalConfig(filePath), downloader: sandbox.createStubInstance(PackageDownloader), keyManager: sandbox.createStubInstance(KeyManager), accountManager: sandbox.createStubInstance(AccountManager), @@ -142,7 +142,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-2'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-2']); @@ -152,7 +152,7 @@ describe('ContextCommandTasks unit tests', () => { it('should prompt for all flags if none are provided', async () => { const argv = {}; await runUpdateLocalConfigTask(argv); //@ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-3'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-3']); @@ -169,7 +169,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-3'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-2']); @@ -186,7 +186,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-2'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-3']); @@ -203,7 +203,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-2'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-2']); @@ -221,7 +221,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-2'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-3']); @@ -239,7 +239,7 @@ describe('ContextCommandTasks unit tests', () => { }; await runUpdateLocalConfigTask(argv); // @ts-ignore - localConfig = new LocalConfig(filePath, loggerStub, command.configManager); + localConfig = new LocalConfig(filePath); expect(localConfig.currentDeploymentName).to.equal('deployment-2'); expect(localConfig.getCurrentDeployment().clusters).to.deep.equal(['cluster-2']); diff --git a/test/unit/commands/network.test.ts b/test/unit/commands/network.test.ts index ae584a817..f8c4eb736 100644 --- a/test/unit/commands/network.test.ts +++ b/test/unit/commands/network.test.ts @@ -79,7 +79,7 @@ describe('NetworkCommand unit tests', () => { opts.helm = container.resolve(Helm); opts.helm.dependency = sinon.stub(); - opts.configManager = new ConfigManager(testLogger); + opts.configManager = container.resolve(ConfigManager); opts.configManager.update(argv); opts.k8 = sinon.stub(); opts.k8.hasNamespace = sinon.stub().returns(true); @@ -92,25 +92,27 @@ describe('NetworkCommand unit tests', () => { run: sinon.stub().returns({}), }); - opts.keyManager = new KeyManager(testLogger); + opts.keyManager = container.resolve(KeyManager); opts.keyManager.copyGossipKeysToStaging = sinon.stub(); opts.keyManager.copyNodeKeysToStaging = sinon.stub(); opts.platformInstaller = sinon.stub(); opts.platformInstaller.copyNodeKeys = sinon.stub(); - opts.profileManager = new ProfileManager(testLogger, opts.configManager); + opts.profileManager = container.resolve(ProfileManager); opts.profileManager.prepareValuesForSoloChart = sinon.stub(); opts.certificateManager = sinon.stub(); - opts.chartManager = new ChartManager(opts.helm, opts.logger); + opts.chartManager = container.resolve(ChartManager) opts.chartManager.isChartInstalled = sinon.stub().returns(true); opts.chartManager.isChartInstalled.onSecondCall().returns(false); opts.chartManager.install = sinon.stub().returns(true); - opts.remoteConfigManager = new RemoteConfigManager(opts.k8, opts.logger, opts.localConfig, opts.configManager); - opts.configManager = new ConfigManager(opts.logger); - opts.leaseManager = new LeaseManager(opts.k8, opts.configManager, opts.logger, new IntervalLeaseRenewalService()); + opts.remoteConfigManager = container.resolve(RemoteConfigManager); + + opts.configManager = container.resolve(ConfigManager); + + opts.leaseManager = container.resolve(LeaseManager); opts.leaseManager.currentNamespace = sinon.stub().returns(testName); }); diff --git a/test/unit/core/certificate_manager.test.ts b/test/unit/core/certificate_manager.test.ts index 60814eed4..94b1915cb 100644 --- a/test/unit/core/certificate_manager.test.ts +++ b/test/unit/core/certificate_manager.test.ts @@ -24,6 +24,7 @@ import {CertificateManager} from '../../../src/core/certificate_manager.js'; import {Flags as flags} from '../../../src/commands/flags.js'; import {testLogger} from '../../test_util.js'; import {SoloError} from '../../../src/core/errors.js'; +import {container} from "tsyringe-neo"; describe('Certificate Manager', () => { const argv = {}; @@ -35,10 +36,10 @@ describe('Certificate Manager', () => { before(() => { argv[flags.namespace.name] = 'namespace'; - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) configManager.update(argv); - k8 = new K8(configManager, testLogger); - certificateManager = new CertificateManager(k8, testLogger, configManager); + k8 = container.resolve(K8); + certificateManager = container.resolve(CertificateManager) }); after(() => { diff --git a/test/unit/core/config_manager.test.ts b/test/unit/core/config_manager.test.ts index 96aa9512f..abd516054 100644 --- a/test/unit/core/config_manager.test.ts +++ b/test/unit/core/config_manager.test.ts @@ -20,11 +20,12 @@ import {describe, it} from 'mocha'; import {ConfigManager} from '../../../src/core/config_manager.js'; import {Flags as flags} from '../../../src/commands/flags.js'; import {testLogger} from '../../test_util.js'; +import {container} from "tsyringe-neo"; describe('ConfigManager', () => { describe('update values using argv', () => { it('should update string flag value', () => { - const cm = new ConfigManager(testLogger); + const cm = container.resolve(ConfigManager) const argv = {}; argv[flags.releaseTag.name] = 'v0.42.5'; @@ -40,7 +41,7 @@ describe('ConfigManager', () => { }); it('should update number flag value', () => { - const cm = new ConfigManager(testLogger); + const cm = container.resolve(ConfigManager) const argv = {}; argv[flags.replicaCount.name] = 1; @@ -56,7 +57,7 @@ describe('ConfigManager', () => { }); it('should update boolean flag value', () => { - const cm = new ConfigManager(testLogger); + const cm = container.resolve(ConfigManager) // boolean values should work const argv = {}; @@ -87,7 +88,7 @@ describe('ConfigManager', () => { it('should take user input as the first preference', () => { // Given: config has value, argv has a different value // Expected: argv should retain the value - const cm = new ConfigManager(testLogger); + const cm = container.resolve(ConfigManager) cm.setFlag(flags.devMode, false); expect(cm.getFlag(flags.devMode)).not.to.be.ok; @@ -102,7 +103,7 @@ describe('ConfigManager', () => { it('should take default as the last preference', () => { // Given: neither config nor argv has the flag value set // Expected: argv should inherit the default flag value - const cm = new ConfigManager(testLogger); + const cm = container.resolve(ConfigManager) expect(cm.hasFlag(flags.devMode)).not.to.be.ok; // shouldn't have set const argv = {}; // devMode flag is not set in argv and cached config doesn't have it either diff --git a/test/unit/core/k8.test.ts b/test/unit/core/k8.test.ts index bef75fb7f..b967fdd35 100644 --- a/test/unit/core/k8.test.ts +++ b/test/unit/core/k8.test.ts @@ -24,6 +24,7 @@ import {ConfigManager} from '../../../src/core/config_manager.js'; import {testLogger} from '../../test_util.js'; import {Flags as flags} from '../../../src/commands/flags.js'; import {Duration} from '../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; function listNamespacedPodMockSetup(k8: K8, numOfFailures: number, result: any) { for (let i = 0; i < numOfFailures - 1; i++) { @@ -72,9 +73,9 @@ describe('K8 Unit Tests', function () { before(() => { argv[flags.namespace.name] = 'namespace'; - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) configManager.update(argv); - k8 = new K8(configManager, testLogger); + k8 = container.resolve(K8); k8.kubeClient = { // @ts-ignore listNamespacedPod: jest.fn(), diff --git a/test/unit/core/key_manager.test.ts b/test/unit/core/key_manager.test.ts index 9662f43f3..abf1f57d0 100644 --- a/test/unit/core/key_manager.test.ts +++ b/test/unit/core/key_manager.test.ts @@ -25,10 +25,11 @@ import * as constants from '../../../src/core/constants.js'; import * as logging from '../../../src/core/logging.js'; import type {NodeAlias} from '../../../src/types/aliases.js'; import {Duration} from '../../../src/core/time/duration.js'; +import {container} from "tsyringe-neo"; describe('KeyManager', () => { const logger = logging.NewLogger('debug', true); - const keyManager = new KeyManager(logger); + const keyManager = container.resolve(KeyManager); it('should generate signing key', async () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'keys-')); diff --git a/test/unit/core/local_config.test.ts b/test/unit/core/local_config.test.ts index 141748dbc..728fd1d94 100644 --- a/test/unit/core/local_config.test.ts +++ b/test/unit/core/local_config.test.ts @@ -33,7 +33,7 @@ describe('LocalConfig', () => { const expectFailedValidation = expectedMessage => { try { - new LocalConfig(filePath, testLogger, configManager); + new LocalConfig(filePath); expect.fail('Expected an error to be thrown'); } catch (error) { expect(error).to.be.instanceOf(SoloError); @@ -43,7 +43,7 @@ describe('LocalConfig', () => { beforeEach(async () => { await fs.promises.writeFile(filePath, stringify(config)); - localConfig = new LocalConfig(filePath, testLogger, configManager); + localConfig = new LocalConfig(filePath); }); afterEach(async () => { @@ -64,7 +64,7 @@ describe('LocalConfig', () => { await localConfig.write(); // reinitialize with updated config file - const newConfig = new LocalConfig(filePath, testLogger, configManager); + const newConfig = new LocalConfig(filePath); expect(newConfig.userEmailAddress).to.eq(newEmailAddress); }); @@ -91,7 +91,7 @@ describe('LocalConfig', () => { expect(localConfig.deployments).to.deep.eq(newDeployments); await localConfig.write(); - const newConfig = new LocalConfig(filePath, testLogger, configManager); + const newConfig = new LocalConfig(filePath); expect(newConfig.deployments).to.deep.eq(newDeployments); }); @@ -146,7 +146,7 @@ describe('LocalConfig', () => { expect(localConfig.currentDeploymentName).to.eq(newCurrentDeployment); await localConfig.write(); - const newConfig = new LocalConfig(filePath, testLogger, configManager); + const newConfig = new LocalConfig(filePath); expect(newConfig.currentDeploymentName).to.eq(newCurrentDeployment); }); @@ -170,7 +170,7 @@ describe('LocalConfig', () => { it('should throw an error if file path is not set', async () => { try { - new LocalConfig('', testLogger, configManager); + new LocalConfig(''); expect.fail('Expected an error to be thrown'); } catch (error) { expect(error).to.be.instanceOf(MissingArgumentError); diff --git a/test/unit/core/platform_installer.test.ts b/test/unit/core/platform_installer.test.ts index 0506ea20b..235b3a282 100644 --- a/test/unit/core/platform_installer.test.ts +++ b/test/unit/core/platform_installer.test.ts @@ -27,14 +27,15 @@ import * as logging from '../../../src/core/logging.js'; import {IllegalArgumentError, MissingArgumentError} from '../../../src/core/errors.js'; import {getK8Instance} from '../../test_util.js'; import type {PodName} from '../../../src/types/aliases.js'; +import {container} from "tsyringe-neo"; describe('PackageInstaller', () => { const testLogger = logging.NewLogger('debug', true); - const configManager = new ConfigManager(testLogger); + const configManager = container.resolve(ConfigManager) const k8 = getK8Instance(configManager); - const installer = new PlatformInstaller(testLogger, k8, configManager); + const 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 a4b01a417..65d995ece 100644 --- a/test/unit/core/profile_manager.test.ts +++ b/test/unit/core/profile_manager.test.ts @@ -27,10 +27,11 @@ import {ProfileManager} from '../../../src/core/profile_manager.js'; import {getTestCacheDir, getTmpDir, testLogger} from '../../test_util.js'; import * as version from '../../../version.js'; import type {NodeAlias} from '../../../src/types/aliases.js'; +import {container} from "tsyringe-neo"; const tmpDir = getTmpDir(); -const configManager = new ConfigManager(testLogger); -const profileManager = new ProfileManager(testLogger, configManager, tmpDir); +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'));