From 18585bdc90b9f5c8f7078a45e963b1edc19093bf Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 21:32:38 +0100 Subject: [PATCH] refactor(config): minimise and standardise between `.sls` & `.tst` --- cron/config/file.sls | 28 +++++++-------- cron/saltcheck-tests/config.tst | 64 +++++++++++++-------------------- cron/saltcheck-tests/map.jinja | 47 ------------------------ 3 files changed, 38 insertions(+), 101 deletions(-) delete mode 100644 cron/saltcheck-tests/map.jinja diff --git a/cron/config/file.sls b/cron/config/file.sls index 66d5e8f..2da2ae1 100644 --- a/cron/config/file.sls +++ b/cron/config/file.sls @@ -5,23 +5,21 @@ {%- set tplroot = tpldir.split('/')[0] %} {%- from tplroot ~ "/map.jinja" import cron with context %} -{%- if 'tasks' in cron %} - {%- for task, task_options in cron.tasks.items() %} +{%- for task, task_options in cron.get('tasks', {}).items() %} +{%- set cron_type = task_options.type|d('present') %} cron.{{ task }}: - cron.{{ task_options.type|d('present') }}: + cron.{{ cron_type }}: - name: {{ task_options.name }} - - identifier: '{{ task }}' - {%- if 'user' in task_options %} - user: {{ task_options.user|d('root') }} + - identifier: '{{ task }}' + {%- if cron_type == 'present' %} + - commented: {{ task_options.commented|d(False) }} + {%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} + {%- if section in task_options %} + - {{ section }}: '{{ task_options[section] }}' + {%- endif %} + {%- endfor %} {%- endif %} - {%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} - {%- if section in task_options %} - - {{ section }}: '{{ task_options.get(section) }}' - {%- endif %} - {%- endfor %} - {%- if task_options.commented|d(False) %} - - commented: True - {%- endif %} - {%- endfor %} -{%- endif %} + +{%- endfor %} diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index c93ba6c..23f0a16 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -3,59 +3,45 @@ {%- set cron = salt['pillar.get']('cron', {}) %} -{%- if 'tasks' in cron %} -{%- for task, task_options in cron.tasks.items() %} +{%- for task, task_options in cron.get('tasks', {}).items() %} +{%- set cron_type = task_options.type|d('present') %} -{%- if task_options.type == 'absent' %} -validate_cron.{{ task }}_absent: +validate_cron.{{ task }}_{{ cron_type }}: module_and_function: cron.get_entry args: - {{ task_options.user|d('root') }} - {{ task }} + {%- if cron_type == 'absent' %} assertion: assertFalse - -{%- elif task_options.type == 'present' %} -validate_cron.{{ task }}_exists: - module_and_function: cron.get_entry - args: - - {{ task_options.user|d('root') }} - - {{ task }} + {%- else %} assertion: assertEqual assertion_section: identifier expected-return: {{ task }} + {%- endif %} -{#- Note: `special` is `spec` in the module #} -{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'spec'] %} -{%- if section in task_options %} -{%- set assertion = 'assertEqual' %} -{%- set expected = task_options.get(section) %} -{%- if expected == 'random' %} -{%- set assertion = 'assertLessEqual' %} -{%- set expected = 0 %} -{%- endif %} -validate_cron.{{ task }}_{{ section }}: - module_and_function: cron.get_entry - args: - - {{ task_options.user|d('root') }} - - {{ task }} - assertion: {{ assertion }} - assertion_section: {{ section }} - expected-return: '{{ expected }}' -{%- endif %} -{%- endfor %} - -{%- set assertion = 'assertFalse' %} -{%- if task_options.commented|d(False) %} -{%- set assertion = 'assertTrue' %} -{%- endif %} +{%- if cron_type == 'present' %} validate_cron.{{ task }}_commented: module_and_function: cron.get_entry args: - {{ task_options.user|d('root') }} - {{ task }} - assertion: {{ assertion }} + assertion: {{ 'assertTrue' if task_options.commented|d(False) else 'assertFalse' }} assertion_section: commented -{%- endif %} -{%- endfor %} -{%- endif %} +{#- Note: `special` is `spec` in the module #} +{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'spec'] %} +{%- if section in task_options %} +{%- set expected = task_options[section] %} +validate_cron.{{ task }}_{{ section }}: + module_and_function: cron.get_entry + args: + - {{ task_options.user|d('root') }} + - {{ task }} + assertion: {{ 'assertLessEqual' if expected == 'random' else 'assertEqual' }} + assertion_section: {{ section }} + expected-return: '{{ 0 if expected == 'random' else expected }}' +{%- endif %} +{%- endfor %} +{%- endif %} + +{%- endfor %} diff --git a/cron/saltcheck-tests/map.jinja b/cron/saltcheck-tests/map.jinja deleted file mode 100644 index 181dff6..0000000 --- a/cron/saltcheck-tests/map.jinja +++ /dev/null @@ -1,47 +0,0 @@ -# -*- coding: utf-8 -*- -# vim: ft=jinja - -{#- Start imports as #} -{%- import_yaml "cron" ~ "/defaults.yaml" as default_settings %} -{%- import_yaml "cron" ~ "/osarchmap.yaml" as osarchmap %} -{%- import_yaml "cron" ~ "/osfamilymap.yaml" as osfamilymap %} -{%- import_yaml "cron" ~ "/osmap.yaml" as osmap %} -{%- import_yaml "cron" ~ "/osfingermap.yaml" as osfingermap %} - -{#- Retrieve the config dict only once #} -{%- set _config = salt['config.get']("cron", default={}) %} - -{%- set defaults = salt['grains.filter_by']( - default_settings, - default="cron", - merge=salt['grains.filter_by']( - osarchmap, - grain='osarch', - merge=salt['grains.filter_by']( - osfamilymap, - grain='os_family', - merge=salt['grains.filter_by']( - osmap, - grain='os', - merge=salt['grains.filter_by']( - osfingermap, - grain='osfinger', - merge=salt['grains.filter_by']( - _config, - default='lookup' - ) - ) - ) - ) - ) - ) -%} - -{%- set config = salt['grains.filter_by']( - {'defaults': defaults}, - default='defaults', - merge=_config - ) -%} - -{%- set cron = config %}