From 32a2d151cc44435c8830ad9e70607a5f6b9cd3c8 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Wed, 3 Jul 2024 16:05:41 +0800 Subject: [PATCH] Move apf test to a standalone file This is a no-op refactoring, which is preparation for follow-up changes to add more apfv4 tests. This includes: 1. Add a entry point for multi-device tests to collect test cases from different files. 2. Move multi-device initialization code to a base class. 3. Make different test classes inherit the base class. Test: m connectivity_multi_devices_snippet && \ atest CtsConnectivityMultiDevicesTestCases Bug: 350880989 Change-Id: I294c85d8e09633bbf02ba1543b9f2ea3e988e20d --- .../host/python/multi_devices_test_base.py | 54 ++++++++++ tests/cts/multidevices/Android.bp | 4 +- tests/cts/multidevices/apfv4_test.py | 68 ++++++++++++ .../connectivity_multi_devices_test.py | 101 +++--------------- tests/cts/multidevices/run_tests.py | 38 +++++++ 5 files changed, 180 insertions(+), 85 deletions(-) create mode 100644 staticlibs/testutils/host/python/multi_devices_test_base.py create mode 100644 tests/cts/multidevices/apfv4_test.py create mode 100644 tests/cts/multidevices/run_tests.py diff --git a/staticlibs/testutils/host/python/multi_devices_test_base.py b/staticlibs/testutils/host/python/multi_devices_test_base.py new file mode 100644 index 0000000000..f8a92f3a3f --- /dev/null +++ b/staticlibs/testutils/host/python/multi_devices_test_base.py @@ -0,0 +1,54 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from mobly import base_test +from mobly import utils +from mobly.controllers import android_device + +CONNECTIVITY_MULTI_DEVICES_SNIPPET_PACKAGE = "com.google.snippet.connectivity" + + +class MultiDevicesTestBase(base_test.BaseTestClass): + + def setup_class(self): + # Declare that two Android devices are needed. + self.clientDevice, self.serverDevice = self.register_controller( + android_device, min_number=2 + ) + + def setup_device(device): + device.load_snippet( + "connectivity_multi_devices_snippet", + CONNECTIVITY_MULTI_DEVICES_SNIPPET_PACKAGE, + ) + + # Set up devices in parallel to save time. + utils.concurrent_exec( + setup_device, + ((self.clientDevice,), (self.serverDevice,)), + max_workers=2, + raise_on_exception=True, + ) diff --git a/tests/cts/multidevices/Android.bp b/tests/cts/multidevices/Android.bp index dc90adb8cc..5f062f1361 100644 --- a/tests/cts/multidevices/Android.bp +++ b/tests/cts/multidevices/Android.bp @@ -19,9 +19,11 @@ package { python_test_host { name: "CtsConnectivityMultiDevicesTestCases", - main: "connectivity_multi_devices_test.py", + main: "run_tests.py", srcs: [ + "apfv4_test.py", "connectivity_multi_devices_test.py", + "run_tests.py", ], libs: [ "mobly", diff --git a/tests/cts/multidevices/apfv4_test.py b/tests/cts/multidevices/apfv4_test.py new file mode 100644 index 0000000000..5844e493b5 --- /dev/null +++ b/tests/cts/multidevices/apfv4_test.py @@ -0,0 +1,68 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from net_tests_utils.host.python import adb_utils, apf_utils, assert_utils, mdns_utils, multi_devices_test_base, tether_utils +from net_tests_utils.host.python.tether_utils import UpstreamType + +COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED = "DROPPED_ETHERTYPE_NOT_ALLOWED" + + +class ApfV4Test(multi_devices_test_base.MultiDevicesTestBase): + + def test_apf_drop_ethercat(self): + tether_utils.assume_hotspot_test_preconditions( + self.serverDevice, self.clientDevice, UpstreamType.NONE + ) + client = self.clientDevice.connectivity_multi_devices_snippet + try: + server_iface_name, client_network = ( + tether_utils.setup_hotspot_and_client_for_upstream_type( + self.serverDevice, self.clientDevice, UpstreamType.NONE + ) + ) + client_iface_name = client.getInterfaceNameFromNetworkHandle( + client_network + ) + + adb_utils.set_doze_mode(self.clientDevice, True) + + count_before_test = apf_utils.get_apf_counter( + self.clientDevice, + client_iface_name, + COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED, + ) + try: + apf_utils.send_broadcast_empty_ethercat_packet( + self.serverDevice, server_iface_name + ) + except apf_utils.UnsupportedOperationException: + asserts.skip( + "NetworkStack is too old to support send raw packet, skip test." + ) + + assert_utils.expect_with_retry( + lambda: apf_utils.get_apf_counter( + self.clientDevice, + client_iface_name, + COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED, + ) + > count_before_test + ) + + # TODO: Verify the packet is not actually received. + finally: + adb_utils.set_doze_mode(self.clientDevice, False) + tether_utils.cleanup_tethering_for_upstream_type( + self.serverDevice, UpstreamType.NONE + ) diff --git a/tests/cts/multidevices/connectivity_multi_devices_test.py b/tests/cts/multidevices/connectivity_multi_devices_test.py index 7e7bbf5b87..eceb5356ef 100644 --- a/tests/cts/multidevices/connectivity_multi_devices_test.py +++ b/tests/cts/multidevices/connectivity_multi_devices_test.py @@ -1,40 +1,24 @@ -# Lint as: python3 -"""Connectivity multi devices tests.""" -import sys +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -from mobly import asserts -from mobly import base_test -from mobly import test_runner -from mobly import utils -from mobly.controllers import android_device -from net_tests_utils.host.python import adb_utils, apf_utils, assert_utils, mdns_utils, tether_utils +from net_tests_utils.host.python import mdns_utils, multi_devices_test_base, tether_utils from net_tests_utils.host.python.tether_utils import UpstreamType -CONNECTIVITY_MULTI_DEVICES_SNIPPET_PACKAGE = "com.google.snippet.connectivity" -COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED = "DROPPED_ETHERTYPE_NOT_ALLOWED" - -class ConnectivityMultiDevicesTest(base_test.BaseTestClass): - - def setup_class(self): - # Declare that two Android devices are needed. - self.clientDevice, self.serverDevice = self.register_controller( - android_device, min_number=2 - ) - - def setup_device(device): - device.load_snippet( - "connectivity_multi_devices_snippet", - CONNECTIVITY_MULTI_DEVICES_SNIPPET_PACKAGE, - ) - - # Set up devices in parallel to save time. - utils.concurrent_exec( - setup_device, - ((self.clientDevice,), (self.serverDevice,)), - max_workers=2, - raise_on_exception=True, - ) +class ConnectivityMultiDevicesTest( + multi_devices_test_base.MultiDevicesTestBase +): def test_hotspot_upstream_wifi(self): tether_utils.assume_hotspot_test_preconditions( @@ -84,54 +68,3 @@ class ConnectivityMultiDevicesTest(base_test.BaseTestClass): tether_utils.cleanup_tethering_for_upstream_type( self.serverDevice, UpstreamType.NONE ) - - def test_apf_drop_ethercat(self): - tether_utils.assume_hotspot_test_preconditions( - self.serverDevice, self.clientDevice, UpstreamType.NONE - ) - client = self.clientDevice.connectivity_multi_devices_snippet - try: - server_iface_name, client_network = ( - tether_utils.setup_hotspot_and_client_for_upstream_type( - self.serverDevice, self.clientDevice, UpstreamType.NONE - ) - ) - client_iface_name = client.getInterfaceNameFromNetworkHandle(client_network) - - adb_utils.set_doze_mode(self.clientDevice, True) - - count_before_test = apf_utils.get_apf_counter( - self.clientDevice, - client_iface_name, - COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED, - ) - try: - apf_utils.send_broadcast_empty_ethercat_packet( - self.serverDevice, server_iface_name - ) - except apf_utils.UnsupportedOperationException: - asserts.skip( - "NetworkStack is too old to support send raw packet, skip test." - ) - - assert_utils.expect_with_retry( - lambda: apf_utils.get_apf_counter( - self.clientDevice, - client_iface_name, - COUNTER_DROPPED_ETHERTYPE_NOT_ALLOWED, - ) - > count_before_test - ) - finally: - adb_utils.set_doze_mode(self.clientDevice, False) - tether_utils.cleanup_tethering_for_upstream_type( - self.serverDevice, UpstreamType.NONE - ) - - -if __name__ == "__main__": - # Take test args - if "--" in sys.argv: - index = sys.argv.index("--") - sys.argv = sys.argv[:1] + sys.argv[index + 1 :] - test_runner.main() diff --git a/tests/cts/multidevices/run_tests.py b/tests/cts/multidevices/run_tests.py new file mode 100644 index 0000000000..1391d131b0 --- /dev/null +++ b/tests/cts/multidevices/run_tests.py @@ -0,0 +1,38 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Main entrypoint for all of test cases.""" + +import sys +from apfv4_test import ApfV4Test +from connectivity_multi_devices_test import ConnectivityMultiDevicesTest +from mobly import suite_runner + + +if __name__ == "__main__": + # For MoblyBinaryHostTest, this entry point will be called twice: + # 1. List tests. + # -- --list_tests + # 2. Run tests. + # -- --config= \ + # --device_serial= --log_path= + # Strip the "--" since suite runner doesn't recognize it. + # While the parameters before "--" is for the infrastructure, + # ignore them if any. Also, do not alter parameters if there + # is no "--", in case the binary is invoked manually. + if "--" in sys.argv: + index = sys.argv.index("--") + sys.argv = sys.argv[:1] + sys.argv[index + 1 :] + # TODO: make the tests can be executed without manually list classes. + suite_runner.run_suite([ConnectivityMultiDevicesTest, ApfV4Test], sys.argv)