Skip to content

Commit

Permalink
Merge pull request #121 from myii/test/manage-map.jinja-verification
Browse files Browse the repository at this point in the history
test(map): verify `map.jinja` dump using `_mapdata` state
  • Loading branch information
myii authored Dec 27, 2020
2 parents 41a15f9 + 8d4ced7 commit 4ed81bb
Show file tree
Hide file tree
Showing 52 changed files with 2,717 additions and 641 deletions.
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ AllCops:
Lint/EmptyWhen:
Exclude:
- test/integration/default/controls/config_spec.rb
Style/FormatStringToken:
Exclude:
- test/integration/default/controls/yaml_dump_spec.rb
4 changes: 3 additions & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

# SECTION: Owner(s) for specific directories
# FILE PATTERN OWNER(S)
/test/ @myii

# SECTION: Owner(s) for files/directories related to `semantic-release`
# FILE PATTERN OWNER(S)
Expand All @@ -20,8 +19,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
Expand Down
7 changes: 3 additions & 4 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ verifier:

suites:
- name: default
driver:
hostname: example.net
provisioner:
state_top:
base:
'*':
- tomcat.yaml_dump
- tomcat._mapdata
- tomcat
- tomcat.native
- tomcat.config
Expand All @@ -169,9 +171,6 @@ suites:
- tomcat
pillars_from_files:
tomcat.sls: pillar.example
dependencies:
- name: comparison_files
path: ./test/salt
verifier:
inspec_tests:
- path: test/integration/default
47 changes: 47 additions & 0 deletions test/integration/default/controls/_mapdata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'yaml'

control '`map.jinja` YAML dump' do
title 'should match the comparison file'

### Method
# The steps below for each file appear convoluted but they are both required
# and similar in nature:
# 1. The earliest method was to simply compare the files textually but this often
# led to false positives due to inconsistencies (e.g. spacing, ordering)
# 2. The next method was to load the files back into YAML structures and then
# compare but InSpec provided block diffs this way, unusable by end users
# 3. The final step was to dump the YAML structures back into a string to use
# for the comparison; this both worked and provided human-friendly diffs

### Comparison file for the specific platform
### Static, adjusted as part of code contributions, as map data is changed
# Strip the `platform[:finger]` version number down to the "OS major release"
platform_finger = system.platform[:finger].split('.').first.to_s
# Use that to set the path to the file (relative to the InSpec suite directory)
mapdata_file_path = "_mapdata/#{platform_finger}.yaml"
# Load the mapdata from profile, into a YAML structure
# https://docs.chef.io/inspec/profiles/#profile-files
mapdata_file_yaml = YAML.safe_load(inspec.profile.file(mapdata_file_path))
# Dump the YAML back into a string for comparison
mapdata_file_dump = YAML.dump(mapdata_file_yaml)

### Output file produced by running the `_mapdata` state
### Dynamic, generated during Kitchen's `converge` phase
# Derive the location of the dumped mapdata (differs for Windows)
output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
# Use that to set the path to the file (absolute path, i.e. within the container)
output_file_path = "#{output_dir}/salt_mapdata_dump.yaml"
# Load the output into a YAML structure using InSpec's `yaml` resource
# https://github.com/inspec/inspec/blob/49b7d10/lib/inspec/resources/yaml.rb#L29
output_file_yaml = yaml(output_file_path).params
# Dump the YAML back into a string for comparison
output_file_dump = YAML.dump(output_file_yaml)

describe 'File content' do
it 'should match profile map data exactly' do
expect(output_file_dump).to eq(mapdata_file_dump)
end
end
end
18 changes: 6 additions & 12 deletions test/integration/default/controls/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

# Prepare platform "finger" and base path to file comparison directory
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"
comparison_files_dir = '/tmp/kitchen/srv/salt/comparison_files'
# Strip the `platform[:finger]` version number down to the "OS major release"
platform_finger = system.platform[:finger].split('.').first.to_s

# Default values for `control 'Tomcat main config'`
main_config_file = '/etc/sysconfig/tomcat'
Expand Down Expand Up @@ -67,8 +66,8 @@
title 'should contain the lines'

# Prepare comparison file
main_config_path = "#{comparison_files_dir}/main_config/#{platform_finger}"
main_config = file(main_config_path).content
main_config_path = "main_config/#{platform_finger}"
main_config = inspec.profile.file(main_config_path)

describe file(main_config_file) do
it { should be_file }
Expand All @@ -94,13 +93,8 @@
title 'should contain the lines'

server_xml_file = "#{conf_dir}/server.xml"
server_xml_path = "#{comparison_files_dir}/server_xml/#{platform_finger}.xml"
server_xml = file(server_xml_path).content
# Need the hostname to be used for `tomcat.cluster`
server_xml = server_xml.gsub(
'HOSTNAME_PLACEHOLDER',
file('/etc/hostname').content.chomp
)
server_xml_path = "server_xml/#{platform_finger}.xml"
server_xml = inspec.profile.file(server_xml_path)

describe file(server_xml_file) do
it { should be_file }
Expand Down
6 changes: 3 additions & 3 deletions test/integration/default/controls/packages_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

# Prepare platform "finger"
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"

control 'Tomcat packages' do
title 'should be installed'

# Strip the `platform[:finger]` version number down to the "OS major release"
platform_finger = system.platform[:finger].split('.').first.to_s

# Overide by platform
packages =
case platform[:family]
Expand Down
6 changes: 3 additions & 3 deletions test/integration/default/controls/services_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

# Prepare platform "finger"
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"

control 'Tomcat services' do
impact 0.5
title 'should be installed, enabled and running'

# Strip the `platform[:finger]` version number down to the "OS major release"
platform_finger = system.platform[:finger].split('.').first.to_s

# Overide by platform
services =
case platform[:family]
Expand Down
Loading

0 comments on commit 4ed81bb

Please sign in to comment.