From 25cb76eded953950a88111b74b8c5480e3a6ca2a Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Dec 04 2017 19:03:25 +0000 Subject: ignore jinja lines in pillar/id/* in CI tests get_roles.py and test_custom_grains.py fail with the added {% if ... %} conditions in pillar/id/* Add a read_file_skip_jinja() function that reads files and drops jinja lines (starting with '{%'), and use this function in get_roles.py and test_custom_grains.py instead of f.read(). Filtering out jinja is not 100% technically correct, but good enough for the CI tests. Note that read_file_skip_jinja() is only used for reading pillar/id/* --- diff --git a/bin/get_roles.py b/bin/get_roles.py index 566e0b8..85422bd 100755 --- a/bin/get_roles.py +++ b/bin/get_roles.py @@ -6,6 +6,17 @@ import argparse import yaml import os +def read_file_skip_jinja(filename): + ''' reads a file and returns its content, except lines starting with '{%' ''' + non_jinja_lines = [] + + with open(filename) as f: + for line in f.read().split('\n'): + if not line.startswith('{%'): + non_jinja_lines.append(line) + + return '\n'.join(non_jinja_lines) + def get_roles(with_base=False): roles = [] @@ -13,13 +24,16 @@ def get_roles(with_base=False): roles.append('base') for sls in os.listdir('pillar/id'): - with open("pillar/id/%s" % sls) as f: - try: - _roles = yaml.load(f)['grains']['roles'] - except KeyError: - continue - for item in _roles: - roles.append(item) + non_jinja_lines = [] + + content = read_file_skip_jinja("pillar/id/%s" % sls) + + try: + _roles = yaml.load(content)['grains']['roles'] + except KeyError: + continue + for item in _roles: + roles.append(item) roles = sorted(set(roles)) return roles diff --git a/bin/test_custom_grains.py b/bin/test_custom_grains.py index 0288baa..fe00d60 100755 --- a/bin/test_custom_grains.py +++ b/bin/test_custom_grains.py @@ -6,6 +6,7 @@ import yaml import os import sys +from get_roles import read_file_skip_jinja def error_msg(sls, key, valid_values): if type(valid_values) == str: @@ -45,17 +46,17 @@ 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'] + content = read_file_skip_jinja("pillar/id/%s" % sls) + mygrains = yaml.load(content)['grains'] - for key, valid_values in valid_global_grains.items(): - status = test_custom_grain(mygrains, sls, key, valid_values, status) + 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()) + 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)