Skip to content

Commit a50a2ba

Browse files
committed
refactor(map): use top-level values: key in map.jinja dumps
* Semi-automated using myii/ssf-formula#284
1 parent b3b8e5e commit a50a2ba

22 files changed

+649
-574
lines changed

TEMPLATE/_mapdata/_mapdata.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# yamllint disable rule:indentation rule:line-length
2-
# {{ grains.get('osfinger', grains.os) }}
2+
# {{ grains.get("osfinger", grains.os) }}
33
---
44
{#- use salt.slsutil.serialize to avoid encoding errors on some platforms #}
5-
{{ salt['slsutil.serialize'](
6-
'yaml',
5+
{{ salt["slsutil.serialize"](
6+
"yaml",
77
map,
88
default_flow_style=False,
99
allow_unicode=True,

TEMPLATE/_mapdata/init.sls

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
# vim: ft=sls
33
---
44
{#- Get the `tplroot` from `tpldir` #}
5-
{%- set tplroot = tpldir.split('/')[0] %}
6-
{%- from tplroot ~ "/map.jinja" import TEMPLATE as mapdata with context %}
5+
{%- set tplroot = tpldir.split("/")[0] %}
6+
{%- from tplroot ~ "/map.jinja" import TEMPLATE with context %}
77
8-
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
8+
{%- set _mapdata = {
9+
"values": {
10+
"TEMPLATE": TEMPLATE,
11+
}
12+
} %}
13+
{%- do salt["log.debug"]("### MAP.JINJA DUMP ###\n" ~ _mapdata | yaml(False)) %}
914
10-
{%- set output_dir = '/temp' if grains.os_family == 'Windows' else '/tmp' %}
11-
{%- set output_file = output_dir ~ '/salt_mapdata_dump.yaml' %}
15+
{%- set output_dir = "/temp" if grains.os_family == "Windows" else "/tmp" %}
16+
{%- set output_file = output_dir ~ "/salt_mapdata_dump.yaml" %}
1217
1318
{{ tplroot }}-mapdata-dump:
1419
file.managed:
1520
- name: {{ output_file }}
1621
- source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja
1722
- template: jinja
1823
- context:
19-
map: {{ mapdata | yaml }}
24+
map: {{ _mapdata | yaml }}

bin/convert-formula.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ convert_formula() {
6868
sedi "s/\([[:space:]]\{1,\}\)TEMPLATE:/\1${NEW_NAME_PYSAFE}:/" "$filename"
6969
done
7070

71+
# Temporarily, until the v5 `map.jinja` is implemented for this formula, this
72+
# specific py-safe replacement is also required
73+
sedi "s/TEMPLATE/${NEW_NAME_PYSAFE}/g" "${NEW_NAME}/_mapdata/init.sls"
74+
# However, this section will probably be needed even for the v5 `map.jinja`
75+
# All of the YAML comparison files need the new py-safe top-level key
76+
git ls-files -- 'test/integration/*.yaml' \
77+
| while read -r filename; do
78+
sedi "/^\( \)TEMPLATE:$/s//\1${NEW_NAME_PYSAFE}:/" "$filename"
79+
done
80+
7181
# Replace all other instances of TEMPLATE with the regular new formula name
7282
grep --recursive --files-with-matches --exclude-dir=.git TEMPLATE . \
7383
| while read -r filename; do

test/integration/default/controls/_mapdata_spec.rb

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,43 @@
55
control '`map.jinja` YAML dump' do
66
title 'should match the comparison file'
77

8-
# Strip the `platform[:finger]` version number down to the "OS major release"
9-
mapdata_file = "_mapdata/#{system.platform[:finger].split('.').first}.yaml"
8+
### Method
9+
# The steps below for each file appear convoluted but they are both required
10+
# and similar in nature:
11+
# 1. The earliest method was to simply compare the files textually but this often
12+
# led to false positives due to inconsistencies (e.g. spacing, ordering)
13+
# 2. The next method was to load the files back into YAML structures and then
14+
# compare but InSpec provided block diffs this way, unusable by end users
15+
# 3. The final step was to dump the YAML structures back into a string to use
16+
# for the comparison; this both worked and provided human-friendly diffs
1017

11-
# Load the mapdata from profile https://docs.chef.io/inspec/profiles/#profile-files
12-
mapdata_dump = YAML.safe_load(inspec.profile.file(mapdata_file))
18+
### Comparison file for the specific platform
19+
### Static, adjusted as part of code contributions, as map data is changed
20+
# Strip the `platform[:finger]` version number down to the "OS major release"
21+
platform_finger = system.platform[:finger].split('.').first.to_s
22+
# Use that to set the path to the file (relative to the InSpec suite directory)
23+
mapdata_file_path = "_mapdata/#{platform_finger}.yaml"
24+
# Load the mapdata from profile, into a YAML structure
25+
# https://docs.chef.io/inspec/profiles/#profile-files
26+
mapdata_file_yaml = YAML.safe_load(inspec.profile.file(mapdata_file_path))
27+
# Dump the YAML back into a string for comparison
28+
mapdata_file_dump = YAML.dump(mapdata_file_yaml)
1329

14-
# Derive the location of the dumped mapdata
30+
### Output file produced by running the `_mapdata` state
31+
### Dynamic, generated during Kitchen's `converge` phase
32+
# Derive the location of the dumped mapdata (differs for Windows)
1533
output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
16-
output_file = "#{output_dir}/salt_mapdata_dump.yaml"
34+
# Use that to set the path to the file (absolute path, i.e. within the container)
35+
output_file_path = "#{output_dir}/salt_mapdata_dump.yaml"
36+
# Load the output into a YAML structure using InSpec's `yaml` resource
37+
# https://github.com/inspec/inspec/blob/49b7d10/lib/inspec/resources/yaml.rb#L29
38+
output_file_yaml = yaml(output_file_path).params
39+
# Dump the YAML back into a string for comparison
40+
output_file_dump = YAML.dump(output_file_yaml)
1741

1842
describe 'File content' do
1943
it 'should match profile map data exactly' do
20-
expect(yaml(output_file).params).to eq(mapdata_dump)
44+
expect(output_file_dump).to eq(mapdata_file_dump)
2145
end
2246
end
2347
end
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# yamllint disable rule:indentation rule:line-length
22
# Amazon Linux AMI-2018
33
---
4-
added_in_defaults: defaults_value
5-
added_in_lookup: lookup_value
6-
added_in_pillar: pillar_value
7-
arch: amd64
8-
config: /etc/template-formula.conf
9-
lookup:
10-
added_in_lookup: lookup_value
11-
master: template-master
12-
winner: lookup
13-
master: template-master
14-
pkg:
15-
name: cronie
16-
rootgroup: root
17-
service:
18-
name: crond
19-
subcomponent:
20-
config: /etc/TEMPLATE-subcomponent-formula.conf
21-
tofs:
22-
files_switch:
23-
- any/path/can/be/used/here
24-
- id
25-
- roles
26-
- osfinger
27-
- os
28-
- os_family
29-
source_files:
30-
TEMPLATE-config-file-file-managed:
31-
- example.tmpl.jinja
32-
TEMPLATE-subcomponent-config-file-file-managed:
33-
- subcomponent-example.tmpl.jinja
34-
winner: pillar
4+
values:
5+
TEMPLATE:
6+
added_in_defaults: defaults_value
7+
added_in_lookup: lookup_value
8+
added_in_pillar: pillar_value
9+
arch: amd64
10+
config: /etc/template-formula.conf
11+
lookup:
12+
added_in_lookup: lookup_value
13+
master: template-master
14+
winner: lookup
15+
master: template-master
16+
pkg:
17+
name: cronie
18+
rootgroup: root
19+
service:
20+
name: crond
21+
subcomponent:
22+
config: /etc/TEMPLATE-subcomponent-formula.conf
23+
tofs:
24+
files_switch:
25+
- any/path/can/be/used/here
26+
- id
27+
- roles
28+
- osfinger
29+
- os
30+
- os_family
31+
source_files:
32+
TEMPLATE-config-file-file-managed:
33+
- example.tmpl.jinja
34+
TEMPLATE-subcomponent-config-file-file-managed:
35+
- subcomponent-example.tmpl.jinja
36+
winner: pillar
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# yamllint disable rule:indentation rule:line-length
22
# Amazon Linux-2
33
---
4-
added_in_defaults: defaults_value
5-
added_in_lookup: lookup_value
6-
added_in_pillar: pillar_value
7-
arch: amd64
8-
config: /etc/template-formula.conf
9-
lookup:
10-
added_in_lookup: lookup_value
11-
master: template-master
12-
winner: lookup
13-
master: template-master
14-
pkg:
15-
name: bash
16-
rootgroup: root
17-
service:
18-
name: systemd-journald
19-
subcomponent:
20-
config: /etc/TEMPLATE-subcomponent-formula.conf
21-
tofs:
22-
files_switch:
23-
- any/path/can/be/used/here
24-
- id
25-
- roles
26-
- osfinger
27-
- os
28-
- os_family
29-
source_files:
30-
TEMPLATE-config-file-file-managed:
31-
- example.tmpl.jinja
32-
TEMPLATE-subcomponent-config-file-file-managed:
33-
- subcomponent-example.tmpl.jinja
34-
winner: pillar
4+
values:
5+
TEMPLATE:
6+
added_in_defaults: defaults_value
7+
added_in_lookup: lookup_value
8+
added_in_pillar: pillar_value
9+
arch: amd64
10+
config: /etc/template-formula.conf
11+
lookup:
12+
added_in_lookup: lookup_value
13+
master: template-master
14+
winner: lookup
15+
master: template-master
16+
pkg:
17+
name: bash
18+
rootgroup: root
19+
service:
20+
name: systemd-journald
21+
subcomponent:
22+
config: /etc/TEMPLATE-subcomponent-formula.conf
23+
tofs:
24+
files_switch:
25+
- any/path/can/be/used/here
26+
- id
27+
- roles
28+
- osfinger
29+
- os
30+
- os_family
31+
source_files:
32+
TEMPLATE-config-file-file-managed:
33+
- example.tmpl.jinja
34+
TEMPLATE-subcomponent-config-file-file-managed:
35+
- subcomponent-example.tmpl.jinja
36+
winner: pillar
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# yamllint disable rule:indentation rule:line-length
22
# Arch
33
---
4-
added_in_defaults: defaults_value
5-
added_in_lookup: lookup_value
6-
added_in_pillar: pillar_value
7-
arch: amd64
8-
config: /etc/template-formula.conf
9-
lookup:
10-
added_in_lookup: lookup_value
11-
master: template-master
12-
winner: lookup
13-
master: template-master
14-
pkg:
15-
name: bash
16-
rootgroup: root
17-
service:
18-
name: systemd-journald
19-
subcomponent:
20-
config: /etc/TEMPLATE-subcomponent-formula.conf
21-
tofs:
22-
files_switch:
23-
- any/path/can/be/used/here
24-
- id
25-
- roles
26-
- osfinger
27-
- os
28-
- os_family
29-
source_files:
30-
TEMPLATE-config-file-file-managed:
31-
- example.tmpl.jinja
32-
TEMPLATE-subcomponent-config-file-file-managed:
33-
- subcomponent-example.tmpl.jinja
34-
winner: pillar
4+
values:
5+
TEMPLATE:
6+
added_in_defaults: defaults_value
7+
added_in_lookup: lookup_value
8+
added_in_pillar: pillar_value
9+
arch: amd64
10+
config: /etc/template-formula.conf
11+
lookup:
12+
added_in_lookup: lookup_value
13+
master: template-master
14+
winner: lookup
15+
master: template-master
16+
pkg:
17+
name: bash
18+
rootgroup: root
19+
service:
20+
name: systemd-journald
21+
subcomponent:
22+
config: /etc/TEMPLATE-subcomponent-formula.conf
23+
tofs:
24+
files_switch:
25+
- any/path/can/be/used/here
26+
- id
27+
- roles
28+
- osfinger
29+
- os
30+
- os_family
31+
source_files:
32+
TEMPLATE-config-file-file-managed:
33+
- example.tmpl.jinja
34+
TEMPLATE-subcomponent-config-file-file-managed:
35+
- subcomponent-example.tmpl.jinja
36+
winner: pillar
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# yamllint disable rule:indentation rule:line-length
22
# CentOS-6
33
---
4-
added_in_defaults: defaults_value
5-
added_in_lookup: lookup_value
6-
added_in_pillar: pillar_value
7-
arch: amd64
8-
config: /etc/template-formula.conf
9-
lookup:
10-
added_in_lookup: lookup_value
11-
master: template-master
12-
winner: lookup
13-
master: template-master
14-
pkg:
15-
name: cronie
16-
rootgroup: root
17-
service:
18-
name: crond
19-
subcomponent:
20-
config: /etc/TEMPLATE-subcomponent-formula.conf
21-
tofs:
22-
files_switch:
23-
- any/path/can/be/used/here
24-
- id
25-
- roles
26-
- osfinger
27-
- os
28-
- os_family
29-
source_files:
30-
TEMPLATE-config-file-file-managed:
31-
- example.tmpl.jinja
32-
TEMPLATE-subcomponent-config-file-file-managed:
33-
- subcomponent-example.tmpl.jinja
34-
winner: pillar
4+
values:
5+
TEMPLATE:
6+
added_in_defaults: defaults_value
7+
added_in_lookup: lookup_value
8+
added_in_pillar: pillar_value
9+
arch: amd64
10+
config: /etc/template-formula.conf
11+
lookup:
12+
added_in_lookup: lookup_value
13+
master: template-master
14+
winner: lookup
15+
master: template-master
16+
pkg:
17+
name: cronie
18+
rootgroup: root
19+
service:
20+
name: crond
21+
subcomponent:
22+
config: /etc/TEMPLATE-subcomponent-formula.conf
23+
tofs:
24+
files_switch:
25+
- any/path/can/be/used/here
26+
- id
27+
- roles
28+
- osfinger
29+
- os
30+
- os_family
31+
source_files:
32+
TEMPLATE-config-file-file-managed:
33+
- example.tmpl.jinja
34+
TEMPLATE-subcomponent-config-file-file-managed:
35+
- subcomponent-example.tmpl.jinja
36+
winner: pillar

0 commit comments

Comments
 (0)