Skip to content

Commit 48c5b77

Browse files
test: add cluster command end to end test (#179)
Signed-off-by: Jeffrey Tang <[email protected]>
1 parent 542d5ad commit 48c5b77

File tree

6 files changed

+205
-8
lines changed

6 files changed

+205
-8
lines changed

src/commands/cluster.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ export class ClusterCommand extends BaseCommand {
185185
}
186186

187187
ctx.isChartInstalled = await this.chartManager.isChartInstalled(ctx.config.clusterSetupNamespace, constants.FULLSTACK_CLUSTER_SETUP_CHART)
188+
if (!ctx.isChartInstalled) {
189+
throw new FullstackTestingError('No chart found for the cluster')
190+
}
188191
}
189192
},
190193
{

test/e2e/commands/cluster.test.mjs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the ""License"");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an ""AS IS"" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {
18+
afterAll,
19+
afterEach,
20+
beforeEach,
21+
describe,
22+
expect,
23+
it
24+
} from '@jest/globals'
25+
import {
26+
bootstrapTestVariables,
27+
getDefaultArgv,
28+
TEST_CLUSTER
29+
} from '../../test_util.js'
30+
import {
31+
constants
32+
} from '../../../src/core/index.mjs'
33+
import { flags } from '../../../src/commands/index.mjs'
34+
import { sleep } from '../../../src/core/helpers.mjs'
35+
import * as version from '../../../version.mjs'
36+
37+
describe('ClusterCommand', () => {
38+
const testName = 'cluster-cmd-e2e'
39+
const namespace = testName
40+
const argv = getDefaultArgv()
41+
argv[flags.namespace.name] = namespace
42+
argv[flags.releaseTag.name] = 'v0.47.0-alpha.0'
43+
argv[flags.keyFormat.name] = constants.KEY_FORMAT_PEM
44+
argv[flags.nodeIDs.name] = 'node0,node1,node2'
45+
argv[flags.generateGossipKeys.name] = true
46+
argv[flags.generateTlsKeys.name] = true
47+
argv[flags.clusterName.name] = TEST_CLUSTER
48+
argv[flags.fstChartVersion.name] = version.FST_CHART_VERSION
49+
argv[flags.force.name] = true
50+
51+
const bootstrapResp = bootstrapTestVariables(testName, argv)
52+
const k8 = bootstrapResp.opts.k8
53+
const accountManager = bootstrapResp.opts.accountManager
54+
const configManager = bootstrapResp.opts.configManager
55+
const chartManager = bootstrapResp.opts.chartManager
56+
57+
const clusterCmd = bootstrapResp.cmd.clusterCmd
58+
59+
afterAll(async () => {
60+
await k8.deleteNamespace(namespace)
61+
await accountManager.close()
62+
})
63+
64+
beforeEach(() => {
65+
configManager.reset()
66+
})
67+
68+
afterEach(async () => {
69+
await sleep(5) // give a few ticks so that connections can close
70+
})
71+
72+
it('should cleanup existing deployment', async () => {
73+
if (await chartManager.isChartInstalled(constants.FULLSTACK_SETUP_NAMESPACE, constants.FULLSTACK_CLUSTER_SETUP_CHART)) {
74+
expect.assertions(1)
75+
try {
76+
await expect(clusterCmd.reset(argv)).resolves.toBeTruthy()
77+
} catch (e) {
78+
clusterCmd.logger.showUserError(e)
79+
expect(e).toBeNull()
80+
}
81+
}
82+
}, 60000)
83+
84+
it('solo cluster setup should fail with invalid cluster name', async () => {
85+
argv[flags.clusterSetupNamespace.name] = 'INVALID'
86+
configManager.update(argv, true)
87+
88+
expect.assertions(1)
89+
try {
90+
await expect(clusterCmd.setup(argv)).rejects.toThrowError('Error on cluster setup')
91+
} catch (e) {
92+
clusterCmd.logger.showUserError(e)
93+
expect(e).toBeNull()
94+
}
95+
}, 60000)
96+
97+
it('solo cluster setup should work with valid args', async () => {
98+
argv[flags.clusterSetupNamespace.name] = namespace
99+
configManager.update(argv, true)
100+
101+
expect.assertions(1)
102+
try {
103+
await expect(clusterCmd.setup(argv)).resolves.toBeTruthy()
104+
} catch (e) {
105+
clusterCmd.logger.showUserError(e)
106+
expect(e).toBeNull()
107+
}
108+
}, 60000)
109+
110+
it('function getClusterInfo should return true', async () => {
111+
try {
112+
await expect(clusterCmd.getClusterInfo()).resolves.toBeTruthy()
113+
} catch (e) {
114+
clusterCmd.logger.showUserError(e)
115+
expect(e).toBeNull()
116+
}
117+
}, 60000)
118+
119+
it('function showClusterList should return right true', async () => {
120+
try {
121+
await expect(clusterCmd.showClusterList()).resolves.toBeTruthy()
122+
} catch (e) {
123+
clusterCmd.logger.showUserError(e)
124+
expect(e).toBeNull()
125+
}
126+
}, 60000)
127+
128+
it('function showInstalledChartList should return right true', async () => {
129+
try {
130+
await expect(clusterCmd.showInstalledChartList()).resolves.toBeUndefined()
131+
} catch (e) {
132+
clusterCmd.logger.showUserError(e)
133+
expect(e).toBeNull()
134+
}
135+
}, 60000)
136+
137+
// helm list would return an empty list if given invalid namespace
138+
it('solo cluster reset should fail with invalid cluster name', async () => {
139+
argv[flags.clusterSetupNamespace.name] = 'INVALID'
140+
configManager.update(argv, true)
141+
142+
expect.assertions(1)
143+
try {
144+
await expect(clusterCmd.reset(argv)).rejects.toThrowError('Error on cluster reset')
145+
} catch (e) {
146+
clusterCmd.logger.showUserError(e)
147+
expect(e).toBeNull()
148+
}
149+
}, 60000)
150+
151+
it('solo cluster reset should work with valid args', async () => {
152+
argv[flags.clusterSetupNamespace.name] = namespace
153+
configManager.update(argv, true)
154+
155+
expect.assertions(1)
156+
try {
157+
await expect(clusterCmd.reset(argv)).resolves.toBeTruthy()
158+
} catch (e) {
159+
clusterCmd.logger.showUserError(e)
160+
expect(e).toBeNull()
161+
}
162+
}, 60000)
163+
})
File renamed without changes.

test/e2e/core/chart_manager.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ describe('ChartManager', () => {
3636
})
3737

3838
it('should be able to check if a chart is installed', async () => {
39-
const ns = configManager.getFlag(flags.clusterSetupNamespace)
39+
const ns = configManager.getFlag(flags.namespace)
4040
expect(ns).not.toBeNull()
41-
const isInstalled = await chartManager.isChartInstalled(ns, constants.FULLSTACK_CLUSTER_SETUP_CHART)
41+
const isInstalled = await chartManager.isChartInstalled(ns, constants.FULLSTACK_DEPLOYMENT_CHART)
4242
expect(isInstalled).toBeTruthy()
4343
})
4444
})

test/test_util.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import { sleep } from '../src/core/helpers.mjs'
3333
import {
3434
ChartManager,
3535
ConfigManager,
36-
constants, Helm, K8,
36+
constants,
37+
Helm,
38+
K8,
3739
KeyManager,
3840
logging,
3941
PackageDownloader,
@@ -79,7 +81,7 @@ export function getDefaultArgv () {
7981
}
8082

8183
/**
82-
* Bootstrap network in a given namespace
84+
* Initialize common test variables
8385
*
8486
* @param testName test name
8587
* @param argv argv for commands
@@ -89,14 +91,14 @@ export function getDefaultArgv () {
8991
* @param networkCmdArg an instance of command/NetworkCommand
9092
* @param nodeCmdArg an instance of command/NodeCommand
9193
*/
92-
export function bootstrapNetwork (testName, argv,
94+
export function bootstrapTestVariables (testName, argv,
9395
k8Arg = null,
9496
initCmdArg = null,
9597
clusterCmdArg = null,
9698
networkCmdArg = null,
9799
nodeCmdArg = null
98100
) {
99-
const namespace = argv[flags.namespace.name] || 'bootstrap'
101+
const namespace = argv[flags.namespace.name] || 'bootstrap-ns'
100102
const cacheDir = argv[flags.cacheDir.name] || getTestCacheDir(testName)
101103
const configManager = getTestConfigManager(`${testName}-solo.config`)
102104
configManager.update(argv, true)
@@ -132,7 +134,8 @@ export function bootstrapNetwork (testName, argv,
132134
const clusterCmd = clusterCmdArg || new ClusterCommand(opts)
133135
const networkCmd = networkCmdArg || new NetworkCommand(opts)
134136
const nodeCmd = nodeCmdArg || new NodeCommand(opts)
135-
const bootstrapResp = {
137+
return {
138+
namespace,
136139
opts,
137140
cmd: {
138141
initCmd,
@@ -141,6 +144,34 @@ export function bootstrapNetwork (testName, argv,
141144
nodeCmd
142145
}
143146
}
147+
}
148+
149+
/**
150+
* Bootstrap network in a given namespace
151+
*
152+
* @param testName test name
153+
* @param argv argv for commands
154+
* @param k8Arg an instance of core/K8
155+
* @param initCmdArg an instance of command/InitCommand
156+
* @param clusterCmdArg an instance of command/ClusterCommand
157+
* @param networkCmdArg an instance of command/NetworkCommand
158+
* @param nodeCmdArg an instance of command/NodeCommand
159+
*/
160+
export function bootstrapNetwork (testName, argv,
161+
k8Arg = null,
162+
initCmdArg = null,
163+
clusterCmdArg = null,
164+
networkCmdArg = null,
165+
nodeCmdArg = null
166+
) {
167+
const bootstrapResp = bootstrapTestVariables(testName, argv, k8Arg, initCmdArg, clusterCmdArg, networkCmdArg, nodeCmdArg)
168+
const namespace = bootstrapResp.namespace
169+
const initCmd = bootstrapResp.cmd.initCmd
170+
const k8 = bootstrapResp.opts.k8
171+
const clusterCmd = bootstrapResp.cmd.clusterCmd
172+
const networkCmd = bootstrapResp.cmd.networkCmd
173+
const nodeCmd = bootstrapResp.cmd.nodeCmd
174+
const chartManager = bootstrapResp.opts.chartManager
144175

145176
describe(`Bootstrap network for test [release ${argv[flags.releaseTag.name]}, keyFormat: ${argv[flags.keyFormat.name]}]`, () => {
146177
it('should cleanup previous deployment', async () => {
@@ -155,7 +186,7 @@ export function bootstrapNetwork (testName, argv,
155186
}
156187
}
157188

158-
if (!await k8.hasNamespace(constants.FULLSTACK_SETUP_NAMESPACE)) {
189+
if (!await chartManager.isChartInstalled(constants.FULLSTACK_SETUP_NAMESPACE, constants.FULLSTACK_CLUSTER_SETUP_CHART)) {
159190
await clusterCmd.setup(argv)
160191
}
161192
}, 60000)

0 commit comments

Comments
 (0)