From 71959211a52f4bc9dd6dd72c6b07e4a04c3a9fcb Mon Sep 17 00:00:00 2001 From: jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com> Date: Wed, 28 Jul 2021 08:59:27 -0700 Subject: [PATCH] [CIFuzz][NFC] Add tests for config_utils and do some minor refactoring (#6128) --- infra/cifuzz/config_utils.py | 22 ++++++++---- infra/cifuzz/config_utils_test.py | 57 ++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/infra/cifuzz/config_utils.py b/infra/cifuzz/config_utils.py index 121390337..dfc9947f3 100644 --- a/infra/cifuzz/config_utils.py +++ b/infra/cifuzz/config_utils.py @@ -20,11 +20,20 @@ import json import environment +DEFAULT_LANGUAGE = 'c++' +DEFAULT_SANITIZER = 'address' + def _get_project_repo_owner_and_name(): - # Includes owner and repo name. - github_repository = os.getenv('GITHUB_REPOSITORY', '') - return os.path.split(github_repository) + """Returns a tuple containing the project repo owner and the name of the + repo.""" + # On GitHub this includes owner and repo name. + repository = os.getenv('GITHUB_REPOSITORY', '') + # Use os.path.split. When GITHUB_REPOSITORY just contains the name of the + # repo, this will return a tuple containing an empty string and the repo name. + # When GITHUB_REPOSITORY contains the repo owner followed by a slash and then + # the repo name, it will return a tuple containing the owner and repo name. + return os.path.split(repository) def _get_pr_ref(event): @@ -34,7 +43,7 @@ def _get_pr_ref(event): def _get_sanitizer(): - return os.getenv('SANITIZER', 'address').lower() + return os.getenv('SANITIZER', DEFAULT_SANITIZER).lower() def _is_dry_run(): @@ -59,9 +68,6 @@ def get_project_src_path(workspace): return os.path.join(workspace, path) -DEFAULT_LANGUAGE = 'c++' - - def _get_language(): """Returns the project language.""" # Get language from environment. We took this approach because the convenience @@ -90,8 +96,10 @@ class BaseConfig: self.oss_fuzz_project_name = os.getenv('OSS_FUZZ_PROJECT_NAME') self.project_repo_owner, self.project_repo_name = ( _get_project_repo_owner_and_name()) + # Check if failures should not be reported. self.dry_run = _is_dry_run() + self.sanitizer = _get_sanitizer() # TODO(ochang): Error out if both oss_fuzz and build_integration_path is not # set. diff --git a/infra/cifuzz/config_utils_test.py b/infra/cifuzz/config_utils_test.py index d9f4c6cbe..e51a9dd38 100644 --- a/infra/cifuzz/config_utils_test.py +++ b/infra/cifuzz/config_utils_test.py @@ -18,7 +18,7 @@ import unittest import config_utils import test_helpers -# pylint: disable=no-self-use +# pylint: disable=no-self-use,protected-access class BaseConfigTest(unittest.TestCase): @@ -105,5 +105,60 @@ class RunFuzzersConfigTest(unittest.TestCase): self.assertEqual(config.run_fuzzers_mode, run_fuzzers_mode) +class GetProjectRepoOwnerAndNameTest(unittest.TestCase): + """Tests for _get_project_repo_owner_and_name.""" + + def setUp(self): + test_helpers.patch_environ(self) + self.repo_owner = 'repo-owner' + self.repo_name = 'repo-name' + + def test_unset_repository(self): + """Tests that the correct result is returned when repository is not set.""" + self.assertEqual(config_utils._get_project_repo_owner_and_name(), ('', '')) + + def test_empty_repository(self): + """Tests that the correct result is returned when repository is an empty + string.""" + os.environ['GITHUB_REPOSITORY'] = '' + self.assertEqual(config_utils._get_project_repo_owner_and_name(), ('', '')) + + def test_github_repository(self): + """Tests that the correct result is returned when repository contains the + owner and repo name (as it does on GitHub).""" + os.environ['GITHUB_REPOSITORY'] = f'{self.repo_owner}/{self.repo_name}' + self.assertEqual(config_utils._get_project_repo_owner_and_name(), + (self.repo_owner, self.repo_name)) + + def test_nongithub_repository(self): + """Tests that the correct result is returned when repository contains the + just the repo name (as it does outside of GitHub).""" + os.environ['GITHUB_REPOSITORY'] = self.repo_name + self.assertEqual(config_utils._get_project_repo_owner_and_name(), + ('', self.repo_name)) + + +class GetSanitizerTest(unittest.TestCase): + """Tests for _get_sanitizer.""" + + def setUp(self): + test_helpers.patch_environ(self) + self.sanitizer = 'memory' + + def test_default_value(self): + """Tests that the default value returned by _get_sanitizer is correct.""" + self.assertEqual(config_utils._get_sanitizer(), 'address') + + def test_normal_case(self): + """Tests that _get_sanitizer returns the correct value in normal cases.""" + os.environ['SANITIZER'] = self.sanitizer + self.assertEqual(config_utils._get_sanitizer(), self.sanitizer) + + def test_capitalization(self): + """Tests that that _get_sanitizer handles capitalization properly.""" + os.environ['SANITIZER'] = self.sanitizer.upper() + self.assertEqual(config_utils._get_sanitizer(), self.sanitizer) + + if __name__ == '__main__': unittest.main()