Skip to content

Commit 5a05b87

Browse files
committed
fix: injections in tests
Signed-off-by: Ivo Yankov <ivo@devlabs.bg>
1 parent 6178ccf commit 5a05b87

File tree

9 files changed

+75
-63
lines changed

9 files changed

+75
-63
lines changed

src/core/config_manager.ts

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export class ConfigManager {
3535
config!: Record<string, any>;
3636

3737
constructor(private readonly logger?: SoloLogger) {
38-
const logger2 = container.resolve(SoloLogger);
3938
this.reset();
4039
}
4140

src/core/dependency_managers/helm_dependency_manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as version from '../../../version.js';
2828
import {ShellRunner} from '../shell_runner.js';
2929
import * as semver from 'semver';
3030
import {OS_WIN32, OS_WINDOWS} from '../constants.js';
31-
import {container, singleton} from 'tsyringe-neo';
31+
import {container, injectable, singleton} from 'tsyringe-neo';
3232

3333
// constants required by HelmDependencyManager
3434
const HELM_RELEASE_BASE_URL = 'https://get.helm.sh';
@@ -42,7 +42,7 @@ const HELM_ARTIFACT_EXT: Map<string, string> = new Map()
4242
/**
4343
* Helm dependency manager installs or uninstalls helm client at SOLO_HOME_DIR/bin directory
4444
*/
45-
@singleton()
45+
@injectable()
4646
export class HelmDependencyManager extends ShellRunner {
4747
private readonly osPlatform: string;
4848
private readonly osArch: string;

src/core/helm.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import os from 'os';
1818
import * as constants from './constants.js';
1919
import {ShellRunner} from './shell_runner.js';
2020
import {Templates} from './templates.js';
21-
import {singleton} from 'tsyringe-neo';
21+
import {injectable} from 'tsyringe-neo';
2222

23-
@singleton()
23+
@injectable()
2424
export class Helm extends ShellRunner {
2525
private readonly helmPath: string;
2626

src/core/logging.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as util from 'util';
2020
import chalk from 'chalk';
2121
import path from 'path';
2222
import * as constants from './constants.js';
23-
import {singleton} from 'tsyringe-neo';
23+
import {autoInjectable, singleton} from 'tsyringe-neo';
2424

2525
const customFormat = winston.format.combine(
2626
winston.format.label({label: 'SOLO', message: false}),
@@ -48,7 +48,7 @@ const customFormat = winston.format.combine(
4848
winston.format(data => (data.private ? false : data))(),
4949
);
5050

51-
@singleton()
51+
@autoInjectable()
5252
export class SoloLogger {
5353
private winstonLogger: winston.Logger;
5454
private traceId?: string;

src/core/profile_manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {SoloLogger} from './logging.js';
3434
import type {AnyObject, DirPath, NodeAlias, NodeAliases, Path} from '../types/aliases.js';
3535
import type {GenesisNetworkDataConstructor} from './genesis_network_models/genesis_network_data_constructor.js';
3636
import type {Optional} from '../types/index.js';
37-
import {container, singleton} from 'tsyringe-neo';
37+
import {container, injectable} from 'tsyringe-neo';
3838

3939
const consensusSidecars = [
4040
'recordStreamUploader',
@@ -44,7 +44,7 @@ const consensusSidecars = [
4444
'otelCollector',
4545
];
4646

47-
@singleton()
47+
@injectable()
4848
export class ProfileManager {
4949
private readonly logger: SoloLogger;
5050
private readonly configManager: ConfigManager;

test/test_container.ts

+27-28
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,30 @@ import * as constants from '../src/core/constants.js';
3535
import {LocalConfig} from '../src/core/config/local_config.js';
3636
import {RemoteConfigManager} from '../src/core/config/remote/remote_config_manager.js';
3737

38-
const cacheDir = path.join('test', 'data', 'tmp');
39-
// if (!cacheDir) cacheDir = path.join('test', 'data', 'tmp');
40-
// container.reset();
41-
container.register<SoloLogger>(SoloLogger, {useValue: new SoloLogger('debug', true)});
42-
container.register<PackageDownloader>(PackageDownloader, {useValue: new PackageDownloader()});
43-
container.register<Zippy>(Zippy, {useValue: new Zippy()});
44-
container.register<HelmDependencyManager>(HelmDependencyManager, {useValue: new HelmDependencyManager()});
45-
container.register<DependencyManager>(DependencyManager, {useValue: new DependencyManager()});
46-
container.register<Helm>(Helm, {useValue: new Helm()});
47-
container.register<ChartManager>(ChartManager, {useValue: new ChartManager()});
48-
container.register<ConfigManager>(ConfigManager, {useValue: new ConfigManager()});
49-
container.register<K8>(K8, {useValue: new K8()});
50-
container.register<AccountManager>(AccountManager, {useValue: new AccountManager()});
51-
container.register<PlatformInstaller>(PlatformInstaller, {useValue: new PlatformInstaller()});
52-
container.register<KeyManager>(KeyManager, {useValue: new KeyManager()});
53-
container.register<ProfileManager>(ProfileManager, {useValue: new ProfileManager()});
54-
container.register<IntervalLeaseRenewalService>(IntervalLeaseRenewalService, {
55-
useValue: new IntervalLeaseRenewalService(),
56-
});
57-
container.register<LeaseManager>(LeaseManager, {
58-
useValue: new LeaseManager(container.resolve(IntervalLeaseRenewalService)),
59-
});
60-
container.register<CertificateManager>(CertificateManager, {useValue: new CertificateManager()});
61-
const localConfigPath = path.join(cacheDir, constants.DEFAULT_LOCAL_CONFIG_FILE);
62-
container.register<LocalConfig>(LocalConfig, {useValue: new LocalConfig(localConfigPath)});
63-
container.register<RemoteConfigManager>(RemoteConfigManager, {useValue: new RemoteConfigManager()});
64-
65-
export function resetTestContainer(cacheDir?: string) {}
38+
export function resetTestContainer(cacheDir?: string) {
39+
if (!cacheDir) cacheDir = path.join('test', 'data', 'tmp');
40+
container.reset();
41+
container.register<SoloLogger>(SoloLogger, {useValue: new SoloLogger('debug', true)});
42+
container.register<PackageDownloader>(PackageDownloader, {useValue: new PackageDownloader()});
43+
container.register<Zippy>(Zippy, {useValue: new Zippy()});
44+
container.register<HelmDependencyManager>(HelmDependencyManager, {useValue: new HelmDependencyManager()});
45+
container.register<DependencyManager>(DependencyManager, {useValue: new DependencyManager()});
46+
container.register<Helm>(Helm, {useValue: new Helm()});
47+
container.register<ChartManager>(ChartManager, {useValue: new ChartManager()});
48+
container.register<ConfigManager>(ConfigManager, {useValue: new ConfigManager()});
49+
container.register<K8>(K8, {useValue: new K8()});
50+
container.register<AccountManager>(AccountManager, {useValue: new AccountManager()});
51+
container.register<PlatformInstaller>(PlatformInstaller, {useValue: new PlatformInstaller()});
52+
container.register<KeyManager>(KeyManager, {useValue: new KeyManager()});
53+
container.register<ProfileManager>(ProfileManager, {useValue: new ProfileManager()});
54+
container.register<IntervalLeaseRenewalService>(IntervalLeaseRenewalService, {
55+
useValue: new IntervalLeaseRenewalService(),
56+
});
57+
container.register<LeaseManager>(LeaseManager, {
58+
useValue: new LeaseManager(container.resolve(IntervalLeaseRenewalService)),
59+
});
60+
container.register<CertificateManager>(CertificateManager, {useValue: new CertificateManager()});
61+
const localConfigPath = path.join(cacheDir, constants.DEFAULT_LOCAL_CONFIG_FILE);
62+
container.register<LocalConfig>(LocalConfig, {useValue: new LocalConfig(localConfigPath)});
63+
container.register<RemoteConfigManager>(RemoteConfigManager, {useValue: new RemoteConfigManager()});
64+
}

test/test_util.ts

+32-22
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import {RemoteConfigManager} from '../src/core/config/remote/remote_config_manag
5151
import * as constants from '../src/core/constants.js';
5252
import {Templates} from '../src/core/templates.js';
5353
import {ConfigManager} from '../src/core/config_manager.js';
54-
import * as logging from '../src/core/logging.js';
5554
import {Helm} from '../src/core/helm.js';
5655
import {ChartManager} from '../src/core/chart_manager.js';
5756
import {PackageDownloader} from '../src/core/package_downloader.js';
@@ -63,12 +62,14 @@ import {Duration} from '../src/core/time/duration.js';
6362
import {container} from 'tsyringe-neo';
6463
import {resetTestContainer} from './test_container.js';
6564

66-
export const testLogger = logging.NewLogger('debug', true);
6765
export const TEST_CLUSTER = 'solo-e2e';
6866
export const HEDERA_PLATFORM_VERSION_TAG = HEDERA_PLATFORM_VERSION;
6967

7068
export const BASE_TEST_DIR = path.join('test', 'data', 'tmp');
7169

70+
container.register<SoloLogger>(SoloLogger, {useValue: new SoloLogger('debug', true)});
71+
export let testLogger = container.resolve(SoloLogger);
72+
7273
export function getTestCacheDir(testName?: string) {
7374
const d = testName ? path.join(BASE_TEST_DIR, testName) : BASE_TEST_DIR;
7475

@@ -158,6 +159,7 @@ export function bootstrapTestVariables(
158159
const certificateManager = container.resolve(CertificateManager);
159160
const localConfig = new LocalConfig(path.join(BASE_TEST_DIR, 'local-config.yaml'));
160161
const remoteConfigManager = container.resolve(RemoteConfigManager);
162+
testLogger = container.resolve(SoloLogger);
161163

162164
const opts: TestOpts = {
163165
logger: testLogger,
@@ -212,26 +214,34 @@ export function e2eTestSuite(
212214
startNodes = true,
213215
testsCallBack: (bootstrapResp: BootstrapResponse) => void = () => {},
214216
) {
215-
const bootstrapResp = bootstrapTestVariables(
216-
testName,
217-
argv,
218-
k8Arg,
219-
initCmdArg,
220-
clusterCmdArg,
221-
networkCmdArg,
222-
nodeCmdArg,
223-
accountCmdArg,
224-
);
225-
const namespace = bootstrapResp.namespace;
226-
const initCmd = bootstrapResp.cmd.initCmd;
227-
const k8 = bootstrapResp.opts.k8;
228-
const clusterCmd = bootstrapResp.cmd.clusterCmd;
229-
const networkCmd = bootstrapResp.cmd.networkCmd;
230-
const nodeCmd = bootstrapResp.cmd.nodeCmd;
231-
const chartManager = bootstrapResp.opts.chartManager;
232-
233-
describe(`E2E Test Suite for '${testName}'`, function () {
234-
this.bail(true); // stop on first failure, nothing else will matter if network doesn't come up correctly
217+
218+
describe(`E2E Test Suite for '${testName}'`, () => {
219+
let bootstrapResp: BootstrapResponse
220+
let namespace, initCmd, k8, clusterCmd, networkCmd, nodeCmd, chartManager, testLogger;
221+
222+
223+
before(() => {
224+
bootstrapResp = bootstrapTestVariables(
225+
testName,
226+
argv,
227+
k8Arg,
228+
initCmdArg,
229+
clusterCmdArg,
230+
networkCmdArg,
231+
nodeCmdArg,
232+
accountCmdArg,
233+
);
234+
namespace = bootstrapResp.namespace;
235+
initCmd = bootstrapResp.cmd.initCmd;
236+
k8 = bootstrapResp.opts.k8;
237+
clusterCmd = bootstrapResp.cmd.clusterCmd;
238+
networkCmd = bootstrapResp.cmd.networkCmd;
239+
nodeCmd = bootstrapResp.cmd.nodeCmd;
240+
chartManager = bootstrapResp.opts.chartManager;
241+
testLogger = bootstrapResp.opts.logger;
242+
243+
this.bail(true); // stop on first failure, nothing else will matter if network doesn't come up correctly
244+
});
235245

236246
describe(`Bootstrap network for test [release ${argv[flags.releaseTag.name]}}]`, () => {
237247
before(() => {

test/unit/commands/base.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {Flags as flags} from '../../../src/commands/flags.js';
2828
import sinon from 'sinon';
2929
import {container} from 'tsyringe-neo';
3030
import {SoloLogger} from '../../../src/core/logging.js';
31+
import {resetTestContainer} from "../../test_container.js";
3132

3233
describe('BaseCommand', () => {
3334
let helm: Helm;
@@ -43,7 +44,7 @@ describe('BaseCommand', () => {
4344

4445
describe('runShell', () => {
4546
before(() => {
46-
// resetTestContainer();
47+
resetTestContainer();
4748
testLogger = container.resolve(SoloLogger);
4849
helm = container.resolve(Helm);
4950
chartManager = container.resolve(ChartManager);

test/unit/core/dependency_managers/dependency_manager.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import {describe, it} from 'mocha';
2020
import {DependencyManager} from '../../../../src/core/dependency_managers/index.js';
2121
import * as logging from '../../../../src/core/logging.js';
2222
import {container} from 'tsyringe-neo';
23+
import {SoloLogger} from "../../../../src/core/logging.js";
2324

24-
const testLogger = logging.NewLogger('debug', true);
2525
describe('DependencyManager', () => {
26-
// prepare dependency manger registry
26+
let depManager, testLogger;
2727

28-
const depManager = container.resolve(DependencyManager);
28+
before(() => {
29+
depManager = container.resolve(DependencyManager);
30+
testLogger = container.resolve(SoloLogger);
31+
});
2932

3033
describe('checkDependency', () => {
3134
it('should fail during invalid dependency check', async () => {

0 commit comments

Comments
 (0)