|
5 | 5 | control '`map.jinja` YAML dump' do
|
6 | 6 | title 'should match the comparison file'
|
7 | 7 |
|
8 |
| - # Strip the `platform[:finger]` version number down to the "OS major release" |
9 |
| - mapdata_file = "_mapdata/#{system.platform[:finger].split('.').first}.yaml" |
| 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 |
10 | 17 |
|
11 |
| - # Load the mapdata from profile https://docs.chef.io/inspec/profiles/#profile-files |
12 |
| - mapdata_dump = YAML.safe_load(inspec.profile.file(mapdata_file)) |
| 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) |
13 | 29 |
|
14 |
| - # Derive the location of the dumped mapdata |
| 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) |
15 | 33 | output_dir = platform[:family] == 'windows' ? '/temp' : '/tmp'
|
16 |
| - output_file = "#{output_dir}/salt_mapdata_dump.yaml" |
| 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) |
17 | 41 |
|
18 | 42 | describe 'File content' do
|
19 | 43 | it 'should match profile map data exactly' do
|
20 |
| - expect(yaml(output_file).params).to eq(mapdata_dump) |
| 44 | + expect(output_file_dump).to eq(mapdata_file_dump) |
21 | 45 | end
|
22 | 46 | end
|
23 | 47 | end
|
0 commit comments