@rophy123/helmtest 2.0.0 → 2.1.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/Makefile ADDED
@@ -0,0 +1,7 @@
1
+ .PHONY: help test
2
+
3
+ help: ## Show this help.
4
+ @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
5
+
6
+ test: ## Run unit tests.
7
+ docker run -t --rm -e DEBUG=helmtest -v `pwd`:/workspace rophy/helmtest
package/README.md CHANGED
@@ -19,4 +19,70 @@ Differences with [Stono/helmtest](https://github.com/Stono/helm-test):
19
19
 
20
20
  ## Getting Started
21
21
 
22
- See [example](../../tree/example) as an example helm chart project which includes unit tests.
22
+ ### Running helmtest as npm module
23
+
24
+ Install helmtest:
25
+
26
+ ```bash
27
+ npm install @rophy123/helmtest
28
+ ```
29
+
30
+ To use helmtest in your code:
31
+
32
+ ```javascript
33
+ const helmtest = require('@rophy123/helmtest');
34
+
35
+ // Run `helm template` under working directory
36
+ helmtest.renderTemplate()
37
+ .then(results => {
38
+ // results are JS objects converted from result YAML.
39
+ // '[{"apiVesrion":"v1","kind":"ServiceAccount","metadata":...
40
+ console.log(JSON.stringify(results));
41
+ });
42
+ ```
43
+
44
+ The results object can be used to verify details. For example, in jest:
45
+
46
+ ```javascript
47
+ const helmtest = require('@rophy123/helmtest');
48
+
49
+ test('exmapleChart should render 4 resources', async () => {
50
+ const result = await helmtest.renderTemplate();
51
+ expect(result.length).toBe(4);
52
+ });
53
+ ```
54
+
55
+ See [API Spec](#api-spec) for the options of `helmtest.renderTemplate()`.
56
+
57
+ ### Running helmtest as CLI
58
+
59
+ Install [jest](https://jestjs.io/) along with helmtest globally:
60
+
61
+ ```bash
62
+ npm install -g @rophy123/helmtest jest
63
+ ```
64
+
65
+ ...and then you can write unit tests under `tests/` dir of your chart project without the need for package.json
66
+
67
+ A docker image [rophy/helmtest](https://hub.docker.com/r/rophy/helmtest) is avilable which includes everything to run up unit tests.
68
+
69
+ See [example](../../tree/example) as an example helm chart project on how to use docker image and write unit tests.
70
+
71
+ ## API Spec
72
+
73
+ helmtest currently only expose one method: `helmtest.renderTemplate(options)`.
74
+
75
+ `options` is a object with option names, descriptions and defaults as below:
76
+
77
+ | Option | Description | Default |
78
+ | -------------------- | ----------------------------------------------------------------------------------- | --------------------------------------------------- |
79
+ | chartDir | Path to chart, the `CHART` arg in helm CLI: `helm template [NAME] [CHART] [flags]`. | `'.'` |
80
+ | releaseName | Release name, the `NAME` arg in helm CLI: `helm template [NAME] [CHART] [flags]`. | `'release-name'` |
81
+ | values | Map of chart values to set. | `{}` |
82
+ | valuesFiles | Paths to additional values.yaml. | `[]` |
83
+ | extraHelmArgs | Extra args to pass to helm CLI, e.g. `--timeout=5s` | `[]` |
84
+ | helmBinary | Path to helm binary. | `process.env.HELM_BINARY \|\| 'helm'` |
85
+ | loadYaml | if `false`, will return rendered template as raw string instead of js object. | `true` |
86
+ | kubeconformEnabled | if `true`, will pass rendered template to kubeconform CLI to validate schema. | `process.env.KUBECONFORM_ENABLED \|\| false` |
87
+ | kubeconformBinary | Path to kubeconform binary. | `process.env.KUBECONFORM_BINARY \|\| 'kubeconform'` |
88
+ | kubeconformArgs | Args to pass to kubeconform CLI. Will append `process.env.KUBECONFORM_EXTRA_ARGS`. | `['-strict','-ignore-missing-schemas','-summary']` |
package/lib/helmtest.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
3
  const util = require('util');
4
- const exec = util.promisify(require('node:child_process').exec);
4
+ const execFile = util.promisify(require('node:child_process').execFile);
5
5
  const yaml = require('js-yaml');
6
6
  const debug = require('debug')('helmtest');
7
7
  const Readable = require('stream').Readable;
@@ -26,8 +26,13 @@ async function renderTemplate(options = {}) {
26
26
  process.env.KUBECONFORM_ENABLED || false;
27
27
  options.kubeconformBinary = options.kubeconformBinary ||
28
28
  process.env.KUBECONFORM_BINARY || 'kubeconform';
29
- options.kubeconformExtraArgs = options.kubeconformExtraArgs ||
30
- process.env.KUBECONFORM_ARGS || false;
29
+ options.kubeconformArgs = options.kubeconformArgs ||
30
+ ['-strict', '-ignore-missing-schemas', '-summary'];
31
+
32
+ if (process.env.KUBECONFORM_EXTRA_ARGS) {
33
+ const kcExtraArgs = process.env.KUBECONFORM_EXTRA_ARGS.split(' ');
34
+ options.kubeConformArgs.concat(kcExtraArgs);
35
+ }
31
36
 
32
37
  debug(`options: ${JSON.stringify(options)}`);
33
38
 
@@ -35,7 +40,8 @@ async function renderTemplate(options = {}) {
35
40
  fs.statSync(options.chartDir);
36
41
 
37
42
  // Now construct the args.
38
- const helmCmd = [options.helmBinary, 'template'];
43
+ const helmCmd = options.helmBinary;
44
+ const helmArgs = ['template'];
39
45
 
40
46
  const valueKeys = Object.keys(options.values);
41
47
  const valuePairs = [];
@@ -43,7 +49,7 @@ async function renderTemplate(options = {}) {
43
49
  valuePairs.push(`${key}=${options.values[key]}`);
44
50
  });
45
51
  if (valuePairs.length > 0) {
46
- helmCmd.push('--set', valuePairs.join(','));
52
+ helmArgs.push('--set', valuePairs.join(','));
47
53
  }
48
54
 
49
55
  if (options.valuesFiles) {
@@ -52,8 +58,7 @@ async function renderTemplate(options = {}) {
52
58
  options.valuesFiles;
53
59
  valuesFiles.forEach((valuesFile) => {
54
60
  fs.statSync(valuesFile);
55
- helmCmd.push('-f');
56
- helmCmd.push(valuesFile);
61
+ helmArgs.push('-f', valuesFile);
57
62
  });
58
63
  }
59
64
 
@@ -66,35 +71,28 @@ async function renderTemplate(options = {}) {
66
71
  fs.statSync(filePath);
67
72
  // Note: get the abs template file path to chek it actually exists,
68
73
  // `helm template` command expects relative path from chartDir.
69
- helmCmd.push('--show-only');
70
- helmCmd.push(templateFile);
74
+ helmArgs.push('--show-only', templateFile);
71
75
  });
72
76
  }
73
77
 
74
- if (options.extraHelmArgs) {
75
- options.extraHelmArgs.forEach((arg) => helmCmd.push(arg));
76
- }
77
-
78
- helmCmd.push(options.releaseName);
79
- helmCmd.push(options.chartDir);
80
-
81
- const command = helmCmd.join(' ');
82
- debug(`command: ${command}`);
83
- const helmOutput = await exec(command);
78
+ options.extraHelmArgs.forEach((arg) => helmArgs.push(arg));
79
+ helmArgs.push(options.releaseName, options.chartDir);
80
+ debug(`helm command: ${helmCmd} ${helmArgs.join(' ')}`);
81
+ const helmOutput = await execFile(helmCmd, helmArgs);
84
82
 
85
83
  if (options.kubeconformEnabled) {
86
- let kcCommand = options.kubeconformBinary;
87
- if (options.kubeconformExtraArgs) {
88
- kcCommand += ' ' + options.kubeconformExtraArgs;
89
- }
90
- debug(`kcCommand: ${kcCommand}`);
91
- const kcProc = exec(kcCommand);
84
+ const kcCmd = options.kubeconformBinary;
85
+ const kcArgs = options.kubeconformArgs;
86
+ debug(`kubeconform command: ${kcCmd} ${kcArgs.join(' ')}`);
87
+ const kcProc = execFile(kcCmd, kcArgs);
92
88
  const piper = new Readable();
93
89
  piper.pipe(kcProc.child.stdin);
94
90
  piper.push(helmOutput.stdout);
95
91
  piper.push(null);
96
92
  try {
97
- await kcProc;
93
+ const kcResult = await kcProc;
94
+ if (kcResult.stdout) debug(kcResult.stdout);
95
+ if (kcResult.stderr) debug(kcResult.stderr);
98
96
  } catch (err) {
99
97
  if (err.stdout) console.error(err.stdout);
100
98
  if (err.stderr) console.error(err.stderr);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rophy123/helmtest",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Write unit tests against your helm charts using JavaScript.",
5
5
  "main": "index.js",
6
6
  "bin": {