voxpupuli / onceover

Your gateway drug to automated infrastructure testing with Puppet
Apache License 2.0
142 stars 45 forks source link

[feature] run syntax checks on control repo files #62

Closed GeoffWilliams closed 7 years ago

GeoffWilliams commented 7 years ago

Overview

At the moment its possible to sneak files into your control repo after giving it the onceover. It would be great to do some basic syntax checking before the main onceover run happens.

How to test syntax

It should be pretty simple, the following commands will do syntax checks on all local non-onceover owned puppet, yaml and ERB files:

# puppet files
find . -name '*.pp' -not -path './.onceover/*' -exec puppet parser validate {} \;

# ERB files
find . -name '*.erb' -not -path './.onceover/*' -exec erb -P -x -T '-' {} \; | ruby -c >/dev/null

# YAML files
- find . -name '*.yaml' -not -path './.onceover/*' -exec ruby -ryaml -e "YAML.load_file '"{}"' " \;

Implementation

Performing these extra tests could be as simple as shelling out and running the checks as one of onceovers first actions on onceover run spec

GeoffWilliams commented 7 years ago

...Actually you need to use xargs -- find will always exit with status 0 unless there was an error traversing the tree, irrespective of -exec exit status.

The fixed commands are:

# puppet files
find . -name '*.pp' -not -path './.onceover/*' | xargs -n1 puppet parser validate

# ERB files
find . -name '*.erb' -not -path './.onceover/*' | xargs -n1 -I {} sh -c "erb -P -x -T '-' {} | ruby -c >/dev/null"

# YAML files
find . -name '*.yaml' -not -path './.onceover/*' | xargs -n1 ruby -ryaml -e "YAML.load_file '"{}"' "
GeoffWilliams commented 7 years ago

This is the Makefile I normally use if anyone needs this right away: https://gist.github.com/GeoffWilliams/c88dedb7d5e6dee2f76121c2e2377afa

Dropping a Makefile lets you run all of onceover just by typing make

dylanratcliffe commented 7 years ago

@GeoffWilliams What do you think of this method? https://github.com/dylanratcliffe/puppet_controlrepo/tree/production/spec/unit

GeoffWilliams commented 7 years ago

@dylanratcliffe interesting approach wrapping it in rspec - tbh I don't mind what it does as long as it does it ;-) If you can get that to work then why not.

If it helps, for my own tool, I just shelled out and ran rake/puppet-lint directly - pretty simple: https://github.com/declarativesystems/pdqtest/blob/master/lib/pdqtest/syntax.rb#L14 https://github.com/declarativesystems/pdqtest/blob/master/lib/pdqtest/lint.rb#L20

dylanratcliffe commented 7 years ago

Yeah, well no reason why you couldn't wrap that in an rspec test too. I don't think I want it to be part of onceover itself, especially with PDK coming out and so many different ways of doing things. I'll put my examples up in the README. If you think it should do it implicitly then that would be the job of a plugin and I'm happy to take PRs for any onceover changes required to make that work. Closing for now