@rophy123/helmtest 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/helmtest ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+
3
+ const jest = require('jest');
4
+ const path = require('path');
5
+
6
+ jest.run([
7
+ '--testMatch',
8
+ '**/*.js',
9
+ '--projects',
10
+ __dirname,
11
+ '--roots',
12
+ path.join(process.cwd(), 'tests'),
13
+ ]);
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/helmtest');
@@ -0,0 +1,81 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const util = require('util');
4
+ const exec = util.promisify(require('node:child_process').exec);
5
+ const yaml = require('js-yaml');
6
+
7
+ /**
8
+ * Run `helm template` to generate javascript object of the result YAML.
9
+ * @param {object} options See below for list of options.
10
+ * @return {object} Rendered template.
11
+ */
12
+ async function renderTemplate(options = {}) {
13
+ // default values
14
+ if (!options) options = {};
15
+ options.chartDir = options.chartDir || '.';
16
+ options.releaseName = options.releaseName || 'release-name';
17
+ options.values = options.values || {};
18
+ options.valuesFiles = options.valuesFiles || [];
19
+ options.templateFiles = options.templateFiles || [];
20
+ options.extraHelmArgs = options.extraHelmArgs || [];
21
+ options.helmBinary = options.helmBinary || process.env.HELM_BINARY || 'helm';
22
+ options.loadYaml = options.loadYaml || true;
23
+
24
+ // Make sure chartDir exists.
25
+ fs.statSync(options.chartDir);
26
+
27
+ // Now construct the args.
28
+ const helmCmd = [options.helmBinary, 'template'];
29
+
30
+ const valueKeys = Object.keys(options.values);
31
+ const valuePairs = [];
32
+ valueKeys.forEach((key) => {
33
+ valuePairs.push(`${key}=${options.values[key]}`);
34
+ });
35
+ if (valuePairs.length > 0) {
36
+ helmCmd.push('--set', valuePairs.join(','));
37
+ }
38
+
39
+ if (options.valuesFiles) {
40
+ const valuesFiles = typeof options.valuesFiles === 'string' ?
41
+ options.valuesFiles.split(','):
42
+ options.valuesFiles;
43
+ valuesFiles.forEach((valuesFile) => {
44
+ fs.statSync(valuesFile);
45
+ helmCmd.push('-f');
46
+ helmCmd.push(valuesFile);
47
+ });
48
+ }
49
+
50
+ if (options.templateFiles) {
51
+ const templateFiles = typeof options.templateFiles === 'string' ?
52
+ options.templateFiles.split(',') :
53
+ options.templateFiles;
54
+ templateFiles.forEach((templateFile) => {
55
+ const filePath = path.join(chartDir, templateFile);
56
+ fs.statSync(filePath);
57
+ // Note: get the abs template file path to chek it actually exists,
58
+ // `helm template` command expects relative path from chartDir.
59
+ helmCmd.push('--show-only');
60
+ helmCmd.push(templateFile);
61
+ });
62
+ }
63
+
64
+ if (options.extraHelmArgs) {
65
+ options.extraHelmArgs.forEach((arg) => helmCmd.push(arg));
66
+ }
67
+
68
+ helmCmd.push(options.releaseName);
69
+ helmCmd.push(options.chartDir);
70
+
71
+ const helmOutput = await exec(helmCmd.join(' '));
72
+ if (options.loadYaml) {
73
+ return yaml.loadAll(helmOutput.stdout);
74
+ } else {
75
+ return helmOutput;
76
+ }
77
+ }
78
+
79
+ module.exports = {
80
+ renderTemplate,
81
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@rophy123/helmtest",
3
+ "version": "1.0.0",
4
+ "description": "Write unit tests against your helm charts using JavaScript.",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "helmtest": "bin/helmtest"
8
+ },
9
+ "scripts": {
10
+ "test": "jest tests"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/rophy/helmtest.git"
15
+ },
16
+ "author": "Rophy Tsai <rophy@users.noreply.github.com>",
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://github.com/rophy/helmtest/issues"
20
+ },
21
+ "homepage": "https://github.com/rophy/helmtest#readme",
22
+ "dependencies": {
23
+ "jest": "^29.5.0",
24
+ "js-yaml": "^4.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "eslint": "^8.36.0",
28
+ "eslint-config-google": "^0.14.0"
29
+ },
30
+ "engines": {
31
+ "node": ">=16.0.0"
32
+ }
33
+ }