1#!/usr/bin/env python 2# 3# Copyright 2018 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of 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, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Tests for acloud.internal.lib.cvd_compute_client.""" 18 19import glob 20import unittest 21 22from unittest import mock 23 24from acloud.create import avd_spec 25from acloud.internal import constants 26from acloud.internal.lib import cvd_compute_client 27from acloud.internal.lib import driver_test_lib 28from acloud.internal.lib import gcompute_client 29from acloud.internal.lib import utils 30from acloud.list import list as list_instances 31 32 33class CvdComputeClientTest(driver_test_lib.BaseDriverTest): 34 """Test CvdComputeClient.""" 35 36 SSH_PUBLIC_KEY_PATH = "" 37 INSTANCE = "fake-instance" 38 IMAGE = "fake-image" 39 IMAGE_PROJECT = "fake-iamge-project" 40 MACHINE_TYPE = "fake-machine-type" 41 NETWORK = "fake-network" 42 ZONE = "fake-zone" 43 BRANCH = "fake-branch" 44 TARGET = "aosp_cf_x86_phone-userdebug" 45 BUILD_ID = "2263051" 46 KERNEL_BRANCH = "fake-kernel-branch" 47 KERNEL_BUILD_ID = "1234567" 48 KERNEL_BUILD_TARGET = "kernel" 49 DPI = 160 50 X_RES = 720 51 Y_RES = 1280 52 METADATA = {"metadata_key": "metadata_value"} 53 EXTRA_DATA_DISK_SIZE_GB = 4 54 BOOT_DISK_SIZE_GB = 10 55 LAUNCH_ARGS = "--setupwizard_mode=REQUIRED" 56 EXTRA_SCOPES = ["scope1"] 57 58 def _GetFakeConfig(self): 59 """Create a fake configuration object. 60 61 Returns: 62 A fake configuration mock object. 63 """ 64 fake_cfg = mock.MagicMock() 65 fake_cfg.ssh_public_key_path = self.SSH_PUBLIC_KEY_PATH 66 fake_cfg.machine_type = self.MACHINE_TYPE 67 fake_cfg.network = self.NETWORK 68 fake_cfg.zone = self.ZONE 69 fake_cfg.resolution = "{x}x{y}x32x{dpi}".format( 70 x=self.X_RES, y=self.Y_RES, dpi=self.DPI) 71 fake_cfg.metadata_variable = self.METADATA 72 fake_cfg.extra_data_disk_size_gb = self.EXTRA_DATA_DISK_SIZE_GB 73 fake_cfg.launch_args = self.LAUNCH_ARGS 74 fake_cfg.extra_scopes = self.EXTRA_SCOPES 75 return fake_cfg 76 77 def setUp(self): 78 """Set up the test.""" 79 super().setUp() 80 self.Patch(cvd_compute_client.CvdComputeClient, "InitResourceHandle") 81 self.Patch(list_instances, "ChooseOneRemoteInstance", return_value=mock.MagicMock()) 82 self.Patch(list_instances, "GetInstancesFromInstanceNames", return_value=mock.MagicMock()) 83 self.cvd_compute_client = cvd_compute_client.CvdComputeClient( 84 self._GetFakeConfig(), mock.MagicMock()) 85 86 @mock.patch.object(utils, "GetBuildEnvironmentVariable", return_value="fake_cf_x86") 87 @mock.patch.object(glob, "glob", return_value=["fake.img"]) 88 @mock.patch.object(gcompute_client.ComputeClient, "CompareMachineSize", 89 return_value=1) 90 @mock.patch.object(gcompute_client.ComputeClient, "GetImage", 91 return_value={"diskSizeGb": 10}) 92 @mock.patch.object(gcompute_client.ComputeClient, "CreateInstance") 93 @mock.patch.object(cvd_compute_client.CvdComputeClient, "_GetDiskArgs", 94 return_value=[{"fake_arg": "fake_value"}]) 95 @mock.patch("getpass.getuser", return_value="fake_user") 96 def testCreateInstance(self, _get_user, _get_disk_args, mock_create, 97 _get_image, _compare_machine_size, mock_check_img, 98 _mock_env): 99 """Test CreateInstance.""" 100 expected_metadata = { 101 "cvd_01_dpi": str(self.DPI), 102 "cvd_01_fetch_android_build_target": self.TARGET, 103 "cvd_01_fetch_android_bid": "{branch}/{build_id}".format( 104 branch=self.BRANCH, build_id=self.BUILD_ID), 105 "cvd_01_fetch_kernel_bid": "{branch}/{build_id}".format( 106 branch=self.KERNEL_BRANCH, build_id=self.KERNEL_BUILD_ID), 107 "cvd_01_fetch_kernel_build_target": self.KERNEL_BUILD_TARGET, 108 "cvd_01_x_res": str(self.X_RES), 109 "cvd_01_y_res": str(self.Y_RES), 110 "user": "fake_user", 111 "cvd_01_data_policy": 112 self.cvd_compute_client.DATA_POLICY_CREATE_IF_MISSING, 113 "cvd_01_blank_data_disk_size": str(self.EXTRA_DATA_DISK_SIZE_GB * 1024), 114 } 115 expected_metadata_local_image = { 116 "cvd_01_dpi": str(self.DPI), 117 "cvd_01_x_res": str(self.X_RES), 118 "cvd_01_y_res": str(self.Y_RES), 119 "user": "fake_user", 120 "cvd_01_data_policy": 121 self.cvd_compute_client.DATA_POLICY_CREATE_IF_MISSING, 122 "cvd_01_blank_data_disk_size": str(self.EXTRA_DATA_DISK_SIZE_GB * 1024), 123 } 124 expected_metadata.update(self.METADATA) 125 expected_metadata_local_image.update(self.METADATA) 126 remote_image_metadata = dict(expected_metadata) 127 remote_image_metadata["cvd_01_launch"] = self.LAUNCH_ARGS 128 expected_disk_args = [{"fake_arg": "fake_value"}] 129 130 self.cvd_compute_client.CreateInstance( 131 self.INSTANCE, self.IMAGE, self.IMAGE_PROJECT, self.TARGET, 132 self.BRANCH, self.BUILD_ID, self.KERNEL_BRANCH, 133 self.KERNEL_BUILD_ID, self.KERNEL_BUILD_TARGET, 134 self.EXTRA_DATA_DISK_SIZE_GB, extra_scopes=self.EXTRA_SCOPES) 135 mock_create.assert_called_with( 136 self.cvd_compute_client, 137 instance=self.INSTANCE, 138 image_name=self.IMAGE, 139 image_project=self.IMAGE_PROJECT, 140 disk_args=expected_disk_args, 141 metadata=remote_image_metadata, 142 machine_type=self.MACHINE_TYPE, 143 network=self.NETWORK, 144 zone=self.ZONE, 145 extra_scopes=self.EXTRA_SCOPES) 146 147 #test use local image in the remote instance. 148 local_image_metadata = dict(expected_metadata_local_image) 149 args = mock.MagicMock() 150 mock_check_img.return_value = True 151 args.local_image = constants.FIND_IN_BUILD_ENV 152 args.local_system_image = None 153 args.config_file = "" 154 args.avd_type = constants.TYPE_CF 155 args.flavor = "phone" 156 args.adb_port = None 157 args.remote_host = False 158 args.launch_args = None 159 fake_avd_spec = avd_spec.AVDSpec(args) 160 fake_avd_spec.hw_property[constants.HW_X_RES] = str(self.X_RES) 161 fake_avd_spec.hw_property[constants.HW_Y_RES] = str(self.Y_RES) 162 fake_avd_spec.hw_property[constants.HW_ALIAS_DPI] = str(self.DPI) 163 fake_avd_spec.hw_property[constants.HW_ALIAS_DISK] = str( 164 self.EXTRA_DATA_DISK_SIZE_GB * 1024) 165 local_image_metadata["avd_type"] = constants.TYPE_CF 166 local_image_metadata["flavor"] = "phone" 167 local_image_metadata[constants.INS_KEY_DISPLAY] = ("%sx%s (%s)" % ( 168 fake_avd_spec.hw_property[constants.HW_X_RES], 169 fake_avd_spec.hw_property[constants.HW_Y_RES], 170 fake_avd_spec.hw_property[constants.HW_ALIAS_DPI])) 171 self.cvd_compute_client.CreateInstance( 172 self.INSTANCE, self.IMAGE, self.IMAGE_PROJECT, self.TARGET, self.BRANCH, 173 self.BUILD_ID, self.KERNEL_BRANCH, self.KERNEL_BUILD_ID, 174 self.KERNEL_BUILD_TARGET, self.EXTRA_DATA_DISK_SIZE_GB, 175 fake_avd_spec, extra_scopes=self.EXTRA_SCOPES) 176 177 mock_create.assert_called_with( 178 self.cvd_compute_client, 179 instance=self.INSTANCE, 180 image_name=self.IMAGE, 181 image_project=self.IMAGE_PROJECT, 182 disk_args=expected_disk_args, 183 metadata=local_image_metadata, 184 machine_type=self.MACHINE_TYPE, 185 network=self.NETWORK, 186 zone=self.ZONE, 187 extra_scopes=self.EXTRA_SCOPES) 188 189 # pylint: disable=protected-access 190 def testGetLaunchCvdArgs(self): 191 """Test GetLaunchCvdArgs""" 192 fake_avd_spec = mock.MagicMock() 193 fake_avd_spec.hw_property = {} 194 fake_avd_spec.hw_property[constants.HW_ALIAS_CPUS] = "2" 195 fake_avd_spec.hw_property[constants.HW_ALIAS_MEMORY] = "4096" 196 197 # Test get launch_args exist from config 198 self.assertEqual(self.cvd_compute_client._GetLaunchCvdArgs(fake_avd_spec), 199 self.LAUNCH_ARGS) 200 201 # Test get launch_args from cpu and memory 202 expected_args = "-cpus=2 -memory_mb=4096" 203 self.cvd_compute_client._launch_args = None 204 self.assertEqual(self.cvd_compute_client._GetLaunchCvdArgs(fake_avd_spec), 205 expected_args) 206 207 # Test to set launch_args as "1" for no customized args 208 expected_args = "1" 209 fake_avd_spec.hw_property = {} 210 self.assertEqual(self.cvd_compute_client._GetLaunchCvdArgs(fake_avd_spec), 211 expected_args) 212 213 self.cvd_compute_client._launch_args = self.LAUNCH_ARGS 214 215 216if __name__ == "__main__": 217 unittest.main() 218