-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added DependencyInstaller to copy dependencies from s3 to maven local #318
Changes from all commits
c2881e9
126702f
a6e16d1
a357fd9
177774a
5649d4f
cb22248
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import os | ||
|
||
from aws.s3_bucket import S3Bucket | ||
|
||
|
||
class DependencyInstaller: | ||
""" | ||
Provides a dependency installer for the test suites. | ||
""" | ||
|
||
ARTIFACT_S3_BUCKET = "artifact-bucket-stack-buildbucket-9omh0hnpg12q" | ||
|
||
def __init__(self, build): | ||
self.build_id = build.id | ||
self.version = build.version | ||
self.arch = build.architecture | ||
self.s3_maven_location = ( | ||
f"/builds/{self.version}/{self.build_id}/{self.arch}/maven/org/opensearch" | ||
) | ||
self.s3_build_location = ( | ||
f"/builds/{self.version}/{self.build_id}/{self.arch}/plugins" | ||
) | ||
|
||
def install_maven_dependencies(self, dependency_dict): | ||
""" | ||
Downloads the maven dependencies from S3 and puts them on the maven local path | ||
for each dependency in the dependency_list. | ||
|
||
:param dependency_dict: list of dependency names with version for which the maven artifacts need to be downloaded. | ||
Example: {'opensearch-job-scheduler':'1.1.0.0', 'opensearch-core':'1.1.0'} | ||
""" | ||
s3_bucket = S3Bucket(self.ARTIFACT_S3_BUCKET) | ||
for dependency, version in dependency_dict.items(): | ||
s3_path = f"{self.s3_maven_location}/{dependency}/{version}" | ||
maven_local_path = self.maven_local_path(dependency, version) | ||
s3_bucket.download_folder(s3_path, maven_local_path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this is s3_relative_path There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be s3_maven_location for the dependency which is a relative path inside the bucket. An example - |
||
|
||
def install_build_dependencies(self, dependency_dict, custom_local_path): | ||
""" | ||
Downloads the build dependencies from S3 and puts them on the given custom path | ||
for each dependency in the dependency_list. | ||
|
||
:param dependency_list: list of dependency names with version for which the build artifacts need to be downloaded. | ||
Example: {'opensearch-job-scheduler':'1.1.0.0'} | ||
:param custom_local_path: the path where the downloaded dependencies need to copied. | ||
""" | ||
s3_bucket = S3Bucket(self.ARTIFACT_S3_BUCKET) | ||
for dependency, version in dependency_dict.items(): | ||
s3_path = f"{self.s3_build_location}/{dependency}-{version}.zip" | ||
s3_bucket.download_file(s3_path, custom_local_path) | ||
|
||
def maven_local_path(self, dependency, version): | ||
return os.path.join( | ||
os.path.expanduser("~"), | ||
f".m2/repository/org/opensearch/{dependency}/{version}/", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import call, patch | ||
|
||
from manifests.build_manifest import BuildManifest | ||
from test_workflow.dependency_installer import DependencyInstaller | ||
|
||
|
||
class DependencyInstallerTests(unittest.TestCase): | ||
def setUp(self): | ||
self.maxDiff = None | ||
self.manifest_filename = os.path.join( | ||
os.path.dirname(__file__), | ||
"../tests_assemble_workflow/data/opensearch-build-1.1.0.yml", | ||
) | ||
self.manifest = BuildManifest.from_path(self.manifest_filename) | ||
self.dependency_installer = DependencyInstaller(self.manifest.build) | ||
|
||
@patch("test_workflow.dependency_installer.S3Bucket") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can replace this with S3Bucket that's checked into main There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the mocked version of the |
||
def test_install_maven_dependencies(self, mock_s3_bucket): | ||
s3_bucket = mock_s3_bucket.return_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = S3Bucket() |
||
dependencies = dict( | ||
{ | ||
"opensearch-job-scheduler": "1.1.0.0", | ||
"opensearch-anomaly-detection": "1.1.0.0", | ||
} | ||
) | ||
self.dependency_installer.install_maven_dependencies(dependencies) | ||
self.assertEqual(s3_bucket.download_folder.call_count, 2) | ||
s3_bucket.download_folder.assert_has_calls( | ||
[ | ||
call( | ||
f"{self.dependency_installer.s3_maven_location}/opensearch-job-scheduler/1.1.0.0", | ||
self.dependency_installer.maven_local_path("opensearch-job-scheduler", "1.1.0.0"), | ||
), | ||
call( | ||
f"{self.dependency_installer.s3_maven_location}/opensearch-anomaly-detection/1.1.0.0", | ||
self.dependency_installer.maven_local_path("opensearch-anomaly-detection", "1.1.0.0"), | ||
), | ||
] | ||
) | ||
|
||
@patch("test_workflow.dependency_installer.S3Bucket") | ||
def test_install_build_dependencies(self, mock_s3_bucket): | ||
s3_bucket = mock_s3_bucket.return_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = S3Bucket() - because it's mocked you will get the mock object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it but it tries to call the actual constructor instead of mocked. |
||
dependencies = dict({"opensearch-job-scheduler": "1.1.0.0"}) | ||
self.dependency_installer.install_build_dependencies( | ||
dependencies, os.path.dirname(__file__) | ||
) | ||
self.assertEqual(s3_bucket.download_file.call_count, 1) | ||
s3_bucket.download_file.assert_has_calls( | ||
[ | ||
call( | ||
f"{self.dependency_installer.s3_build_location}/opensearch-job-scheduler-1.1.0.0.zip", | ||
os.path.dirname(__file__), | ||
) | ||
] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should provide a cleanup function to remove the dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dependency installer requirements - #276 (comment)