Skip to content

Commit

Permalink
Merge pull request #44170 from wcannon/saltcheck
Browse files Browse the repository at this point in the history
Adding salt rendering to saltcheck tests
  • Loading branch information
Nicole Thomas committed Dec 11, 2017
2 parents 3c5b591 + 5fb9b33 commit 37a6355
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions salt/modules/saltcheck.py
Expand Up @@ -10,6 +10,7 @@
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.
Expand Down Expand Up @@ -50,6 +51,7 @@
import logging
import os
import time
from json import loads, dumps
import yaml
try:
import salt.utils
Expand Down Expand Up @@ -198,6 +200,14 @@ def run_highstate_tests():
return out_list


def _render_file(file_path):
'''call the salt utility to render a file'''
# salt-call slsutil.renderer /srv/salt/jinjatest/saltcheck-tests/test1.tst
rendered = __salt__['slsutil.renderer'](file_path)
log.info("rendered: {}".format(rendered))
return rendered


def _is_valid_module(module):
'''return a list of all modules available on minion'''
modules = __salt__['sys.list_modules']()
Expand Down Expand Up @@ -534,15 +544,17 @@ def load_test_suite(self):
'''load tests either from one file, or a set of files'''
self.test_dict = {}
for myfile in self.test_files:
self.load_file(myfile)
# self.load_file(myfile)
self.load_file_salt_rendered(myfile)
self.test_files = []

def load_file(self, filepath):
'''
loads in one test file
'''
try:
with salt.utils.files.fopen(filepath, 'r') as myfile:
with __utils__['files.fopen'](filepath, 'r') as myfile:
# with salt.utils.files.fopen(filepath, 'r') as myfile:
# with open(filepath, 'r') as myfile:
contents_yaml = yaml.load(myfile)
for key, value in contents_yaml.items():
Expand All @@ -551,6 +563,18 @@ def load_file(self, filepath):
raise
return

def load_file_salt_rendered(self, filepath):
'''
loads in one test file
'''
# use the salt renderer module to interpret jinja and etc
tests = _render_file(filepath)
# use json as a convenient way to convert the OrderedDicts from salt renderer
mydict = loads(dumps(tests))
for key, value in mydict.items():
self.test_dict[key] = value
return

def gather_files(self, filepath):
'''gather files for a test suite'''
self.test_files = []
Expand Down

0 comments on commit 37a6355

Please sign in to comment.