def test_settings_dir_alternate_dynamic(self): """ Tests that changes to the data_dir settings are applied dynamically To guarantee that, first the data_dir module is loaded. Then a new, alternate set of data directories are created and set in the "canonical" settings location, that is, avocado.core.settings.settings. No data_dir module reload should be necessary to get the new locations from data_dir APIs. """ # Initial settings with initial data_dir locations stg = settings.Settings(self.config_file_path) with unittest.mock.patch('avocado.core.data_dir.settings.settings', stg): from avocado.core import data_dir for key in self.mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) self.assertEqual(data_dir_func(), self.mapping[key]) ( self.alt_base_dir, # pylint: disable=W0201 alt_mapping, self.alt_config_file_path ) = self._get_temporary_dirs_mapping_and_config() # pylint: disable=W0201 # Alternate setttings with different data_dir location alt_stg = settings.Settings(self.alt_config_file_path) with unittest.mock.patch('avocado.core.data_dir.settings.settings', alt_stg): for key in alt_mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) self.assertEqual(data_dir_func(), alt_mapping[key])
def test_argparser_long_or_short_or_positional(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) parser = argparse.ArgumentParser(description='description') stgs.register_option('section', 'key', 'default', 'help') with self.assertRaises(settings.SettingsError): stgs.add_argparser_to_option('section.key', parser)
def test_multiple_parsers(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) parser1 = argparse.ArgumentParser(description="description") stgs.register_option( "section", "key", "default", "help", parser=parser1, long_arg="--first-option", ) parser2 = argparse.ArgumentParser(description="other description") with self.assertRaises(settings.DuplicatedNamespace): stgs.register_option( "section", "key", "default", "help", parser=parser2, long_arg="--other-option", ) stgs.register_option( "section", "key", "default", "help", parser=parser2, long_arg="--other-option", allow_multiple=True, ) self.assertIs(stgs._namespaces["section.key"].parser, parser2)
def test_multiple_parsers(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) parser1 = argparse.ArgumentParser(description='description') stgs.register_option('section', 'key', 'default', 'help', parser=parser1, long_arg='--first-option') parser2 = argparse.ArgumentParser(description='other description') with self.assertRaises(settings.DuplicatedNamespace): stgs.register_option('section', 'key', 'default', 'help', parser=parser2, long_arg='--other-option') stgs.register_option('section', 'key', 'default', 'help', parser=parser2, long_arg='--other-option', allow_multiple=True) self.assertIs(stgs._namespaces['section.key'].parser, parser2)
def test_settings_dir_alternate_dynamic(self): """ Tests that changes to the data_dir settings are applied dynamically To guarantee that, first the data_dir module is loaded. Then a new, alternate set of data directories are created and set in the "canonical" settings location, that is, avocado.core.settings.settings. No data_dir module reload should be necessary to get the new locations from data_dir APIs. """ # Initial settings with initial data_dir locations stg = settings.Settings() with unittest.mock.patch("avocado.core.stgs", stg): import avocado.core avocado.core.register_core_options() stg.process_config_path(self.config_file_path) stg.merge_with_configs() with unittest.mock.patch("avocado.core.data_dir.settings", stg): from avocado.core import data_dir for key, value in self.mapping.items(): data_dir_func = getattr(data_dir, f"get_{key}") self.assertEqual(data_dir_func(), value) ( self.alt_base_dir, # pylint: disable=W0201 alt_mapping, # pylint: disable=W0201 self.alt_config_file_path, ) = self._get_temp_dirs_mapping_and_config() # Alternate settings with different data_dir location alt_stg = settings.Settings() with unittest.mock.patch("avocado.core.stgs", alt_stg): import avocado.core avocado.core.register_core_options() alt_stg.process_config_path(self.alt_config_file_path) alt_stg.merge_with_configs() with unittest.mock.patch("avocado.core.data_dir.settings", alt_stg): for key, value in alt_mapping.items(): data_dir_func = getattr(data_dir, f"get_{key}") self.assertEqual(data_dir_func(), value)
def test_non_registered_option(self): """Config file options that are not registered should be ignored. This should force plugins to run register_option(). """ stgs = settings.Settings() stgs.process_config_path(self.config_file.name) config = stgs.as_dict() self.assertIsNone(config.get('foo.non_registered'))
def test_argparser_positional(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) parser = argparse.ArgumentParser(description='description') stgs.register_option('section', 'key', [], 'help', list) stgs.add_argparser_to_option('section.key', parser, positional_arg=True) self.assertEqual(stgs.as_dict().get('section.key'), [])
def test_argparser_positional(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) parser = argparse.ArgumentParser(description="description") stgs.register_option("section", "key", [], "help", list) stgs.add_argparser_to_option("section.key", parser, positional_arg=True) self.assertEqual(stgs.as_dict().get("section.key"), [])
def test_string(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option(section="foo", key="bar", default="just a test", help_msg="just a test") config = stgs.as_dict() result = config.get("foo.bar") self.assertIsInstance(result, str) self.assertEqual(result, "just a test")
def test_string(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option(section='foo', key='bar', default='just a test', help_msg='just a test') config = stgs.as_dict() result = config.get('foo.bar') self.assertIsInstance(result, str) self.assertEqual(result, 'just a test')
def test_add_argparser(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option("section", "key", "default", "help") parser = argparse.ArgumentParser(description="description") stgs.add_argparser_to_option("section.key", parser, "--long-arg") with self.assertRaises(settings.SettingsError): stgs.add_argparser_to_option("section.key", parser, "--other-arg") stgs.add_argparser_to_option("section.key", parser, "--other-arg", allow_multiple=True)
def test_add_argparser(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option('section', 'key', 'default', 'help') parser = argparse.ArgumentParser(description='description') stgs.add_argparser_to_option('section.key', parser, '--long-arg') with self.assertRaises(settings.SettingsError): stgs.add_argparser_to_option('section.key', parser, '--other-arg') stgs.add_argparser_to_option('section.key', parser, '--other-arg', allow_multiple=True)
def test_bool(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option(section='foo', key='bar', default=False, key_type=bool, help_msg='just a test') config = stgs.as_dict() result = config.get('foo.bar') self.assertIsInstance(result, bool) self.assertFalse(result)
def test_list(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option(section='foo', key='bar', key_type=list, default=[], help_msg='just a test') config = stgs.as_dict() result = config.get('foo.bar') self.assertIsInstance(result, list) self.assertEqual(0, len(result))
def test_override_default(self): """Test if default option is being overwritten by configfile.""" stgs = settings.Settings() stgs.process_config_path(self.config_file.name) default = "default from code" stgs.register_option(section="foo", key="bar", default=default, help_msg="just a test") stgs.merge_with_configs() config = stgs.as_dict() self.assertEqual(config.get("foo.bar"), "default from file")
def test_override_default(self): """Test if default option is being overwritten by configfile.""" stgs = settings.Settings() stgs.process_config_path(self.config_file.name) default = 'default from code' stgs.register_option(section='foo', key='bar', default=default, help_msg='just a test') stgs.merge_with_configs() config = stgs.as_dict() self.assertEqual(config.get('foo.bar'), 'default from file')
def test_filter(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option(section='foo', key='bar', default='default from file', help_msg='just a test') stgs.register_option(section='foo', key='baz', default='other default from file', help_msg='just a test') self.assertEqual(stgs.as_dict(r'foo.bar$'), {'foo.bar': 'default from file'})
def test_datadir_from_config(self): """ When avocado.conf is present, honor the values coming from it. """ stg = settings.Settings(self.config_file_path) # Trick the module to think we're on a system wide install stg.intree = False with unittest.mock.patch('avocado.core.data_dir.settings.settings', stg): from avocado.core import data_dir self.assertFalse(data_dir.settings.settings.intree) for key in self.mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) self.assertEqual(data_dir_func(), stg.get_value('datadir.paths', key)) # make sure that without the patch, we have a different value here self.assertTrue(data_dir.settings.settings.intree)
def test_datadir_from_config(self): """ When avocado.conf is present, honor the values coming from it. """ stg = settings.Settings() with unittest.mock.patch('avocado.core.stgs', stg): import avocado.core avocado.core.register_core_options() stg.process_config_path(self.config_file_path) stg.merge_with_configs() with unittest.mock.patch('avocado.core.data_dir.settings', stg): from avocado.core import data_dir for key in self.mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) namespace = 'datadir.paths.{}'.format(key) self.assertEqual(data_dir_func(), stg.as_dict().get(namespace))
def test_filter(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) stgs.register_option( section="foo", key="bar", default="default from file", help_msg="just a test", ) stgs.register_option( section="foo", key="baz", default="other default from file", help_msg="just a test", ) self.assertEqual(stgs.as_dict(r"foo.bar$"), {"foo.bar": "default from file"})
def testDataDirFromConfig(self): """ When avocado.conf is present, honor the values coming from it. """ stg_orig = settings.settings stg = settings.Settings(self.config_file_path) try: # Trick the module to think we're on a system wide install stg.intree = False flexmock(settings, settings=stg) from avocado.core import data_dir flexmock(data_dir.settings, settings=stg) self.assertFalse(data_dir.settings.settings.intree) for key in self.mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) self.assertEqual(data_dir_func(), stg.get_value('datadir.paths', key)) finally: flexmock(settings, settings=stg_orig) del data_dir
def test_settings_dir_alternate_dynamic(self): """ Tests that changes to the data_dir settings are applied dynamically To guarantee that, first the data_dir module is loaded. Then a new, alternate set of data directories are created and set in the "canonical" settings location, that is, avocado.core.settings.settings. No data_dir module reload should be necessary to get the new locations from data_dir APIs. """ stg_orig = settings.settings from avocado.core import data_dir (self.alt_mapping, self.alt_config_file_path ) = self._get_temporary_dirs_mapping_and_config() stg = settings.Settings(self.alt_config_file_path) flexmock(settings, settings=stg) for key in self.alt_mapping.keys(): data_dir_func = getattr(data_dir, 'get_%s' % key) self.assertEqual(data_dir_func(), self.alt_mapping[key]) flexmock(settings, settings=stg_orig) del data_dir
def setUp(self): self.config_file = tempfile.NamedTemporaryFile('w', delete=False) self.config_file.write(example_1) self.config_file.close() self.settings = settings.Settings(self.config_file.name)
def test_non_existing_key(self): stgs = settings.Settings() stgs.process_config_path(self.config_file.name) config = stgs.as_dict() self.assertIsNone(config.get('foo.non_existing'))
def test_get_job_results_dir(self): from avocado.core import data_dir, job_id # First let's mock a jobs results directory # logs_dir = self.mapping.get('logs_dir') self.assertNotEqual(None, logs_dir) unique_id = job_id.create_unique_job_id() # Expected job results dir expected_jrd = data_dir.create_job_logs_dir(logs_dir, unique_id) # Now let's test some cases # self.assertEqual(None, data_dir.get_job_results_dir(expected_jrd, logs_dir), ("If passing a directory reference, it expects the id" "file")) # Create the id file. id_file_path = os.path.join(expected_jrd, 'id') with open(id_file_path, 'w') as id_file: id_file.write("%s\n" % unique_id) id_file.flush() os.fsync(id_file) self.assertEqual(expected_jrd, data_dir.get_job_results_dir(expected_jrd, logs_dir), "It should get from the path to the directory") results_dirname = os.path.basename(expected_jrd) self.assertEqual( None, data_dir.get_job_results_dir(results_dirname, logs_dir), "It should not get from a valid path to the directory") pwd = os.getcwd() os.chdir(logs_dir) self.assertEqual( expected_jrd, data_dir.get_job_results_dir(results_dirname, logs_dir), "It should get from relative path to the directory") os.chdir(pwd) self.assertEqual(expected_jrd, data_dir.get_job_results_dir(id_file_path, logs_dir), "It should get from the path to the id file") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id, logs_dir), "It should get from the id") another_id = job_id.create_unique_job_id() self.assertNotEqual(unique_id, another_id) self.assertEqual(None, data_dir.get_job_results_dir(another_id, logs_dir), "It should not get from unexisting job") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id[:7], logs_dir), "It should get from partial id equals to 7 digits") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id[:4], logs_dir), "It should get from partial id less than 7 digits") almost_id = unique_id[:7] + ('a' * (len(unique_id) - 7)) self.assertNotEqual(unique_id, almost_id) self.assertEqual(None, data_dir.get_job_results_dir(almost_id, logs_dir), ("It should not get if the id is equal on only" "the first 7 characters")) os.symlink(expected_jrd, os.path.join(logs_dir, 'latest')) self.assertEqual(expected_jrd, data_dir.get_job_results_dir('latest', logs_dir), "It should get from the 'latest' id") stg = settings.Settings() with unittest.mock.patch('avocado.core.stgs', stg): import avocado.core avocado.core.register_core_options() stg.process_config_path(self.config_file_path) stg.merge_with_configs() with unittest.mock.patch('avocado.core.data_dir.settings', stg): self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id), "It should use the default base logs directory")
def setUp(self): (self.base_dir, self.mapping, self.config_file_path) = self._get_temporary_dirs_mapping_and_config() self.stg = settings.Settings(self.config_file_path) self.expected_images = self._create_test_files()