Skip to content

Commit e9591db

Browse files
committed
test(map): verify map.jinja dump using _mapdata state
* Automated using myii/ssf-formula#283
1 parent 6ea8950 commit e9591db

File tree

9 files changed

+196
-0
lines changed

9 files changed

+196
-0
lines changed

CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
/docs/AUTHORS.rst @saltstack-formulas/ssf
1616
/docs/CHANGELOG.rst @saltstack-formulas/ssf
1717
/docs/TOFS_pattern.rst @saltstack-formulas/ssf
18+
/*/_mapdata/ @saltstack-formulas/ssf
1819
/*/libsaltcli.jinja @saltstack-formulas/ssf
1920
/*/libtofs.jinja @saltstack-formulas/ssf
21+
/test/integration/**/_mapdata_spec.rb @saltstack-formulas/ssf
22+
/test/integration/**/libraries/system.rb @saltstack-formulas/ssf
2023
/test/integration/**/inspec.yml @saltstack-formulas/ssf
2124
/test/integration/**/README.md @saltstack-formulas/ssf
2225
/.gitignore @saltstack-formulas/ssf

kitchen.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ suites:
162162
state_top:
163163
base:
164164
'*':
165+
- php._mapdata
165166
- php.repo
166167
- php.fpm.install
167168
- php.fpm.config
@@ -188,6 +189,7 @@ suites:
188189
state_top:
189190
base:
190191
'*':
192+
- php._mapdata
191193
- php.repo
192194
- php.fpm.install
193195
- php.fpm.config
@@ -225,6 +227,7 @@ suites:
225227
state_top:
226228
base:
227229
'*':
230+
- php._mapdata
228231
- php
229232
pillars:
230233
top.sls:
@@ -246,6 +249,7 @@ suites:
246249
state_top:
247250
base:
248251
'*':
252+
- php._mapdata
249253
- php
250254
pillars:
251255
top.sls:

php/_mapdata/_mapdata.jinja

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

php/_mapdata/init.sls

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
3+
---
4+
{#- Get the `tplroot` from `tpldir` #}
5+
{%- set tplroot = tpldir.split('/')[0] %}
6+
{%- from tplroot ~ "/map.jinja" import php as mapdata with context %}
7+
8+
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
9+
10+
{%- set output_dir = '/temp' if grains.os_family == 'Windows' else '/tmp' %}
11+
{%- set output_file = output_dir ~ '/salt_mapdata_dump.yaml' %}
12+
13+
{{ tplroot }}-mapdata-dump:
14+
file.managed:
15+
- name: {{ output_file }}
16+
- source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja
17+
- template: jinja
18+
- context:
19+
map: {{ mapdata | yaml }}
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

test/integration/default/inspec.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ title: php formula
66
maintainer: SaltStack Formulas
77
license: Apache-2.0
88
summary: Verify that the php formula is setup and configured correctly
9+
depends:
10+
- name: share
11+
path: test/integration/share
912
supports:
1013
- platform-name: debian
1114
- platform-name: ubuntu

test/integration/share/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# InSpec Profile: `share`
2+
3+
This shows the implementation of the `share` InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).
4+
5+
Its goal is to share the libraries between all profiles.
6+
7+
## Libraries
8+
9+
### `system`
10+
11+
The `system` library provides easy access to system dependent information:
12+
13+
- `system.platform`: based on `inspec.platform`, modify to values that are more consistent from a SaltStack perspective
14+
- `system.platform[:family]` provide a family name for Arch and Gentoo
15+
- `system.platform[:name]` append `linux` to both `amazon` and `oracle`; ensure Windows platforms are resolved as simply `windows`
16+
- `system.platform[:release]` tweak Arch, Amazon Linux, Gentoo and Windows:
17+
- `Arch` is always `base-latest`
18+
- `Amazon Linux` release `2018` is resolved as `1`
19+
- `Gentoo` release is trimmed to its major version number and then the init system is appended (i.e. `sysv` or `sysd`)
20+
- `Windows` uses the widely-used release number (e.g. `8.1` or `2019-server`) in place of the actual system release version
21+
- `system.platform[:finger]` is the concatenation of the name and the major release number (except for Ubuntu, which gives `ubuntu-20.04` for example)

test/integration/share/inspec.yml

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=yaml
3+
---
4+
name: share
5+
title: InSpec shared resources
6+
maintainer: SaltStack Formulas
7+
license: Apache-2.0
8+
summary: shared resources
9+
supports:
10+
- platform-name: debian
11+
- platform-name: ubuntu
12+
- platform-name: centos
13+
- platform-name: fedora
14+
- platform-name: opensuse
15+
- platform-name: suse
16+
- platform-name: freebsd
17+
- platform-name: amazon
18+
- platform-name: oracle
19+
- platform-name: arch
20+
- platform-name: gentoo
21+
- platform: windows
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

0 commit comments

Comments
 (0)