Skip to content

Commit 7459385

Browse files
committed
Add mock data scripts for HANA DB role testing and enhance existing test scripts
1 parent e858592 commit 7459385

16 files changed

+526
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
- name: "Test Execution: Validate HANA DB cluster status"
5252
when: ansible_hostname == cluster_status_pre.secondary_node
5353
block:
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 == "true"
5656
get_cluster_status_db:
5757
operation_step: "test_execution"
@@ -113,7 +113,7 @@
113113
changed_when: cleanup_failed_resource_post.rc == 0
114114
ignore_errors: true
115115

116-
- name: "Test Execution: Validate HANA DB cluster status"
116+
- name: "Test Execution: Validate HANA DB cluster status 2"
117117
get_cluster_status_db:
118118
operation_step: "post_failover"
119119
database_sid: "{{ db_sid | lower }}"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
- name: "Test Execution: Switch to secondary node"
4343
when: ansible_hostname == cluster_status_pre.secondary_node
4444
block:
45-
- name: "Test Execution: Validate HANA DB cluster status"
45+
- name: "Test Execution: Validate HANA DB cluster status 1"
4646
when: cluster_status_pre.AUTOMATED_REGISTER == "true"
4747
get_cluster_status_db:
4848
operation_step: "test_execution"
@@ -100,7 +100,7 @@
100100
changed_when: cleanup_failed_resource_post.rc == 0
101101
ignore_errors: true
102102

103-
- name: "Test Execution: Validate HANA DB cluster status"
103+
- name: "Test Execution: Validate HANA DB cluster status 2"
104104
get_cluster_status_db:
105105
operation_step: "post_failover"
106106
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/primary-node-crash.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
changed_when: hana_db_stop_results == 0
4040
failed_when: hana_db_stop_results.rc != 0
4141

42-
- name: "Test Execution: Validate HANA DB cluster status"
42+
- name: "Test Execution: Validate HANA DB cluster status 1"
4343
get_cluster_status_db:
4444
operation_step: "test_execution"
4545
database_sid: "{{ db_sid | lower }}"
@@ -81,7 +81,7 @@
8181
changed_when: cleanup_failed_resource_post.rc == 0
8282
ignore_errors: true
8383

84-
- name: "Test execution: Validate HANA DB cluster status"
84+
- name: "Test execution: Validate HANA DB cluster status 2"
8585
get_cluster_status_db:
8686
operation_step: "post_failover"
8787
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/primary-node-kill.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
changed_when: hana_db_kill_results == 0
4040
failed_when: hana_db_kill_results.rc != 0
4141

42-
- name: "Test Execution: Validate HANA DB cluster status"
42+
- name: "Test Execution: Validate HANA DB cluster status 1"
4343
when: cluster_status_pre.AUTOMATED_REGISTER == "true"
4444
get_cluster_status_db:
4545
operation_step: "test_execution"
@@ -98,7 +98,7 @@
9898
changed_when: cleanup_failed_resource_post.rc == 0
9999
ignore_errors: true
100100

101-
- name: "Test execution: Validate HANA DB cluster status"
101+
- name: "Test execution: Validate HANA DB cluster status 2"
102102
get_cluster_status_db:
103103
operation_step: "post_failover"
104104
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/resource-migration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
failed_when: hana_db_resource_migration.rc != 0
4040
changed_when: hana_db_resource_migration.rc == 0
4141

42-
- name: "Test Execution: Validate HANA DB cluster status"
42+
- name: "Test Execution: Validate HANA DB cluster status 1"
4343
get_cluster_status_db:
4444
operation_step: "test_execution"
4545
database_sid: "{{ db_sid | lower }}"
@@ -94,7 +94,7 @@
9494
ansible.builtin.wait_for:
9595
timeout: 100
9696

97-
- name: "Test Execution: Validate HANA DB cluster status"
97+
- name: "Test Execution: Validate HANA DB cluster status 2"
9898
get_cluster_status_db:
9999
operation_step: "test_execution"
100100
database_sid: "{{ db_sid | lower }}"

src/roles/ha_db_hana/tasks/secondary-node-kill.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- node_tier == "hana"
2424
- pre_validations_status == "PASSED"
2525
block:
26-
- name: "Test Execution: Kill the secondary node."
26+
- name: "Test Execution: Kill the secondary node"
2727
when: ansible_hostname == cluster_status_pre.primary_node
2828
block:
2929
- name: "Test Execution: Start timer"
@@ -57,7 +57,7 @@
5757
cluster_status_test_execution.primary_node == cluster_status_pre.primary_node and
5858
cluster_status_test_execution.secondary_node == ""
5959
60-
- name: "Test execution: Validate HANA DB cluster status"
60+
- name: "Test execution: Validate HANA DB cluster status 2"
6161
get_cluster_status_db:
6262
operation_step: "post_failover"
6363
ansible_os_family: "{{ ansible_os_family | upper }}"

tests/roles/ha_db_hana/ha_config_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Test class for HANA DB HA config validation tasks
66
77
This test class uses pytest to run functional tests on the HANA DB HA config validation tasks
8-
defined in roles/ha_db/tasks/ha-config.yml. It sets up a temporary test environment,
8+
defined in roles/ha_db_hana/tasks/ha-config.yml. It sets up a temporary test environment,
99
mocks necessary Python modules and commands, and verifies the execution of the tasks.
1010
"""
1111

@@ -60,6 +60,9 @@ def test_environment(self, ansible_inventory):
6060
"bin/crm_resource",
6161
"bin/crm",
6262
],
63+
extra_vars_override={
64+
"node_tier": "hana"
65+
}
6366
)
6467

6568
os.makedirs(f"{temp_dir}/project/roles/ha_db_hana/tasks/files", exist_ok=True)
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
Test class for HANA DB primary node crash and node kill tasks
6+
7+
This test class uses pytest to run functional tests on the HANA DB primary node crash, kill, echo-b,
8+
and crash-index tasks defined in roles/ha_db_hana/tasks/primary-node-crash.yml.
9+
It sets up a temporary test environment, mocks necessary Python modules and commands, and verifies
10+
the execution of the tasks.
11+
"""
12+
13+
import os
14+
import shutil
15+
from pathlib import Path
16+
import pytest
17+
from tests.roles.ha_db_hana.roles_testing_base_db import RolesTestingBaseDB
18+
19+
20+
class TestDbHDBOperations(RolesTestingBaseDB):
21+
"""
22+
Test class for HANA DB primary node crash, kill, echo-b, and crash-index tasks.
23+
"""
24+
25+
@pytest.fixture(
26+
params=["primary-node-crash", "primary-node-kill", "primary-echo-b", "primary-crash-index"]
27+
)
28+
def task_type(self, request):
29+
"""
30+
Parameterized fixture to test both primary and secondary node operations.
31+
32+
:param request: pytest request object containing the parameter
33+
:type request: pytest.request
34+
:return: Dictionary with task configuration details
35+
:rtype: dict
36+
"""
37+
task_name = request.param
38+
39+
if task_name == "primary-node-crash":
40+
return {
41+
"task_name": task_name,
42+
"command_task": "Stop the HANA DB",
43+
"command_type": "stop",
44+
"validate_task": "Test execution: Validate HANA DB cluster status 2",
45+
}
46+
elif task_name == "primary-node-kill":
47+
return {
48+
"task_name": task_name,
49+
"command_task": "Test Execution: Kill the HANA DB",
50+
"command_type": "kill-9",
51+
"validate_task": "Test execution: Validate HANA DB cluster status 2",
52+
}
53+
elif task_name == "primary-echo-b":
54+
return {
55+
"task_name": task_name,
56+
"command_task": "Test Execution: Echo b",
57+
"command_type": "echo b",
58+
"validate_task": "Test Execution: Validate HANA DB cluster status 2",
59+
}
60+
elif task_name == "primary-crash-index":
61+
return {
62+
"task_name": task_name,
63+
"command_task": "Test Execution: Crash the index server",
64+
"command_type": "killall",
65+
"validate_task": "Test Execution: Validate HANA DB cluster status",
66+
}
67+
68+
@pytest.fixture
69+
def test_environment(self, ansible_inventory, task_type):
70+
"""
71+
Set up a temporary test environment for the HANA DB primary node operations tasks.
72+
73+
:param ansible_inventory: Path to the Ansible inventory file.
74+
:type ansible_inventory: str
75+
:param task_type: Dictionary with task configuration details.
76+
:type task_type: dict
77+
:yield temp_dir: Path to the temporary test environment.
78+
:ytype: str
79+
"""
80+
81+
task_counter_file = f"/tmp/get_cluster_status_counter_{task_type['task_name']}"
82+
if os.path.exists(task_counter_file):
83+
os.remove(task_counter_file)
84+
85+
temp_dir = self.setup_test_environment(
86+
role_type="ha_db_hana",
87+
ansible_inventory=ansible_inventory,
88+
task_name=task_type["task_name"],
89+
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"},
102+
)
103+
104+
os.makedirs(f"{temp_dir}/bin", exist_ok=True)
105+
self.file_operations(
106+
operation="write",
107+
file_path=f"{temp_dir}/bin/HDB",
108+
content=self.file_operations(
109+
operation="read",
110+
file_path=Path(__file__).parent.parent / "mock_data/HDB.txt",
111+
),
112+
)
113+
os.chmod(f"{temp_dir}/bin/HDB", 0o755)
114+
115+
playbook_content = self.file_operations(
116+
operation="read",
117+
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/{task_type['task_name']}.yml",
118+
)
119+
self.file_operations(
120+
operation="write",
121+
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/{task_type['task_name']}.yml",
122+
content=playbook_content.replace(
123+
"/usr/sap/{{ db_sid | upper }}/HDB{{ db_instance_number }}/", ""
124+
),
125+
)
126+
127+
yield temp_dir
128+
shutil.rmtree(temp_dir)
129+
130+
def test_functional_db_primary_node_success(
131+
self, test_environment, ansible_inventory, task_type
132+
):
133+
"""
134+
Test the HANA DB primary node operations tasks using Ansible Runner.
135+
136+
:param test_environment: Path to the temporary test environment.
137+
:type test_environment: str
138+
:param ansible_inventory: Path to the Ansible inventory file.
139+
:type ansible_inventory: str
140+
:param task_type: Dictionary with task configuration details.
141+
:type task_type: dict
142+
"""
143+
result = self.run_ansible_playbook(
144+
test_environment=test_environment,
145+
inventory_file_name="inventory_db.txt",
146+
task_type=task_type["task_name"],
147+
)
148+
149+
assert result.rc == 0, (
150+
f"Playbook failed with status: {result.rc}\n"
151+
f"STDOUT: {result.stdout.read() if result.stdout else 'No output'}\n"
152+
f"STDERR: {result.stderr.read() if result.stderr else 'No errors'}\n"
153+
f"Events: {[e.get('event') for e in result.events if 'event' in e]}"
154+
)
155+
156+
ok_events, failed_events = [], []
157+
for event in result.events:
158+
if event.get("event") == "runner_on_ok":
159+
ok_events.append(event)
160+
elif event.get("event") == "runner_on_failed":
161+
failed_events.append(event)
162+
163+
assert len(ok_events) > 0
164+
assert len(failed_events) == 0
165+
166+
post_status = {}
167+
pre_status = {}
168+
169+
for event in ok_events:
170+
task = event.get("event_data", {}).get("task")
171+
task_result = event.get("event_data", {}).get("res")
172+
173+
if task and task_type["command_task"] in task:
174+
if task_type["command_type"] == "echo b":
175+
assert task_result.get("changed") is True
176+
else:
177+
assert task_result.get("rc") == 0
178+
elif (
179+
task
180+
and "Test Execution: Validate HANA DB cluster status 1" in task
181+
and task_type["task_name"] == "primary-node-crash"
182+
):
183+
assert not task_result.get("secondary_node")
184+
elif task and task_type["validate_task"] in task:
185+
assert task_result.get("secondary_node")
186+
assert task_result.get("primary_node")
187+
post_status = task_result
188+
elif task and "Pre Validation: Validate HANA DB" in task:
189+
pre_status = task_result
190+
elif task and "Remove any location_constraints" in task:
191+
assert task_result.get("changed")
192+
193+
assert post_status.get("primary_node") == pre_status.get("secondary_node")
194+
assert post_status.get("secondary_node") == pre_status.get("primary_node")

0 commit comments

Comments
 (0)