Skip to content

Commit ee89ee0

Browse files
authored
Merge pull request #281 from myii/feat/manage-map.jinja-verification-files
feat(_mapdata): manage `map.jinja` verification
2 parents ca53465 + 47acb81 commit ee89ee0

File tree

11 files changed

+224
-44
lines changed

11 files changed

+224
-44
lines changed

pillar.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,12 @@ ssf:
178178
- bin/kitchen
179179
- docs/CONTRIBUTING.rst
180180
- docs/TOFS_pattern.rst
181+
- formula/_mapdata/_mapdata.jinja
182+
- formula/_mapdata/init.sls
181183
- formula/libsaltcli.jinja
182184
- formula/libtofs.jinja
185+
- inspec/controls/_mapdata_spec.rb
186+
- inspec/libraries/system.rb
183187
- inspec/inspec.yml
184188
- inspec/README.md
185189
- .gitignore

ssf/config/formulas.sls

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,19 @@ prepare-git-branch-for-{{ formula }}:
7979
{%- do matching_test_suite.update({'found': True}) %}
8080
{%- endfor %}
8181
{#- Now use that to set the `dest_file` accordingly #}
82-
{%- if not matching_test_suite.found %}
82+
{#- Start by stripping out the `inspec/` prefix from the path #}
83+
{%- set dest_file = dest_file.replace('inspec/', '') %}
84+
{#- Do not manage the file in the following situations: #}
85+
{#- - If a matching test suite isn't found #}
86+
{#- - Or if `libraries/system.rb` and is not the `share` suite #}
87+
{#- - Or if `controls/_mapdata_spec.rb` and is the `share` suite #}
88+
{%- if (not matching_test_suite.found) or
89+
(dest_file == 'libraries/system.rb' and suite.name != 'share') or
90+
(dest_file == 'controls/_mapdata_spec.rb' and suite.name == 'share')
91+
%}
8392
{%- set dest_file = '' %}
8493
{%- else %}
85-
{%- set dest_file = '{0}/{1}/{2}'.format(inspec_tests_path_prefix, suite.name, dest_file.split('/')[-1]) %}
94+
{%- set dest_file = '{0}/{1}/{2}'.format(inspec_tests_path_prefix, suite.name, dest_file) %}
8695
{%- endif %}
8796
{%- endif %}
8897
{%- set dest = '{0}/{1}/{2}'.format(ssf.formulas_path, formula, dest_file) %}
@@ -140,6 +149,7 @@ prepare-git-branch-for-{{ formula }}:
140149
inspec_suites_kitchen: {{ inspec_suites_kitchen | yaml }}
141150
inspec_suites_matrix: {{ context.inspec_suites_matrix | yaml }}
142151
kitchen: {{ context.kitchen | yaml }}
152+
map_jinja: {{ context.map_jinja | yaml }}
143153
platforms: {{ context.platforms | yaml }}
144154
platforms_matrix: {{ context.platforms_matrix | yaml }}
145155
platforms_matrix_commented_includes: {{ context.platforms_matrix_commented_includes | yaml }}

ssf/defaults.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ ssf_node_anchors:
2222
- '/docs/AUTHORS.rst': '@saltstack-formulas/ssf'
2323
- '/docs/CHANGELOG.rst': '@saltstack-formulas/ssf'
2424
- '/docs/TOFS_pattern.rst': '@saltstack-formulas/ssf'
25+
- '/*/_mapdata/': '@saltstack-formulas/ssf'
2526
- '/*/libsaltcli.jinja': '@saltstack-formulas/ssf'
2627
- '/*/libtofs.jinja': '@saltstack-formulas/ssf'
28+
- '/test/integration/**/_mapdata_spec.rb': '@saltstack-formulas/ssf'
29+
- '/test/integration/**/libraries/system.rb': '@saltstack-formulas/ssf'
2730
- '/test/integration/**/inspec.yml': '@saltstack-formulas/ssf'
2831
- '/test/integration/**/README.md': '@saltstack-formulas/ssf'
2932
- '/.gitignore': '@saltstack-formulas/ssf'
@@ -59,8 +62,8 @@ ssf_node_anchors:
5962
# An alternative method could be to use:
6063
# `git describe --abbrev=0 --tags`
6164
# yamllint disable rule:line-length rule:quoted-strings
62-
title: "ci(kitchen+gitlab): adjust matrix to add '`'3002'`'"
63-
body: '* Semi-automated using https://github.com/myii/ssf-formula/pull/280'
65+
title: "test(map): standardise '`'map.jinja'`' verification"
66+
body: '* Automated using https://github.com/myii/ssf-formula/pull/281'
6467
# yamllint enable rule:line-length rule:quoted-strings
6568
github:
6669
owner: 'saltstack-formulas'
@@ -128,6 +131,8 @@ ssf_node_anchors:
128131
kitchen:
129132
driver:
130133
run_options: {}
134+
map_jinja:
135+
verification: {}
131136
platforms:
132137
# Could use `opensuse-leap` throughout since `/` never used at this end
133138
# Would have to modify the `if` in the `kitchen.yml` template(s), though
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# yamllint disable rule:indentation rule:line-length
2+
# {{ grains.get('osfinger', grains.os) }}
3+
---
4+
{#- use salt.slsutil.serialize to avoid encoding errors on some platforms #}
5+
{{ salt['slsutil.serialize'](
6+
'yaml',
7+
map,
8+
default_flow_style=False,
9+
allow_unicode=True,
10+
)
11+
| regex_replace("^\s+'$", "'", multiline=True)
12+
| trim
13+
}}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
${ '# -*- coding: utf-8 -*-' }
2+
${ '# vim: ft=sls' }
3+
---
4+
{#- Get the `tplroot` from `tpldir` #}
5+
{%- set tplroot = tpldir.split('/')[0] %}
6+
<% import_var = map_jinja['verification']['import'] %>\
7+
<% import_var_as = '' if import_var == 'mapdata' else ' as mapdata' %>\
8+
{%- from tplroot ~ "/map.jinja" import ${ import_var }${ import_var_as } with context %}
9+
10+
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
11+
12+
{%- set output_dir = '/temp' if grains.os_family == 'Windows' else '/tmp' %}
13+
{%- set output_file = output_dir ~ '/salt_mapdata_dump.yaml' %}
14+
15+
{{ tplroot }}-mapdata-dump:
16+
file.managed:
17+
- name: {{ output_file }}
18+
- source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja
19+
- template: jinja
20+
- context:
21+
map: {{ mapdata | yaml }}

ssf/files/default/inspec/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ Its goal is to share the libraries between all profiles.
1212
The `system` library provides easy access to system dependent information:
1313

1414
- `system.platform`: based on `inspec.platform`, modify to values that are more consistent from a SaltStack perspective
15-
- `system.platform[:family]` provide a family name for Arch
16-
- `system.platform[:name]` modify `amazon` to `amazonlinux`
17-
- `system.platform[:release]` tweak Arch and Amazon Linux:
15+
- `system.platform[:family]` provide a family name for Arch and Gentoo
16+
- `system.platform[:name]` append `linux` to both `amazon` and `oracle`; ensure Windows platforms are resolved as simply `windows`
17+
- `system.platform[:release]` tweak Arch, Amazon Linux, Gentoo and Windows:
1818
- `Arch` is always `base-latest`
1919
- `Amazon Linux` release `2018` is resolved as `1`
20+
- `Gentoo` release is trimmed to its major version number and then the init system is appended (i.e. `sysv` or `sysd`)
21+
- `Windows` uses the widely-used release number (e.g. `8.1` or `2019-server`) in place of the actual system release version
2022
- `system.platform[:finger]` is the concatenation of the name and the major release number (except for Ubuntu, which gives `ubuntu-20.04` for example)
2123
{%- else %}
2224

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'yaml'
4+
5+
control '`map.jinja` YAML dump' do
6+
title 'should match the comparison file'
7+
8+
# Strip the `platform[:finger]` version number down to the "OS major release"
9+
mapdata_file = "_mapdata/#{system.platform[:finger].split('.').first}.yaml"
10+
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))
13+
14+
# Derive the location of the dumped mapdata
15+
output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
16+
output_file = "#{output_dir}/salt_mapdata_dump.yaml"
17+
18+
describe 'File content' do
19+
it 'should match profile map data exactly' do
20+
expect(yaml(output_file).params).to eq(mapdata_dump)
21+
end
22+
end
23+
end
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# frozen_string_literal: true
2+
3+
# system.rb -- InSpec resources for system values
4+
# Author: Daniel Dehennin <[email protected]>
5+
# Copyright (C) 2020 Daniel Dehennin <[email protected]>
6+
7+
class SystemResource < Inspec.resource(1)
8+
name 'system'
9+
10+
attr_reader :platform
11+
12+
def initialize
13+
super
14+
@platform = build_platform
15+
end
16+
17+
private
18+
19+
def build_platform
20+
{
21+
family: build_platform_family,
22+
name: build_platform_name,
23+
release: build_platform_release,
24+
finger: build_platform_finger
25+
}
26+
end
27+
28+
def build_platform_family
29+
case inspec.platform[:name]
30+
when 'arch', 'gentoo'
31+
inspec.platform[:name]
32+
else
33+
inspec.platform[:family]
34+
end
35+
end
36+
37+
def build_platform_name
38+
case inspec.platform[:name]
39+
when 'amazon', 'oracle'
40+
"#{inspec.platform[:name]}linux"
41+
when 'windows_8.1_pro', 'windows_server_2019_datacenter'
42+
'windows'
43+
else
44+
inspec.platform[:name]
45+
end
46+
end
47+
48+
# rubocop:disable Metrics/MethodLength
49+
def build_platform_release
50+
case inspec.platform[:name]
51+
when 'amazon'
52+
# `2018` relase is named `1` in kitchen.yaml
53+
inspec.platform[:release].gsub(/2018.*/, '1')
54+
when 'arch'
55+
'base-latest'
56+
when 'gentoo'
57+
"#{inspec.platform[:release].split('.')[0]}-#{derive_gentoo_init_system}"
58+
when 'windows_8.1_pro'
59+
'8.1'
60+
when 'windows_server_2019_datacenter'
61+
'2019-server'
62+
else
63+
inspec.platform[:release]
64+
end
65+
end
66+
# rubocop:enable Metrics/MethodLength
67+
68+
def derive_gentoo_init_system
69+
case inspec.command('systemctl').exist?
70+
when true
71+
'sysd'
72+
else
73+
'sysv'
74+
end
75+
end
76+
77+
def build_platform_finger
78+
"#{build_platform_name}-#{build_finger_release}"
79+
end
80+
81+
def build_finger_release
82+
case inspec.platform[:name]
83+
when 'ubuntu'
84+
build_platform_release.split('.').slice(0, 2).join('.')
85+
else
86+
build_platform_release.split('.')[0]
87+
end
88+
end
89+
end

ssf/files/tofs_openntpd-formula/inspec/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

ssf/files/tofs_sudoers-formula/inspec/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)