-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenSearchSourceDownloader Migration (#184)
* Initial OpenSearchSourceDownloader migration Signed-off-by: Chase Engelbrecht <[email protected]> * Finish OpenSearchDownloader implementation Signed-off-by: Chase Engelbrecht <[email protected]> * Finish OpenSearchSourceDownloader and add unit tests Signed-off-by: Chase Engelbrecht <[email protected]>
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
osbenchmark/builder/downloaders/opensearch_source_downloader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import logging | ||
import os | ||
|
||
from osbenchmark.builder.downloaders.downloader import Downloader | ||
from osbenchmark.builder.utils.binary_keys import BinaryKeys | ||
|
||
|
||
class OpenSearchSourceDownloader(Downloader): | ||
def __init__(self, provision_config_instance, executor, source_repository_provider, binary_builder, template_renderer, | ||
artifact_variables_provider): | ||
super().__init__(executor) | ||
self.logger = logging.getLogger(__name__) | ||
self.provision_config_instance = provision_config_instance | ||
self.source_repository_provider = source_repository_provider | ||
self.binary_builder = binary_builder | ||
self.template_renderer = template_renderer | ||
self.artifact_variables_provider = artifact_variables_provider | ||
|
||
def download(self, host): | ||
opensearch_source_path = self._get_opensearch_source_path() | ||
self._fetch(host, opensearch_source_path) | ||
|
||
artifact_variables = self.artifact_variables_provider.get_artifact_variables(host) | ||
self._prepare(host, artifact_variables) | ||
|
||
return {BinaryKeys.OPENSEARCH: self._get_zip_path(opensearch_source_path, artifact_variables)} | ||
|
||
def _get_opensearch_source_path(self): | ||
node_root_dir = self.provision_config_instance.variables["source"]["root"]["dir"] | ||
opensearch_source_subdir = self.provision_config_instance.variables["source"]["opensearch"]["subdir"] | ||
return os.path.join(node_root_dir, opensearch_source_subdir) | ||
|
||
def _fetch(self, host, opensearch_source_path): | ||
plugin_remote_url = self.provision_config_instance.variables["source"]["remote"]["repo"]["url"] | ||
plugin_revision = self.provision_config_instance.variables["source"]["revision"] | ||
|
||
self.source_repository_provider.fetch_repository(host, plugin_remote_url, plugin_revision, opensearch_source_path) | ||
|
||
def _prepare(self, host, artifact_variables): | ||
clean_command_template = self.provision_config_instance.variables["source"]["clean"]["command"] | ||
build_command_template = self.provision_config_instance.variables["source"]["build"]["command"] | ||
|
||
if self.binary_builder: | ||
self.binary_builder.build(host, [ | ||
self.template_renderer.render_template_string(clean_command_template, artifact_variables), | ||
self.template_renderer.render_template_string(build_command_template, artifact_variables) | ||
]) | ||
|
||
def _get_zip_path(self, opensearch_source_path, artifact_variables): | ||
artifact_path_pattern_template = self.provision_config_instance.variables["source"]["artifact_path_pattern"] | ||
artifact_path_pattern = self.template_renderer.render_template_string(artifact_path_pattern_template, artifact_variables) | ||
|
||
return os.path.join(opensearch_source_path, artifact_path_pattern) |
66 changes: 66 additions & 0 deletions
66
tests/builder/downloaders/opensearch_source_downloader_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from unittest import TestCase, mock | ||
from unittest.mock import Mock | ||
|
||
from osbenchmark.builder.downloaders.opensearch_source_downloader import OpenSearchSourceDownloader | ||
from osbenchmark.builder.provision_config import ProvisionConfigInstance | ||
from osbenchmark.builder.utils.binary_keys import BinaryKeys | ||
|
||
|
||
class OpenSearchSourceDownloaderTest(TestCase): | ||
def setUp(self): | ||
self.host = None | ||
|
||
self.executor = Mock() | ||
self.source_repository_provider = Mock() | ||
self.binary_builder = Mock() | ||
self.template_renderer = Mock() | ||
self.artifact_variables_provider = Mock() | ||
|
||
self.provision_config_instance = ProvisionConfigInstance(names="fake", root_path="also fake", config_paths="fake2", variables={ | ||
"source": { | ||
"root": { | ||
"dir": "/fake/dir/for/source" | ||
}, | ||
"opensearch": { | ||
"subdir": "opensearch_sub-dir" | ||
}, | ||
"remote": { | ||
"repo": { | ||
"url": "https://git.remote.fake" | ||
} | ||
}, | ||
"revision": "current", | ||
"artifact_path_pattern": "{{OSNAME}}.tar.gz", | ||
"build": { | ||
"command": "gradle build" | ||
}, | ||
"clean": { | ||
"command": "gradle clean" | ||
} | ||
} | ||
}) | ||
|
||
self.opensearch_source_downloader = OpenSearchSourceDownloader(self.provision_config_instance, self.executor, | ||
self.source_repository_provider, self.binary_builder, | ||
self.template_renderer, self.artifact_variables_provider) | ||
|
||
def test_download(self): | ||
self.artifact_variables_provider.get_artifact_variables.return_value = {"OSNAME": "fake_OS"} | ||
self.template_renderer.render_template_string.side_effect = ["fake clean", "fake build", "fake artifact path"] | ||
|
||
opensearch_binary = self.opensearch_source_downloader.download(self.host) | ||
self.assertEqual(opensearch_binary, {BinaryKeys.OPENSEARCH: "/fake/dir/for/source/opensearch_sub-dir/fake artifact path"}) | ||
self.source_repository_provider.fetch_repository.assert_has_calls([ | ||
mock.call(self.host, "https://git.remote.fake", "current", "/fake/dir/for/source/opensearch_sub-dir") | ||
]) | ||
self.binary_builder.build.assert_has_calls([ | ||
mock.call(self.host, ["fake clean", "fake build"]) | ||
]) | ||
self.artifact_variables_provider.get_artifact_variables.assert_has_calls([ | ||
mock.call(self.host) | ||
]) | ||
self.template_renderer.render_template_string.assert_has_calls([ | ||
mock.call("gradle clean", {"OSNAME": "fake_OS"}), | ||
mock.call("gradle build", {"OSNAME": "fake_OS"}), | ||
mock.call("{{OSNAME}}.tar.gz", {"OSNAME": "fake_OS"}) | ||
]) |