Exemple #1
0
 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)
Exemple #2
0
    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(self.config_file.name)
        config = stgs.as_dict()
        self.assertIsNone(config.get('foo.non_registered'))
Exemple #3
0
 def test_string(self):
     stgs = settings.Settings(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')
Exemple #4
0
 def setUp(self):
     (self.base_dir, self.mapping,
      self.config_file_path) = self._get_temporary_dirs_mapping_and_config()
     self.stg = future_settings.Settings()
     with unittest.mock.patch('avocado.core.future_settings', self.stg):
         import avocado.core
         avocado.core.register_core_options()
     self.stg.process_config_path(self.config_file_path)
     self.stg.merge_with_configs()
     self.expected_images = self._create_test_files()
Exemple #5
0
    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 = future_settings.Settings()
        with unittest.mock.patch('avocado.core.future_settings', 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.future_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,
            # pylint: disable=W0201
            self.alt_config_file_path
        ) = self._get_temp_dirs_mapping_and_config()

        # Alternate settings with different data_dir location
        alt_stg = future_settings.Settings()
        with unittest.mock.patch('avocado.core.future_settings', 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.future_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])
Exemple #6
0
 def test_list(self):
     stgs = settings.Settings(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))
Exemple #7
0
 def test_bool(self):
     stgs = settings.Settings(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)
Exemple #8
0
    def test_override_default(self):
        """Test if default option is being overwritten by configfile."""

        stgs = settings.Settings(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')
Exemple #9
0
 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)
Exemple #10
0
 def test_datadir_from_config(self):
     """
     When avocado.conf is present, honor the values coming from it.
     """
     stg = future_settings.Settings()
     with unittest.mock.patch('avocado.core.future_settings', 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.future_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))
Exemple #11
0
 def test_non_existing_key(self):
     stgs = settings.Settings(self.config_file.name)
     config = stgs.as_dict()
     self.assertIsNone(config.get('foo.non_existing'))
Exemple #12
0
    def test_get_job_results_dir(self):
        from avocado.core import data_dir
        from avocado.core import 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 = future_settings.Settings()
        with unittest.mock.patch('avocado.core.future_settings', 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.future_settings', stg):
            self.assertEqual(expected_jrd,
                             data_dir.get_job_results_dir(unique_id),
                             "It should use the default base logs directory")