• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for remote_instance_cf_device_factory."""
15
16import glob
17import os
18import unittest
19
20from unittest import mock
21
22from acloud.create import avd_spec
23from acloud.internal import constants
24from acloud.internal.lib import android_build_client
25from acloud.internal.lib import auth
26from acloud.internal.lib import cvd_compute_client_multi_stage
27from acloud.internal.lib import driver_test_lib
28from acloud.internal.lib import ssh
29from acloud.list import list as list_instances
30from acloud.public.actions import remote_instance_fvp_device_factory
31
32
33class RemoteInstanceDeviceFactoryTest(driver_test_lib.BaseDriverTest):
34    """Test RemoteInstanceDeviceFactory."""
35
36    def setUp(self):
37        super().setUp()
38        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
39        self.Patch(android_build_client.AndroidBuildClient, "InitResourceHandle")
40        self.Patch(cvd_compute_client_multi_stage.CvdComputeClient, "InitResourceHandle")
41        self.Patch(list_instances, "GetInstancesFromInstanceNames", return_value=mock.MagicMock())
42        self.Patch(list_instances, "ChooseOneRemoteInstance", return_value=mock.MagicMock())
43        self.Patch(glob, "glob", return_vale=["fake.img"])
44
45    # pylint: disable=protected-access
46    @staticmethod
47    @mock.patch.object(
48        remote_instance_fvp_device_factory.RemoteInstanceDeviceFactory,
49        "CreateGceInstance")
50    @mock.patch.object(ssh, "ShellCmdWithRetry")
51    @mock.patch.dict(os.environ, {
52        constants.ENV_BUILD_TARGET:'fvp',
53        "ANDROID_HOST_OUT":'/path/to/host/out',
54        "ANDROID_PRODUCT_OUT":'/path/to/product/out',
55        "MODEL_BIN":'/path/to/model/FVP_Base_RevC-2xAEMv8A',
56    })
57    def testCreateInstance(mock_shell, mock_create_gce):
58        """Test CreateInstance."""
59        fake_ip = ssh.IP(external="1.1.1.1", internal="10.1.1.1")
60        args = mock.MagicMock()
61        # Test local image extract from image zip case.
62        args.config_file = ""
63        args.avd_type = constants.TYPE_FVP
64        args.flavor = "phone"
65        args.local_image = "fake_local_image"
66        args.local_system_image = None
67        args.adb_port = None
68        args.launch_args = None
69        avd_spec_local_image = avd_spec.AVDSpec(args)
70        factory = remote_instance_fvp_device_factory.RemoteInstanceDeviceFactory(
71            avd_spec_local_image)
72        factory._ssh = ssh.Ssh(ip=fake_ip,
73                               user=constants.GCE_USER,
74                               ssh_private_key_path="/fake/acloud_rea")
75        mock_open = mock.mock_open(read_data = (
76            "bl1.bin\n"
77            "boot.img\n"
78            "fip.bin\n"
79            "system-qemu.img\n"
80            "userdata.img\n"))
81        with mock.patch("builtins.open", mock_open):
82            factory.CreateInstance()
83
84        mock_create_gce.assert_called_once()
85
86        expected_cmds = [
87            ("tar -cf - --lzop -S -C /path/to/product/out bl1.bin boot.img "
88             "fip.bin system-qemu.img userdata.img | "
89             "%s -- tar -xf - --lzop -S" %
90             factory._ssh.GetBaseCmd(constants.SSH_BIN)),
91            ("tar -cf - --lzop -S -C /path/to/model . | "
92             "%s -- tar -xf - --lzop -S" %
93             factory._ssh.GetBaseCmd(constants.SSH_BIN)),
94            ("%s device/generic/goldfish/fvpbase/run_model_only "
95             "vsoc-01@1.1.1.1:run_model_only" %
96             factory._ssh.GetBaseCmd(constants.SCP_BIN)),
97            ("%s -- mkdir -p lib64" %
98             factory._ssh.GetBaseCmd(constants.SSH_BIN)),
99            ("%s /path/to/host/out/lib64/bind_to_localhost.so "
100             "vsoc-01@1.1.1.1:lib64/bind_to_localhost.so" %
101             factory._ssh.GetBaseCmd(constants.SCP_BIN)),
102            ("%s -- sh -c \"'ANDROID_HOST_OUT=. ANDROID_PRODUCT_OUT=. "
103             "MODEL_BIN=./FVP_Base_RevC-2xAEMv8A "
104             "./run_model_only > /dev/null 2> /dev/null &'\"" %
105             factory._ssh.GetBaseCmd(constants.SSH_BIN)),
106        ]
107        mock_shell.assert_has_calls([mock.call(cmd) for cmd in expected_cmds])
108
109if __name__ == "__main__":
110    unittest.main()
111