Skip to content

Commit 46ad65e

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

14 files changed

+3782
-3731
lines changed

php/_mapdata/_mapdata.jinja

+3-3
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,

php/_mapdata/init.sls

+11-6
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 php as mapdata with context %}
5+
{%- set tplroot = tpldir.split("/")[0] %}
6+
{%- from tplroot ~ "/map.jinja" import php with context %}
77
8-
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
8+
{%- set _mapdata = {
9+
"values": {
10+
"php": php,
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 }}

test/integration/default/controls/_mapdata_spec.rb

+31-7
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

0 commit comments

Comments
 (0)