diff --git a/README.md b/README.md index dafa22f..a66308f 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/bin/helm-test b/bin/helm-test index d6f7e61..86eb4e4 100755 --- a/bin/helm-test +++ b/bin/helm-test @@ -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) { diff --git a/lib/all-globals.js b/lib/all-globals.js new file mode 100644 index 0000000..38758cb --- /dev/null +++ b/lib/all-globals.js @@ -0,0 +1,5 @@ +'use strict'; +const exec = new require('./exec')(); +const Helm = require('./helm'); +global.helm = new Helm(exec); +global.yaml = require('yamljs'); diff --git a/lib/app.js b/lib/app.js index 254227d..86b2084 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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); }; diff --git a/lib/globals.js b/lib/globals.js index 38758cb..74ecb9c 100644 --- a/lib/globals.js +++ b/lib/globals.js @@ -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'); diff --git a/lib/helm.js b/lib/helm.js index fad440a..c407704 100644 --- a/lib/helm.js +++ b/lib/helm.js @@ -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; }; @@ -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 '); } @@ -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); }; diff --git a/test/helm.js b/test/helm.js index adbf6de..0195b5d 100644 --- a/test/helm.js +++ b/test/helm.js @@ -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'); + }); }); });