From 0df86c01f03dc427061a3156970e170984c7746d Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Sat, 31 Oct 2020 14:57:26 +0000 Subject: [PATCH] test(map): verify `map.jinja` dump using `_mapdata` state [skip ci] * Automated using https://github.com/myii/ssf-formula/pull/275 --- openntpd/_mapdata/init.sls | 12 ++--- .../default/controls/_mapdata_spec.rb | 20 +++++-- test/integration/share/libraries/system.rb | 52 ++++++++----------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/openntpd/_mapdata/init.sls b/openntpd/_mapdata/init.sls index f1bfdfe..4994bc5 100644 --- a/openntpd/_mapdata/init.sls +++ b/openntpd/_mapdata/init.sls @@ -3,14 +3,12 @@ --- {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} -{%- from tplroot ~ "/map.jinja" import openntpd with context %} +{%- from tplroot ~ "/map.jinja" import openntpd as mapdata with context %} -{%- set map = { - 'openntpd': openntpd, - } %} -{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ map | yaml(False)) %} +{%- do salt['log.debug']('### MAP.JINJA DUMP ###\n' ~ mapdata | yaml(False)) %} -{%- set output_file = '/tmp/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: @@ -18,4 +16,4 @@ - source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja - template: jinja - context: - map: {{ map | yaml }} + map: {{ mapdata | yaml }} diff --git a/test/integration/default/controls/_mapdata_spec.rb b/test/integration/default/controls/_mapdata_spec.rb index bd3e990..2cced0a 100644 --- a/test/integration/default/controls/_mapdata_spec.rb +++ b/test/integration/default/controls/_mapdata_spec.rb @@ -1,13 +1,23 @@ # frozen_string_literal: true +require 'yaml' + control '`map.jinja` YAML dump' do - title 'should contain the lines' + 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" - mapdata_dump = inspec.profile.file(mapdata_file) - describe file('/tmp/salt_mapdata_dump.yaml') do - it { should exist } - its('content') { should include mapdata_dump } + # 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/share/libraries/system.rb b/test/integration/share/libraries/system.rb index 3c6304c..91ebbc8 100644 --- a/test/integration/share/libraries/system.rb +++ b/test/integration/share/libraries/system.rb @@ -4,47 +4,18 @@ # Author: Daniel Dehennin # Copyright (C) 2020 Daniel Dehennin -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 + super @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, @@ -67,11 +38,16 @@ def build_platform_name case inspec.platform[:name] when 'amazon' 'amazonlinux' + when 'windows_8.1_pro' + 'windows' + when 'windows_server_2019_datacenter' + 'windows-server' else inspec.platform[:name] end end + # rubocop:disable Metrics/MethodLength def build_platform_release case inspec.platform[:name] when 'amazon' @@ -79,10 +55,26 @@ def build_platform_release 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' 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}"