Skip to content

Commit

Permalink
fix(tests): start Kind cluster on demand if not having a kube config …
Browse files Browse the repository at this point in the history
…errors (#624)

Signed-off-by: instamenta <instamenta@abv.bg>
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
Co-authored-by: Jeromy Cannon <jeromy@swirldslabs.com>
  • Loading branch information
instamenta and jeromy-cannon committed Sep 30, 2024
1 parent 16d40ff commit 538e40d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
26 changes: 26 additions & 0 deletions test/test_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -362,3 +364,27 @@ 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)
// 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
}

// 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)
}
}
5 changes: 3 additions & 2 deletions test/unit/commands/base.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions test/unit/commands/init.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion test/unit/core/platform_installer.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 538e40d

Please sign in to comment.