Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bdf0b1bb authored by William Hester's avatar William Hester
Browse files

Create integration test for fastboot CLI

We rely on parsing the `fastboot devices` output in device labs, and a
format change previously broke us. This test will ensure that the output
is exactly what we expect.

Test: atest --host fastboot_integration_test

Change-Id: I7606e7b3e4a1edfe487ff14bb06cbd3c0a3aa777
parent d1e286f1
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -446,3 +446,14 @@ cc_library_headers {
    host_supported: true,
    export_include_dirs: ["."],
}

python_test_host {
    name: "fastboot_integration_test",
    main: "test_fastboot.py",
    srcs: ["test_fastboot.py"],
    data: [":fastboot"],
    test_config: "fastboot_integration_test.xml",
    test_options: {
        unit_test: false,
    },
}
+19 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2023 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.
-->
<configuration description="Config to run fastboot integration tests">
    <option name="test-suite-tag" value="fastboot_unittests" />
    <test class="com.android.tradefed.testtype.python.PythonBinaryHostTest" >
        <option name="par-file-name" value="fastboot_integration_test" />
        <option name="test-timeout" value="1m" />
    </test>
</configuration>
 No newline at end of file
+53 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
#
# Copyright (C) 2023 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.
#
"""Tests for the fastboot command line utility.
"""

import re
import subprocess
import unittest


class DevicesTest(unittest.TestCase):
    """Tests for `fastboot devices`."""


    def test_devices(self):
        """Ensure that the format of `fastboot devices` does not change.

        `fastboot devices` should alternate between a line containing the
        device's serial number and fastboot state and an empty line
        """
        output = subprocess.check_output(["fastboot", "devices"])

        previous_line_was_empty = True
        for line in output.decode().splitlines():
            if previous_line_was_empty:
                if not re.match(r"[a-zA-Z\d]+\s+(bootloader|fastbootd)", line):
                    self.fail("%s does not match the expected format <serial no>\\s+(bootloader|fastbootd)" % line)
                previous_line_was_empty = False
            else:
                if line:
                    self.fail("Expected an empty line. Received '%s'" % line)
                previous_line_was_empty = True

        if len(output) == 0:
            self.fail("Output is empty. Are any devices connected?")


if __name__ == '__main__':
    unittest.main()