Skip to content

Commit 8e105cc

Browse files
dhruvmicrosoftdevanshjainmshdamecharla
authored
Block network test (#14)
* Add functional tests for HANA DB block network and update task names * Add mock sleep command script for testing purposes Co-Authored-by: Dhruv Aggarwal <[email protected]> * Fix typo in docstring and clean up conditional checks in block network test --------- Co-authored-by: devanshjain <[email protected]> Co-authored-by: hdamecharla <[email protected]>
1 parent fd14f69 commit 8e105cc

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

src/roles/ha_db_hana/tasks/block-network.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
ansible.builtin.wait_for:
117117
timeout: 60
118118

119-
- name: "Test Execution: Validate HANA DB cluster status"
119+
- name: "Test Execution: Validate HANA DB cluster status 2"
120120
get_cluster_status_db:
121121
operation_step: "test_execution"
122122
database_sid: "{{ db_sid | lower }}"
@@ -150,7 +150,7 @@
150150
ansible.builtin.wait_for:
151151
timeout: 60
152152

153-
- name: "Test Execution: Validate HANA DB cluster status"
153+
- name: "Test Execution: Validate HANA DB cluster status 2"
154154
get_cluster_status_db:
155155
operation_step: "test_execution"
156156
database_sid: "{{ db_sid | lower }}"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
Test class for HANA DB block network between primary and secondary nodes.
6+
7+
This test class uses pytest to run functional tests on the HANA DB block-network
8+
tasks defined in roles/ha_db_hana/tasks/block-network.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 TestBlockNetworkTest(RolesTestingBaseDB):
21+
"""
22+
Test class for HANA DB block network between primary and secondary nodes.
23+
"""
24+
25+
@pytest.fixture
26+
def test_environment(self, ansible_inventory):
27+
"""
28+
Set up a temporary test environment for the HANA DB block-network test
29+
30+
:param ansible_inventory: Path to the Ansible inventory file.
31+
:type ansible_inventory: str
32+
:param task_type: Dictionary with task configuration details.
33+
:type task_type: dict
34+
:yield temp_dir: Path to the temporary test environment.
35+
:type: str
36+
"""
37+
38+
task_counter_file = "/tmp/get_cluster_status_counter_block-network"
39+
if os.path.exists(task_counter_file):
40+
os.remove(task_counter_file)
41+
42+
if os.path.exists("/tmp/ping_counter_block-network"):
43+
os.remove("/tmp/ping_counter_block-network")
44+
45+
module_names = [
46+
"project/library/get_cluster_status_db",
47+
"project/library/log_parser",
48+
"project/library/send_telemetry_data",
49+
"project/library/location_constraints",
50+
"project/library/check_indexserver",
51+
"project/library/filesystem_freeze",
52+
"bin/crm_resource",
53+
"bin/iptables",
54+
"bin/ping",
55+
"bin/echo",
56+
"bin/sleep",
57+
]
58+
59+
temp_dir = self.setup_test_environment(
60+
role_type="ha_db_hana",
61+
ansible_inventory=ansible_inventory,
62+
task_name="block-network",
63+
task_description="The block network test validates failover scenarios",
64+
module_names=module_names,
65+
extra_vars_override={
66+
"node_tier": "hana",
67+
"NFS_provider": "ANF",
68+
"database_cluster_type": "ISCSI",
69+
},
70+
)
71+
72+
playbook_content = self.file_operations(
73+
operation="read",
74+
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/block-network.yml",
75+
)
76+
self.file_operations(
77+
operation="write",
78+
file_path=f"{temp_dir}/project/roles/ha_db_hana/tasks/block-network.yml",
79+
content=playbook_content.replace(
80+
"for i in $(seq 1 20); do",
81+
"for i in {1..20}; do",
82+
),
83+
)
84+
85+
yield temp_dir
86+
shutil.rmtree(temp_dir)
87+
88+
def test_functional_db_primary_node_success(self, test_environment, ansible_inventory):
89+
"""
90+
Test the HANA DB block network between primary and secondary nodes.
91+
This test verifies the successful execution of the block network task and checks
92+
93+
:param test_environment: Path to the temporary test environment.
94+
:type test_environment: str
95+
:param ansible_inventory: Path to the Ansible inventory file.
96+
:type ansible_inventory: str
97+
"""
98+
result = self.run_ansible_playbook(
99+
test_environment=test_environment,
100+
inventory_file_name="inventory_db.txt",
101+
task_type="block-network",
102+
)
103+
104+
assert result.rc == 0, (
105+
f"Playbook failed with status: {result.rc}\n"
106+
f"STDOUT: {result.stdout.read() if result.stdout else 'No output'}\n"
107+
f"STDERR: {result.stderr.read() if result.stderr else 'No errors'}\n"
108+
f"Events: {[e.get('event') for e in result.events if 'event' in e]}"
109+
)
110+
111+
ok_events, failed_events = [], []
112+
for event in result.events:
113+
if event.get("event") == "runner_on_ok":
114+
ok_events.append(event)
115+
elif event.get("event") == "runner_on_failed":
116+
failed_events.append(event)
117+
118+
assert len(ok_events) > 0
119+
# There will be 1 failed event, connection failure to primary node
120+
# This is the behavior be have mocked in the ping functionality
121+
assert len(failed_events) == 1
122+
123+
post_status = {}
124+
pre_status = {}
125+
126+
for event in ok_events:
127+
task = event.get("event_data", {}).get("task")
128+
task_result = event.get("event_data", {}).get("res")
129+
130+
if task and "Create a firewall" in task:
131+
assert task_result.get("rc") == 0
132+
elif task and "Pre Validation: Validate HANA DB" in task:
133+
pre_status = task_result
134+
elif task and "Test Execution: Validate HANA DB cluster status 2" in task:
135+
post_status = task_result
136+
elif task and "Remove any location_constraints" in task:
137+
assert task_result.get("changed")
138+
139+
assert post_status.get("primary_node") == pre_status.get("secondary_node")
140+
assert post_status.get("secondary_node") == pre_status.get("primary_node")

tests/roles/mock_data/ping.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
task_type="${TEST_TASK_TYPE:-default}"
4+
counter_file="/tmp/ping_counter_${task_type}"
5+
6+
if [[ -f "$counter_file" ]]; then
7+
counter=$(< "$counter_file")
8+
counter=${counter:-0}
9+
else
10+
counter=0
11+
fi
12+
13+
counter=$((counter + 1))
14+
echo "$counter" > "$counter_file"
15+
16+
if (( counter % 2 == 1 )); then
17+
exit 1
18+
else
19+
exit 0
20+
fi

tests/roles/mock_data/sleep.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
echo "Mock: sleep command executed"
4+
exit 0

0 commit comments

Comments
 (0)