Skip to content

Commit 4ed81bb

Browse files
authored
Merge pull request #121 from myii/test/manage-map.jinja-verification
test(map): verify `map.jinja` dump using `_mapdata` state
2 parents 41a15f9 + 8d4ced7 commit 4ed81bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2717
-641
lines changed

.rubocop.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,3 @@ AllCops:
2323
Lint/EmptyWhen:
2424
Exclude:
2525
- test/integration/default/controls/config_spec.rb
26-
Style/FormatStringToken:
27-
Exclude:
28-
- test/integration/default/controls/yaml_dump_spec.rb

CODEOWNERS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
# SECTION: Owner(s) for specific directories
1212
# FILE PATTERN OWNER(S)
13-
/test/ @myii
1413

1514
# SECTION: Owner(s) for files/directories related to `semantic-release`
1615
# FILE PATTERN OWNER(S)
@@ -20,8 +19,11 @@
2019
/docs/AUTHORS.rst @saltstack-formulas/ssf
2120
/docs/CHANGELOG.rst @saltstack-formulas/ssf
2221
/docs/TOFS_pattern.rst @saltstack-formulas/ssf
22+
/*/_mapdata/ @saltstack-formulas/ssf
2323
/*/libsaltcli.jinja @saltstack-formulas/ssf
2424
/*/libtofs.jinja @saltstack-formulas/ssf
25+
/test/integration/**/_mapdata_spec.rb @saltstack-formulas/ssf
26+
/test/integration/**/libraries/system.rb @saltstack-formulas/ssf
2527
/test/integration/**/inspec.yml @saltstack-formulas/ssf
2628
/test/integration/**/README.md @saltstack-formulas/ssf
2729
/.gitignore @saltstack-formulas/ssf

kitchen.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ verifier:
149149

150150
suites:
151151
- name: default
152+
driver:
153+
hostname: example.net
152154
provisioner:
153155
state_top:
154156
base:
155157
'*':
156-
- tomcat.yaml_dump
158+
- tomcat._mapdata
157159
- tomcat
158160
- tomcat.native
159161
- tomcat.config
@@ -169,9 +171,6 @@ suites:
169171
- tomcat
170172
pillars_from_files:
171173
tomcat.sls: pillar.example
172-
dependencies:
173-
- name: comparison_files
174-
path: ./test/salt
175174
verifier:
176175
inspec_tests:
177176
- path: test/integration/default
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
### Method
9+
# The steps below for each file appear convoluted but they are both required
10+
# and similar in nature:
11+
# 1. The earliest method was to simply compare the files textually but this often
12+
# led to false positives due to inconsistencies (e.g. spacing, ordering)
13+
# 2. The next method was to load the files back into YAML structures and then
14+
# compare but InSpec provided block diffs this way, unusable by end users
15+
# 3. The final step was to dump the YAML structures back into a string to use
16+
# for the comparison; this both worked and provided human-friendly diffs
17+
18+
### Comparison file for the specific platform
19+
### Static, adjusted as part of code contributions, as map data is changed
20+
# Strip the `platform[:finger]` version number down to the "OS major release"
21+
platform_finger = system.platform[:finger].split('.').first.to_s
22+
# Use that to set the path to the file (relative to the InSpec suite directory)
23+
mapdata_file_path = "_mapdata/#{platform_finger}.yaml"
24+
# Load the mapdata from profile, into a YAML structure
25+
# https://docs.chef.io/inspec/profiles/#profile-files
26+
mapdata_file_yaml = YAML.safe_load(inspec.profile.file(mapdata_file_path))
27+
# Dump the YAML back into a string for comparison
28+
mapdata_file_dump = YAML.dump(mapdata_file_yaml)
29+
30+
### Output file produced by running the `_mapdata` state
31+
### Dynamic, generated during Kitchen's `converge` phase
32+
# Derive the location of the dumped mapdata (differs for Windows)
33+
output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
34+
# Use that to set the path to the file (absolute path, i.e. within the container)
35+
output_file_path = "#{output_dir}/salt_mapdata_dump.yaml"
36+
# Load the output into a YAML structure using InSpec's `yaml` resource
37+
# https://github.com/inspec/inspec/blob/49b7d10/lib/inspec/resources/yaml.rb#L29
38+
output_file_yaml = yaml(output_file_path).params
39+
# Dump the YAML back into a string for comparison
40+
output_file_dump = YAML.dump(output_file_yaml)
41+
42+
describe 'File content' do
43+
it 'should match profile map data exactly' do
44+
expect(output_file_dump).to eq(mapdata_file_dump)
45+
end
46+
end
47+
end

test/integration/default/controls/config_spec.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# frozen_string_literal: true
22

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

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

6968
# Prepare comparison file
70-
main_config_path = "#{comparison_files_dir}/main_config/#{platform_finger}"
71-
main_config = file(main_config_path).content
69+
main_config_path = "main_config/#{platform_finger}"
70+
main_config = inspec.profile.file(main_config_path)
7271

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

9695
server_xml_file = "#{conf_dir}/server.xml"
97-
server_xml_path = "#{comparison_files_dir}/server_xml/#{platform_finger}.xml"
98-
server_xml = file(server_xml_path).content
99-
# Need the hostname to be used for `tomcat.cluster`
100-
server_xml = server_xml.gsub(
101-
'HOSTNAME_PLACEHOLDER',
102-
file('/etc/hostname').content.chomp
103-
)
96+
server_xml_path = "server_xml/#{platform_finger}.xml"
97+
server_xml = inspec.profile.file(server_xml_path)
10498

10599
describe file(server_xml_file) do
106100
it { should be_file }

test/integration/default/controls/packages_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22

3-
# Prepare platform "finger"
4-
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"
5-
63
control 'Tomcat packages' do
74
title 'should be installed'
85

6+
# Strip the `platform[:finger]` version number down to the "OS major release"
7+
platform_finger = system.platform[:finger].split('.').first.to_s
8+
99
# Overide by platform
1010
packages =
1111
case platform[:family]

test/integration/default/controls/services_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# frozen_string_literal: true
22

3-
# Prepare platform "finger"
4-
platform_finger = "#{platform[:name]}-#{platform[:release].split('.')[0]}"
5-
63
control 'Tomcat services' do
74
impact 0.5
85
title 'should be installed, enabled and running'
96

7+
# Strip the `platform[:finger]` version number down to the "OS major release"
8+
platform_finger = system.platform[:finger].split('.').first.to_s
9+
1010
# Overide by platform
1111
services =
1212
case platform[:family]

0 commit comments

Comments
 (0)