more refinements to unittesting

This commit is contained in:
Thomas S Hatch 2012-02-12 16:03:31 -07:00
parent 83adaa0899
commit fd82c47a95
4 changed files with 87 additions and 133 deletions

View File

@ -1,75 +0,0 @@
'''
Classes used to set up the main components
'''
# Import python libs
import multiprocessing
import os
import sys
# Set up paths
TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
SALT_LIBS = os.path.dirname(TEST_DIR)
sys.path.insert(0, TEST_DIR)
sys.path.insert(0, SALT_LIBS)
# Import salt libs
from saltunittest import TestLoader, TextTestRunner, TestCase, TestSuite
import salt
import salt.config
import salt.master
import salt.minion
class DaemonCase(object):
'''
Set up the master and minion daemons, and run related cases
'''
def __init__(self):
'''
Start a master and minion
'''
master_opts = salt.config.master_config(os.path.join(TEST_DIR, 'files/conf/master'))
minion_opts = salt.config.minion_config(os.path.join(TEST_DIR, 'files/conf/minion'))
print minion_opts
salt.verify_env([os.path.join(master_opts['pki_dir'], 'minions'),
os.path.join(master_opts['pki_dir'], 'minions_pre'),
os.path.join(master_opts['pki_dir'], 'minions_rejected'),
os.path.join(master_opts['cachedir'], 'jobs'),
os.path.dirname(master_opts['log_file']),
minion_opts['extension_modules'],
master_opts['sock_dir'],
])
# Start the master
master = salt.master.Master(master_opts)
multiprocessing.Process(target=master.start).start()
# Start the minion
minion = salt.minion.Minion(minion_opts)
multiprocessing.Process(target=minion.tune_in).start()
def tearDown(self):
'''
Kill the minion and master processes
'''
pass
class ModuleCase(TestCase):
'''
Execute a module function
'''
def setUp(self):
'''
Generate the tools to test a module
'''
self.client = salt.client.LocalClient('files/conf/master')
def run_function(self, function, arg=()):
'''
Run a single salt function and condition the return down to match the
behavior of the raw function call
'''
orig = self.client.cmd('minion', function, arg)
return orig['minion']

View File

@ -1,20 +1,18 @@
from salt.modules.hosts import list_hosts, get_ip, get_alias, has_pair, add_host,\
set_host, rm_host
from os import path
'''
Test the hosts module
'''
# Import python libs
import os
import shutil
import sys
import saltunittest
TEMPLATES_DIR = path.dirname(path.abspath(__file__))
# Import Salt libs
import daemon
monkey_pathed = (list_hosts, set_host, add_host, rm_host)
class HostsModuleTest(saltunittest.TestCase):
class HostsModuleTest(daemon.ModuleCase):
def setUp(self):
self._hfn = [f.hosts_filename for f in monkey_pathed]
self.files = path.join(TEMPLATES_DIR, 'files')
self.hostspath = path.join(self.files, 'hosts')
self.not_found = path.join(self.files, 'not_found')
self.files = os.path.join(TEMPLATES_DIR, 'files')
self.hostspath = os.path.join(self.files, 'hosts')
self.not_found = os.path.join(self.files, 'not_found')
self.tmpfiles = []
def tearDown(self):

View File

@ -1,58 +1,20 @@
#!/usr/bin/env python
'''
Discover all instances of unittest.TestCase in this directory.
The current working directory must be set to the build of the salt you want to test.
'''
# Import python libs
from os.path import dirname, abspath, relpath, splitext, normpath
import sys
import os
import fnmatch
TEST_DIR = dirname(normpath(abspath(__file__)))
SALT_BUILD = os.getcwd()
TEST_FILES = '*.py'
sys.path.insert(0, TEST_DIR)
sys.path.insert(0, SALT_BUILD)
# Import salt libs
from saltunittest import TestLoader, TextTestRunner, TestCase
import daemon
import salt
import salt.config
import salt.master
import salt.minion
import saltunittest
TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
def main():
names = find_tests()
tests = TestLoader().loadTestsFromNames(names)
TextTestRunner(verbosity=1).run(tests)
def find_tests():
names = []
for root, _, files in os.walk(TEST_DIR):
for name in files:
if fnmatch.fnmatch(name, TEST_FILES) \
and not name == 'runtests.py':
module = get_test_name(root, name)
if module: names.append(module)
return names
def get_test_name(root, name):
if name.startswith("_"): return None
rel = relpath(root, TEST_DIR).lstrip(".")
prefix = "%s." % rel.replace('/','.') if rel else ""
return "".join((prefix, splitext(name)[0]))
saltunittest.TestDaemon()
loader = saltunittest.TestLoader()
tests = loader.discover(os.path.join(TEST_DIR, 'modules'))
saltunittest.TextTestRunner(verbosity=1).run(tests)
if __name__ == "__main__":
daemon.DaemonCase()
loader = TestLoader()
tests = loader.discover(os.path.join(TEST_DIR, 'modules'))
TextTestRunner(verbosity=1).run(tests)
main()

View File

@ -7,6 +7,9 @@ imported here from the relevant module and then imported into your
test from here
"""
# Import python libs
import multiprocessing
import os
import sys
# support python < 2.7 via unittest2
@ -22,3 +25,69 @@ else:
from unittest import TestLoader, TextTestRunner,\
TestCase, expectedFailure, \
TestSuite
# Set up paths
TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
SALT_LIBS = os.path.dirname(TEST_DIR)
sys.path.insert(0, TEST_DIR)
sys.path.insert(0, SALT_LIBS)
# Import salt libs
import salt
import salt.config
import salt.master
import salt.minion
class TestDaemon(object):
'''
Set up the master and minion daemons, and run related cases
'''
def __init__(self):
'''
Start a master and minion
'''
master_opts = salt.config.master_config(os.path.join(TEST_DIR, 'files/conf/master'))
minion_opts = salt.config.minion_config(os.path.join(TEST_DIR, 'files/conf/minion'))
print minion_opts
salt.verify_env([os.path.join(master_opts['pki_dir'], 'minions'),
os.path.join(master_opts['pki_dir'], 'minions_pre'),
os.path.join(master_opts['pki_dir'], 'minions_rejected'),
os.path.join(master_opts['cachedir'], 'jobs'),
os.path.dirname(master_opts['log_file']),
minion_opts['extension_modules'],
master_opts['sock_dir'],
])
# Start the master
master = salt.master.Master(master_opts)
multiprocessing.Process(target=master.start).start()
# Start the minion
minion = salt.minion.Minion(minion_opts)
multiprocessing.Process(target=minion.tune_in).start()
def tearDown(self):
'''
Kill the minion and master processes
'''
pass
class ModuleCase(TestCase):
'''
Execute a module function
'''
def setUp(self):
'''
Generate the tools to test a module
'''
self.client = salt.client.LocalClient('files/conf/master')
def run_function(self, function, arg=()):
'''
Run a single salt function and condition the return down to match the
behavior of the raw function call
'''
orig = self.client.cmd('minion', function, arg)
return orig['minion']