From 72f7f3ff21465862d593bdf0d935745df1f70736 Mon Sep 17 00:00:00 2001 From: Vacha Date: Thu, 26 Aug 2021 14:45:36 -0700 Subject: [PATCH] Addressing comments and refactoring Signed-off-by: Vacha --- .../src/test_workflow/dependency_installer.py | 43 +++++----- .../tests/test_workflow/helper/test_utils.py | 26 ++++++ .../test_dependency_installer.py | 81 ++++++++----------- 3 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 bundle-workflow/tests/test_workflow/helper/test_utils.py diff --git a/bundle-workflow/src/test_workflow/dependency_installer.py b/bundle-workflow/src/test_workflow/dependency_installer.py index 3fdb12c490..92c4014fcd 100644 --- a/bundle-workflow/src/test_workflow/dependency_installer.py +++ b/bundle-workflow/src/test_workflow/dependency_installer.py @@ -4,7 +4,7 @@ class DependencyInstaller: """ - Provides functionality to copy the maven dependencies from S3 to maven local to be used by tests. + Provides functionality to copy the maven dependencies from S3 to maven local. """ def __init__(self, build_id, dependency_name, version, arch): @@ -12,36 +12,41 @@ def __init__(self, build_id, dependency_name, version, arch): self.dependency_name = dependency_name self.version = version self.arch = arch - - def get_dependency_path(self): - return f"org/opensearch/{self.dependency_name}/{self.version}/" - - def get_maven_local_path(self): - return os.path.join( - os.path.expanduser("~"), ".m2/repository/", self.get_dependency_path() + self.dependency_path = f"org/opensearch/{self.dependency_name}/{self.version}/" + self.maven_local_path = os.path.join( + os.path.expanduser("~"), ".m2/repository/", self.dependency_path ) # TODO: This is currently a stubbed function which returns files from the current directory, # to be replaced after it is implemented - def download_from_s3(self): + def download(self): return [ file_name for file_name in os.listdir(os.path.dirname(os.path.abspath(__file__))) - if os.path.isfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name)) + if os.path.isfile( + os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name) + ) ] - def copy_to_maven_local(self, dependency_from_s3, maven_local_path): + def install(self): + # s3_path = f"/builds/{self.version}/{self.build_id}/{self.arch}/maven/{self.dependency_path()}" + file_handler = MavenLocalFileHandler() + file_handler.copy(self.download(), self.maven_local_path) + + +class MavenLocalFileHandler: + """ + Copies given dependencies to maven local. + """ + + def copy(self, dependency_from_s3, maven_local_path): + if not os.path.exists(maven_local_path): + os.makedirs(maven_local_path) for file_name in dependency_from_s3: local_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), file_name ) if os.path.isfile(local_file_path): shutil.copy(local_file_path, maven_local_path) - - def install(self): - # s3_path = f"/builds/{self.version}/{self.build_id}/{self.arch}/maven/{self.get_dependency_path()}" - maven_local_path = self.get_maven_local_path() - if not os.path.exists(maven_local_path): - os.makedirs(maven_local_path) - dependency_from_s3 = self.download_from_s3() - self.copy_to_maven_local(dependency_from_s3, maven_local_path) + else: + raise ValueError(f"No file found at path: {local_file_path}") diff --git a/bundle-workflow/tests/test_workflow/helper/test_utils.py b/bundle-workflow/tests/test_workflow/helper/test_utils.py new file mode 100644 index 0000000000..d8a95bfb70 --- /dev/null +++ b/bundle-workflow/tests/test_workflow/helper/test_utils.py @@ -0,0 +1,26 @@ +import os +import shutil + + +class TestUtils: + def get_test_dependencies(): + test_dir = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "../../../src/test_workflow" + ) + return [ + file_name + for file_name in os.listdir(test_dir) + if os.path.isfile(os.path.join(test_dir, file_name)) + ] + + def clean_maven_local_path(maven_local_path): + for file_name in os.listdir(maven_local_path): + local_file_path = os.path.join(maven_local_path, file_name) + try: + if os.path.isfile(local_file_path) or os.path.islink(local_file_path): + os.unlink(local_file_path) + elif os.path.isdir(local_file_path): + shutil.rmtree(local_file_path) + except OSError as e: + print(f"Failed to clean {local_file_path}. Reason: {e}") + raise diff --git a/bundle-workflow/tests/test_workflow/test_dependency_installer.py b/bundle-workflow/tests/test_workflow/test_dependency_installer.py index 43176940b3..cbdee4a269 100644 --- a/bundle-workflow/tests/test_workflow/test_dependency_installer.py +++ b/bundle-workflow/tests/test_workflow/test_dependency_installer.py @@ -1,59 +1,48 @@ import os -import shutil +import tempfile import unittest +from unittest.mock import patch from src.test_workflow.dependency_installer import DependencyInstaller +from src.test_workflow.dependency_installer import MavenLocalFileHandler +from helper.test_utils import TestUtils class DependencyInstallerTests(unittest.TestCase): - dependency_installer = DependencyInstaller("7", "job-scheduler", "1.1.0.0", "arm64") + def setUp(self): + self.dependency_installer = DependencyInstaller( + "7", "job-scheduler", "1.1.0.0", "arm64" + ) - def test_get_dependency_path(self): + def test_dependency_path(self): self.assertEqual( "org/opensearch/job-scheduler/1.1.0.0/", - self.dependency_installer.get_dependency_path(), - ) - - def test_copy_to_maven_local(self): - dependency_from_s3 = self.get_test_dependencies() - maven_local_path = self.dependency_installer.get_maven_local_path() - if not os.path.exists(maven_local_path): - os.makedirs(maven_local_path) - else: - self.clean_maven_local_path(maven_local_path) - self.dependency_installer.copy_to_maven_local( - dependency_from_s3, maven_local_path + self.dependency_installer.dependency_path, ) - self.assertCountEqual(dependency_from_s3, os.listdir(maven_local_path)) - self.assertListEqual(dependency_from_s3, os.listdir(maven_local_path)) def test_install(self): - maven_local_path = self.dependency_installer.get_maven_local_path() - self.clean_maven_local_path(maven_local_path) - self.dependency_installer.install() - self.assertCountEqual( - self.get_test_dependencies(), os.listdir(maven_local_path) - ) - self.assertListEqual(self.get_test_dependencies(), os.listdir(maven_local_path)) - - def get_test_dependencies(self): - test_dir = os.path.join( - os.path.dirname(os.path.abspath(__file__)), "../../src/test_workflow" - ) - return [ - file_name - for file_name in os.listdir(test_dir) - if os.path.isfile(os.path.join(test_dir, file_name)) - ] - - def clean_maven_local_path(self, maven_local_path): - for file_name in os.listdir(maven_local_path): - local_file_path = os.path.join(maven_local_path, file_name) - try: - if os.path.isfile(local_file_path) or os.path.islink(local_file_path): - os.unlink(local_file_path) - elif os.path.isdir(local_file_path): - shutil.rmtree(local_file_path) - except OSError as e: - print(f"Failed to clean {local_file_path}. Reason: {e}") - raise + maven_local_path = self.dependency_installer.maven_local_path + TestUtils.clean_maven_local_path(maven_local_path) + with patch( + "src.test_workflow.dependency_installer.MavenLocalFileHandler.copy" + ) as mock_maven_copy: + self.dependency_installer.maven_local_file_handler = [ + MavenLocalFileHandler(), + MavenLocalFileHandler(), + ] + self.dependency_installer.install() + mock_maven_copy.assert_called_with( + TestUtils.get_test_dependencies(), maven_local_path + ) + + +class MavenLocalFileHandlerTests(unittest.TestCase): + def setUp(self): + self.maven_local_file_handler = MavenLocalFileHandler() + + def test_copy(self): + maven_local_path = tempfile.mkdtemp() + test_files = TestUtils.get_test_dependencies() + self.maven_local_file_handler.copy(test_files, maven_local_path) + self.assertCountEqual(test_files, os.listdir(maven_local_path)) + self.assertListEqual(test_files, os.listdir(maven_local_path))