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