salt.modules.saltcheck

A module for testing the logic of states and highstates

codeauthor:William Cannon <william.cannon@gmail.com>
maturity:new

Saltcheck provides unittest like functionality requiring only the knowledge of salt module execution and yaml.

In order to run state and highstate saltcheck tests a sub-folder of a state must be created and named saltcheck-tests.

Tests for a state should be created in files ending in *.tst and placed in the saltcheck-tests folder.

Multiple tests can be created in a file. Multiple *.tst files can be created in the saltcheck-tests folder. Salt rendering is supported in test files (e.g. yaml + jinja). The id of a test works in the same manner as in salt state files. They should be unique and descriptive.

Example file system layout:

Example:

echo-test-hello:
  module_and_function: test.echo
  args:
    - "hello"
  kwargs:
  assertion: assertEqual
  expected-return:  'hello'
class salt.modules.saltcheck.SaltCheck

This class implements the saltcheck

call_salt_command(fun, args, kwargs)

Generic call of salt Caller command

static cast_expected_to_returned_type(expected, returned)

Determine the type of variable returned Cast the expected to the type of variable returned

static get_state_search_path_list()

For the state file system, return a list of paths to search for states

run_test(test_dict)

Run a single saltcheck test

class salt.modules.saltcheck.StateTestLoader(search_paths)

Class loads in test files for a state e.g. state_dir/saltcheck-tests/[1.tst, 2.tst, 3.tst]

add_test_files_for_sls(sls_path)

Adding test files

static convert_sls_to_path(sls)

Converting sls to paths

static convert_sls_to_paths(sls_list)

Converting sls to paths

gather_files(filepath)

Gather files for a test suite

load_file(filepath)

loads in one test file

load_file_salt_rendered(filepath)

loads in one test file

load_test_suite()

Load tests either from one file, or a set of files

salt.modules.saltcheck.run_highstate_tests()

Execute all tests for a salt highstate and return results

CLI Example:

salt '*' saltcheck.run_highstate_tests
salt.modules.saltcheck.run_state_tests(state)

Execute all tests for a salt state and return results Nested states will also be tested

Parameters:state (str) -- the name of a user defined state

CLI Example:

salt '*' saltcheck.run_state_tests postfix
salt.modules.saltcheck.run_test(**kwargs)

Execute one saltcheck test and return result

Parameters:arg test (keyword) --

CLI Example:

salt '*' saltcheck.run_test
    test='{"module_and_function": "test.echo",
           "assertion": "assertEqual",
           "expected-return": "This works!",
           "args":["This works!"] }'
salt.modules.saltcheck.update_master_cache()

Updates the master cache onto the minion - transfers all salt-check-tests Should be done one time before running tests, and if tests are updated Can be automated by setting "auto_update_master_cache: True" in minion config

CLI Example:

salt '*' saltcheck.update_master_cache