From 840b54dec1ae66b597176076dac88c2fb433d007 Mon Sep 17 00:00:00 2001 From: instamenta Date: Tue, 24 Sep 2024 14:51:46 +0300 Subject: [PATCH 1/3] fix(tests): start Kind cluster on demand if not having a kubeconfig errors Signed-off-by: instamenta --- test/test_util.js | 25 ++++++++++++++++++++++ test/unit/commands/base.test.mjs | 5 +++-- test/unit/commands/init.test.mjs | 5 +++-- test/unit/core/platform_installer.test.mjs | 7 ++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/test/test_util.js b/test/test_util.js index 9987d3bad..baa221d3a 100644 --- a/test/test_util.js +++ b/test/test_util.js @@ -51,6 +51,8 @@ import { import { ROOT_CONTAINER } from '../src/core/constants.mjs' import crypto from 'crypto' import { AccountCommand } from '../src/commands/account.mjs' +import { FullstackTestingError } from '../src/core/errors.mjs' +import { execSync } from 'child_process' export const testLogger = logging.NewLogger('debug', true) export const TEST_CLUSTER = 'solo-e2e' @@ -360,3 +362,26 @@ async function addKeyHashToMap (k8, nodeId, keyDir, uniqueNodeDestDir, keyHashMa const keyString = keyBytes.toString() keyHashMap.set(privateKeyFileName, crypto.createHash('sha256').update(keyString).digest('base64')) } + +/** + * @param {ConfigManager} configManager + * @returns {K8} + */ +export function getK8Instance (configManager) { + try { + return new K8(configManager, testLogger) + } catch (e) { + if (!(e instanceof FullstackTestingError)) { + throw e + } + + // Set envs + process.env.SOLO_CLUSTER_NAME = 'solo-e2e' + process.env.SOLO_NAMESPACE = 'solo-e2e' + process.env.SOLO_CLUSTER_SETUP_NAMESPACE = 'fullstack-setup' + + // Create cluster + execSync(`kind create cluster --name "${process.env.SOLO_CLUSTER_NAME}"`, { stdio: 'inherit' }) + return new K8(configManager, testLogger) + } +} diff --git a/test/unit/commands/base.test.mjs b/test/unit/commands/base.test.mjs index 943671dd4..7c637f0bc 100644 --- a/test/unit/commands/base.test.mjs +++ b/test/unit/commands/base.test.mjs @@ -24,8 +24,8 @@ import { constants } from '../../../src/core/index.mjs' import { BaseCommand } from '../../../src/commands/base.mjs' -import { K8 } from '../../../src/core/k8.mjs' import * as flags from '../../../src/commands/flags.mjs' +import { getK8Instance } from '../../test_util.js' const testLogger = logging.NewLogger('debug', true) @@ -40,7 +40,8 @@ describe('BaseCommand', () => { const helmDepManager = new HelmDependencyManager(downloader, zippy, testLogger) const depManagerMap = new Map().set(constants.HELM, helmDepManager) const depManager = new DependencyManager(testLogger, depManagerMap) - const k8 = new K8(configManager, testLogger) + + const k8 = getK8Instance(configManager) const baseCmd = new BaseCommand({ logger: testLogger, diff --git a/test/unit/commands/init.test.mjs b/test/unit/commands/init.test.mjs index ec426202a..5521348a1 100644 --- a/test/unit/commands/init.test.mjs +++ b/test/unit/commands/init.test.mjs @@ -28,7 +28,7 @@ import { KeyManager, logging, PackageDownloader, Zippy } from '../../../src/core/index.mjs' -import { K8 } from '../../../src/core/k8.mjs' +import { getK8Instance } from '../../test_util.js' const testLogger = logging.NewLogger('debug', true) describe('InitCommand', () => { @@ -47,7 +47,8 @@ describe('InitCommand', () => { const configManager = new ConfigManager(testLogger) const keyManager = new KeyManager(testLogger) - const k8 = new K8(configManager, testLogger) + + const k8 = getK8Instance(configManager) const initCmd = new InitCommand({ logger: testLogger, diff --git a/test/unit/core/platform_installer.test.mjs b/test/unit/core/platform_installer.test.mjs index b3fc21dd2..962fad133 100644 --- a/test/unit/core/platform_installer.test.mjs +++ b/test/unit/core/platform_installer.test.mjs @@ -16,7 +16,7 @@ */ import { describe, expect, it } from '@jest/globals' import * as core from '../../../src/core/index.mjs' -import { ConfigManager, PlatformInstaller } from '../../../src/core/index.mjs' +import { ConfigManager, K8, PlatformInstaller } from '../../../src/core/index.mjs' import * as fs from 'fs' import * as path from 'path' import * as os from 'os' @@ -25,10 +25,13 @@ import { MissingArgumentError } from '../../../src/core/errors.mjs' import { AccountManager } from '../../../src/core/account_manager.mjs' +import { getK8Instance } from '../../test_util.js' describe('PackageInstaller', () => { const testLogger = core.logging.NewLogger('debug', true) const configManager = new ConfigManager(testLogger) - const k8 = new core.K8(configManager, testLogger) + + const k8 = getK8Instance(configManager) + const accountManager = new AccountManager(testLogger, k8) const installer = new PlatformInstaller(testLogger, k8, configManager, accountManager) From 68ae9b072cd1b2880745f462ed58dadab1968d43 Mon Sep 17 00:00:00 2001 From: instamenta Date: Tue, 24 Sep 2024 14:54:18 +0300 Subject: [PATCH 2/3] remove unused import Signed-off-by: instamenta --- test/unit/core/platform_installer.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/core/platform_installer.test.mjs b/test/unit/core/platform_installer.test.mjs index 962fad133..9943a40ac 100644 --- a/test/unit/core/platform_installer.test.mjs +++ b/test/unit/core/platform_installer.test.mjs @@ -16,7 +16,7 @@ */ import { describe, expect, it } from '@jest/globals' import * as core from '../../../src/core/index.mjs' -import { ConfigManager, K8, PlatformInstaller } from '../../../src/core/index.mjs' +import { ConfigManager, PlatformInstaller } from '../../../src/core/index.mjs' import * as fs from 'fs' import * as path from 'path' import * as os from 'os' From c61b7bbfc92c4b4f84e7c657db182c99fe86fce8 Mon Sep 17 00:00:00 2001 From: Jeromy Cannon Date: Mon, 30 Sep 2024 10:48:45 -0500 Subject: [PATCH 3/3] Update test/test_util.js Signed-off-by: Jeromy Cannon --- test/test_util.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_util.js b/test/test_util.js index baa221d3a..71e55f78f 100644 --- a/test/test_util.js +++ b/test/test_util.js @@ -370,6 +370,7 @@ async function addKeyHashToMap (k8, nodeId, keyDir, uniqueNodeDestDir, keyHashMa export function getK8Instance (configManager) { try { return new K8(configManager, testLogger) + // 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 FullstackTestingError)) { throw e