Skip to content

Commit

Permalink
Update component spec by versions
Browse files Browse the repository at this point in the history
Signed-off-by: Zelin Hao <[email protected]>
  • Loading branch information
zelinh committed Dec 5, 2024
1 parent 03c7e58 commit 8b85933
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/test_workflow/smoke_test/smoke_test_cluster_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(
self.dist = self.bundle_manifest.build.distribution
self.distribution = Distributions.get_distribution(self.product, self.dist, self.version, work_dir)

def cluster_version(self) -> str:
return self.version

def download_or_copy_bundle(self, work_dir: str) -> str:
extension = "tar.gz" if self.dist == "tar" else self.dist
artifact_name = f"{self.product}-{self.version}-{self.platform}-{self.arch}.{extension}"
Expand Down Expand Up @@ -87,11 +90,11 @@ def __check_cluster_ready__(self) -> bool:
logging.info(f"Pinging {url}")
try:
request = requests.get(url, verify=False, auth=("admin", "myStrongPassword123!"))
logging.info(f"Request is {request.text}")
logging.info(f"Cluster response is {request.text}")
return 200 <= request.status_code < 300
except requests.RequestException as e:
logging.info(f"Request is {request.text}")
print(f"Request failed: {e}")
print(f"Cluster check fails: {e}")
return False

def __uninstall__(self) -> None:
Expand Down
30 changes: 18 additions & 12 deletions src/test_workflow/smoke_test/smoke_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,27 @@ def __init__(self, args: TestArgs, test_manifest: TestManifest) -> None:
os.makedirs(self.tests_dir, exist_ok=True)
self.test_recorder = TestRecorder(self.args.test_run_id, "smoke-test", self.tests_dir, args.base_path)
self.save_log = self.test_recorder.test_results_logs
self.version = ""

def start_test(self, work_dir: Path) -> Any:
pass

def extract_paths_from_yaml(self, component: str) -> Any:
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "smoke_tests_spec", f"{component}.yml")
if os.path.exists(file_path):
logging.info(f"Component spec for {component} is found.")
with open(file_path, 'r') as file:
data = yaml.safe_load(file) # Load the YAML content
# Extract paths
paths = data.get('paths', {})
return paths
else:
logging.error("No spec found.")
sys.exit(1)
def extract_paths_from_yaml(self, component: str, version: str) -> Any:
base_path = os.path.dirname(os.path.abspath(__file__))
paths = [
os.path.join(base_path, "smoke_tests_spec", f"{version.split('.')[0]}.x", f"{component}.yml"),
os.path.join(base_path, "smoke_tests_spec", "default", f"{component}.yml")
]
for file_path in paths:
if os.path.exists(file_path):
logging.info(f"Component spec for {component} with path {file_path} is found.")
with open(file_path, 'r') as file:
data = yaml.safe_load(file) # Load the YAML content
# Extract paths
paths = data.get('paths', {})
return paths
logging.error("No spec found.")
sys.exit(1)

def convert_parameter_json(self, data: list) -> str:
return "\n".join(json.dumps(item) for item in data) + "\n" if data else ""
Expand All @@ -64,6 +69,7 @@ def run(self) -> Any:

logging.info("Initiating smoke tests.")
test_cluster = SmokeTestClusterOpenSearch(self.args, os.path.join(work_dir.path), self.test_recorder)
self.version = test_cluster.cluster_version()
test_cluster.__start_cluster__(os.path.join(work_dir.path))
is_cluster_ready = False
for i in range(10):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def start_test(self, work_dir: Path) -> TestSuiteResults:
for component in self.test_manifest.components.select(self.args.components):
if component.smoke_test:
logging.info(f"Running smoke test on {component.name} component.")
component_spec = self.extract_paths_from_yaml(component.name)
component_spec = self.extract_paths_from_yaml(component.name, self.version)
logging.info(f"component spec is {component_spec}")
test_results = TestComponentResults()
for api_requests, api_details in component_spec.items():
Expand Down
30 changes: 30 additions & 0 deletions src/test_workflow/smoke_test/smoke_tests_spec/2.x/opensearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# yamllint disable
---
info:
title: OpenSearch Core smoke tests
version: 2.x
name: opensearch
paths:
/:
GET:
parameters: []
/_bulk:
POST:
header: {
"Content-Type": "application/x-ndjson"
}
parameters: [
{ "delete": { "_index": "movies", "_id": "tt2229499" } },
{ "index": { "_index": "movies", "_id": "tt1979320" } },
{ "title": "Rush", "year": 2013 },
{ "create": { "_index": "movies", "_id": "tt1392214" } },
{ "title": "Prisoners", "year": 2013 },
{ "update": { "_index": "movies", "_id": "tt0816711" } },
{ "doc" : { "title": "World War Z" } }
]
/_cat/indices:
GET:
parameters: []
/_cat/plugins:
GET:
parameters: []
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def test_extract_paths_from_yaml(self, mock_exists: Mock, mock_dirname: Mock, mo
component_name = "test_component"

# Run extract_paths_from_yaml and check output
result = runner.extract_paths_from_yaml(component_name)
result = runner.extract_paths_from_yaml(component_name, "2.19.0")
expected_output = {"/_cat/plugins": {"get": {"parameters": []}}} # type: Any

self.assertEqual(result, expected_output)
mock_open_file.assert_called_once_with(os.path.join("/dummy-path", "smoke_tests_spec", f"{component_name}.yml"), 'r')
mock_open_file.assert_called_once_with(os.path.join("/dummy-path", "smoke_tests_spec", "2.x", f"{component_name}.yml"), 'r')

@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
def test_convert_parameter_json(self, mock_recorder: Mock) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_smoke_test_runner_opensearch_start_test(self, mock_dirname: Mock, mock_
mock_extract_spec.return_value = {"/": {"GET": {}}, "/_cat/plugins": {"GET": {}}}

runner = SmokeTestRunnerOpenSearch(self.args, self.test_manifest)
runner.version = "2.19.0"

# Mock a successful API response
response = Response()
Expand All @@ -53,7 +54,7 @@ def test_smoke_test_runner_opensearch_start_test(self, mock_dirname: Mock, mock_

results = runner.start_test(work_dir=Path("/temp/path"))
self.assertTrue(results)
mock_extract_spec.assert_called_with("opensearch")
mock_extract_spec.assert_called_with("opensearch", "2.19.0")
self.test_manifest.components.select.assert_called()
mock_get.assert_has_calls(
[call('https://localhost:9200/', verify=False, auth=('admin', 'myStrongPassword123!'),
Expand Down

0 comments on commit 8b85933

Please sign in to comment.