refactor: Lazy load IP addresses for APF tests

Previously, IPv4 and IPv6 addresses were retrieved in `ApfTestBase.setup_class`,
which meant all APF tests assumed the presence of both.

This change refactors `ApfTestBase` to introduce dedicated methods:
- `get_and_expect_ipv4_addresses_exist()`
- `get_and_expect_ipv6_addresses_exist()`

These methods now handle the retrieval and assertion of IP addresses.
Individual test cases in `ApfV6Test` are updated to call these methods
only when the respective address type is required for the specific test
scenario.

This improves test robustness by ensuring tests only depend on the
necessary IP addresses, allowing for more precise test setup and failure
reporting. Additionally, the doze mode activation is reordered to occur
after initial network setup.

Flag: EXEMPT mainline
Bug: 419427388
Test: atest CtsConnectivityMultiDevicesTestCases:ApfV6Test
Change-Id: I340bcb77fe66cc9927dcd58e5899cdf9a4375daa
This commit is contained in:
Jimi Chen 2025-07-14 14:24:42 +00:00
commit 419e088796
2 changed files with 44 additions and 8 deletions

View file

@ -61,18 +61,6 @@ class ApfTestBase(multi_devices_test_base.MultiDevicesTestBase):
self.client_mac_address = apf_utils.get_hardware_address(
self.clientDevice, self.client_iface_name
)
self.server_ipv4_addresses = apf_utils.get_ipv4_addresses(
self.serverDevice, self.server_iface_name
)
self.client_ipv4_addresses = apf_utils.get_ipv4_addresses(
self.clientDevice, self.client_iface_name
)
self.server_ipv6_addresses = apf_utils.get_non_tentative_ipv6_addresses(
self.serverDevice, self.server_iface_name
)
self.client_ipv6_addresses = apf_utils.get_non_tentative_ipv6_addresses(
self.clientDevice, self.client_iface_name
)
# Enable doze mode to activate APF.
adb_utils.set_doze_mode(self.clientDevice, True)
@ -83,6 +71,42 @@ class ApfTestBase(multi_devices_test_base.MultiDevicesTestBase):
self.serverDevice, UpstreamType.NONE
)
def get_and_expect_ipv4_addresses_exist(self):
self.server_ipv4_addresses = apf_utils.get_ipv4_addresses(
self.serverDevice, self.server_iface_name
)
asserts.assert_true(
self.server_ipv4_addresses,
'Server does not have IPv4 address, fail the test.',
)
self.client_ipv4_addresses = apf_utils.get_ipv4_addresses(
self.clientDevice, self.client_iface_name
)
asserts.assert_true(
self.client_ipv4_addresses,
'Client does not have IPv4 address, fail the test.',
)
def get_and_expect_ipv6_addresses_exist(self):
self.server_ipv6_addresses = apf_utils.get_non_tentative_ipv6_addresses(
self.serverDevice, self.server_iface_name
)
asserts.assert_true(
self.server_ipv6_addresses,
'Server does not have IPv6 address, fail the test.',
)
self.client_ipv6_addresses = apf_utils.get_non_tentative_ipv6_addresses(
self.clientDevice, self.client_iface_name
)
asserts.assert_true(
self.client_ipv6_addresses,
'Client does not have IPv6 address, fail the test.',
)
def send_packet_and_expect_counter_increased(
self, packet: str, counter_name: str
) -> None:

View file

@ -56,6 +56,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
super().teardown_class()
def test_unicast_arp_request_offload(self):
self.get_and_expect_ipv4_addresses_exist()
# Longer wait time is required for APF to become active in CTS test suite.
time.sleep(apf_test_base.APF_ACTIVATION_WAIT_TIME_SEC)
self.expect_apf_offload_enabled('ARP')
@ -89,6 +91,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
)
def test_non_dad_ipv6_neighbor_solicitation_offload(self):
self.get_and_expect_ipv6_addresses_exist()
# Longer wait time is required for APF to become active in CTS test suite.
time.sleep(apf_test_base.APF_ACTIVATION_WAIT_TIME_SEC)
self.expect_apf_offload_enabled('ND')
@ -116,6 +120,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
@apf_utils.at_least_B()
def test_ipv4_icmp_echo_request_offload(self):
self.get_and_expect_ipv4_addresses_exist()
# Longer wait time is required for APF to become active in CTS test suite.
time.sleep(apf_test_base.APF_ACTIVATION_WAIT_TIME_SEC)
self.expect_apf_offload_enabled('Ping4')
@ -140,6 +146,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
@apf_utils.at_least_B()
@apf_utils.apf_ram_at_least(3000)
def test_ipv6_icmp_echo_request_offload(self):
self.get_and_expect_ipv6_addresses_exist()
# Longer wait time is required for APF to become active in CTS test suite.
time.sleep(apf_test_base.APF_ACTIVATION_WAIT_TIME_SEC)
self.expect_apf_offload_enabled('Ping6')
@ -166,6 +174,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
@apf_utils.at_least_B()
def test_igmpv3_general_query_offload(self):
self.get_and_expect_ipv4_addresses_exist()
mcast_addrs = ['239.0.0.1', '239.0.0.2', '239.0.0.3']
self.client.createMulticastSocket(self.client_iface_name)
@ -214,6 +224,8 @@ class ApfV6Test(apf_test_base.ApfTestBase):
@apf_utils.at_least_B()
@apf_utils.apf_ram_at_least(3000)
def test_mldv2_general_query_offload(self):
self.get_and_expect_ipv6_addresses_exist()
# Longer wait time is required for APF to become active in CTS test suite.
time.sleep(apf_test_base.APF_ACTIVATION_WAIT_TIME_SEC)
self.expect_apf_offload_enabled('MLD')