saltstack-formulas / template-formula

SaltStack formula template filled with dummy content
http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
119 stars 85 forks source link

[FEATURE] Use dynamic values in Inspec tests instead of hardcoded #242

Closed noelmcloughlin closed 2 years ago

noelmcloughlin commented 2 years ago

Is your feature request related to a problem?

I am writing some Inspec tests using saltstack-formulas as a reference and the hard coding of values is anti-pattern which I'm trying to solve. I want to move hard coded values from Inspec Ruby files to YAML and import them dynamically. When something changes I want to update test\vars.yaml instead of Ruby strings. Consider two files here:

config/vars.yaml:

   # values as configuration data
   configured:
       name: dos2unix`
       binary_name: /usr/bin/dos2unix
      .. etc ..

test/vals.yaml:

    #  values for testing suites
    expecting:
       package_name: dos2unix
       binary_name: /usr/bin/dos2unix
      .. etc ..

After some tests, the Ruby YAML module is good solution I think. We do not need to hard code values as ruby strings if test data is separated from tests.

# frozen_string_literal: true

require 'yaml'
expect = YAML.load_file("test/vals.yaml")['expecting']

control 'package install' do
   impact 1.0
   title 'verify package #{expect(package_name}  is installed'

  describe package(expect['package_name']) do
     it { should be_installed }
 end
end

I think this could work in saltstack-formluas so just raising for discussion.

Describe the solution you'd like

I think this idea will work well in saltstack-formulas too.

Describe alternatives you've considered

I'm using similar pattern using Molecule (ansible verifier) and trying to reuse the pattern with Inspec Verifier

Additional context

myii commented 2 years ago

@noelmcloughlin The correct answer is probably InSpec Inputs. An example of this is already shown throughout the Kitchen YAML files in the salt-formula (look for verifier:inputs). That's a long-winded version, where there had to be inputs for each platform. When there are general inputs required, they can simply be added in one block under the top-level verifier key. Here's one example of how to access the input from the actual control.

noelmcloughlin commented 2 years ago

Yes, so it looks like that solution exists already. Its coupled to kitchen but the file is YAML so could be reused outside kitchen. I am using molecule test framework for some experiments. Inspec can be used with molecule OR kitchen frameworks, so I was seeking a common way of presenting verifier:inputs (good terminology) to the verifiers. Kitchen.yaml could be solution as long as inputs could be defined in one block instead of repetition (even if not using kitchen) although needs some thought where the verifier is not Inspec.

noelmcloughlin commented 2 years ago

Salt would be an excellent verifier for kitchen of course ;-)

noelmcloughlin commented 2 years ago

Closing.

myii commented 2 years ago

Salt would be an excellent verifier for kitchen of course ;-)

There's always salt.modules.saltcheck... there's an example in the cron-formula.