Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ There are some global helper variables defined for use in your tests:
#### helm
This is the root context and exposes the following functions:

- `withValueFile(path)`: Specify a value file to use when running helm, relative to the root of your chart. You can call this multiple times
- `withValueFile(path, [chartName])`: Specify a value file to use when running helm, relative to the root of your chart. You can call this multiple times
- `set(key, value)`: Allows you to override a specific value, for example `set('service.port', '80')` would do `--set service.port=80` when running helm
- `go(done)`: Run a helm template generation and parse the output
- `go(done, [chartName])`: Run a helm template generation and parse the output
- `lint(done, [chartName])`: Run a helm lint

*`chartName` is an optional parameter but if you want to run helm-test on main folder (against all charts), you need to define 'chartName'*

#### yaml
This global helper function allows you to parse yaml using `yamljs`. This is useful for scenarios like a configmap containing a string block which sub contains yaml, that you wish to assert on.
Expand Down Expand Up @@ -98,6 +101,9 @@ Is a simple as doing `helm-test`:
### Constantly running tests and watching for changes
You can have helm-test run every time it detects a change in your chart by simply doing `helm-test --watch`

### Running tests against all helm charts
You can have helm-test run on main folder to test all charts in subfolder by `helm-test --all` on main folder

## License
Copyright (c) 2017 Karl Stoney
Licensed under the MIT license.
4 changes: 3 additions & 1 deletion bin/helm-test
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const program = require('commander');
program
.version(version)
.option('-w, --watch', 'Watch for file changes and re-run tests')
.option('-a, --all', 'Run test against all charts')
.parse(process.argv);

app.test({
watch: program.watch
watch: program.watch,
all: program.all
}, err => {
logger.log('Finished.');
if(err) {
Expand Down
5 changes: 5 additions & 0 deletions lib/all-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
const exec = new require('./exec')();
const Helm = require('./helm');
global.helm = new Helm(exec);
global.yaml = require('yamljs');
9 changes: 7 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ module.exports = function App(exec) {
self.test = function(options, done) {
const execOptions = { output: true, cwd: process.cwd() };
const mocha = path.join(__dirname, '../node_modules/mocha/bin/mocha');
const globals = path.join(__dirname, 'globals.js');
let globals = path.join(__dirname, 'globals.js');
let pattern = 'tests'
if(options.all) {
globals = path.join(__dirname, 'all-globals.js');
pattern = './**/tests/**/*.js';
}
let watch = '';
if(options.watch) {
logger.log('Watching for file changes enabled.');
watch = ' --watch --watch-extensions yaml,tpl';
}
const command = `${mocha}${watch} -r should -r ${globals} --recursive tests`;
const command = `${mocha}${watch} -r should -r ${globals} --recursive ${pattern}`;
logger.log('Testing...');
exec.command(command, execOptions, done);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/globals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const exec = new require('./exec')();
const Helm = require('./helm');
global.helm = new Helm(exec);
global.helm = new Helm(exec, '.');
global.yaml = require('yamljs');
21 changes: 17 additions & 4 deletions lib/helm.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ const HelmResultParser = function() {
};
const helmResultParser = new HelmResultParser();

module.exports = function Helm(exec) {
module.exports = function Helm(exec, chartPath) {
let self = {};
let files = [];
let sets = [];
self.withValueFile = function(valueFile) {
const pathToValueFile = path.join(process.cwd(), valueFile);
self.withValueFile = function(valueFile, chartName) {
let pathToValueFile = path.join(process.cwd(), valueFile);
if (!chartPath && chartName) {
pathToValueFile = path.join(process.cwd(), chartName, valueFile);
}

files.push(pathToValueFile);
return self;
};
Expand All @@ -74,7 +78,8 @@ module.exports = function Helm(exec) {
return self;
};
self.go = function(done) {
let command = 'helm template -n release-name .';
chartName = chartPath || chartName || '.';
let command = 'helm template -n release-name ${chartName}';
if(files.length > 0) {
command = command + ' -f ' + files.join(' -f ');
}
Expand All @@ -87,5 +92,13 @@ module.exports = function Helm(exec) {
exec.command(command, options, helmResultParser.parse(done));
return self;
};
self.lint = function(done, chartName) {
chartName = chartPath || chartName || '.';
let command = `helm lint ${chartName}`;

const options = { output: true };
exec.command(command, options, helmResultParser.parse(done));
return self;
}
return Object.freeze(self);
};
31 changes: 26 additions & 5 deletions test/helm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@ describe('Helm', () => {
beforeEach(() => {
exec = deride.stub(['command']);
exec.setup.command.toCallbackWith([null, { stdout: '' }]);
helm = new Helm(exec);
});
it('should accept value files', () => {
helm.withValueFile('some-file.yaml');
describe('Specific charts', () => {
beforeEach(() => {
helm = new Helm(exec, '.');
});
it('should accept value files', () => {
helm.withValueFile('some-file.yaml');
});
it('should run a helm template', done => {
helm.go(done);
});
it('should run a helm lint', done => {
helm.lint(done);
});
});
it('should run a helm template', done => {
helm.go(done);
describe('All charts', () => {
beforeEach(() => {
helm = new Helm(exec);
});
it('should accept value files', () => {
helm.withValueFile('some-file.yaml', 'chart-name');
});
it('should run a helm template', done => {
helm.go(done, 'chart-name');
});
it('should run a helm lint', done => {
helm.lint(done, 'chart-name');
});
});
});