• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17Script for to flash Fuchsia devices and reports the DUT's version of Fuchsia in
18the Sponge test result properties. Uses the built in flashing tool for
19fuchsia_devices.
20"""
21from acts import asserts
22from acts.base_test import BaseTestClass
23from acts.controllers.fuchsia_lib.base_lib import DeviceOffline
24from acts.utils import get_device
25
26MAX_FLASH_ATTEMPTS = 3
27
28
29class FlashTest(BaseTestClass):
30    def setup_class(self):
31        super().setup_class()
32        success_str = ("Congratulations! Fuchsia controllers have been "
33                       "initialized successfully!")
34        err_str = ("Sorry, please try verifying FuchsiaDevice is in your "
35                   "config file and try again.")
36        if len(self.fuchsia_devices) > 0:
37            self.log.info(success_str)
38        else:
39            raise signals.TestAbortClass("err_str")
40
41    def teardown_class(self):
42        try:
43            dut = get_device(self.fuchsia_devices, 'DUT')
44            version = dut.version()
45            self.record_data({'sponge_properties': {
46                'DUT_VERSION': version,
47            }})
48            self.log.info("DUT version found: {}".format(version))
49        except ValueError as err:
50            self.log.warn("Failed to determine DUT: %s" % err)
51        except DeviceOffline as err:
52            self.log.warn("Failed to get DUT's version: %s" % err)
53
54        return super().teardown_class()
55
56    def test_flash_devices(self):
57        for device in self.fuchsia_devices:
58            flash_counter = 0
59            while True:
60                try:
61                    device.reboot(reboot_type='flash',
62                                  use_ssh=True,
63                                  unreachable_timeout=120,
64                                  ping_timeout=120)
65                    self.log.info(f'{device.orig_ip} has been flashed.')
66                    break
67                except Exception as err:
68                    self.log.error(
69                        f'Failed to flash {device.orig_ip} with error:\n{err}')
70
71                    if not device.device_pdu_config:
72                        asserts.abort_all(
73                            f'Failed to flash {device.orig_ip} and no PDU available for hard reboot'
74                        )
75
76                    flash_counter = flash_counter + 1
77                    if flash_counter == MAX_FLASH_ATTEMPTS:
78                        asserts.abort_all(
79                            f'Failed to flash {device.orig_ip} after {MAX_FLASH_ATTEMPTS} attempts'
80                        )
81
82                    self.log.info(
83                        f'Hard rebooting {device.orig_ip} and retrying flash.')
84                    device.reboot(reboot_type='hard',
85                                  testbed_pdus=self.pdu_devices)
86
87    def test_report_dut_version(self):
88        """Empty test to ensure the version of the DUT is reported in the Sponge
89        results in the case when flashing the device is not necessary.
90
91        Useful for when flashing the device is not necessary; specify ACTS to
92        only run this test from the test class.
93        """
94        pass
95