xref: /aosp_15_r20/external/cronet/build/fuchsia/test/serial_boot_device_unittests.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env vpython3
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""File for testing serial_boot_device.py."""
6
7import json
8import os
9import unittest
10import unittest.mock as mock
11
12from subprocess import CompletedProcess
13
14import serial_boot_device
15
16from boot_device import BootMode
17
18
19# pylint: disable=too-many-public-methods, missing-function-docstring
20@mock.patch('shutil.which', return_value='/bin')
21class SerialBootDeviceTest(unittest.TestCase):
22    """Unittests for serial_boot_device.py."""
23    def setUp(self) -> None:
24        os.environ['FUCHSIA_NODENAME'] = 'fuchsia-node-id'
25        os.environ['FUCHSIA_FASTBOOT_SERNUM'] = 'fuchsia-serial-num'
26
27    def test_does_not_boot_without_binaries(self, *_) -> None:
28        with mock.patch('shutil.which', return_value=None):
29            self.assertNotEqual(serial_boot_device.main('reboot'), 0)
30
31    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[True])
32    @mock.patch('builtins.print')
33    def test_check_health_in_fuchsia(self, mock_print, *_) -> None:
34        self.assertEqual(serial_boot_device.main('health-check'), 0)
35        result = json.loads(mock_print.call_args.args[0])
36        self.assertEqual(result[0]['nodename'], 'fuchsia-node-id')
37        self.assertEqual(result[0]['state'], 'healthy')
38
39    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[False])
40    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[True])
41    @mock.patch('builtins.print')
42    def test_check_health_in_fastboot(self, mock_print, *_) -> None:
43        self.assertEqual(serial_boot_device.main('health-check'), 0)
44        result = json.loads(mock_print.call_args.args[0])
45        self.assertEqual(result[0]['nodename'], 'fuchsia-node-id')
46        self.assertEqual(result[0]['state'], 'healthy')
47
48    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[False])
49    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[False])
50    def test_check_health_undetectable(self, *_) -> None:
51        self.assertNotEqual(serial_boot_device.main('health-check'), 0)
52
53    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[False])
54    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[False])
55    @mock.patch('subprocess.run',
56                return_value=CompletedProcess(args=['/bin'], returncode=0))
57    def test_boot_undetectable(self, mock_run, *_) -> None:
58        self.assertNotEqual(serial_boot_device.main('reboot'), 0)
59        mock_run.assert_not_called()
60
61    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[True, True])
62    @mock.patch('subprocess.run',
63                return_value=CompletedProcess(args=['/bin'], returncode=0))
64    def test_boot_from_fuchsia_to_fuchsia(self, mock_run, *_) -> None:
65        self.assertEqual(serial_boot_device.main('reboot'), 0)
66        mock_run.assert_called_once_with(
67            ['serialio', 'fuchsia-node-id', 'send', 'dm', 'reboot'])
68
69    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[True])
70    @mock.patch('subprocess.run',
71                return_value=CompletedProcess(args=['/bin'], returncode=0))
72    def test_boot_from_fuchsia_to_fuchsia_not_must_boot(self, mock_run,
73                                                        *_) -> None:
74        self.assertTrue(
75            serial_boot_device.boot_device('fuchsia-node-id',
76                                           'fuchsia-serial-num',
77                                           BootMode.REGULAR,
78                                           must_boot=False))
79        mock_run.assert_not_called()
80
81    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[False, True])
82    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[True])
83    @mock.patch('subprocess.run',
84                return_value=CompletedProcess(args=['/bin'], returncode=0))
85    def test_boot_from_fastboot_to_fuchsia(self, mock_run, *_) -> None:
86        self.assertEqual(serial_boot_device.main('reboot'), 0)
87        mock_run.assert_called_once_with(
88            ['fastboot', 'reboot', '-s', 'fuchsia-serial-num'],
89            capture_output=True,
90            timeout=30)
91
92    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[True])
93    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[True])
94    @mock.patch('subprocess.run',
95                return_value=CompletedProcess(args=['/bin'], returncode=0))
96    def test_boot_from_fuchsia_to_fastboot(self, mock_run, *_) -> None:
97        self.assertEqual(serial_boot_device.main('reboot-fastboot'), 0)
98        mock_run.assert_called_once_with(
99            ['serialio', 'fuchsia-node-id', 'send', 'dm', 'reboot-bootloader'])
100
101    @mock.patch('serial_boot_device.is_in_fuchsia', side_effect=[False])
102    @mock.patch('serial_boot_device.is_in_fastboot', side_effect=[True])
103    @mock.patch('subprocess.run',
104                return_value=CompletedProcess(args=['/bin'], returncode=0))
105    def test_boot_from_fastboot_to_fastboot(self, mock_run, *_) -> None:
106        self.assertEqual(serial_boot_device.main('reboot-fastboot'), 0)
107        mock_run.assert_not_called()
108
109
110if __name__ == '__main__':
111    unittest.main()
112