Skip to content

Commit 4175278

Browse files
authored
net, dhcpd: move helpers to a dedicated module, add type annotation (RedHatQE#1900)
* net, dhcpd: move helpers to a dedicated module, add type annotation Improve code organization and re-usability with separate dhcpd module Signed-off-by: Asia Khromov <azhivovk@redhat.com> * net, dhcpd: move dhcp ip related constants to dhcp module Signed-off-by: Asia Khromov <azhivovk@redhat.com> * net, dhcpd: edit DHCP_IP_SUBNET in dhcpd constants Signed-off-by: Asia Khromov <azhivovk@redhat.com> --------- Signed-off-by: Asia Khromov <azhivovk@redhat.com>
1 parent 94d4146 commit 4175278

6 files changed

Lines changed: 67 additions & 56 deletions

File tree

tests/network/constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from utilities.constants import CLUSTER_NETWORK_ADDONS_OPERATOR
22

3-
DHCP_IP_RANGE_START = "10.200.3.3"
4-
DHCP_IP_RANGE_END = "10.200.3.10"
53
IPV4_ADDRESS_SUBNET_PREFIX = "10.200.0"
64
EXPECTED_CNAO_COMP_NAMES = [
75
"multus",

tests/network/l2_bridge/conftest.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
import pytest
77
from pyhelper_utils.shell import run_ssh_commands
88

9-
from tests.network.constants import DHCP_IP_RANGE_END, DHCP_IP_RANGE_START
10-
from tests.network.utils import (
9+
from tests.network.libs.dhcpd import (
10+
DHCP_IP_RANGE_END,
11+
DHCP_IP_RANGE_START,
12+
DHCP_IP_SUBNET,
1113
DHCP_SERVER_CONF_FILE,
1214
DHCP_SERVICE_RESTART,
13-
update_cloud_init_extra_user_data,
15+
verify_dhcpd_activated,
1416
)
17+
from tests.network.utils import update_cloud_init_extra_user_data
1518
from utilities.infra import get_node_selector_dict, name_prefix
1619
from utilities.network import (
1720
cloud_init_network_data,
1821
get_vmi_mac_address_by_iface_name,
1922
network_device,
2023
network_nad,
21-
verify_dhcpd_activated,
2224
)
2325
from utilities.virt import (
2426
VirtualMachineForTests,
@@ -27,8 +29,8 @@
2729
)
2830

2931
#: Test setup
30-
# ......... ..........
31-
# | |---eth1:10.200.0.1: 10.200.0.2:---eth1:| |
32+
# ......... ..........
33+
# | |---eth1:10.200.0.1: 10.200.0.2:eth1---| |
3234
# | VM-A |---eth2:10.200.2.1 : multicast(ICMP), custom eth type test: 10.200.2.2:eth2---| VM-B |
3335
# | |---eth3:10.200.3.1 : DHCP test : 10.200.3.2:eth3---| |
3436
# |.......|---eth4:10.200.4.1 : mpls test : 10.200.4.2:eth4---|........|
@@ -297,7 +299,7 @@ def l2_bridge_running_vm_a(
297299
namespace, worker_node1, l2_bridge_all_nads, dhcp_nad, unprivileged_client, l2_bridge_running_vm_b
298300
):
299301
dhcpd_data = DHCP_SERVER_CONF_FILE.format(
300-
DHCP_IP_SUBNET="10.200.3",
302+
DHCP_IP_SUBNET=DHCP_IP_SUBNET,
301303
DHCP_IP_RANGE_START=DHCP_IP_RANGE_START,
302304
DHCP_IP_RANGE_END=DHCP_IP_RANGE_END,
303305
CLIENT_MAC_ADDRESS=get_vmi_mac_address_by_iface_name(vmi=l2_bridge_running_vm_b.vmi, iface_name=dhcp_nad.name),

tests/network/l2_bridge/test_l2_ovs_linux_bridge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyhelper_utils.shell import run_ssh_commands
77
from timeout_sampler import TimeoutSampler
88

9-
from tests.network.constants import DHCP_IP_RANGE_START
9+
from tests.network.libs.dhcpd import DHCP_IP_RANGE_START
1010
from utilities.constants import TIMEOUT_2MIN
1111
from utilities.network import assert_ping_successful, get_vmi_ip_v4_by_name, ping
1212

tests/network/libs/dhcpd.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import shlex
2+
3+
from pyhelper_utils.shell import run_ssh_commands
4+
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
5+
6+
from utilities.constants import TIMEOUT_5SEC, TIMEOUT_30SEC
7+
from utilities.network import LOGGER
8+
from utilities.virt import VirtualMachineForTests
9+
10+
DHCP_IP_SUBNET = "10.200.3"
11+
DHCP_IP_RANGE_START = f"{DHCP_IP_SUBNET}.3"
12+
DHCP_IP_RANGE_END = f"{DHCP_IP_SUBNET}.10"
13+
DHCP_SERVICE_RESTART = "sudo systemctl restart dhcpd"
14+
DHCP_SERVER_CONF_FILE = """
15+
cat <<EOF >> /etc/dhcp/dhcpd.conf
16+
default-lease-time 3600;
17+
max-lease-time 7200;
18+
authoritative;
19+
subnet {DHCP_IP_SUBNET}.0 netmask 255.255.255.0 {{
20+
option subnet-mask 255.255.255.0;
21+
option routers {DHCP_IP_SUBNET}.1;
22+
option domain-name-servers {DHCP_IP_SUBNET}.1;
23+
24+
pool {{
25+
range {DHCP_IP_RANGE_START} {DHCP_IP_RANGE_END};
26+
allow known-clients;
27+
deny unknown-clients;
28+
}}
29+
}}
30+
host intended_client_vm {{
31+
hardware ethernet {CLIENT_MAC_ADDRESS};
32+
}}
33+
EOF
34+
"""
35+
36+
37+
def verify_dhcpd_activated(vm: VirtualMachineForTests) -> bool:
38+
active = "active"
39+
dhcpd = "dhcpd"
40+
sample = None
41+
sampler = TimeoutSampler(
42+
wait_timeout=TIMEOUT_30SEC,
43+
sleep=TIMEOUT_5SEC,
44+
func=run_ssh_commands,
45+
host=vm.ssh_exec,
46+
commands=[shlex.split(f"sudo systemctl is-{active} {dhcpd}")],
47+
)
48+
try:
49+
for sample in sampler:
50+
if sample[0].strip() == active:
51+
return True
52+
53+
except TimeoutExpiredError:
54+
LOGGER.error(f"{dhcpd} status is not '{active}' but rather '{sample}'")
55+
raise
56+
57+
return False

tests/network/utils.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,6 @@
2727
from utilities.virt import VirtualMachineForTests, fedora_vm_body, running_vm
2828

2929
LOGGER = logging.getLogger(__name__)
30-
DHCP_SERVICE_RESTART = "sudo systemctl restart dhcpd"
31-
DHCP_SERVER_CONF_FILE = """
32-
cat <<EOF >> /etc/dhcp/dhcpd.conf
33-
default-lease-time 3600;
34-
max-lease-time 7200;
35-
authoritative;
36-
subnet {DHCP_IP_SUBNET}.0 netmask 255.255.255.0 {{
37-
option subnet-mask 255.255.255.0;
38-
option routers {DHCP_IP_SUBNET}.1;
39-
option domain-name-servers {DHCP_IP_SUBNET}.1;
40-
41-
pool {{
42-
range {DHCP_IP_RANGE_START} {DHCP_IP_RANGE_END};
43-
allow known-clients;
44-
deny unknown-clients;
45-
}}
46-
}}
47-
host intended_client_vm {{
48-
hardware ethernet {CLIENT_MAC_ADDRESS};
49-
}}
50-
EOF
51-
"""
5230
SERVICE_MESH_INJECT_ANNOTATION = "sidecar.istio.io/inject"
5331

5432

utilities/network.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from ocp_resources.pod import Pod
2424
from ocp_resources.sriov_network import SriovNetwork
2525
from ocp_resources.sriov_network_node_policy import SriovNetworkNodePolicy
26-
from pyhelper_utils.shell import run_ssh_commands
2726
from pytest_testconfig import config as py_config
2827
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
2928

@@ -40,9 +39,7 @@
4039
SRIOV,
4140
TIMEOUT_2MIN,
4241
TIMEOUT_3MIN,
43-
TIMEOUT_5SEC,
4442
TIMEOUT_8MIN,
45-
TIMEOUT_30SEC,
4643
TIMEOUT_90SEC,
4744
WORKERS_TYPE,
4845
)
@@ -1083,27 +1080,6 @@ def create_sriov_node_policy(
10831080
wait_for_ready_sriov_nodes(snns=sriov_nodes_states)
10841081

10851082

1086-
def verify_dhcpd_activated(vm):
1087-
active = "active"
1088-
dhcpd = "dhcpd"
1089-
sample = None
1090-
sampler = TimeoutSampler(
1091-
wait_timeout=TIMEOUT_30SEC,
1092-
sleep=TIMEOUT_5SEC,
1093-
func=run_ssh_commands,
1094-
host=vm.ssh_exec,
1095-
commands=[shlex.split(f"sudo systemctl is-{active} {dhcpd}")],
1096-
)
1097-
try:
1098-
for sample in sampler:
1099-
if sample[0].strip() == active:
1100-
return True
1101-
1102-
except TimeoutExpiredError:
1103-
LOGGER.error(f"{dhcpd} status is not '{active}' but rather '{sample}'")
1104-
raise
1105-
1106-
11071083
def wait_for_node_marked_by_bridge(bridge_nad: LinuxBridgeNetworkAttachmentDefinition, node: Node) -> None:
11081084
bridge_annotation = bridge_nad.resource_name
11091085
sampler = TimeoutSampler(

0 commit comments

Comments
 (0)