|
| 1 | +# Copyright OpenSearch Contributors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# The OpenSearch Contributors require contributions made to |
| 5 | +# this file be licensed under the Apache-2.0 license or a |
| 6 | +# compatible open source license. |
| 7 | + |
| 8 | +import logging |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import time |
| 12 | + |
| 13 | +import requests |
| 14 | + |
| 15 | +from git.git_repository import GitRepository |
| 16 | +from manifests.build_manifest import BuildManifest |
| 17 | +from manifests.bundle_manifest import BundleManifest |
| 18 | +from manifests.test_manifest import TestManifest |
| 19 | +from system.process import Process |
| 20 | +from test_workflow.integ_test.distributions import Distributions |
| 21 | +from test_workflow.test_args import TestArgs |
| 22 | +from test_workflow.test_recorder.test_recorder import TestRecorder |
| 23 | + |
| 24 | + |
| 25 | +class SmokeTestClusterOpenSearch(): |
| 26 | + # dependency_installer: DependencyInstallerOpenSearch |
| 27 | + repo: GitRepository |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + args: TestArgs, |
| 32 | + work_dir: str, |
| 33 | + test_recorder: TestRecorder |
| 34 | + ) -> None: |
| 35 | + self.args = args |
| 36 | + self.work_dir = work_dir |
| 37 | + self.test_recorder = test_recorder |
| 38 | + self.process_handler = Process() |
| 39 | + self.test_manifest = TestManifest.from_path(args.test_manifest_path) |
| 40 | + self.product = self.test_manifest.name.lower().replace(" ", "-") |
| 41 | + self.path = args.paths.get(self.product) |
| 42 | + self.build_manifest = BuildManifest.from_urlpath(os.path.join(self.path, "builds", f"{self.product}", "manifest.yml")) |
| 43 | + self.bundle_manifest = BundleManifest.from_urlpath(os.path.join(self.path, "dist", f"{self.product}", "manifest.yml")) |
| 44 | + self.version = self.bundle_manifest.build.version |
| 45 | + self.platform = self.bundle_manifest.build.platform |
| 46 | + self.arch = self.bundle_manifest.build.architecture |
| 47 | + self.dist = self.bundle_manifest.build.distribution |
| 48 | + self.distribution = Distributions.get_distribution(self.product, self.dist, self.version, work_dir) |
| 49 | + |
| 50 | + def cluster_version(self) -> str: |
| 51 | + return self.version |
| 52 | + |
| 53 | + def download_or_copy_bundle(self, work_dir: str) -> str: |
| 54 | + extension = "tar.gz" if self.dist == "tar" else self.dist |
| 55 | + artifact_name = f"{self.product}-{self.version}-{self.platform}-{self.arch}.{extension}" |
| 56 | + src_path = '/'.join([self.path.rstrip("/"), "dist", f"{self.product}", f"{artifact_name}"]) \ |
| 57 | + if self.path.startswith("https://") else os.path.join(self.path, "dist", |
| 58 | + f"{self.product}", f"{artifact_name}") |
| 59 | + dest_path = os.path.join(work_dir, artifact_name) |
| 60 | + |
| 61 | + if src_path.startswith("https://"): |
| 62 | + logging.info(f"Downloading artifacts to {dest_path}") |
| 63 | + response = requests.get(src_path) |
| 64 | + with open(dest_path, "wb") as file: |
| 65 | + file.write(response.content) |
| 66 | + else: |
| 67 | + logging.info(f"Trying to copy {src_path} to {dest_path}") |
| 68 | + # Only copy if it's a file |
| 69 | + if os.path.isfile(src_path): |
| 70 | + shutil.copy2(src_path, dest_path) |
| 71 | + logging.info(f"Copied {src_path} to {dest_path}") |
| 72 | + return artifact_name |
| 73 | + |
| 74 | + # Reason we don't re-use test-suite from integ-test is that it's too specific and not generic and lightweight. |
| 75 | + def __installation__(self, work_dir: str) -> None: |
| 76 | + self.distribution.install(self.download_or_copy_bundle(work_dir)) |
| 77 | + logging.info("Cluster is installed and ready to be start.") |
| 78 | + |
| 79 | + # Start the cluster after installed and provide endpoint. |
| 80 | + def __start_cluster__(self, work_dir: str) -> None: |
| 81 | + self.__installation__(work_dir) |
| 82 | + self.process_handler.start(self.distribution.start_cmd, self.distribution.install_dir, self.distribution.require_sudo) |
| 83 | + logging.info(f"Started OpenSearch with parent PID {self.process_handler.pid}") |
| 84 | + time.sleep(30) |
| 85 | + logging.info("Cluster is started.") |
| 86 | + |
| 87 | + # Check if the cluster is ready |
| 88 | + def __check_cluster_ready__(self) -> bool: |
| 89 | + url = "https://localhost:9200/" |
| 90 | + logging.info(f"Pinging {url}") |
| 91 | + try: |
| 92 | + request = requests.get(url, verify=False, auth=("admin", "myStrongPassword123!")) |
| 93 | + logging.info(f"Cluster response is {request.text}") |
| 94 | + return 200 <= request.status_code < 300 |
| 95 | + except requests.RequestException as e: |
| 96 | + logging.info(f"Request is {request.text}") |
| 97 | + logging.info(f"Cluster check fails: {e}") |
| 98 | + return False |
| 99 | + |
| 100 | + def __uninstall__(self) -> None: |
| 101 | + self.process_handler.terminate() |
| 102 | + logging.info("Cluster is terminated.") |
0 commit comments