Skip to content

Commit

Permalink
fix: e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
  • Loading branch information
leninmehedy committed Jan 23, 2024
1 parent 1a5981b commit 2b3f9a3
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 197 deletions.
2 changes: 1 addition & 1 deletion fullstack-network-manager/src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class ClusterCommand extends BaseCommand {
desc: 'Setup cluster with shared components',
builder: y => flags.setCommandFlags(y,
flags.clusterName,
flags.namespace,
flags.withDefaultValue(flags.namespace, constants.DEFAULT_NAMESPACE),
flags.chartDirectory,
flags.deployPrometheusStack,
flags.deployMinio,
Expand Down
6 changes: 6 additions & 0 deletions fullstack-network-manager/src/commands/flags.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export function setCommandFlags (y, ...commandFlags) {
})
}

export function withDefaultValue (f, value) {
const clone = JSON.parse(JSON.stringify(f))
clone.definition.default = value
return clone
}

export const devMode = {
name: 'dev',
definition: {
Expand Down
29 changes: 27 additions & 2 deletions fullstack-network-manager/src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class NodeCommand extends BaseCommand {
}
},
{
title: 'Fetch platform',
title: 'Fetch platform software',
task: async (ctx, _) => {
const config = ctx.config

Expand All @@ -163,6 +163,31 @@ export class NodeCommand extends BaseCommand {
return self.plaformInstaller.taskPrepareStaging(config.nodeIds, config.stagingDir, config.releaseTag, config.force, config.chainId)
}
},
{
title: 'Extract platform software into network node',
task:
async (ctx, task) => {
const config = ctx.config

const subTasks = []
for (const nodeId of ctx.config.nodeIds) {
const podName = ctx.config.podNames[nodeId]
subTasks.push({
title: `Node: ${chalk.yellow(nodeId)}`,
task: () =>
self.plaformInstaller.copyPlatform(podName, config.buildZipFile)
})
}

// set up the sub-tasks
return task.newListr(subTasks, {
concurrent: false,
rendererOptions: {
collapseSubtasks: false
}
})
}
},
{
title: 'Setup network nodes',
task:
Expand All @@ -173,7 +198,7 @@ export class NodeCommand extends BaseCommand {
for (const nodeId of ctx.config.nodeIds) {
const podName = ctx.config.podNames[nodeId]
subTasks.push({
title: `Setup node: ${chalk.yellow(nodeId)}`,
title: `Node: ${chalk.yellow(nodeId)}`,
task: () =>
self.plaformInstaller.taskInstall(podName, config.buildZipFile, config.stagingDir, config.force)
})
Expand Down
2 changes: 2 additions & 0 deletions fullstack-network-manager/src/core/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as logging from './logging.mjs'
import * as constants from './constants.mjs'
import { Helm } from './helm.mjs'
import { Kubectl } from './kubectl.mjs'
import { Kubectl2 } from './kubectl2.mjs'
import { PackageDownloader } from './package_downloader.mjs'
import { PlatformInstaller } from './platform_installer.mjs'
import { Zippy } from './zippy.mjs'
Expand All @@ -16,6 +17,7 @@ export {
constants,
Helm,
Kubectl,
Kubectl2,
PackageDownloader,
PlatformInstaller,
Zippy,
Expand Down
4 changes: 2 additions & 2 deletions fullstack-network-manager/src/core/kubectl2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class Kubectl2 {
* @param delay delay between attempts to check if file is copied successfully or not
* @returns {Promise<>}
*/
async copyTo (podName, containerName, srcPath, destDir, maxAttempts = 100, delay = 250) {
async copyTo (podName, containerName, srcPath, destDir, maxAttempts = 100, delay = 500) {
const ns = this._getNamespace()

try {
Expand Down Expand Up @@ -401,7 +401,7 @@ export class Kubectl2 {
* @param delay delay between attempts to check if file is copied successfully or not
* @returns {Promise<boolean>}
*/
async copyFrom (podName, containerName, srcPath, destDir, maxAttempts = 100, delay = 250) {
async copyFrom (podName, containerName, srcPath, destDir, maxAttempts = 100, delay = 500) {
const ns = this._getNamespace()

try {
Expand Down
12 changes: 1 addition & 11 deletions fullstack-network-manager/src/core/platform_installer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class PlatformInstaller {

try {
await this.copyFiles(podName, [buildZipSrc], constants.HEDERA_USER_HOME_DIR)
return true
return this.extractPlatform(podName, buildZipSrc)
} catch (e) {
throw new FullstackTestingError(`failed to copy platform code in to pod '${podName}': ${e.message}`, e)
}
Expand Down Expand Up @@ -361,16 +361,6 @@ export class PlatformInstaller {
taskInstall (podName, buildZipFile, stagingDir, force = false) {
const self = this
return new Listr([
{
title: 'Copy platform zip file',
task: (_, task) =>
self.copyPlatform(podName, buildZipFile)
},
{
title: 'Extract platform zip file',
task: (_, task) =>
self.extractPlatform(podName, buildZipFile)
},
{
title: 'Copy Gossip keys',
task: (_, task) =>
Expand Down
64 changes: 32 additions & 32 deletions fullstack-network-manager/test/e2e/commands/node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,54 @@ import {
PrivateKey,
Wallet
} from '@hashgraph/sdk'
import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'
import { afterEach, beforeAll, describe, expect, it } from '@jest/globals'
import net from 'net'
import { namespace } from '../../../src/commands/flags.mjs'
import { flags } from '../../../src/commands/index.mjs'
import { NodeCommand } from '../../../src/commands/node.mjs'
import { FullstackTestingError, MissingArgumentError } from '../../../src/core/errors.mjs'
import { sleep } from '../../../src/core/helpers.mjs'
import {
ChartManager,
ConfigManager,
Helm,
Kind,
Kubectl,
Kubectl2,
PackageDownloader,
PlatformInstaller,
constants,
DependencyManager,
Templates,
ClusterManager
Templates
} from '../../../src/core/index.mjs'
import { TEST_CACHE_DIR, testLogger } from '../../test_util.js'

class TestHelper {
static killKubectlPortForwardProcesses = async function (nodeCmd) {
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) throw new MissingArgumentError('An instance of command/NodeCommand is required')
static portForwards = []
static stopPortForwards () {
TestHelper.portForwards.forEach(server => {
server.close()
})

// kill any previous port-forwarding commands if possible
const processIds = await nodeCmd.run('ps aux | grep "kubectl port-forward" | awk \'{print $2}\'')
for (const pid of processIds) {
try {
await nodeCmd.run(`kill -9 ${pid}`)
} catch (e) {
// best effort kill, so ignore errors
}
}
TestHelper.portForwards = []
}

static prepareNodeClient = async function (nodeCmd, nodeIds) {
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) throw new MissingArgumentError('An instance of command/NodeCommand is required')

try {
if (typeof nodeIds === 'string') {
nodeIds = nodeIds.split(',')
}

await TestHelper.killKubectlPortForwardProcesses(nodeCmd, nodeIds)

let localPort = 30212
const grpcPort = constants.HEDERA_NODE_GRPC_PORT.toString()
const network = {}

let accountIdNum = parseInt(constants.HEDERA_NODE_ACCOUNT_ID_START.num.toString(), 10)
for (let nodeId of nodeIds) {
nodeId = nodeId.trim()
const nodeSvc = Templates.renderNodeSvcName(nodeId)
await nodeCmd.kubectl.portForward(`svc/${nodeSvc}`, localPort, grpcPort)
const podName = Templates.renderNetworkPodName(nodeId)
const server = await nodeCmd.kubectl2.portForward(podName, localPort, grpcPort)
TestHelper.portForwards.push(server)

// check if the port is actually accessible
let attempt = 1
Expand All @@ -80,7 +74,8 @@ class TestHelper {
if (!socket) {
throw new FullstackTestingError(`failed to expose port '${grpcPort}' of node '${nodeId}'`)
}
socket.end()

socket.destroy()

network[`127.0.0.1:${localPort}`] = `${constants.HEDERA_NODE_ACCOUNT_ID_START.realm}.${constants.HEDERA_NODE_ACCOUNT_ID_START.shard}.${accountIdNum}`

Expand All @@ -96,40 +91,45 @@ class TestHelper {
}

describe('NodeCommand', () => {
const kind = new Kind(testLogger)
const helm = new Helm(testLogger)
const kubectl = new Kubectl(testLogger)
const chartManager = new ChartManager(helm, testLogger)
const configManager = new ConfigManager(testLogger)
const packageDownloader = new PackageDownloader(testLogger)
const platformInstaller = new PlatformInstaller(testLogger, kubectl)
const depManager = new DependencyManager(testLogger)
const clusterManager = new ClusterManager(kind, kubectl)
const kubectl2 = new Kubectl2(configManager, testLogger)
const platformInstaller = new PlatformInstaller(testLogger, kubectl2)

const nodeCmd = new NodeCommand({
logger: testLogger,
kind,
helm,
kubectl,
kubectl2,
chartManager,
configManager,
downloader: packageDownloader,
platformInstaller,
depManager,
clusterManager
depManager
})

const argv = {
releaseTag: 'v0.42.5',
namespace: constants.NAMESPACE_NAME,
nodeIds: 'node0,node1,node2',
cacheDir: TEST_CACHE_DIR,
force: false,
chainId: constants.HEDERA_CHAIN_ID
}

beforeAll(() => TestHelper.killKubectlPortForwardProcesses(nodeCmd))
afterAll(() => TestHelper.killKubectlPortForwardProcesses(nodeCmd))
beforeAll(async () => {
// load cached namespace
await configManager.load()
argv[namespace] = configManager.getFlag(flags.namespace)
}
)

afterEach(() => {
TestHelper.stopPortForwards()
})

describe('start', () => {
it('node setup should succeed', async () => {
Expand All @@ -140,7 +140,7 @@ describe('NodeCommand', () => {
nodeCmd.logger.showUserError(e)
expect(e).toBeNull()
}
}, 20000)
}, 60000)

it('node start should succeed', async () => {
expect.assertions(1)
Expand All @@ -165,7 +165,7 @@ describe('NodeCommand', () => {

await nodeCmd.run(`tail ${constants.FST_LOGS_DIR}/fst.log`)
}
}, 50000)
}, 60000)

it('balance query should succeed', async () => {
expect.assertions(1)
Expand Down
10 changes: 2 additions & 8 deletions fullstack-network-manager/test/unit/commands/base.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ import {
ConfigManager,
Helm,
Kubectl,
logging,
ClusterManager
logging
} from '../../../src/core/index.mjs'
import { BaseCommand } from '../../../src/commands/base.mjs'
import { Kind } from '../../../src/core/kind.mjs'
import { Kubectl2 } from '../../../src/core/kubectl2.mjs'

const testLogger = logging.NewLogger('debug')

describe('BaseCommand', () => {
const kind = new Kind(testLogger)
const helm = new Helm(testLogger)
const kubectl = new Kubectl(testLogger)
const chartManager = new ChartManager(helm, testLogger)
const configManager = new ConfigManager(testLogger)
const depManager = new DependencyManager(testLogger)
const clusterManager = new ClusterManager(testLogger, kind)
const kubectl2 = new Kubectl2(configManager, testLogger)

const baseCmd = new BaseCommand({
logger: testLogger,
kind,
helm,
kubectl,
kubectl2,
chartManager,
configManager,
depManager,
clusterManager
depManager
})

describe('runShell', () => {
Expand Down
8 changes: 1 addition & 7 deletions fullstack-network-manager/test/unit/commands/init.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@ import { expect, describe, it } from '@jest/globals'
import * as core from '../../../src/core/index.mjs'
import {
ChartManager,
ClusterManager,
ConfigManager,
DependencyManager,
Helm,
Kind,
Kubectl
} from '../../../src/core/index.mjs'
import { Kubectl2 } from '../../../src/core/kubectl2.mjs'

const testLogger = core.logging.NewLogger('debug')
describe('InitCommand', () => {
const kind = new Kind(testLogger)
const helm = new Helm(testLogger)
const kubectl = new Kubectl(testLogger)
const chartManager = new ChartManager(helm, testLogger)
const configManager = new ConfigManager(testLogger)
const depManager = new DependencyManager(testLogger)
const clusterManager = new ClusterManager(testLogger, kind)
const kubectl2 = new Kubectl2(configManager, testLogger)

const initCmd = new InitCommand({
logger: testLogger,
kind,
helm,
kubectl,
kubectl2,
chartManager,
configManager,
depManager,
clusterManager
depManager
})

describe('commands', () => {
Expand Down
Loading

0 comments on commit 2b3f9a3

Please sign in to comment.