1#!/usr/bin/env python3 2# 3# Copyright (C) 2020 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16import os 17 18from acts import context 19from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest 20 21from mobly import utils 22from mobly.base_test import STAGE_NAME_TEARDOWN_CLASS 23 24 25class AbstractDeviceWlanDeviceBaseTest(WifiBaseTest): 26 def setup_class(self): 27 super().setup_class() 28 29 def teardown_class(self): 30 begin_time = utils.get_current_epoch_time() 31 super().teardown_class() 32 for device in getattr(self, "android_devices", []): 33 device.take_bug_report(STAGE_NAME_TEARDOWN_CLASS, begin_time) 34 for device in getattr(self, "fuchsia_devices", []): 35 device.take_bug_report(STAGE_NAME_TEARDOWN_CLASS, begin_time) 36 37 def on_fail(self, test_name, begin_time): 38 """Gets a wlan_device log and calls the generic device fail on DUT.""" 39 self.dut.get_log(test_name, begin_time) 40 self.on_device_fail(self.dut.device, test_name, begin_time) 41 42 def on_device_fail(self, device, test_name, begin_time): 43 """Gets a generic device DUT bug report. 44 45 This method takes a bug report if the generic device does not have a 46 'take_bug_report_on_fail', or if the flag is true. This method also 47 power cycles if 'hard_reboot_on_fail' is True. 48 49 Args: 50 device: Generic device to gather logs from. 51 test_name: Name of the test that triggered this function. 52 begin_time: Logline format timestamp taken when the test started. 53 """ 54 if (not hasattr(device, "take_bug_report_on_fail") 55 or device.take_bug_report_on_fail): 56 device.take_bug_report(test_name, begin_time) 57 58 if device.hard_reboot_on_fail: 59 device.reboot(reboot_type='hard', testbed_pdus=self.pdu_devices) 60 61 def download_ap_logs(self): 62 """Downloads the DHCP and hostapad logs from the access_point. 63 64 Using the current TestClassContext and TestCaseContext this method pulls 65 the DHCP and hostapd logs and outputs them to the correct path. 66 """ 67 current_path = context.get_current_context().get_full_output_path() 68 dhcp_full_out_path = os.path.join(current_path, "dhcp_log.txt") 69 70 dhcp_log = self.access_point.get_dhcp_logs() 71 if dhcp_log: 72 dhcp_log_file = open(dhcp_full_out_path, 'w') 73 dhcp_log_file.write(dhcp_log) 74 dhcp_log_file.close() 75 76 hostapd_logs = self.access_point.get_hostapd_logs() 77 for interface in hostapd_logs: 78 out_name = interface + "_hostapd_log.txt" 79 hostapd_full_out_path = os.path.join(current_path, out_name) 80 hostapd_log_file = open(hostapd_full_out_path, 'w') 81 hostapd_log_file.write(hostapd_logs[interface]) 82 hostapd_log_file.close() 83