From 02fadfee17ab22def7df8adb839d8e40e46594aa Mon Sep 17 00:00:00 2001 From: Theo Chatzimichos Date: Nov 11 2017 15:04:18 +0000 Subject: Add test to validate the custom grains in pillar/id/*.sls The test validates that the data that are in pillar/id/*.sls match the acceptable values as defined in pillar/valid_custom_grains.yaml. The data are separated in two groups, global and localized. The global ones are checked only if they are defined and if they have valid values. The localized ones are also checked based on the defined "country" grain of the minion that have realistic values (eg the city matches the country) --- diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f00fdeb..5e0c7c4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,6 +10,14 @@ test_roles: tags: - docker +test_custom_grains: + stage: test + before_script: + - zypper -qn in --no-recommends python3 python3-PyYAML + script: bin/test_custom_grains.py + tags: + - docker + test_show_highstate: stage: test before_script: diff --git a/bin/test_custom_grains.py b/bin/test_custom_grains.py new file mode 100755 index 0000000..0288baa --- /dev/null +++ b/bin/test_custom_grains.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +# Validates if the pillar/id/$FQDN.sls has correct custom grains + +import yaml +import os +import sys + + +def error_msg(sls, key, valid_values): + if type(valid_values) == str: + msg = ' is' + result = valid_values + else: + msg = 's are' + result = ', '.join(valid_values) + print('pillar/id/%s has invalid value for the "%s" key. Valid value%s: %s' % (sls, key, msg, result)) + return 1 + + +def test_custom_grain(mygrains, sls, key, valid_values, status): + try: + value = mygrains[key] + except KeyError: + print('pillar/id/%s is missing the "%s" key' % (sls, key)) + return 1 + + if type(valid_values) == str: + if value != valid_values: + status = error_msg(sls, key, valid_values) + else: + if value not in valid_values: + status = error_msg(sls, key, valid_values) + + return status + + +status = 0 + +with open('pillar/valid_custom_grains.yaml', 'r') as f: + VALID_CUSTOM_GRAINS = yaml.load(f) + +valid_global_grains = VALID_CUSTOM_GRAINS['global'] +all_localized_grains = VALID_CUSTOM_GRAINS['localized'] + +all_ids = sorted(os.listdir('pillar/id')) +for sls in all_ids: + with open("pillar/id/%s" % sls) as f: + mygrains = yaml.load(f)['grains'] + + for key, valid_values in valid_global_grains.items(): + status = test_custom_grain(mygrains, sls, key, valid_values, status) + + try: + valid_localized_grains = all_localized_grains[mygrains['country']] + for key, valid_values in valid_localized_grains.items(): + status = test_custom_grain(mygrains, sls, key, valid_values, status) + except KeyError: + status = error_msg(sls, 'country', all_localized_grains.keys()) + +sys.exit(status) diff --git a/pillar/valid_custom_grains.yaml b/pillar/valid_custom_grains.yaml new file mode 100644 index 0000000..285daa3 --- /dev/null +++ b/pillar/valid_custom_grains.yaml @@ -0,0 +1,10 @@ +--- +global: + salt_cluster: opensuse +localized: + de: + city: nuremberg + virt_cluster: atreju + us: + city: provo + virt_cluster: bryce