def test_ini_noext(self): """Testing ini config file(s) without extension""" print "Running: %s - %s" % (self.id(), self.shortDescription()) g.store_config(self.config, self.ini_file) self.assertTrue(os.path.exists(self.ini_file)) g.store_config(self.config, self.ini_noext, config_type='ini') self.assertTrue(os.path.exists(self.ini_noext)) print "--------------" g.show_file(self.ini_file) print "--------------" g.show_file(self.ini_noext) print "--------------" # read the config file config = g.load_config(self.ini_file) g.show_config(config) self.assertEqual(config['defaults']['this'], 'yada1') self.assertEqual(config['defaults']['that'], 'yada2') self.assertEqual(config['defaults']['this_and_that'], 'yada1 and yada2') self.assertEqual(config['globals']['the_other'], 'yada3') config_noext = g.load_config(self.ini_file, config_type='ini') g.show_config(config_noext) self.assertEqual(config_noext['defaults']['this'], 'yada1') self.assertEqual(config_noext['defaults']['that'], 'yada2') self.assertEqual(config_noext['defaults']['this_and_that'], 'yada1 and yada2') self.assertEqual(config_noext['globals']['the_other'], 'yada3') self.assertEqual(config, config_noext, 'config files are not the same')
def setUpClass(cls): """unittest standard setUpClass method Runs before all test_ methods in the class """ print "Setting Up Class: %s" % cls.__name__ # Setting class attributes for use across all test methods cls.yaml_file = '/tmp/testconfig.yml' cls.ini_file = '/tmp/testconfig.ini' cls.ini_ordered_file = '/tmp/testconfig_ordered.ini' cls.config = {} cls.config['defaults'] = {} cls.config['defaults']['this'] = 'yada1' cls.config['defaults']['that'] = 'yada2' cls.config['globals'] = {} cls.config['globals']['the_other'] = 'yada3' # to test ini substitution cls.config['defaults']['this_and_that'] = '%(this)s and %(that)s' g.show_config(cls.config) cls.order = ['defaults', 'globals'] # cleanup files if they exist '''
def test_ini_mixedcase(self): """Testing ini mixed case in config file(s)""" print "Running: %s - %s" % (self.id(), self.shortDescription()) g.store_config(self.config, self.ini_mixedcase_file) self.assertTrue(os.path.exists(self.ini_mixedcase_file)) # read the config file config = g.load_config(self.ini_mixedcase_file) g.show_config(config) self.assertEqual(config['mixed']['mixed_CASE'], 'mixedCaseValue')
def setUp(self): """unittest standard setUp method Runs before each test_ method """ print "Setting Up: %s" % self.id() # render the template g.render_template(self.template_file, self.template_vars, self.output_file, self.search_path) # read the resulting config file built from template self.output_config = g.load_config(self.output_file) g.show_config(self.output_config)
def test_yaml(self): """Testing yaml config file""" print "Running: %s - %s" % (self.id(), self.shortDescription()) # write the config file g.store_config(self.config, self.yaml_file) # TODO: does unittest have a file exists assert? self.assertTrue(os.path.exists(self.yaml_file)) # read the config file config = g.load_config(self.yaml_file) g.show_config(config) self.assertEqual(config['defaults']['this'], 'yada1') self.assertEqual(config['defaults']['that'], 'yada2') self.assertEqual(config['globals']['the_other'], 'yada3')
def test_ini(self): """Testing ini config file(s)""" print "Running: %s - %s" % (self.id(), self.shortDescription()) g.store_config(self.config, self.ini_file) self.assertTrue(os.path.exists(self.ini_file)) # read the config file config = g.load_config(self.ini_file) g.show_config(config) self.assertEqual(config['defaults']['this'], 'yada1') self.assertEqual(config['defaults']['that'], 'yada2') self.assertEqual(config['defaults']['this_and_that'], 'yada1 and yada2') self.assertEqual(config['globals']['the_other'], 'yada3')
def main(): """Main function""" args = parse_args() if args.config_list: handle_configs(args.config_list) g.show_config(g.config) output_file = "rendered_template.txt" if args.output_file: output_file = args.output_file if args.template_file: g.render_template(args.template_file, g.config, output_file)
def test_yaml(self): """Testing yaml config file""" print "Running: %s - %s" % (self.id(), self.shortDescription()) g.show_config(self.config) # write the config file g.store_config(self.config, self.yaml_file) self.assertTrue(os.path.exists(self.yaml_file)) # read the config file config = g.load_config(self.yaml_file) g.show_config(config) self.assertEqual(config['defaults']['this'], 'yada1') self.assertEqual(config['defaults']['that'], 'yada2') self.assertEqual(config['globals']['the_other'], 'yada3')
def setUp(self): """unittest standard setUp method Runs before each test_ method """ print "Setting Up: %s" % self.id() # render the template self.returned_template = g.render_template(self.template_file, self.template_vars, self.output_file, self.search_path) # read the resulting config file built from template self.output_config = g.load_config(self.output_file) g.show_config(self.output_config) self.returned_config = g.load_yaml_string(self.returned_template) g.show_config(self.returned_config)
def setUpClass(cls): """unittest standard setUpClass method Runs before all test_ methods in the class """ print "Setting Up Class: %s" % cls.__name__ # Setting class attributes for use across all test methods cls.config_file = ('supporting_files/templates/' 'glusto_templates-vars.yml') config = g.load_config(cls.config_file) g.show_config(config) if config: g.update_config(config) cls.template_vars = g.config['templates'] cls.template_file = ('templates/' 'glusto_templates-template.jinja') cls.search_path = 'supporting_files' cls.output_file = '/tmp/glusto_templates-output.yml'
def test_ini_novalue(self): """Testing ini config file(s) without values""" print "Running: %s - %s" % (self.id(), self.shortDescription()) g.store_config(self.config_novalue, self.ini_novalue_file) self.assertTrue(os.path.exists(self.ini_novalue_file)) print "--------------" g.show_file(self.ini_novalue_file) print "--------------" # read the config file config = g.load_config(self.ini_novalue_file) g.show_config(config) self.assertEqual(config['defaults'].get('this'), '') self.assertEqual(config['defaults'].get('that'), '') self.assertEqual(config['globals'].get('and_one_more_thing'), '') self.assertEqual(config['globals'].get('the_other'), '')
def setUpClass(cls): """unittest standard setUpClass method Runs before all test methods in the class """ print "Setting Up Class: %s" % cls.__name__ # Setting class attributes for use across all test methods cls.yaml_file = '/tmp/testconfig.yml' cls.json_file = '/tmp/testconfig.json' cls.ini_file = '/tmp/testconfig.ini' cls.ini_novalue_file = '/tmp/testconfig_novalue.ini' cls.ini_ordered_file = '/tmp/testconfig_ordered.ini' cls.ini_mixedcase_file = '/tmp/testconfig_mixedcase.ini' cls.ini_lowercase_file = '/tmp/testconfig_lowercase.ini' cls.yaml_noext = '/tmp/testyaml' cls.json_noext = '/tmp/testjson' cls.ini_noext = '/tmp/testini' cls.config = {} cls.config['defaults'] = {} cls.config['defaults']['this'] = 'yada1' cls.config['defaults']['that'] = 'yada2' cls.config['globals'] = {} cls.config['globals']['the_other'] = 'yada3' # to test ini substitution cls.config['defaults']['this_and_that'] = '%(this)s and %(that)s' # to test mixedcase cls.config['mixed'] = {} cls.config['mixed']['mixed_CASE'] = "mixedCaseValue" g.show_config(cls.config) cls.config_novalue = {} cls.config_novalue['defaults'] = ['this', 'that'] cls.config_novalue['globals'] = ['the_other', 'and_one_more_thing'] g.show_config(cls.config_novalue) cls.ordered_config = OrderedDict() cls.ordered_config['defaults'] = OrderedDict() cls.ordered_config['defaults']['this'] = 'yada1' cls.ordered_config['defaults']['that'] = 'yada2' cls.ordered_config['globals'] = OrderedDict() cls.ordered_config['globals']['the_other'] = 'yada3' # to test ini substitution cls.ordered_config['defaults']['this_and_that'] = \ '%(this)s and %(that)s' g.show_config(cls.ordered_config) # cleanup files if they exist '''
def main(): """Entry point console script for setuptools. Provides a command-line interface to Glusto. Currently does nothing useful, but plan to wrap Glusto functionality in a CLI interface that can be injected into shell scripts, etc. Example: # glusto run hostname.example.com "uname -a" """ epilog = ('NOTE: If encountering an "unknown option" issue ' 'with the -t and -n options, use param=\'args\' syntax.' '(e.g., -t="-v -x tests")') parser = argparse.ArgumentParser(description="Glusto CLI wrapper", epilog=epilog) parser.add_argument("-c", "--config", help="Config file(s) to read.", action="store", dest="config_list", default=None) parser.add_argument("--ssh-keyfile", help="SSH keyfile for connections.", action="store", dest="ssh_keyfile") parser.add_argument("-l", "--log", help="Default logfile location.", action="store", dest="log_filename", default=None) parser.add_argument("--log-level", help="Default log level.", action="store", dest="log_level", default=None) parser.add_argument("--pytest", help="Run tests using the pytest framework.", action="store", dest="run_pytest") parser.add_argument("--nosetests", help="Run tests using the nose framework.", action="store", dest="run_nosetests") parser.add_argument("--unittest", help="Run tests using the unittest framework.", action="store", dest="run_unittest") parser.add_argument("-u", help="Run unittests per provided config file.", action="store_true", dest="run_unittest_config") parser.add_argument("-d", "--discover", help="Discover unittests from directory.", action="store", dest="discover_dir") args = parser.parse_args() # read config files and update g.config attributes handle_configs(args.config_list) # TODO: break everything into separate methods # handle actionable config items # logging # set defaults log_name = "glustomain" log_filename = "/tmp/glustomain.log" log_level = "INFO" # override with config log_filename = g.config.get('log_filename', log_filename) log_level = g.config.get('log_level', log_level) # override with CLI options if args.log_filename: log_filename = args.log_filename if args.log_level: log_level = args.log_level g.log = g.create_log(name=log_name, filename=log_filename, level=log_level) print("Log %s created as %s with log level %s" % (log_name, log_filename, log_level)) g.log.info("Starting glusto via main()") print "Starting glusto via main()" # override ssh_keyfile @ CLI if args.ssh_keyfile: g.ssh_set_keyfile(args.ssh_keyfile) g.show_config(g.config) # unittest # TODO: functionalize this so it can be used for standalone test scripts if args.run_unittest_config or args.discover_dir: tsuite = unittest.TestSuite() if args.discover_dir: unittest_config = {'cli_discover': 'true'} else: unittest_config = g.config.get('unittest', False) if not unittest_config: print ("ERROR: Unittest option requires a unittest configuration.") return False output_junit = unittest_config.get('output_junit', False) if output_junit: trunner = xmlrunner.XMLTestRunner(output='/tmp/glustoreports') else: trunner = unittest.TextTestRunner(verbosity=2) loader = unittest.TestLoader() loader.testMethodPrefix = unittest_config.get('test_method_prefix', 'test') discover = unittest_config.get('discover_tests') if args.discover_dir: discover = {'start_dir': args.discover_dir} # TODO: ??? Add ability to run multiple discoveries in a single config if discover: g.log.debug('unittest - discover') start_dir = discover.get('start_dir', '.') pattern = discover.get('pattern', 'test_*') top_level_dir = discover.get('top_level_dir', None) discovered_tests = loader.discover(start_dir, pattern, top_level_dir) tsuite.addTests(discovered_tests) run_list = unittest_config.get('load_tests_from_list') # TODO: ability to load multiple lists # TODO: ??? list format more conducive to automation if run_list: g.log.debug('unittest - load_tests_from_list') unittest_config = g.config['unittest'] unittest_list = g.config['unittest_list'] module_name = unittest_list['module_name'] test_list = unittest_list['list'] test_module_obj = importlib.import_module(module_name) tests_to_run = loader.loadTestsFromNames(test_list, test_module_obj) tsuite.addTests(tests_to_run) run_module = unittest_config.get('load_tests_from_module') if run_module: g.log.debug('unittest - load_tests_from_module') module_name = run_module.get('module_name') use_load_tests = run_module.get('use_load_tests', True) # TODO: is there a better way to do this without the dual import? __import__(module_name) class_list = inspect.getmembers(sys.modules[module_name], inspect.isclass) # TODO: is __import__ Python3.x friendly??? test_module_obj = __import__(module_name, fromlist=class_list) tests_from_module = loader.loadTestsFromModule(test_module_obj, use_load_tests) tsuite.addTests(tests_from_module) # Load tests from a name (string) # NOTE: does not use load_test() test_name = unittest_config.get('load_tests_from_name') if test_name: g.log.debug('unittest - load_tests_from_name') # TODO: can we collapse these loader instances into one at top??? tests_from_name = loader.loadTestsFromName(test_name) tsuite.addTests(tests_from_name) # Load tests from names (list) # NOTE: only uses load_test() when name is module level # e.g., tests.test_glusto test_name_list = unittest_config.get('load_tests_from_names') if test_name_list: g.log.debug('unittest - load_tests_from_names') # TODO: can we collapse these loader instances into one at top??? tests_from_names = loader.loadTestsFromNames(test_name_list) tsuite.addTests(tests_from_names) # TODO: Add a skip test option trunner.run(tsuite) PYTEST_FAIL = 1 NOSETESTS_FAIL = 2 UNITTEST_FAIL = 4 retcode = 0 if args.run_pytest: print "pytest: %s" % args.run_pytest # using shlex.split() to handle quoted arguments with spaces argv = shlex.split(args.run_pytest) result = pytest.main(argv) if result > 0: retcode = retcode | PYTEST_FAIL if args.run_nosetests: print "nosetests: %s" % args.run_nosetests argv = shlex.split(args.run_nosetests) argv.insert(0, 'glusto-nosetests') print(argv) result = nose.run(argv=argv) if not result: retcode = retcode | NOSETESTS_FAIL if args.run_unittest: print "unittest: %s" % args.run_unittest argv = shlex.split(args.run_unittest) argv.insert(0, 'glusto-unittest') print(argv) test_object = unittest.main(exit=False, argv=argv) num_errors = len(test_object.result.errors) num_failures = len(test_object.result.failures) if num_errors > 0 or num_failures > 0: retcode = retcode | UNITTEST_FAIL g.log.info("Ending glusto via main()") print "Ending glusto via main()" return retcode
def main(): """Entry point console script for setuptools. Provides a command-line interface to Glusto. Currently does nothing useful, but plan to wrap Glusto functionality in a CLI interface that can be injected into shell scripts, etc. Example: # glusto run hostname.example.com "uname -a" """ epilog = ('NOTE: If encountering an "unknown option" issue ' 'with the -t and -n options, use param=\'args\' syntax.' '(e.g., -t="-v -x tests")') parser = argparse.ArgumentParser(description="Glusto CLI wrapper", epilog=epilog) parser.add_argument("-c", "--config", help="Config file(s) to read.", action="store", dest="config_list", default=None) parser.add_argument("--ssh-keyfile", help="SSH keyfile for connections.", action="store", dest="ssh_keyfile") parser.add_argument("-l", "--log", help="Default logfile location.", action="store", dest="log_filename", default=None) parser.add_argument("--log-level", help="Default log level.", action="store", dest="log_level", default=None) parser.add_argument("--pytest", help="Run tests using the pytest framework.", action="store", dest="run_pytest") parser.add_argument("--nosetests", help="Run tests using the nose framework.", action="store", dest="run_nosetests") parser.add_argument("--unittest", help="Run tests using the unittest framework.", action="store", dest="run_unittest") parser.add_argument("-u", help="Run unittests per provided config file.", action="store_true", dest="run_unittest_config") parser.add_argument("-d", "--discover", help="Discover unittests from directory.", action="store", dest="discover_dir") args = parser.parse_args() # read config files and update g.config attributes handle_configs(args.config_list) # TODO: break everything into separate methods # handle actionable config items # logging # set defaults log_name = "glustomain" log_filename = "/tmp/glustomain.log" log_level = "INFO" # override with config log_filename = g.config.get('log_filename', log_filename) log_level = g.config.get('log_level', log_level) # override with CLI options if args.log_filename: log_filename = args.log_filename if args.log_level: log_level = args.log_level g.log = g.create_log(name=log_name, filename=log_filename, level=log_level) print("Log %s created as %s with log level %s" % (log_name, log_filename, log_level)) g.log.info("Starting glusto via main()") print "Starting glusto via main()" # override ssh_keyfile @ CLI if args.ssh_keyfile: g.ssh_set_keyfile(args.ssh_keyfile) g.show_config(g.config) # unittest # TODO: functionalize this so it can be used for standalone test scripts if args.run_unittest_config or args.discover_dir: tsuite = unittest.TestSuite() if args.discover_dir: unittest_config = {'cli_discover': 'true'} else: unittest_config = g.config.get('unittest', False) if not unittest_config: print("ERROR: Unittest option requires a unittest configuration.") return False output_junit = unittest_config.get('output_junit', False) if output_junit: trunner = xmlrunner.XMLTestRunner(output='/tmp/glustoreports') else: trunner = unittest.TextTestRunner(verbosity=2) loader = unittest.TestLoader() loader.testMethodPrefix = unittest_config.get('test_method_prefix', 'test') discover = unittest_config.get('discover_tests') if args.discover_dir: discover = {'start_dir': args.discover_dir} # TODO: ??? Add ability to run multiple discoveries in a single config if discover: g.log.debug('unittest - discover') start_dir = discover.get('start_dir', '.') pattern = discover.get('pattern', 'test_*') top_level_dir = discover.get('top_level_dir', None) discovered_tests = loader.discover(start_dir, pattern, top_level_dir) tsuite.addTests(discovered_tests) run_list = unittest_config.get('load_tests_from_list') # TODO: ability to load multiple lists # TODO: ??? list format more conducive to automation if run_list: g.log.debug('unittest - load_tests_from_list') unittest_config = g.config['unittest'] unittest_list = g.config['unittest_list'] module_name = unittest_list['module_name'] test_list = unittest_list['list'] test_module_obj = importlib.import_module(module_name) tests_to_run = loader.loadTestsFromNames(test_list, test_module_obj) tsuite.addTests(tests_to_run) run_module = unittest_config.get('load_tests_from_module') if run_module: g.log.debug('unittest - load_tests_from_module') module_name = run_module.get('module_name') use_load_tests = run_module.get('use_load_tests', True) # TODO: is there a better way to do this without the dual import? __import__(module_name) class_list = inspect.getmembers(sys.modules[module_name], inspect.isclass) # TODO: is __import__ Python3.x friendly??? test_module_obj = __import__(module_name, fromlist=class_list) tests_from_module = loader.loadTestsFromModule( test_module_obj, use_load_tests) tsuite.addTests(tests_from_module) # Load tests from a name (string) # NOTE: does not use load_test() test_name = unittest_config.get('load_tests_from_name') if test_name: g.log.debug('unittest - load_tests_from_name') # TODO: can we collapse these loader instances into one at top??? tests_from_name = loader.loadTestsFromName(test_name) tsuite.addTests(tests_from_name) # Load tests from names (list) # NOTE: only uses load_test() when name is module level # e.g., tests.test_glusto test_name_list = unittest_config.get('load_tests_from_names') if test_name_list: g.log.debug('unittest - load_tests_from_names') # TODO: can we collapse these loader instances into one at top??? tests_from_names = loader.loadTestsFromNames(test_name_list) tsuite.addTests(tests_from_names) # TODO: Add a skip test option trunner.run(tsuite) PYTEST_FAIL = 1 NOSETESTS_FAIL = 2 UNITTEST_FAIL = 4 retcode = 0 if args.run_pytest: print "pytest: %s" % args.run_pytest # using shlex.split() to handle quoted arguments with spaces argv = shlex.split(args.run_pytest) result = pytest.main(argv) if result > 0: retcode = retcode | PYTEST_FAIL if args.run_nosetests: print "nosetests: %s" % args.run_nosetests argv = shlex.split(args.run_nosetests) argv.insert(0, 'glusto-nosetests') print(argv) result = nose.run(argv=argv) if not result: retcode = retcode | NOSETESTS_FAIL if args.run_unittest: print "unittest: %s" % args.run_unittest argv = shlex.split(args.run_unittest) argv.insert(0, 'glusto-unittest') print(argv) test_object = unittest.main(exit=False, argv=argv) num_errors = len(test_object.result.errors) num_failures = len(test_object.result.failures) if num_errors > 0 or num_failures > 0: retcode = retcode | UNITTEST_FAIL g.log.info("Ending glusto via main()") print "Ending glusto via main()" return retcode