@rophy123/helmtest 1.2.0 → 2.0.1

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/README.md CHANGED
@@ -1,4 +1,88 @@
1
1
  # helmtest
2
2
 
3
- Write unit tests for your helm chart using JavaScript.
3
+ A npm module which enables writing unit tests with nodejs / Jest.
4
4
 
5
+ Heavily inspired by [Stono/helmtest](https://github.com/Stono/helm-test).
6
+
7
+ ## Why not just use Stono/helmtest?
8
+
9
+ I originally wanted to update [Stono/helmtest](https://github.com/Stono/helm-test) to fit my needs,
10
+ but after attempting to customize it, I figured it's actually easier to rewrite one.
11
+
12
+ Differences with [Stono/helmtest](https://github.com/Stono/helm-test):
13
+
14
+ - Single js file for the core logic (lib/helmtest.js)
15
+ - Supports [kubeconform](https://github.com/yannh/kubeconform) instead of [kubeval](https://github.com/instrumenta/kubeval/)
16
+ - No Grunt and TypeScript
17
+ - Can be used as a npm library without assuming test framework and passing objects around global scope.
18
+ - Can also run independently as CLI. In this case [jest](https://jestjs.io/) is bundled instead of mocha/chai.
19
+
20
+ ## Getting Started
21
+
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 | A map of chart values to set. | `{}` |
82
+ | valuesFiles | A list of paths to additional values.yaml. | `[]` |
83
+ | extraHelmArgs | A list of 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
+ | kubeconformExtraArgs | A list of extra args to pass to kubeconform CLI. | `process.env.KUBECONFORM_ARGS || []` |
package/bin/helmtest CHANGED
@@ -1,9 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const jest = require('jest');
4
3
  const path = require('path');
5
4
  const helmtest = require('../lib/helmtest');
5
+
6
+ let jest;
7
+
8
+ try {
9
+ jest = require('jest');
10
+ } catch (err) {
11
+ if (err.code == 'MODULE_NOT_FOUND') {
12
+ console.error(`ERROR: ${err.message}`);
13
+ // eslint-disable-next-line max-len
14
+ console.error('jest is required when running helmtest as CLI. Install it by `npm install [-g] jest`');
15
+ process.exit(1);
16
+ }
17
+ }
18
+
6
19
  globalThis.helmtest = helmtest;
20
+
7
21
  jest.run([
8
22
  '--testMatch',
9
23
  '**/*.js',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rophy123/helmtest",
3
- "version": "1.2.0",
3
+ "version": "2.0.1",
4
4
  "description": "Write unit tests against your helm charts using JavaScript.",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -21,12 +21,17 @@
21
21
  "homepage": "https://github.com/rophy/helmtest#readme",
22
22
  "dependencies": {
23
23
  "debug": "^4.3.4",
24
- "jest": "^29.5.0",
25
24
  "js-yaml": "^4.1.0"
26
25
  },
27
26
  "devDependencies": {
28
27
  "eslint": "^8.36.0",
29
- "eslint-config-google": "^0.14.0"
28
+ "eslint-config-google": "^0.14.0",
29
+ "jest": "^29.5.0"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "jest": {
33
+ "optional": true
34
+ }
30
35
  },
31
36
  "engines": {
32
37
  "node": ">=16.0.0"
package/.dockerignore DELETED
@@ -1,2 +0,0 @@
1
- node_modules/
2
- .git/
package/Dockerfile DELETED
@@ -1,28 +0,0 @@
1
- FROM node:16.19.1-bullseye-slim
2
-
3
- RUN apt-get update && apt-get install -y curl git
4
-
5
- RUN curl -sLo ./helm.tar.gz https://get.helm.sh/helm-v3.11.2-linux-amd64.tar.gz \
6
- && tar -zxvf helm.tar.gz linux-amd64/helm \
7
- --strip-components 1 && mv ./helm /usr/local/bin/ \
8
- && rm ./helm.tar.gz
9
-
10
- RUN curl -sLo ./helm.tar.gz https://get.helm.sh/helm-v3.11.2-linux-amd64.tar.gz \
11
- && tar -zxvf helm.tar.gz linux-amd64/helm \
12
- --strip-components 1 && mv ./helm /usr/local/bin/ \
13
- && rm ./helm.tar.gz
14
-
15
- RUN curl -sLo kubeconform.tar.gz https://github.com/yannh/kubeconform/releases/download/v0.6.1/kubeconform-linux-amd64.tar.gz \
16
- && tar -zxvf kubeconform.tar.gz kubeconform \
17
- && mv ./kubeconform /usr/local/bin/ \
18
- && rm kubeconform.tar.gz
19
-
20
- ENV KUBERNETES_VERSION=1.22.9
21
- RUN cd / && git clone --depth 1 --filter=blob:none --sparse https://github.com/yannh/kubernetes-json-schema \
22
- && cd kubernetes-json-schema \
23
- && git sparse-checkout set v${KUBERNETES_VERSION}-standalone-strict \
24
- && rm -rf ./.git
25
-
26
- ENV KUBECONFORM_ENABLED=true KUBECONFORM_ARGS="-kubernetes-version ${KUBERNETES_VERSION} -schema-location /kubernetes-json-schema -strict"
27
- ENV HELM_REGISTRY_CONFIG=/tmp/registry.json DOCKER_CONFIG=/tmp/config.json
28
- RUN npm install -g @rophy123/helmtest