From e9591db3551073ad9b7a747b848702dc4f75a22c Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 22 Dec 2020 14:30:35 +0000 Subject: [PATCH] test(map): verify `map.jinja` dump using `_mapdata` state * Automated using https://github.com/myii/ssf-formula/pull/283 --- CODEOWNERS | 3 + kitchen.yml | 4 + php/_mapdata/_mapdata.jinja | 13 +++ php/_mapdata/init.sls | 19 ++++ .../default/controls/_mapdata_spec.rb | 23 +++++ test/integration/default/inspec.yml | 3 + test/integration/share/README.md | 21 +++++ test/integration/share/inspec.yml | 21 +++++ test/integration/share/libraries/system.rb | 89 +++++++++++++++++++ 9 files changed, 196 insertions(+) create mode 100644 php/_mapdata/_mapdata.jinja create mode 100644 php/_mapdata/init.sls create mode 100644 test/integration/default/controls/_mapdata_spec.rb create mode 100644 test/integration/share/README.md create mode 100644 test/integration/share/inspec.yml create mode 100644 test/integration/share/libraries/system.rb diff --git a/CODEOWNERS b/CODEOWNERS index 05c8e5d..e09bf98 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -15,8 +15,11 @@ /docs/AUTHORS.rst @saltstack-formulas/ssf /docs/CHANGELOG.rst @saltstack-formulas/ssf /docs/TOFS_pattern.rst @saltstack-formulas/ssf +/*/_mapdata/ @saltstack-formulas/ssf /*/libsaltcli.jinja @saltstack-formulas/ssf /*/libtofs.jinja @saltstack-formulas/ssf +/test/integration/**/_mapdata_spec.rb @saltstack-formulas/ssf +/test/integration/**/libraries/system.rb @saltstack-formulas/ssf /test/integration/**/inspec.yml @saltstack-formulas/ssf /test/integration/**/README.md @saltstack-formulas/ssf /.gitignore @saltstack-formulas/ssf diff --git a/kitchen.yml b/kitchen.yml index b1020c6..ab2fb62 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -162,6 +162,7 @@ suites: state_top: base: '*': + - php._mapdata - php.repo - php.fpm.install - php.fpm.config @@ -188,6 +189,7 @@ suites: state_top: base: '*': + - php._mapdata - php.repo - php.fpm.install - php.fpm.config @@ -225,6 +227,7 @@ suites: state_top: base: '*': + - php._mapdata - php pillars: top.sls: @@ -246,6 +249,7 @@ suites: state_top: base: '*': + - php._mapdata - php pillars: top.sls: diff --git a/php/_mapdata/_mapdata.jinja b/php/_mapdata/_mapdata.jinja new file mode 100644 index 0000000..ad54eaa --- /dev/null +++ b/php/_mapdata/_mapdata.jinja @@ -0,0 +1,13 @@ +# yamllint disable rule:indentation rule:line-length +# {{ grains.get('osfinger', grains.os) }} +--- +{#- use salt.slsutil.serialize to avoid encoding errors on some platforms #} +{{ salt['slsutil.serialize']( + 'yaml', + map, + default_flow_style=False, + allow_unicode=True, + ) + | regex_replace("^\s+'$", "'", multiline=True) + | trim +}} diff --git a/php/_mapdata/init.sls b/php/_mapdata/init.sls new file mode 100644 index 0000000..eaaad0a --- /dev/null +++ b/php/_mapdata/init.sls @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls +--- +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import php as mapdata with context %} + +{%- 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' %} + +{{ tplroot }}-mapdata-dump: + file.managed: + - name: {{ output_file }} + - source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja + - template: jinja + - context: + map: {{ mapdata | yaml }} diff --git a/test/integration/default/controls/_mapdata_spec.rb b/test/integration/default/controls/_mapdata_spec.rb new file mode 100644 index 0000000..2cced0a --- /dev/null +++ b/test/integration/default/controls/_mapdata_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'yaml' + +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" + + # Load the mapdata from profile https://docs.chef.io/inspec/profiles/#profile-files + mapdata_dump = YAML.safe_load(inspec.profile.file(mapdata_file)) + + # Derive the location of the dumped mapdata + output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp' + output_file = "#{output_dir}/salt_mapdata_dump.yaml" + + describe 'File content' do + it 'should match profile map data exactly' do + expect(yaml(output_file).params).to eq(mapdata_dump) + end + end +end diff --git a/test/integration/default/inspec.yml b/test/integration/default/inspec.yml index c338227..a2501a7 100644 --- a/test/integration/default/inspec.yml +++ b/test/integration/default/inspec.yml @@ -6,6 +6,9 @@ title: php formula maintainer: SaltStack Formulas license: Apache-2.0 summary: Verify that the php formula is setup and configured correctly +depends: + - name: share + path: test/integration/share supports: - platform-name: debian - platform-name: ubuntu diff --git a/test/integration/share/README.md b/test/integration/share/README.md new file mode 100644 index 0000000..5bc510c --- /dev/null +++ b/test/integration/share/README.md @@ -0,0 +1,21 @@ +# InSpec Profile: `share` + +This shows the implementation of the `share` InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md). + +Its goal is to share the libraries between all profiles. + +## Libraries + +### `system` + +The `system` library provides easy access to system dependent information: + +- `system.platform`: based on `inspec.platform`, modify to values that are more consistent from a SaltStack perspective + - `system.platform[:family]` provide a family name for Arch and Gentoo + - `system.platform[:name]` append `linux` to both `amazon` and `oracle`; ensure Windows platforms are resolved as simply `windows` + - `system.platform[:release]` tweak Arch, Amazon Linux, Gentoo and Windows: + - `Arch` is always `base-latest` + - `Amazon Linux` release `2018` is resolved as `1` + - `Gentoo` release is trimmed to its major version number and then the init system is appended (i.e. `sysv` or `sysd`) + - `Windows` uses the widely-used release number (e.g. `8.1` or `2019-server`) in place of the actual system release version + - `system.platform[:finger]` is the concatenation of the name and the major release number (except for Ubuntu, which gives `ubuntu-20.04` for example) diff --git a/test/integration/share/inspec.yml b/test/integration/share/inspec.yml new file mode 100644 index 0000000..697417a --- /dev/null +++ b/test/integration/share/inspec.yml @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# vim: ft=yaml +--- +name: share +title: InSpec shared resources +maintainer: SaltStack Formulas +license: Apache-2.0 +summary: shared resources +supports: + - platform-name: debian + - platform-name: ubuntu + - platform-name: centos + - platform-name: fedora + - platform-name: opensuse + - platform-name: suse + - platform-name: freebsd + - platform-name: amazon + - platform-name: oracle + - platform-name: arch + - platform-name: gentoo + - platform: windows diff --git a/test/integration/share/libraries/system.rb b/test/integration/share/libraries/system.rb new file mode 100644 index 0000000..ef23499 --- /dev/null +++ b/test/integration/share/libraries/system.rb @@ -0,0 +1,89 @@ +# frozen_string_literal: true + +# system.rb -- InSpec resources for system values +# Author: Daniel Dehennin +# Copyright (C) 2020 Daniel Dehennin + +class SystemResource < Inspec.resource(1) + name 'system' + + attr_reader :platform + + def initialize + super + @platform = build_platform + end + + private + + def build_platform + { + family: build_platform_family, + name: build_platform_name, + release: build_platform_release, + finger: build_platform_finger + } + end + + def build_platform_family + case inspec.platform[:name] + when 'arch', 'gentoo' + inspec.platform[:name] + else + inspec.platform[:family] + end + end + + def build_platform_name + case inspec.platform[:name] + when 'amazon', 'oracle' + "#{inspec.platform[:name]}linux" + when 'windows_8.1_pro', 'windows_server_2019_datacenter' + 'windows' + else + inspec.platform[:name] + end + end + + # rubocop:disable Metrics/MethodLength + def build_platform_release + case inspec.platform[:name] + when 'amazon' + # `2018` relase is named `1` in kitchen.yaml + inspec.platform[:release].gsub(/2018.*/, '1') + when 'arch' + 'base-latest' + when 'gentoo' + "#{inspec.platform[:release].split('.')[0]}-#{derive_gentoo_init_system}" + when 'windows_8.1_pro' + '8.1' + when 'windows_server_2019_datacenter' + '2019-server' + else + inspec.platform[:release] + end + end + # rubocop:enable Metrics/MethodLength + + def derive_gentoo_init_system + case inspec.command('systemctl').exist? + when true + 'sysd' + else + 'sysv' + end + end + + def build_platform_finger + "#{build_platform_name}-#{build_finger_release}" + end + + def build_finger_release + case inspec.platform[:name] + when 'ubuntu' + build_platform_release.split('.').slice(0, 2).join('.') + else + build_platform_release.split('.')[0] + end + end +end