From 9847aff516c4fbdfa64115a5660780b3eb2eba37 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Mon, 8 Apr 2019 16:40:48 +0100 Subject: [PATCH 01/29] test(saltcheck): add first tests --- cron/saltcheck-tests/config.tst | 103 +++++++++++++++++++++++++++++++ cron/saltcheck-tests/install.tst | 8 +++ cron/saltcheck-tests/service.tst | 15 +++++ 3 files changed, 126 insertions(+) create mode 100644 cron/saltcheck-tests/config.tst create mode 100644 cron/saltcheck-tests/install.tst create mode 100644 cron/saltcheck-tests/service.tst diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst new file mode 100644 index 0000000..2b3b4e7 --- /dev/null +++ b/cron/saltcheck-tests/config.tst @@ -0,0 +1,103 @@ +{%- from "cron/map.jinja" import cron_settings with context %} + +{%- if 'tasks' in cron_settings %} + {%- for task,task_options in cron_settings.tasks.items() %} + + {%- if task_options.type == 'present' %} +validate_cron.{{ task }}_exists: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertEqual + assertion_section: identifier + expected-return: {{ task }} + + {%- if 'minute' in task_options %} +validate_cron.{{ task }}_minute: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertEqual + assertion_section: minute + expected-return: {{ task_options.minute }} + {%- endif %} + + {%- if 'hour' in task_options %} +validate_cron.{{ task }}_hour: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: hour + assertion: assertEqual + expected-return: {{ task_options.hour }} + {%- endif %} + + {%- if 'daymonth' in task_options %} +validate_cron.{{ task }}_daymonth: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: daymonth + assertion: assertEqual + expected-return: {{ task_options.daymonth }} + {%- endif %} + + {%- if 'month' in task_options %} +validate_cron.{{ task }}_month: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: month + assertion: assertEqual + expected-return: {{ task_options.month }} + {%- endif %} + + {%- if 'dayweek' in task_options %} +validate_cron.{{ task }}_dayweek: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: dayweek + assertion: assertEqual + expected-return: {{ task_options.dayweek }} + {%- endif %} + + {%- if 'comment' in task_options %} +validate_cron.{{ task }}_comment: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: comment + assertion: assertEqual + expected-return: {{ task_options.comment }} + {%- endif %} + + {%- if 'commented' in task_options and task_options.commented %} +validate_cron.{{ task }}_commented: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: commented + assertion: assertTrue + {%- endif %} + {%- endif %} + + {%- if task_options.type == 'absent' %} +validate_cron.{{ task }}_absent: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertFalse + {%- endif %} + + {%- endfor %} +{%- endif %} diff --git a/cron/saltcheck-tests/install.tst b/cron/saltcheck-tests/install.tst new file mode 100644 index 0000000..290de07 --- /dev/null +++ b/cron/saltcheck-tests/install.tst @@ -0,0 +1,8 @@ + +m "cron/map.jinja" import cron_settings with context %} + +verify_cron.install: + module_and_function: pkg.version + args: + - {{ cron_settings.pkg }} + assertion: assertNotEmpty diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst new file mode 100644 index 0000000..1e46a94 --- /dev/null +++ b/cron/saltcheck-tests/service.tst @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{%- from "cron/map.jinja" import cron_settings with context %} + +cron.service: +{%- if 'enabled' not in cron_settings or ( 'enabled' in cron_settings and cron_settings.enabled ) %} + service.running: + - name: {{ cron_settings.service }} + - enable: True +{%- else %} + service.dead: + - name: {{ cron_settings.service }} + - enable: False +{%- endif %} From 7911b716e8d19cad0dc310472934381a0c9cd9e4 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 13:56:15 +0100 Subject: [PATCH 02/29] fix(saltcheck): fix broken import and standardise across test files --- cron/saltcheck-tests/config.tst | 5 ++++- cron/saltcheck-tests/install.tst | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 2b3b4e7..ad89650 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -1,4 +1,7 @@ -{%- from "cron/map.jinja" import cron_settings with context %} +# -*- coding: utf-8 -*- +# vim: ft=sls + +{%- from "cron/map.jinja" import cron_settings with context %} {%- if 'tasks' in cron_settings %} {%- for task,task_options in cron_settings.tasks.items() %} diff --git a/cron/saltcheck-tests/install.tst b/cron/saltcheck-tests/install.tst index 290de07..09c953b 100644 --- a/cron/saltcheck-tests/install.tst +++ b/cron/saltcheck-tests/install.tst @@ -1,5 +1,7 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls -m "cron/map.jinja" import cron_settings with context %} +{%- from "cron/map.jinja" import cron_settings with context %} verify_cron.install: module_and_function: pkg.version From 8845b3c50fe22a45f5a57713bce39132b4d13b96 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 13:57:31 +0100 Subject: [PATCH 03/29] test(saltcheck): use local `map.jinja` to workaround missing `tpldata` --- cron/saltcheck-tests/config.tst | 2 +- cron/saltcheck-tests/install.tst | 2 +- cron/saltcheck-tests/map.jinja | 47 ++++++++++++++++++++++++++++++++ cron/saltcheck-tests/service.tst | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 cron/saltcheck-tests/map.jinja diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index ad89650..e8802bc 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} {%- if 'tasks' in cron_settings %} {%- for task,task_options in cron_settings.tasks.items() %} diff --git a/cron/saltcheck-tests/install.tst b/cron/saltcheck-tests/install.tst index 09c953b..8547f55 100644 --- a/cron/saltcheck-tests/install.tst +++ b/cron/saltcheck-tests/install.tst @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} verify_cron.install: module_and_function: pkg.version diff --git a/cron/saltcheck-tests/map.jinja b/cron/saltcheck-tests/map.jinja new file mode 100644 index 0000000..181dff6 --- /dev/null +++ b/cron/saltcheck-tests/map.jinja @@ -0,0 +1,47 @@ +# -*- 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 %} diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index 1e46a94..43e5baa 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} cron.service: {%- if 'enabled' not in cron_settings or ( 'enabled' in cron_settings and cron_settings.enabled ) %} From 26cfa4f6c48ccb2a915915a7bc73b00303fa92d8 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 14:17:29 +0100 Subject: [PATCH 04/29] fix(saltcheck): update for `cron` instead of `cron_settings` --- cron/saltcheck-tests/config.tst | 6 +++--- cron/saltcheck-tests/install.tst | 4 ++-- cron/saltcheck-tests/service.tst | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index e8802bc..ac37344 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} -{%- if 'tasks' in cron_settings %} - {%- for task,task_options in cron_settings.tasks.items() %} +{%- if 'tasks' in cron %} + {%- for task,task_options in cron.tasks.items() %} {%- if task_options.type == 'present' %} validate_cron.{{ task }}_exists: diff --git a/cron/saltcheck-tests/install.tst b/cron/saltcheck-tests/install.tst index 8547f55..aa02be9 100644 --- a/cron/saltcheck-tests/install.tst +++ b/cron/saltcheck-tests/install.tst @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} verify_cron.install: module_and_function: pkg.version args: - - {{ cron_settings.pkg }} + - {{ cron.pkg }} assertion: assertNotEmpty diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index 43e5baa..26b72b6 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -1,15 +1,15 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron_settings with context %} +{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} cron.service: -{%- if 'enabled' not in cron_settings or ( 'enabled' in cron_settings and cron_settings.enabled ) %} +{%- if 'enabled' not in cron or ( 'enabled' in cron and cron.enabled ) %} service.running: - - name: {{ cron_settings.service }} + - name: {{ cron.service }} - enable: True {%- else %} service.dead: - - name: {{ cron_settings.service }} + - name: {{ cron.service }} - enable: False {%- endif %} From d2c95440e4b595a95ec5f5642945a58cd8d4680b Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 06:01:09 +0100 Subject: [PATCH 05/29] refactor(saltcheck): use `package.tst` instead of `install.tst` * Make consistent with the actual states --- cron/saltcheck-tests/{install.tst => package.tst} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename cron/saltcheck-tests/{install.tst => package.tst} (90%) diff --git a/cron/saltcheck-tests/install.tst b/cron/saltcheck-tests/package.tst similarity index 90% rename from cron/saltcheck-tests/install.tst rename to cron/saltcheck-tests/package.tst index aa02be9..cc8ffb5 100644 --- a/cron/saltcheck-tests/install.tst +++ b/cron/saltcheck-tests/package.tst @@ -3,7 +3,7 @@ {%- from "cron/saltcheck-tests/map.jinja" import cron with context %} -verify_cron.install: +verify_cron.package: module_and_function: pkg.version args: - {{ cron.pkg }} From 677c956de43a5473fc6970afad08f0fbb4ff8cee Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 14:35:33 +0100 Subject: [PATCH 06/29] fix(saltcheck): fix invalid `service` test --- cron/saltcheck-tests/service.tst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index 26b72b6..b831b81 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -3,13 +3,13 @@ {%- from "cron/saltcheck-tests/map.jinja" import cron with context %} -cron.service: +{%- set service_function = 'disabled' %} {%- if 'enabled' not in cron or ( 'enabled' in cron and cron.enabled ) %} - service.running: - - name: {{ cron.service }} - - enable: True -{%- else %} - service.dead: - - name: {{ cron.service }} - - enable: False +{%- set service_function = 'enabled' %} {%- endif %} + +verify_cron.service: + module_and_function: service.{{ service_function }} + args: + - {{ cron.service }} + assertion: assertTrue From 9225b18202275a1a3438b05b4392a37d54e9bf85 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 14:49:43 +0100 Subject: [PATCH 07/29] test(saltcheck): fix `config` tests --- cron/saltcheck-tests/config.tst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index ac37344..bd404b4 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -24,7 +24,7 @@ validate_cron.{{ task }}_minute: - {{ task }} assertion: assertEqual assertion_section: minute - expected-return: {{ task_options.minute }} + expected-return: '{{ task_options.minute }}' {%- endif %} {%- if 'hour' in task_options %} @@ -35,7 +35,7 @@ validate_cron.{{ task }}_hour: - {{ task }} assertion_section: hour assertion: assertEqual - expected-return: {{ task_options.hour }} + expected-return: '{{ task_options.hour }}' {%- endif %} {%- if 'daymonth' in task_options %} @@ -46,10 +46,10 @@ validate_cron.{{ task }}_daymonth: - {{ task }} assertion_section: daymonth assertion: assertEqual - expected-return: {{ task_options.daymonth }} + expected-return: '{{ task_options.daymonth }}' {%- endif %} - {%- if 'month' in task_options %} + {%- if 'month' in task_options %} validate_cron.{{ task }}_month: module_and_function: cron.get_entry args: @@ -57,7 +57,7 @@ validate_cron.{{ task }}_month: - {{ task }} assertion_section: month assertion: assertEqual - expected-return: {{ task_options.month }} + expected-return: '{{ task_options.month }}' {%- endif %} {%- if 'dayweek' in task_options %} @@ -68,7 +68,7 @@ validate_cron.{{ task }}_dayweek: - {{ task }} assertion_section: dayweek assertion: assertEqual - expected-return: {{ task_options.dayweek }} + expected-return: '{{ task_options.dayweek }}' {%- endif %} {%- if 'comment' in task_options %} @@ -100,7 +100,7 @@ validate_cron.{{ task }}_absent: - {{ task_options.user|default('root') }} - {{ task }} assertion: assertFalse - {%- endif %} + {%- endif %} {%- endfor %} -{%- endif %} +{%- endif %} From aada0ae835d40f61344b976b113eeb8519eb3edc Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 15:39:38 +0100 Subject: [PATCH 08/29] fix(saltcheck): remove trailing spaces --- cron/saltcheck-tests/config.tst | 188 +++++++++++++++---------------- cron/saltcheck-tests/service.tst | 2 +- 2 files changed, 95 insertions(+), 95 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index bd404b4..3553d1e 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -2,105 +2,105 @@ # vim: ft=sls {%- from "cron/saltcheck-tests/map.jinja" import cron with context %} - -{%- if 'tasks' in cron %} - {%- for task,task_options in cron.tasks.items() %} - - {%- if task_options.type == 'present' %} -validate_cron.{{ task }}_exists: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion: assertEqual - assertion_section: identifier - expected-return: {{ task }} - - {%- if 'minute' in task_options %} -validate_cron.{{ task }}_minute: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion: assertEqual - assertion_section: minute + +{%- if 'tasks' in cron %} + {%- for task,task_options in cron.tasks.items() %} + + {%- if task_options.type == 'present' %} +validate_cron.{{ task }}_exists: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertEqual + assertion_section: identifier + expected-return: {{ task }} + + {%- if 'minute' in task_options %} +validate_cron.{{ task }}_minute: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertEqual + assertion_section: minute expected-return: '{{ task_options.minute }}' - {%- endif %} - - {%- if 'hour' in task_options %} -validate_cron.{{ task }}_hour: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: hour - assertion: assertEqual + {%- endif %} + + {%- if 'hour' in task_options %} +validate_cron.{{ task }}_hour: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: hour + assertion: assertEqual expected-return: '{{ task_options.hour }}' - {%- endif %} - - {%- if 'daymonth' in task_options %} -validate_cron.{{ task }}_daymonth: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: daymonth - assertion: assertEqual + {%- endif %} + + {%- if 'daymonth' in task_options %} +validate_cron.{{ task }}_daymonth: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: daymonth + assertion: assertEqual expected-return: '{{ task_options.daymonth }}' - {%- endif %} - + {%- endif %} + {%- if 'month' in task_options %} -validate_cron.{{ task }}_month: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: month - assertion: assertEqual +validate_cron.{{ task }}_month: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: month + assertion: assertEqual expected-return: '{{ task_options.month }}' - {%- endif %} - - {%- if 'dayweek' in task_options %} -validate_cron.{{ task }}_dayweek: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: dayweek - assertion: assertEqual + {%- endif %} + + {%- if 'dayweek' in task_options %} +validate_cron.{{ task }}_dayweek: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: dayweek + assertion: assertEqual expected-return: '{{ task_options.dayweek }}' - {%- endif %} - - {%- if 'comment' in task_options %} -validate_cron.{{ task }}_comment: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: comment - assertion: assertEqual - expected-return: {{ task_options.comment }} - {%- endif %} - - {%- if 'commented' in task_options and task_options.commented %} -validate_cron.{{ task }}_commented: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: commented - assertion: assertTrue - {%- endif %} - {%- endif %} - - {%- if task_options.type == 'absent' %} -validate_cron.{{ task }}_absent: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion: assertFalse + {%- endif %} + + {%- if 'comment' in task_options %} +validate_cron.{{ task }}_comment: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: comment + assertion: assertEqual + expected-return: {{ task_options.comment }} + {%- endif %} + + {%- if 'commented' in task_options and task_options.commented %} +validate_cron.{{ task }}_commented: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion_section: commented + assertion: assertTrue + {%- endif %} + {%- endif %} + + {%- if task_options.type == 'absent' %} +validate_cron.{{ task }}_absent: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertFalse {%- endif %} - - {%- endfor %} + + {%- endfor %} {%- endif %} diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index b831b81..1a29b20 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -12,4 +12,4 @@ verify_cron.service: module_and_function: service.{{ service_function }} args: - {{ cron.service }} - assertion: assertTrue + assertion: assertTrue From 8abec548c1723017ae47e3ca0955f4324e1c5c30 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Mon, 14 Oct 2019 15:45:27 +0100 Subject: [PATCH 09/29] style(config.tst): rearrange Jinja statements for clarity --- cron/config/file.sls | 2 +- cron/saltcheck-tests/config.tst | 40 ++++++++++++++++----------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cron/config/file.sls b/cron/config/file.sls index ccc0333..269af05 100644 --- a/cron/config/file.sls +++ b/cron/config/file.sls @@ -6,7 +6,7 @@ {%- 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.tasks.items() %} cron.{{ task }}: cron.{{ task_options.type|default('present') }}: diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 3553d1e..3197bea 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -4,9 +4,9 @@ {%- from "cron/saltcheck-tests/map.jinja" import cron with context %} {%- if 'tasks' in cron %} - {%- for task,task_options in cron.tasks.items() %} +{%- for task, task_options in cron.tasks.items() %} - {%- if task_options.type == 'present' %} +{%- if task_options.type == 'present' %} validate_cron.{{ task }}_exists: module_and_function: cron.get_entry args: @@ -16,7 +16,7 @@ validate_cron.{{ task }}_exists: assertion_section: identifier expected-return: {{ task }} - {%- if 'minute' in task_options %} +{%- if 'minute' in task_options %} validate_cron.{{ task }}_minute: module_and_function: cron.get_entry args: @@ -25,9 +25,9 @@ validate_cron.{{ task }}_minute: assertion: assertEqual assertion_section: minute expected-return: '{{ task_options.minute }}' - {%- endif %} +{%- endif %} - {%- if 'hour' in task_options %} +{%- if 'hour' in task_options %} validate_cron.{{ task }}_hour: module_and_function: cron.get_entry args: @@ -36,9 +36,9 @@ validate_cron.{{ task }}_hour: assertion_section: hour assertion: assertEqual expected-return: '{{ task_options.hour }}' - {%- endif %} +{%- endif %} - {%- if 'daymonth' in task_options %} +{%- if 'daymonth' in task_options %} validate_cron.{{ task }}_daymonth: module_and_function: cron.get_entry args: @@ -47,9 +47,9 @@ validate_cron.{{ task }}_daymonth: assertion_section: daymonth assertion: assertEqual expected-return: '{{ task_options.daymonth }}' - {%- endif %} +{%- endif %} - {%- if 'month' in task_options %} +{%- if 'month' in task_options %} validate_cron.{{ task }}_month: module_and_function: cron.get_entry args: @@ -58,9 +58,9 @@ validate_cron.{{ task }}_month: assertion_section: month assertion: assertEqual expected-return: '{{ task_options.month }}' - {%- endif %} +{%- endif %} - {%- if 'dayweek' in task_options %} +{%- if 'dayweek' in task_options %} validate_cron.{{ task }}_dayweek: module_and_function: cron.get_entry args: @@ -69,9 +69,9 @@ validate_cron.{{ task }}_dayweek: assertion_section: dayweek assertion: assertEqual expected-return: '{{ task_options.dayweek }}' - {%- endif %} +{%- endif %} - {%- if 'comment' in task_options %} +{%- if 'comment' in task_options %} validate_cron.{{ task }}_comment: module_and_function: cron.get_entry args: @@ -80,9 +80,9 @@ validate_cron.{{ task }}_comment: assertion_section: comment assertion: assertEqual expected-return: {{ task_options.comment }} - {%- endif %} +{%- endif %} - {%- if 'commented' in task_options and task_options.commented %} +{%- if 'commented' in task_options and task_options.commented %} validate_cron.{{ task }}_commented: module_and_function: cron.get_entry args: @@ -90,17 +90,17 @@ validate_cron.{{ task }}_commented: - {{ task }} assertion_section: commented assertion: assertTrue - {%- endif %} - {%- endif %} +{%- endif %} +{%- endif %} - {%- if task_options.type == 'absent' %} +{%- if task_options.type == 'absent' %} validate_cron.{{ task }}_absent: module_and_function: cron.get_entry args: - {{ task_options.user|default('root') }} - {{ task }} assertion: assertFalse - {%- endif %} +{%- endif %} - {%- endfor %} +{%- endfor %} {%- endif %} From 88229f08bd9ea0e29615094607bb2ac071f53e62 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 19:46:15 +0100 Subject: [PATCH 10/29] style(saltcheck): use consistent order of assertions --- cron/saltcheck-tests/config.tst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 3197bea..87ad761 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -88,8 +88,8 @@ validate_cron.{{ task }}_commented: args: - {{ task_options.user|default('root') }} - {{ task }} - assertion_section: commented assertion: assertTrue + assertion_section: commented {%- endif %} {%- endif %} From 8ae46e5d490801b492daa17dbdeda55bb4325fce Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 05:43:38 +0100 Subject: [PATCH 11/29] ci(travis): obtain `saltcheck.py` and run the tests (only on `develop`) * Based on the latest development version from the `neon` branch - Also includes sending the appropriate exit codes (e.g. for failures) --- .travis.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.travis.yml b/.travis.yml index 55c36b0..3f21c40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,24 @@ cache: bundler ## Script to run for the test stage script: + # yamllint disable rule:line-length + # Converge and verify the instance; `saltcheck` relies upon the `converge` as well - bin/kitchen verify "${INSTANCE}" + # If a `develop` instance, get the appropriate version of `saltcheck.py` (temporary) + - if [ ! -z $(echo "${INSTANCE}" | grep \\-develop-) ]; then + bin/kitchen exec "${INSTANCE}" -c + "sudo curl -o \$(find /usr/lib/ -type d -name modules | grep packages/salt/modules)/saltcheck.py + https://raw.githubusercontent.com/myii/salt/fix/add-retcode/salt/modules/saltcheck.py"; + fi + # If a `develop` instance, run all of the `saltcheck` tests + - if [ ! -z $(echo "${INSTANCE}" | grep \\-develop-) ]; then + bin/kitchen exec "${INSTANCE}" -c + "sudo salt-call + --config-dir=/tmp/kitchen/etc/salt + saltcheck.run_state_tests + cron check_all=True"; + fi + # yamllint enable rule:line-length ## Stages and jobs matrix stages: From baab964f1653bc3b025e0007d5607cb048b188c2 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 05:46:43 +0100 Subject: [PATCH 12/29] ci(travis): run `salt-lint` for `.tst` files as well --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3f21c40..27992af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,7 +50,7 @@ jobs: script: # Install and run `salt-lint` - pip install --user salt-lint - - git ls-files | grep '\.sls$\|\.jinja$\|\.j2$\|\.tmpl$' + - git ls-files | grep '\.sls$\|\.jinja$\|\.j2$\|\.tmpl$\|\.tst$' | xargs -I {} salt-lint {} # Install and run `yamllint` # Need at least `v1.17.0` for the `yaml-files` setting From 226eb88078b6eff7772a0ce297b5420bcefd9074 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 17:32:13 +0100 Subject: [PATCH 13/29] test(saltcheck): add test for `service.available` --- cron/saltcheck-tests/service.tst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index 1a29b20..579750b 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -8,7 +8,13 @@ {%- set service_function = 'enabled' %} {%- endif %} -verify_cron.service: +verify_cron.service_available: + module_and_function: service.available + args: + - {{ cron.service }} + assertion: assertTrue + +verify_cron.service_{{ service_function }}: module_and_function: service.{{ service_function }} args: - {{ cron.service }} From 5cdc50f521205f738e7ab7372555dc741c4eb879 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 17:48:28 +0100 Subject: [PATCH 14/29] test(saltcheck): add test for `service.running` --- cron/saltcheck-tests/service.tst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index 579750b..d2cfe1a 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -4,8 +4,10 @@ {%- from "cron/saltcheck-tests/map.jinja" import cron with context %} {%- set service_function = 'disabled' %} +{%- set service_running = 'assertFalse' %} {%- if 'enabled' not in cron or ( 'enabled' in cron and cron.enabled ) %} {%- set service_function = 'enabled' %} +{%- set service_running = 'assertTrue' %} {%- endif %} verify_cron.service_available: @@ -19,3 +21,9 @@ verify_cron.service_{{ service_function }}: args: - {{ cron.service }} assertion: assertTrue + +verify_cron.service_running: + module_and_function: service.status + args: + - {{ cron.service }} + assertion: {{ service_running }} From 72281c7905f0ac1762c677315a0d7eeb6f21b5de Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 19:44:38 +0100 Subject: [PATCH 15/29] test(saltcheck): remove duplication in `config.tst` using a loop --- cron/saltcheck-tests/config.tst | 67 ++++----------------------------- 1 file changed, 7 insertions(+), 60 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 87ad761..c5ce251 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -16,71 +16,18 @@ validate_cron.{{ task }}_exists: assertion_section: identifier expected-return: {{ task }} -{%- if 'minute' in task_options %} -validate_cron.{{ task }}_minute: +{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment'] %} +{%- if section in task_options %} +validate_cron.{{ task }}_{{ section }}: module_and_function: cron.get_entry args: - {{ task_options.user|default('root') }} - {{ task }} assertion: assertEqual - assertion_section: minute - expected-return: '{{ task_options.minute }}' -{%- endif %} - -{%- if 'hour' in task_options %} -validate_cron.{{ task }}_hour: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: hour - assertion: assertEqual - expected-return: '{{ task_options.hour }}' -{%- endif %} - -{%- if 'daymonth' in task_options %} -validate_cron.{{ task }}_daymonth: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: daymonth - assertion: assertEqual - expected-return: '{{ task_options.daymonth }}' -{%- endif %} - -{%- if 'month' in task_options %} -validate_cron.{{ task }}_month: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: month - assertion: assertEqual - expected-return: '{{ task_options.month }}' -{%- endif %} - -{%- if 'dayweek' in task_options %} -validate_cron.{{ task }}_dayweek: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: dayweek - assertion: assertEqual - expected-return: '{{ task_options.dayweek }}' -{%- endif %} - -{%- if 'comment' in task_options %} -validate_cron.{{ task }}_comment: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion_section: comment - assertion: assertEqual - expected-return: {{ task_options.comment }} -{%- endif %} + assertion_section: {{ section }} + expected-return: '{{ task_options.get(section) }}' +{%- endif %} +{%- endfor %} {%- if 'commented' in task_options and task_options.commented %} validate_cron.{{ task }}_commented: From 652ebff7349d6e8737e7e6e68e5ac556f9657f0c Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 21:58:55 +0100 Subject: [PATCH 16/29] refactor(config): remove duplication in using a loop --- cron/config/file.sls | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/cron/config/file.sls b/cron/config/file.sls index 269af05..9e5510f 100644 --- a/cron/config/file.sls +++ b/cron/config/file.sls @@ -11,34 +11,17 @@ cron.{{ task }}: cron.{{ task_options.type|default('present') }}: - name: {{ task_options.name }} + - identifier: '{{ task }}' {%- if 'user' in task_options %} - user: {{ task_options.user|default('root') }} {%- endif %} - {%- if 'minute' in task_options %} - - minute: '{{ task_options.minute }}' - {%- endif %} - {%- if 'hour' in task_options %} - - hour: '{{ task_options.hour }}' - {%- endif %} - {%- if 'daymonth' in task_options %} - - daymonth: '{{ task_options.daymonth }}' - {%- endif %} - {%- if 'month' in task_options %} - - month: '{{ task_options.month }}' - {%- endif %} - {%- if 'dayweek' in task_options %} - - dayweek: '{{ task_options.dayweek }}' - {%- endif %} + {%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} + {%- if section in task_options %} + - {{ section }}: '{{ task_options.get(section) }}' + {%- endif %} + {%- endfor %} {%- if 'commented' in task_options and task_options.commented %} - commented: True {%- endif %} - {%- if 'special' in task_options %} - - special: '{{ task_options.special }}' - {%- endif %} - - identifier: '{{ task }}' - {%- if 'comment' in task_options %} - - comment: {{ task_options.comment }} - {%- endif %} - {%- endfor %} {%- endif %} From 6f2b32355a1af12a5b0d46317dacc49f05e6b80b Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 22:00:12 +0100 Subject: [PATCH 17/29] test(saltcheck): test for `special` in `config.tst` as well --- cron/saltcheck-tests/config.tst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index c5ce251..ec2c2ce 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -16,7 +16,7 @@ validate_cron.{{ task }}_exists: assertion_section: identifier expected-return: {{ task }} -{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment'] %} +{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} {%- if section in task_options %} validate_cron.{{ task }}_{{ section }}: module_and_function: cron.get_entry From 33f344c3bae7a04dced88eef53f3d13ee01523f8 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 19:53:37 +0100 Subject: [PATCH 18/29] style(saltcheck): merge `absent` & `present` into one `if` block --- cron/saltcheck-tests/config.tst | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index ec2c2ce..5d6b8e1 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -6,7 +6,15 @@ {%- if 'tasks' in cron %} {%- for task, task_options in cron.tasks.items() %} -{%- if task_options.type == 'present' %} +{%- if task_options.type == 'absent' %} +validate_cron.{{ task }}_absent: + module_and_function: cron.get_entry + args: + - {{ task_options.user|default('root') }} + - {{ task }} + assertion: assertFalse + +{%- elif task_options.type == 'present' %} validate_cron.{{ task }}_exists: module_and_function: cron.get_entry args: @@ -40,14 +48,5 @@ validate_cron.{{ task }}_commented: {%- endif %} {%- endif %} -{%- if task_options.type == 'absent' %} -validate_cron.{{ task }}_absent: - module_and_function: cron.get_entry - args: - - {{ task_options.user|default('root') }} - - {{ task }} - assertion: assertFalse -{%- endif %} - {%- endfor %} {%- endif %} From 951a95980ab7fbf65475eb9619a595d51a4cfc29 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 15 Oct 2019 22:10:07 +0100 Subject: [PATCH 19/29] test(pillar): ensure `special` is being tested as well --- .travis.yml | 6 +++++- cron/saltcheck-tests/config.tst | 3 ++- test/salt/pillar/cron.sls | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 27992af..f799d7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,11 +16,15 @@ script: # yamllint disable rule:line-length # Converge and verify the instance; `saltcheck` relies upon the `converge` as well - bin/kitchen verify "${INSTANCE}" - # If a `develop` instance, get the appropriate version of `saltcheck.py` (temporary) + # If a `develop` instance, get the appropriate version of `saltcheck.py` & + # `cron.py` (temporary) - if [ ! -z $(echo "${INSTANCE}" | grep \\-develop-) ]; then bin/kitchen exec "${INSTANCE}" -c "sudo curl -o \$(find /usr/lib/ -type d -name modules | grep packages/salt/modules)/saltcheck.py https://raw.githubusercontent.com/myii/salt/fix/add-retcode/salt/modules/saltcheck.py"; + bin/kitchen exec "${INSTANCE}" -c + "sudo curl -o \$(find /usr/lib/ -type d -name modules | grep packages/salt/modules)/cron.py + https://raw.githubusercontent.com/myii/salt/fix/add-retcode/salt/modules/cron.py"; fi # If a `develop` instance, run all of the `saltcheck` tests - if [ ! -z $(echo "${INSTANCE}" | grep \\-develop-) ]; then diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 5d6b8e1..2206f55 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -24,7 +24,8 @@ validate_cron.{{ task }}_exists: assertion_section: identifier expected-return: {{ task }} -{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} +{#- Note: `special` is `spec` in the module #} +{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'spec'] %} {%- if section in task_options %} validate_cron.{{ task }}_{{ section }}: module_and_function: cron.get_entry diff --git a/test/salt/pillar/cron.sls b/test/salt/pillar/cron.sls index bad5a78..1d3e2b4 100644 --- a/test/salt/pillar/cron.sls +++ b/test/salt/pillar/cron.sls @@ -21,7 +21,7 @@ cron: minute: random hour: 1 task3: - type: absent + type: present name: echo task3 > /tmp/test3 user: root special: '@hourly' From a0f891e012f4863fe20634b60a4600027f19595e Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 19:27:23 +0100 Subject: [PATCH 20/29] refactor(jinja): used shortened form of `|default` filter --- cron/config/file.sls | 4 ++-- cron/saltcheck-tests/config.tst | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cron/config/file.sls b/cron/config/file.sls index 9e5510f..c5afe32 100644 --- a/cron/config/file.sls +++ b/cron/config/file.sls @@ -9,11 +9,11 @@ {%- for task, task_options in cron.tasks.items() %} cron.{{ task }}: - cron.{{ task_options.type|default('present') }}: + cron.{{ task_options.type|d('present') }}: - name: {{ task_options.name }} - identifier: '{{ task }}' {%- if 'user' in task_options %} - - user: {{ task_options.user|default('root') }} + - user: {{ task_options.user|d('root') }} {%- endif %} {%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %} {%- if section in task_options %} diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 2206f55..fe3646e 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -10,7 +10,7 @@ validate_cron.{{ task }}_absent: module_and_function: cron.get_entry args: - - {{ task_options.user|default('root') }} + - {{ task_options.user|d('root') }} - {{ task }} assertion: assertFalse @@ -18,7 +18,7 @@ validate_cron.{{ task }}_absent: validate_cron.{{ task }}_exists: module_and_function: cron.get_entry args: - - {{ task_options.user|default('root') }} + - {{ task_options.user|d('root') }} - {{ task }} assertion: assertEqual assertion_section: identifier @@ -30,7 +30,7 @@ validate_cron.{{ task }}_exists: validate_cron.{{ task }}_{{ section }}: module_and_function: cron.get_entry args: - - {{ task_options.user|default('root') }} + - {{ task_options.user|d('root') }} - {{ task }} assertion: assertEqual assertion_section: {{ section }} @@ -42,7 +42,7 @@ validate_cron.{{ task }}_{{ section }}: validate_cron.{{ task }}_commented: module_and_function: cron.get_entry args: - - {{ task_options.user|default('root') }} + - {{ task_options.user|d('root') }} - {{ task }} assertion: assertTrue assertion_section: commented From 3d0dcb2e4391f291c783cf4271092e6beac0c7c6 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 19:28:32 +0100 Subject: [PATCH 21/29] test(pillar): add test for `commented` and clarify each test --- test/salt/pillar/cron.sls | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/salt/pillar/cron.sls b/test/salt/pillar/cron.sls index 1d3e2b4..d7f6a77 100644 --- a/test/salt/pillar/cron.sls +++ b/test/salt/pillar/cron.sls @@ -4,6 +4,7 @@ cron: enabled: true # Default tasks: + # Test all of the available options task1: type: present # Default name: echo test > /tmp/test @@ -14,18 +15,22 @@ cron: month: 1 dayweek: 6 comment: comment1 + commented: false + # Test `type: absent` task2: type: absent # To remove that crontask name: echo task2 > /tmp/test2 user: root minute: random hour: 1 + # Test `special` task3: type: present name: echo task3 > /tmp/test3 user: root special: '@hourly' comment: comment3 + # Test `*` task4: type: present # run every 5 minutes name: echo task4 > /tmp/test4 @@ -33,3 +38,11 @@ cron: minute: '*/5' hour: '*' comment: comment4 + # Test `commented: true` and `minute: random` + task5: + type: present + name: echo task5 > /tmp/test5 + user: root + minute: random + hour: 1 + commented: true From 007970f9352dad1661df5ce8720fa62aaaab8018 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 19:44:50 +0100 Subject: [PATCH 22/29] test(saltcheck): add support for `random` values --- cron/saltcheck-tests/config.tst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index fe3646e..d928e9d 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -27,14 +27,20 @@ validate_cron.{{ task }}_exists: {#- 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: assertEqual + assertion: {{ assertion }} assertion_section: {{ section }} - expected-return: '{{ task_options.get(section) }}' + expected-return: '{{ expected }}' {%- endif %} {%- endfor %} From 50706119fae12ce5a504152827d8be0ced529422 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 19:49:00 +0100 Subject: [PATCH 23/29] test(saltcheck): test for `commented` and not `commented` --- cron/config/file.sls | 2 +- cron/saltcheck-tests/config.tst | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cron/config/file.sls b/cron/config/file.sls index c5afe32..66d5e8f 100644 --- a/cron/config/file.sls +++ b/cron/config/file.sls @@ -20,7 +20,7 @@ cron.{{ task }}: - {{ section }}: '{{ task_options.get(section) }}' {%- endif %} {%- endfor %} - {%- if 'commented' in task_options and task_options.commented %} + {%- if task_options.commented|d(False) %} - commented: True {%- endif %} {%- endfor %} diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index d928e9d..21f225d 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -44,15 +44,17 @@ validate_cron.{{ task }}_{{ section }}: {%- endif %} {%- endfor %} -{%- if 'commented' in task_options and task_options.commented %} +{%- set assertion = 'assertFalse' %} +{%- if task_options.commented|d(False) %} +{%- set assertion = 'assertTrue' %} +{%- endif %} validate_cron.{{ task }}_commented: module_and_function: cron.get_entry args: - {{ task_options.user|d('root') }} - {{ task }} - assertion: assertTrue + assertion: {{ assertion }} assertion_section: commented -{%- endif %} {%- endif %} {%- endfor %} From cce5e67c33b8e499c88ede4c800291879dccc70f Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 20:12:15 +0100 Subject: [PATCH 24/29] test(saltcheck): avoid `map.jinja`, use the test pillar instead --- cron/saltcheck-tests/config.tst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/saltcheck-tests/config.tst b/cron/saltcheck-tests/config.tst index 21f225d..c93ba6c 100644 --- a/cron/saltcheck-tests/config.tst +++ b/cron/saltcheck-tests/config.tst @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} +{%- set cron = salt['pillar.get']('cron', {}) %} {%- if 'tasks' in cron %} {%- for task, task_options in cron.tasks.items() %} From 18585bdc90b9f5c8f7078a45e963b1edc19093bf Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Thu, 17 Oct 2019 21:32:38 +0100 Subject: [PATCH 25/29] refactor(config): minimise and standardise between `.sls` & `.tst` --- cron/config/file.sls | 28 ++++++++-------- cron/saltcheck-tests/config.tst | 58 +++++++++++++-------------------- cron/saltcheck-tests/map.jinja | 47 -------------------------- 3 files changed, 35 insertions(+), 98 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 }}: +{%- if cron_type == 'present' %} +validate_cron.{{ task }}_commented: module_and_function: cron.get_entry args: - {{ task_options.user|d('root') }} - {{ task }} - assertion: {{ assertion }} - assertion_section: {{ section }} - expected-return: '{{ expected }}' -{%- endif %} -{%- endfor %} + assertion: {{ 'assertTrue' if task_options.commented|d(False) else 'assertFalse' }} + assertion_section: commented -{%- set assertion = 'assertFalse' %} -{%- if task_options.commented|d(False) %} -{%- set assertion = 'assertTrue' %} -{%- endif %} -validate_cron.{{ task }}_commented: +{#- 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: {{ assertion }} - assertion_section: commented -{%- endif %} + assertion: {{ 'assertLessEqual' if expected == 'random' else 'assertEqual' }} + assertion_section: {{ section }} + expected-return: '{{ 0 if expected == 'random' else expected }}' +{%- endif %} +{%- endfor %} +{%- 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 %} From 7e9e619a379b869d270458a99a6f9b04c3b2785d Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Fri, 18 Oct 2019 14:04:26 +0100 Subject: [PATCH 26/29] fix(saltcheck): replace `map.jinja` references with InSpec conditionals --- cron/saltcheck-tests/package.tst | 7 +++++-- cron/saltcheck-tests/service.tst | 24 +++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cron/saltcheck-tests/package.tst b/cron/saltcheck-tests/package.tst index cc8ffb5..5ea2cd0 100644 --- a/cron/saltcheck-tests/package.tst +++ b/cron/saltcheck-tests/package.tst @@ -1,10 +1,13 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} +{%- set package_name = 'cronie' %} +{%- if grains.os_family in ['Debian'] %} +{%- set package_name = 'cron' %} +{%- endif %} verify_cron.package: module_and_function: pkg.version args: - - {{ cron.pkg }} + - {{ package_name }} assertion: assertNotEmpty diff --git a/cron/saltcheck-tests/service.tst b/cron/saltcheck-tests/service.tst index d2cfe1a..cb971fd 100644 --- a/cron/saltcheck-tests/service.tst +++ b/cron/saltcheck-tests/service.tst @@ -1,29 +1,27 @@ # -*- coding: utf-8 -*- # vim: ft=sls -{%- from "cron/saltcheck-tests/map.jinja" import cron with context %} - -{%- set service_function = 'disabled' %} -{%- set service_running = 'assertFalse' %} -{%- if 'enabled' not in cron or ( 'enabled' in cron and cron.enabled ) %} -{%- set service_function = 'enabled' %} -{%- set service_running = 'assertTrue' %} +{%- set service_name = 'crond' %} +{%- if grains.os_family in ['Debian', 'Suse'] %} +{%- set service_name = 'cron' %} +{%- elif grains.os_family in ['Arch'] %} +{%- set service_name = 'cronie' %} {%- endif %} verify_cron.service_available: module_and_function: service.available args: - - {{ cron.service }} + - {{ service_name }} assertion: assertTrue -verify_cron.service_{{ service_function }}: - module_and_function: service.{{ service_function }} +verify_cron.service_enabled: + module_and_function: service.enabled args: - - {{ cron.service }} + - {{ service_name }} assertion: assertTrue verify_cron.service_running: module_and_function: service.status args: - - {{ cron.service }} - assertion: {{ service_running }} + - {{ service_name }} + assertion: assertTrue From ee652363ff9f88e60aef138096758fcd097a8cdb Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Sat, 19 Oct 2019 14:15:29 +0100 Subject: [PATCH 27/29] refactor(saltcheck): relocate `.tst` files according to 1:1 `.sls` files * https://github.com/saltstack-formulas/cron-formula/pull/4#issuecomment-544140377 --- .../config.tst => config/saltcheck-tests/file.tst} | 0 .../package.tst => package/saltcheck-tests/install.tst} | 0 .../service.tst => service/saltcheck-tests/running.tst} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename cron/{saltcheck-tests/config.tst => config/saltcheck-tests/file.tst} (100%) rename cron/{saltcheck-tests/package.tst => package/saltcheck-tests/install.tst} (100%) rename cron/{saltcheck-tests/service.tst => service/saltcheck-tests/running.tst} (100%) diff --git a/cron/saltcheck-tests/config.tst b/cron/config/saltcheck-tests/file.tst similarity index 100% rename from cron/saltcheck-tests/config.tst rename to cron/config/saltcheck-tests/file.tst diff --git a/cron/saltcheck-tests/package.tst b/cron/package/saltcheck-tests/install.tst similarity index 100% rename from cron/saltcheck-tests/package.tst rename to cron/package/saltcheck-tests/install.tst diff --git a/cron/saltcheck-tests/service.tst b/cron/service/saltcheck-tests/running.tst similarity index 100% rename from cron/saltcheck-tests/service.tst rename to cron/service/saltcheck-tests/running.tst From 6e54c3f4061ac8d72b8471af8f7a62bb332b3eb8 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 22 Oct 2019 01:04:56 +0100 Subject: [PATCH 28/29] refactor(saltcheck): use root-level `saltcheck-tests` directory --- cron/{config/saltcheck-tests => saltcheck-tests/config}/file.tst | 0 .../saltcheck-tests => saltcheck-tests/package}/install.tst | 0 .../saltcheck-tests => saltcheck-tests/service}/running.tst | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename cron/{config/saltcheck-tests => saltcheck-tests/config}/file.tst (100%) rename cron/{package/saltcheck-tests => saltcheck-tests/package}/install.tst (100%) rename cron/{service/saltcheck-tests => saltcheck-tests/service}/running.tst (100%) diff --git a/cron/config/saltcheck-tests/file.tst b/cron/saltcheck-tests/config/file.tst similarity index 100% rename from cron/config/saltcheck-tests/file.tst rename to cron/saltcheck-tests/config/file.tst diff --git a/cron/package/saltcheck-tests/install.tst b/cron/saltcheck-tests/package/install.tst similarity index 100% rename from cron/package/saltcheck-tests/install.tst rename to cron/saltcheck-tests/package/install.tst diff --git a/cron/service/saltcheck-tests/running.tst b/cron/saltcheck-tests/service/running.tst similarity index 100% rename from cron/service/saltcheck-tests/running.tst rename to cron/saltcheck-tests/service/running.tst From e23276b72a6702a2417ce6b836141f8140f9303b Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Wed, 23 Oct 2019 18:52:16 +0100 Subject: [PATCH 29/29] ci(travis): standardise `saltcheck` comments * Automated using https://github.com/myii/ssf-formula/pull/83 --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f799d7b..763738f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,9 @@ script: # yamllint disable rule:line-length # Converge and verify the instance; `saltcheck` relies upon the `converge` as well - bin/kitchen verify "${INSTANCE}" - # If a `develop` instance, get the appropriate version of `saltcheck.py` & - # `cron.py` (temporary) + # If a `develop` instance, get the appropriate version of `saltcheck.py` (temporary) + # Likewise, use a custom `cron.py` temporarily until the upstream PR is merged: + # - https://github.com/saltstack/salt/pull/55016 - if [ ! -z $(echo "${INSTANCE}" | grep \\-develop-) ]; then bin/kitchen exec "${INSTANCE}" -c "sudo curl -o \$(find /usr/lib/ -type d -name modules | grep packages/salt/modules)/saltcheck.py