summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/build/build_flasher_test.py
blob: 21e81b3da8007ad826637dca3fb39bcdffa8cfbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
#
# Copyright (C) 2017 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.
#

import os
import sys
import unittest

try:
    from unittest import mock
except ImportError:
    import mock

from host_controller.build import build_flasher


class BuildFlasherTest(unittest.TestCase):
    """Tests for Build Flasher"""

    @mock.patch(
        "host_controller.build.build_flasher.android_device")
    @mock.patch("host_controller.build.build_flasher.os")
    def testFlashGSIBadPath(self, mock_os, mock_class):
        flasher = build_flasher.BuildFlasher("thisismyserial")
        mock_os.path.exists.return_value = False
        with self.assertRaises(ValueError) as cm:
            flasher.FlashGSI("notexists.img")
        self.assertEqual("Couldn't find system image at notexists.img",
                         str(cm.exception))

    @mock.patch(
        "host_controller.build.build_flasher.android_device")
    @mock.patch("host_controller.build.build_flasher.os")
    def testFlashGSISystemOnly(self, mock_os, mock_class):
        mock_device = mock.Mock()
        mock_class.AndroidDevice.return_value = mock_device
        flasher = build_flasher.BuildFlasher("thisismyserial")
        mock_os.path.exists.return_value = True
        flasher.FlashGSI("exists.img")
        mock_device.fastboot.erase.assert_any_call('system')
        mock_device.fastboot.flash.assert_any_call('system', 'exists.img')
        mock_device.fastboot.erase.assert_any_call('metadata')

    @mock.patch(
        "host_controller.build.build_flasher.android_device")
    def testFlashall(self, mock_class):
        mock_device = mock.Mock()
        mock_class.AndroidDevice.return_value = mock_device
        flasher = build_flasher.BuildFlasher("thisismyserial")
        flasher.Flashall("path/to/dir")
        mock_device.fastboot.flashall.assert_called_with()

    @mock.patch(
        "host_controller.build.build_flasher.android_device")
    def testEmptySerial(self, mock_class):
        mock_class.list_adb_devices.return_value = ['oneserial']
        flasher = build_flasher.BuildFlasher(serial="")
        mock_class.AndroidDevice.assert_called_with(
            "oneserial", device_callback_port=-1)

    @mock.patch(
        "host_controller.build.build_flasher.android_device")
    def testFlashUsingCustomBinary(self, mock_class):
        """Test for FlashUsingCustomBinary().

            Tests if the method passes right args to customflasher
            and the execution path of the method.
        """
        mock_device = mock.Mock()
        mock_class.AndroidDevice.return_value = mock_device
        flasher = build_flasher.BuildFlasher("mySerial", "myCustomBinary")

        mock_device.isBootloaderMode = False
        device_images = {"img": "my/image/path"}
        flasher.FlashUsingCustomBinary(device_images, "reboottowhatever",
                                       ["myarg"])
        mock_device.adb.reboot.assert_called_with("reboottowhatever")
        mock_device.customflasher.ExecCustomFlasherCmd.assert_called_with(
            "myarg", "my/image/path")

        mock_device.reset_mock()
        mock_device.isBootloaderMode = True
        device_images["img"] = "my/other/image/path"
        flasher.FlashUsingCustomBinary(device_images, "reboottowhatever",
                                       ["myarg"])
        mock_device.adb.reboot.assert_not_called()
        mock_device.customflasher.ExecCustomFlasherCmd.assert_called_with(
            "myarg", "my/other/image/path")

    @mock.patch("host_controller.build.build_flasher.android_device")
    @mock.patch("host_controller.build.build_flasher.logging")
    @mock.patch("host_controller.build.build_flasher.cmd_utils")
    @mock.patch("host_controller.build.build_flasher.os")
    @mock.patch("host_controller.build.build_flasher.open",
                new_callable=mock.mock_open)
    def testRepackageArtifacts(self, mock_open, mock_os, mock_cmd_utils,
                               mock_logger, mock_class):
        """Test for RepackageArtifacts().

            Tests if the method executes in correct path regarding
            given arguments.
        """
        mock_device = mock.Mock()
        mock_class.AndroidDevice.return_value = mock_device
        flasher = build_flasher.BuildFlasher("serial")
        device_images = {
            "system.img": "/my/tmp/path/system.img",
            "vendor.img": "/my/tmp/path/vendor.img"
        }
        mock_cmd_utils.ExecuteOneShellCommand.return_value = "", "", 0
        ret = flasher.RepackageArtifacts(device_images, "tar.md5")
        self.assertEqual(ret, True)
        self.assertIsNotNone(device_images["img"])

        ret = flasher.RepackageArtifacts(device_images, "incorrect")
        self.assertFalse(ret)
        mock_logger.error.assert_called_with(
            "Please specify correct repackage form: --repackage=%s" ,"incorrect")


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