Skip to content

Commit 9ae4fab

Browse files
authored
Merge pull request #222 from myii/refactor/use-top-level-values-in-map-jinja-dumps
refactor(map): use top-level `values:` key in `map.jinja` dumps
2 parents f1f94d4 + 46ad65e commit 9ae4fab

16 files changed

+3795
-3747
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ repos:
1717
stages: [manual]
1818
additional_dependencies: ['@commitlint/[email protected]']
1919
always_run: true
20+
- repo: https://github.com/adithyabsk/mirrors-rubocop
21+
rev: v0.91.0
22+
hooks:
23+
- id: rubocop
24+
name: Check Ruby files with rubocop
25+
args: [--debug]
26+
always_run: true
27+
pass_filenames: false
2028
- repo: https://github.com/jumanjihouse/pre-commit-hooks
2129
rev: 2.1.3
2230
hooks:

.travis.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@ stages:
3636
# - name: 'release'
3737
# if: 'branch = master AND type != pull_request'
3838
jobs:
39-
allow_failures:
40-
- env: Lint_rubocop
41-
fast_finish: true
4239
include:
4340
## Define the test stage that runs the linters (and testing matrix, if applicable)
4441

45-
# Run all of the linters in a single job (except `rubocop`)
42+
# Run all of the linters in a single job
4643
- language: 'node_js'
4744
node_js: 'lts/*'
4845
env: 'Lint'
49-
name: 'Lint: salt-lint, yamllint, shellcheck & commitlint'
46+
name: 'Lint: salt-lint, yamllint, rubocop, shellcheck & commitlint'
5047
before_install: 'skip'
5148
script:
5249
# Install and run `salt-lint`
@@ -57,6 +54,9 @@ jobs:
5754
# Need at least `v1.17.0` for the `yaml-files` setting
5855
- pip install --user yamllint>=1.17.0
5956
- yamllint -s .
57+
# Install and run `rubocop`
58+
- gem install rubocop
59+
- rubocop -d
6060
# Run `shellcheck` (already pre-installed in Travis)
6161
- shellcheck --version
6262
- git ls-files -- '*.sh' '*.bash' '*.ksh'
@@ -65,17 +65,6 @@ jobs:
6565
- npm i -D @commitlint/config-conventional
6666
@commitlint/travis-cli
6767
- commitlint-travis
68-
# Run the `rubocop` linter in a separate job that is allowed to fail
69-
# Once these lint errors are fixed, this can be merged into a single job
70-
- language: node_js
71-
node_js: lts/*
72-
env: Lint_rubocop
73-
name: 'Lint: rubocop'
74-
before_install: skip
75-
script:
76-
# Install and run `rubocop`
77-
- gem install rubocop
78-
- rubocop -d
7968

8069
# Run `pre-commit` linters in a single job
8170
- language: 'python'

php/_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,

php/_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 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

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

0 commit comments

Comments
 (0)