Office, programming

Testing hiera data


I have been looking around for ways to validate puppet hiera data files.

my requirements were

  • check if the yaml is well formed.
  • check if all required settings are provided in the yamls
  • check if the required settings values are of expected format
  • inform the how a value is being considered when the setting is present in multiple files in hierarchy.

The basic idea that i had arrived at was to use json schema based validation. Basically create schemas for the various files in the Hiera Hierarchy, and validate each file against its own schema.

I already had a toy project which does that, which i made sometime last year. But, thought would research a little bit and do things a little properly.

I was curious to see if this problem hasn’t been faced by others or not. Searching around the internet found that puppet has a command which sort of does this. it’s not exactly a validation of the data, but what would hiera return when the setting is being requested.

puppet lookup command

That still wouldn’t help in our case. we can see what setting would be applied, but if the engineer has filled it in the correct format or not, if this is a boolean or string, those cannot be validated. also we can lookup only one setting at a time. But it helps in seeing how puppet arrives at the value with the helpful “explain” option.

on further searching i found that the exact same idea has been thought off and they also referred to tools which would help to achieve that.

https://logicminds.github.io/blog/2016-01-16-testing-hiera-data/

The post describes about puppet-retrospec and kwalify to achieve that. but those didn’t work out much, seems they are for older version of puppet.

for ensuring if the yaml is right or not, thought there should be something like pylint or jslint, and i was right, a simple search pointed me to something called yamllint. Which also had a python interface with which it can be included into the our scripts. a simple example of it would be like

from yamllint import linter
from yamllint.config import YamlLintConfig

conf = YamlLintConfig('extends: default')

filepath = "./test.yaml"
with open(filepath) as yamlfile:
    yaml = yamlfile.read()
problems = linter.run(yaml, conf, filepath)
for problem in problems:
    print(f"{problem.line}:{problem.column} \t {problem.level} \t {problem.desc} ({problem.rule})")

which produces

$ python testyamllint.py 
1:1      warning         missing document start "---" (document-start)
3:1      error   duplication of key "a" in mapping (key-duplicates)
3:7      error   no new line character at the end of file (new-line-at-end-of-file)

for a sample yaml

a: "b"
b: "c"
a: "d"

let’s see how it goes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s