From 07b94e1ea695b290e36583c9607ba5b0147e3b42 Mon Sep 17 00:00:00 2001 From: Ang Li Date: Thu, 3 Dec 2026 02:31:32 +0000 Subject: [PATCH] Handle empty YAML sections and validate config structure in config_parser --- mobly/config_parser.py | 45 ++++++++++++++++---- tests/mobly/config_parser_test.py | 69 +++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 9 deletions(-) diff --git a/mobly/config_parser.py b/mobly/config_parser.py index 72f3883e2..1bda0a2ed 100644 --- a/mobly/config_parser.py +++ b/mobly/config_parser.py @@ -30,16 +30,40 @@ class MoblyConfigError(Exception): """Raised when there is a problem in test configuration file.""" +def _get_dict_section(mapping, key): + """Returns a dict config section, treating missing or None (empty YAML) as {}.""" + value = mapping.get(key) + if value is None: + return {} + if not isinstance(value, dict): + raise MoblyConfigError( + 'Value for %s must be a dict, got %s.' % (key, type(value).__name__) + ) + return value + + def _validate_test_config(test_config): """Validates the raw configuration loaded from the config file. - Making sure the required key 'TestBeds' is present. + Making sure the config is a dict and the required key 'TestBeds' is a + non-empty list. """ + if not isinstance(test_config, dict): + raise MoblyConfigError( + 'Test config must be a dict, got %s.' % type(test_config).__name__ + ) required_key = keys.Config.key_testbed.value if required_key not in test_config: raise MoblyConfigError( 'Required key %s missing in test config.' % required_key ) + if ( + not isinstance(test_config[required_key], list) + or not test_config[required_key] + ): + raise MoblyConfigError( + 'Value for %s must be a non-empty list.' % required_key + ) def _validate_testbed_name(name): @@ -103,6 +127,8 @@ def load_test_config_file(test_config_path, tb_filters=None): test_runner.TestRunner. """ configs = _load_config_file(test_config_path) + # Validate top-level config structure before accessing fields. + _validate_test_config(configs) if tb_filters: tbs = [] for tb in configs[keys.Config.key_testbed.value]: @@ -114,14 +140,15 @@ def load_test_config_file(test_config_path, tb_filters=None): ' you have the correct test bed names.' % (len(tb_filters), len(tbs)) ) configs[keys.Config.key_testbed.value] = tbs - mobly_params = configs.get(keys.Config.key_mobly_params.value, {}) + mobly_params = _get_dict_section(configs, keys.Config.key_mobly_params.value) # Decide log path. - log_path = mobly_params.get(keys.Config.key_log_path.value, _DEFAULT_LOG_PATH) + log_path = mobly_params.get(keys.Config.key_log_path.value) + if not log_path: + log_path = _DEFAULT_LOG_PATH if ENV_MOBLY_LOGPATH in os.environ: log_path = os.environ[ENV_MOBLY_LOGPATH] log_path = utils.abs_path(log_path) - # Validate configs - _validate_test_config(configs) + # Validate testbed configs. _validate_testbed_configs(configs[keys.Config.key_testbed.value]) # Transform config dict from user-facing key mapping to internal config object. test_configs = [] @@ -133,11 +160,11 @@ def load_test_config_file(test_config_path, tb_filters=None): # Deprecated, use testbed_name test_run_config.test_bed_name = test_run_config.testbed_name test_run_config.log_path = log_path - test_run_config.controller_configs = original_bed_config.get( - keys.Config.key_testbed_controllers.value, {} + test_run_config.controller_configs = _get_dict_section( + original_bed_config, keys.Config.key_testbed_controllers.value ) - test_run_config.user_params = original_bed_config.get( - keys.Config.key_testbed_test_params.value, {} + test_run_config.user_params = _get_dict_section( + original_bed_config, keys.Config.key_testbed_test_params.value ) test_configs.append(test_run_config) return test_configs diff --git a/tests/mobly/config_parser_test.py b/tests/mobly/config_parser_test.py index 46548f0f2..4f7f51066 100644 --- a/tests/mobly/config_parser_test.py +++ b/tests/mobly/config_parser_test.py @@ -56,6 +56,75 @@ def test__load_config_file_with_unicode(self): config = config_parser._load_config_file(tmp_file_path) self.assertEqual(config['TestBeds'][0]['Name'], '\u901a') + def _write_config(self, content): + tmp_file_path = os.path.join(self.tmp_dir, 'config.yml') + with io.open(tmp_file_path, 'w', encoding='utf-8') as f: + f.write(content) + return tmp_file_path + + def test_load_test_config_file_with_empty_sections(self): + path = self._write_config( + 'MoblyParams:\n' + 'TestBeds:\n' + ' - Name: SampleTestBed\n' + ' Controllers:\n' + ' TestParams:\n' + ) + configs = config_parser.load_test_config_file(path) + self.assertEqual(len(configs), 1) + self.assertEqual(configs[0].controller_configs, {}) + self.assertEqual(configs[0].user_params, {}) + self.assertEqual( + configs[0].log_path, + os.path.abspath(config_parser._DEFAULT_LOG_PATH), + ) + + def test_load_test_config_file_with_empty_log_path(self): + path = self._write_config( + 'MoblyParams:\n LogPath:\nTestBeds:\n - Name: SampleTestBed\n' + ) + configs = config_parser.load_test_config_file(path) + self.assertEqual( + configs[0].log_path, + os.path.abspath(config_parser._DEFAULT_LOG_PATH), + ) + + def test_load_test_config_file_empty_file_raises(self): + path = self._write_config('') + with self.assertRaisesRegex( + config_parser.MoblyConfigError, 'Test config must be a dict' + ): + config_parser.load_test_config_file(path) + + def test_load_test_config_file_empty_testbeds_raises(self): + path = self._write_config('TestBeds:\n') + with self.assertRaisesRegex( + config_parser.MoblyConfigError, 'must be a non-empty list' + ): + config_parser.load_test_config_file(path, tb_filters=['SampleTestBed']) + + def test_load_test_config_file_non_dict_sections_raise(self): + for yaml_body, key in [ + ( + 'MoblyParams: []\nTestBeds:\n - Name: SampleTestBed\n', + 'MoblyParams', + ), + ( + 'TestBeds:\n - Name: SampleTestBed\n Controllers: []\n', + 'Controllers', + ), + ( + 'TestBeds:\n - Name: SampleTestBed\n TestParams: false\n', + 'TestParams', + ), + ]: + with self.subTest(key=key): + path = self._write_config(yaml_body) + with self.assertRaisesRegex( + config_parser.MoblyConfigError, f'Value for {key} must be a dict' + ): + config_parser.load_test_config_file(path) + def test_run_config_type(self): config = config_parser.TestRunConfig() self.assertNotIn('summary_writer', str(config))