Merge pull request #203 from baby-gnu/test/validate-map.jinja

test(map): verify `map.jinja` dump using `_mapdata` state
This commit is contained in:
Imran Iqbal 2020-09-04 21:47:16 +01:00 committed by GitHub
commit 933e0be3bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 674 additions and 4 deletions

View file

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

View file

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
---
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import TEMPLATE as mapdata with context %}
{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %}
{%- set output_file = '/tmp/salt_mapdata_dump.yaml' %}
{{ tplroot }}-mapdata-dump:
file.managed:
- name: {{ output_file }}
- source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja
- template: jinja
- context:
map: {{ mapdata | yaml }}

View file

@ -156,6 +156,7 @@ suites:
state_top:
base:
'*':
- TEMPLATE._mapdata
- TEMPLATE
pillars:
top.sls:
@ -177,6 +178,7 @@ suites:
state_top:
base:
'*':
- TEMPLATE._mapdata
- TEMPLATE
pillars:
top.sls:

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
# Replace per minion strings
replacement = {
hostname: system.hostname
}
# Keep only first 2 digits from Ubuntu finger
mapdata_file = "_mapdata/#{system.platform[:finger].split('.').first}.yaml"
# Load the mapdata from profile https://docs.chef.io/inspec/profiles/#profile-files
mapdata_dump = inspec.profile.file(mapdata_file) % replacement
control '`map.jinja` YAML dump' do
title 'should contain the lines'
describe file('/tmp/salt_mapdata_dump.yaml') do
it { should exist }
its('content') { should eq mapdata_dump }
end
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
# Prepare platform "finger"
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"
platform_finger = system.platform[:finger].split('.').first.to_s
control 'TEMPLATE package' do
title 'should be installed'
@ -9,7 +9,7 @@ control 'TEMPLATE package' do
# Overide by `platform_finger`
package_name =
case platform_finger
when 'centos-6', 'amazon-2018'
when 'centos-6', 'amazonlinux-1'
'cronie'
else
'bash'

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
# Prepare platform "finger"
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"
platform_finger = system.platform[:finger].split('.').first.to_s
control 'TEMPLATE service' do
impact 0.5
@ -10,7 +10,7 @@ control 'TEMPLATE service' do
# Overide by `platform_finger`
service_name =
case platform_finger
when 'centos-6', 'amazon-2018'
when 'centos-6', 'amazonlinux-1'
'crond'
else
'systemd-journald'

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Amazon Linux AMI-2018
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: cronie
rootgroup: root
service:
name: crond
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Amazon Linux-2
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Arch
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# CentOS-6
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: cronie
rootgroup: root
service:
name: crond
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# CentOS Linux-7
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# CentOS Linux-8
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Debian-10
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Debian-9
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Fedora-31
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Fedora-32
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Leap-15
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Ubuntu-16.04
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Ubuntu-18.04
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -0,0 +1,34 @@
# yamllint disable rule:indentation rule:line-length
# Ubuntu-20.04
---
added_in_defaults: defaults_value
added_in_lookup: lookup_value
added_in_pillar: pillar_value
arch: amd64
config: /etc/template-formula.conf
lookup:
added_in_lookup: lookup_value
master: template-master
winner: lookup
master: template-master
pkg:
name: bash
rootgroup: root
service:
name: systemd-journald
subcomponent:
config: /etc/TEMPLATE-subcomponent-formula.conf
tofs:
files_switch:
- any/path/can/be/used/here
- id
- roles
- osfinger
- os
- os_family
source_files:
TEMPLATE-config-file-file-managed:
- example.tmpl.jinja
TEMPLATE-subcomponent-config-file-file-managed:
- subcomponent-example.tmpl.jinja
winner: pillar

View file

@ -6,6 +6,9 @@ title: TEMPLATE formula
maintainer: SaltStack Formulas
license: Apache-2.0
summary: Verify that the TEMPLATE formula is setup and configured correctly
depends:
- name: share
path: test/integration/share
supports:
- platform-name: debian
- platform-name: ubuntu

View file

@ -0,0 +1,20 @@
# 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.hostname`: return the result of `hostname -s` or `hostnamectl --static` based on the availability of either command
- `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
- `system.platform[:name]` modify `amazon` to `amazonlinux`
- `system.platform[:release]` tweak Arch and Amazon Linux:
- `Arch` is always `base-latest`
- `Amazon Linux` release `2018` is resolved as `1`
- `system.platform[:finger]` is the concatenation of the name and the major release number (except for Ubuntu, which gives `ubuntu-20.04` for example)

View file

@ -0,0 +1,18 @@
# -*- 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: arch

View file

@ -0,0 +1,99 @@
# frozen_string_literal: true
# system.rb -- InSpec resources for system values
# Author: Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
# Copyright (C) 2020 Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
HOSTNAME_CMDS = %w[hostname hostnamectl].freeze
HOSTNAME_CMDS_OPT = {
'hostname' => '-s',
'hostnamectl' => '--static'
}.freeze
class SystemResource < Inspec.resource(1)
name 'system'
attr_reader :platform
attr_reader :hostname
def initialize
@platform = build_platform
@hostname = found_hostname
end
private
def found_hostname
cmd = guess_hostname_cmd
unless cmd.exit_status.zero?
raise Inspec::Exceptions::ResourceSkipped,
"Error running '#{cmd}': #{cmd.stderr}"
end
cmd.stdout.chomp
end
def guess_hostname_cmd
HOSTNAME_CMDS.each do |cmd|
if inspec.command(cmd).exist?
return inspec.command("#{cmd} #{HOSTNAME_CMDS_OPT[cmd]}")
end
end
raise Inspec::Exceptions::ResourceSkipped,
"Error: #{@platform[:finger]}} has none of #{HOSTNAME_CMDS.join(', ')}"
end
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'
'arch'
else
inspec.platform[:family]
end
end
def build_platform_name
case inspec.platform[:name]
when 'amazon'
'amazonlinux'
else
inspec.platform[:name]
end
end
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'
else
inspec.platform[:release]
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