Skip to content

Commit

Permalink
Merge pull request #64 from myii/refactor/use-top-level-values-in-map…
Browse files Browse the repository at this point in the history
…-jinja-dumps

refactor(map): use top-level `values:` key in `map.jinja` dumps
  • Loading branch information
myii authored Dec 23, 2020
2 parents ee5aa71 + 7cff4de commit ecdf7c1
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 25 deletions.
6 changes: 3 additions & 3 deletions rabbitmq/_mapdata/_mapdata.jinja
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# yamllint disable rule:indentation rule:line-length
# {{ grains.get('osfinger', grains.os) }}
# {{ grains.get("osfinger", grains.os) }}
---
{#- use salt.slsutil.serialize to avoid encoding errors on some platforms #}
{{ salt['slsutil.serialize'](
'yaml',
{{ salt["slsutil.serialize"](
"yaml",
map,
default_flow_style=False,
allow_unicode=True,
Expand Down
17 changes: 11 additions & 6 deletions rabbitmq/_mapdata/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
# vim: ft=sls
---
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/package-map.jinja" import pkgs as mapdata with context %}
{%- set tplroot = tpldir.split("/")[0] %}
{%- from tplroot ~ "/package-map.jinja" import pkgs with context %}
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
{%- set _mapdata = {
"values": {
"pkgs": pkgs,
}
} %}
{%- do salt["log.debug"]("### MAP.JINJA DUMP ###\n" ~ _mapdata | yaml(False)) %}
{%- set output_dir = '/temp' if grains.os_family == 'Windows' else '/tmp' %}
{%- set output_file = output_dir ~ '/salt_mapdata_dump.yaml' %}
{%- set output_dir = "/temp" if grains.os_family == "Windows" else "/tmp" %}
{%- set output_file = output_dir ~ "/salt_mapdata_dump.yaml" %}
{{ tplroot }}-mapdata-dump:
file.managed:
- name: {{ output_file }}
- source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja
- template: jinja
- context:
map: {{ mapdata | yaml }}
map: {{ _mapdata | yaml }}
38 changes: 31 additions & 7 deletions test/integration/default/controls/_mapdata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,43 @@
control '`map.jinja` YAML dump' do
title 'should match the comparison file'

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

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

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

describe 'File content' do
it 'should match profile map data exactly' do
expect(yaml(output_file).params).to eq(mapdata_dump)
expect(output_file_dump).to eq(mapdata_file_dump)
end
end
end
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/centos-7.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# CentOS Linux-7
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/centos-8.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# CentOS Linux-8
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/debian-10.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Debian-10
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/debian-9.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Debian-9
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/fedora-30.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Fedora-30
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/fedora-31.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Fedora-31
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/opensuse-15.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Leap-15
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/ubuntu-16.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Ubuntu-16.04
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server
4 changes: 3 additions & 1 deletion test/integration/default/files/_mapdata/ubuntu-18.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# yamllint disable rule:indentation rule:line-length
# Ubuntu-18.04
---
rabbitmq-server: rabbitmq-server
values:
pkgs:
rabbitmq-server: rabbitmq-server

0 comments on commit ecdf7c1

Please sign in to comment.