Skip to content

Commit acc4ee8

Browse files
committed
Add mock scripts for various commands and enhance Azure Load Balancer metadata handling
1 parent 4557e04 commit acc4ee8

16 files changed

+573
-32
lines changed

src/roles/ha_db_hana/tasks/fs-freeze.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
changed_when: cleanup_failed_resource_post.rc == 0
5252
ignore_errors: true
5353

54-
- name: "Test Execution: Validate HANA DB cluster status"
54+
- name: "Test Execution: Validate HANA DB cluster status 1"
5555
when: cluster_status_pre.AUTOMATED_REGISTER | lower == "true"
5656
get_cluster_status_db:
5757
operation_step: "test_execution"
@@ -67,7 +67,7 @@
6767
- name: "Test Execution: Freeze File System on Primary Node init"
6868
when: ansible_hostname == cluster_status_pre.primary_node
6969
block:
70-
- name: "Test Execution: Validate HANA DB cluster status"
70+
- name: "Test Execution: Validate HANA DB cluster status 2"
7171
get_cluster_status_db:
7272
operation_step: "post_failover"
7373
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/sbd-fencing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
- name: "Test Execution: SBD Inquisitor kill manual fail over"
7070
when: ansible_hostname == cluster_status_pre.primary_node
7171
block:
72-
- name: "Test Execution: Validate HANA DB cluster status"
72+
- name: "Test Execution: Validate HANA DB cluster status 2"
7373
get_cluster_status_db:
7474
operation_step: "test_execution"
7575
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/secondary-crash-index.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
cluster_status_test_execution.primary_node == cluster_status_pre.primary_node and
6565
cluster_status_test_execution.secondary_node == ""
6666
67-
- name: "Test Execution: Validate HANA DB cluster status"
67+
- name: "Test Execution: Validate HANA DB cluster status 2"
6868
get_cluster_status_db:
6969
operation_step: "post_failover"
7070
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/secondary-echo-b.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
cluster_status_test_execution.primary_node == cluster_status_pre.primary_node and
6060
cluster_status_test_execution.secondary_node == ""
6161
62-
- name: "Test Execution: Validate HANA DB cluster status"
62+
- name: "Test Execution: Validate HANA DB cluster status 2"
6363
get_cluster_status_db:
6464
operation_step: "post_failover"
6565
database_sid: "{{ db_sid | lower }}"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
Test class for Azure LB configuration validation tasks.
6+
7+
This test class uses pytest to run functional tests on the Azure LB configuration validation tasks
8+
defined in roles/ha_db_hana/tasks/azure-lb.yml. It sets up a temporary test environment,
9+
mocks necessary Python modules and commands, and verifies the execution of the tasks.
10+
"""
11+
12+
import os
13+
import shutil
14+
from pathlib import Path
15+
import pytest
16+
from tests.roles.ha_db_hana.roles_testing_base_db import RolesTestingBaseDB
17+
18+
19+
class TestAzLBConfigValidation(RolesTestingBaseDB):
20+
"""
21+
Test class for Azure LB configuration validation tasks.
22+
"""
23+
24+
@pytest.fixture
25+
def test_environment(self, ansible_inventory):
26+
"""
27+
Set up a temporary test environment for the Azure LB configuration validation tasks.
28+
29+
:param ansible_inventory: Path to the Ansible inventory file.
30+
:type ansible_inventory: str
31+
:yield temp_dir: Path to the temporary test environment.
32+
:ytype: str
33+
"""
34+
35+
temp_dir = self.setup_test_environment(
36+
role_type="ha_db_hana",
37+
ansible_inventory=ansible_inventory,
38+
task_name="azure-lb",
39+
task_description="The test validates the Azure load balancer configuration.",
40+
module_names=[
41+
"project/library/get_azure_lb",
42+
"project/library/log_parser",
43+
"project/library/send_telemetry_data",
44+
"project/library/get_package_list",
45+
"bin/crm_resource",
46+
],
47+
extra_vars_override={"node_tier": "hana"},
48+
)
49+
50+
os.makedirs(f"{temp_dir}/project/roles/ha_db_hana/tasks/files", exist_ok=True)
51+
self.file_operations(
52+
operation="write",
53+
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/files/constants.yaml",
54+
content=self.file_operations(
55+
operation="read",
56+
file_path=Path(__file__).parent.parent / "mock_data/mock_azure_lb.txt",
57+
),
58+
)
59+
60+
os.makedirs(f"{temp_dir}/project/library", exist_ok=True)
61+
self.file_operations(
62+
operation="write",
63+
file_path=f"{temp_dir}/project/library/uri",
64+
content=self.file_operations(
65+
operation="read",
66+
file_path=Path(__file__).parent.parent / "mock_data/azure_metadata.txt",
67+
),
68+
)
69+
os.chmod(f"{temp_dir}/project/library/uri", 0o755)
70+
71+
yield temp_dir
72+
shutil.rmtree(temp_dir)
73+
74+
def test_ha_config_validation_success(self, test_environment, ansible_inventory):
75+
"""
76+
Test the Azure LB configuration validation tasks using Ansible Runner.
77+
78+
:param test_environment: Path to the temporary test environment.
79+
:type test_environment: str
80+
:param ansible_inventory: Path to the Ansible inventory file.
81+
:type ansible_inventory: str
82+
"""
83+
result = self.run_ansible_playbook(
84+
test_environment=test_environment, inventory_file_name="inventory_db.txt"
85+
)
86+
87+
assert result.rc == 0, (
88+
f"Playbook failed with status: {result.rc}\n"
89+
f"STDOUT: {result.stdout.read() if result.stdout else 'No output'}\n"
90+
f"STDERR: {result.stderr.read() if result.stderr else 'No errors'}\n"
91+
f"Events: {[e.get('event') for e in result.events if 'event' in e]}"
92+
)
93+
94+
ok_events, failed_events = [], []
95+
for event in result.events:
96+
if event.get("event") == "runner_on_ok":
97+
ok_events.append(event)
98+
elif event.get("event") == "runner_on_failed":
99+
failed_events.append(event)
100+
101+
assert len(ok_events) > 0
102+
assert len(failed_events) == 0
103+
104+
for event in ok_events:
105+
task = event.get("event_data", {}).get("task")
106+
task_result = event.get("event_data", {}).get("res")
107+
if "Retrieve Subscription ID" in task:
108+
assert task_result.get("changed") is False
109+
if "Azure Load Balancer check" in task:
110+
assert task_result.get("changed") is False
111+
assert task_result["details"]["parameters"][1].get("name") == "probe_threshold"
112+
assert task_result["details"]["parameters"][1].get("value") == "2"

tests/roles/ha_db_hana/primary_node_ops_test.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ class TestDbHDBOperations(RolesTestingBaseDB):
2323
"""
2424

2525
@pytest.fixture(
26-
params=["primary-node-crash", "primary-node-kill", "primary-echo-b", "primary-crash-index"]
26+
params=[
27+
"primary-node-crash",
28+
"primary-node-kill",
29+
"primary-echo-b",
30+
"primary-crash-index",
31+
"sbd-fencing",
32+
"fs-freeze",
33+
]
2734
)
2835
def task_type(self, request):
2936
"""
@@ -64,6 +71,19 @@ def task_type(self, request):
6471
"command_type": "killall",
6572
"validate_task": "Test Execution: Validate HANA DB cluster status",
6673
}
74+
elif task_name == "sbd-fencing":
75+
return {
76+
"task_name": task_name,
77+
"command_task": "Test Execution: Kill the inquisitor process",
78+
"command_type": "kill",
79+
"validate_task": "Test Execution: Validate HANA DB cluster status 2",
80+
}
81+
elif task_name == "fs-freeze":
82+
return {
83+
"task_name": task_name,
84+
"validate_task": "Test Execution: Validate HANA DB cluster status 2",
85+
"command_task": "dummy (no command)",
86+
}
6787

6888
@pytest.fixture
6989
def test_environment(self, ansible_inventory, task_type):
@@ -82,23 +102,33 @@ def test_environment(self, ansible_inventory, task_type):
82102
if os.path.exists(task_counter_file):
83103
os.remove(task_counter_file)
84104

105+
module_names = [
106+
"project/library/get_cluster_status_db",
107+
"project/library/log_parser",
108+
"project/library/send_telemetry_data",
109+
"project/library/location_constraints",
110+
"project/library/check_indexserver",
111+
"project/library/filesystem_freeze",
112+
"bin/crm_resource",
113+
"bin/crm",
114+
"bin/echo",
115+
"bin/killall",
116+
]
117+
118+
if task_type["task_name"] == "sbd-fencing":
119+
module_names.extend(["bin/pgrep", "bin/kill", "bin/head"])
120+
85121
temp_dir = self.setup_test_environment(
86122
role_type="ha_db_hana",
87123
ansible_inventory=ansible_inventory,
88124
task_name=task_type["task_name"],
89125
task_description=f"The {task_type['task_name']} test validates failover scenarios",
90-
module_names=[
91-
"project/library/get_cluster_status_db",
92-
"project/library/log_parser",
93-
"project/library/send_telemetry_data",
94-
"project/library/location_constraints",
95-
"project/library/check_indexserver",
96-
"bin/crm_resource",
97-
"bin/crm",
98-
"bin/echo",
99-
"bin/killall",
100-
],
101-
extra_vars_override={"node_tier": "hana"},
126+
module_names=module_names,
127+
extra_vars_override={
128+
"node_tier": "hana",
129+
"NFS_provider": "ANF",
130+
"database_cluster_type": "ISCSI",
131+
},
102132
)
103133

104134
os.makedirs(f"{temp_dir}/bin", exist_ok=True)
@@ -119,7 +149,7 @@ def test_environment(self, ansible_inventory, task_type):
119149
self.file_operations(
120150
operation="write",
121151
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/{task_type['task_name']}.yml",
122-
content=playbook_content.replace(
152+
content=playbook_content.replace("set -o pipefail\n", "").replace(
123153
"/usr/sap/{{ db_sid | upper }}/HDB{{ db_instance_number }}/", ""
124154
),
125155
)
@@ -170,8 +200,8 @@ def test_functional_db_primary_node_success(
170200
task = event.get("event_data", {}).get("task")
171201
task_result = event.get("event_data", {}).get("res")
172202

173-
if task and task_type["command_task"] in task:
174-
if task_type["command_type"] == "echo b":
203+
if task and task_type.get("command_task") in task:
204+
if task_type["command_type"] == "echo b" or task_type["command_type"] == "kill":
175205
assert task_result.get("changed") is True
176206
else:
177207
assert task_result.get("rc") == 0

0 commit comments

Comments
 (0)