1#!/usr/bin/env python 2# 3# Copyright 2019 - 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"""Tests for acloud.internal.lib.cheeps_compute_client.""" 17 18import unittest 19 20from unittest import mock 21 22from acloud.internal import constants 23from acloud.internal.lib import cheeps_compute_client 24from acloud.internal.lib import driver_test_lib 25from acloud.internal.lib import gcompute_client 26 27 28class CheepsComputeClientTest(driver_test_lib.BaseDriverTest): 29 """Test CheepsComputeClient.""" 30 31 SSH_PUBLIC_KEY_PATH = "" 32 INSTANCE = "fake-instance" 33 IMAGE = "fake-image" 34 IMAGE_PROJECT = "fake-image-project" 35 MACHINE_TYPE = "fake-machine-type" 36 NETWORK = "fake-network" 37 ZONE = "fake-zone" 38 METADATA = {"metadata_key": "metadata_value"} 39 BOOT_DISK_SIZE_GB = 10 40 ANDROID_BUILD_ID = 123 41 ANDROID_BUILD_TARGET = 'cheese-userdebug' 42 DPI = 320 43 X_RES = 720 44 Y_RES = 1280 45 USER = "test_user" 46 PASSWORD = "test_password" 47 CHEEPS_BETTY_IMAGE = 'abcasdf' 48 49 def _GetFakeConfig(self): 50 """Create a fake configuration object. 51 52 Returns: 53 A fake configuration mock object. 54 """ 55 56 fake_cfg = mock.MagicMock() 57 fake_cfg.ssh_public_key_path = self.SSH_PUBLIC_KEY_PATH 58 fake_cfg.machine_type = self.MACHINE_TYPE 59 fake_cfg.network = self.NETWORK 60 fake_cfg.zone = self.ZONE 61 fake_cfg.resolution = "{x}x{y}x32x{dpi}".format( 62 x=self.X_RES, y=self.Y_RES, dpi=self.DPI) 63 fake_cfg.metadata_variable = self.METADATA 64 return fake_cfg 65 66 def setUp(self): 67 """Set up the test.""" 68 69 super(CheepsComputeClientTest, self).setUp() 70 self.Patch(cheeps_compute_client.CheepsComputeClient, 71 "InitResourceHandle") 72 self.cheeps_compute_client = ( 73 cheeps_compute_client.CheepsComputeClient(self._GetFakeConfig(), 74 mock.MagicMock())) 75 self.Patch( 76 gcompute_client.ComputeClient, "CompareMachineSize", return_value=1) 77 self.Patch( 78 gcompute_client.ComputeClient, 79 "GetImage", 80 return_value={"diskSizeGb": self.BOOT_DISK_SIZE_GB}) 81 self.Patch(gcompute_client.ComputeClient, "CreateInstance") 82 83 def testCreateInstance(self): 84 """Test CreateInstance.""" 85 86 expected_metadata = { 87 'android_build_id': self.ANDROID_BUILD_ID, 88 'android_build_target': self.ANDROID_BUILD_TARGET, 89 'avd_type': "cheeps", 90 'betty_image': self.CHEEPS_BETTY_IMAGE, 91 'cvd_01_dpi': str(self.DPI), 92 'cvd_01_x_res': str(self.X_RES), 93 'cvd_01_y_res': str(self.Y_RES), 94 'display': "%sx%s (%s)"%( 95 str(self.X_RES), 96 str(self.Y_RES), 97 str(self.DPI)), 98 'user': self.USER, 99 'password': self.PASSWORD, 100 } 101 expected_metadata.update(self.METADATA) 102 103 avd_spec = mock.MagicMock() 104 avd_spec.hw_property = {constants.HW_X_RES: str(self.X_RES), 105 constants.HW_Y_RES: str(self.Y_RES), 106 constants.HW_ALIAS_DPI: str(self.DPI)} 107 avd_spec.username = self.USER 108 avd_spec.password = self.PASSWORD 109 avd_spec.remote_image = { 110 constants.BUILD_ID: self.ANDROID_BUILD_ID, 111 constants.BUILD_TARGET: self.ANDROID_BUILD_TARGET, 112 constants.CHEEPS_BETTY_IMAGE: self.CHEEPS_BETTY_IMAGE, 113 } 114 115 self.cheeps_compute_client.CreateInstance( 116 self.INSTANCE, 117 self.IMAGE, 118 self.IMAGE_PROJECT, 119 avd_spec) 120 # pylint: disable=no-member 121 gcompute_client.ComputeClient.CreateInstance.assert_called_with( 122 self.cheeps_compute_client, 123 instance=self.INSTANCE, 124 image_name=self.IMAGE, 125 image_project=self.IMAGE_PROJECT, 126 disk_args=None, 127 metadata=expected_metadata, 128 machine_type=self.MACHINE_TYPE, 129 network=self.NETWORK, 130 zone=self.ZONE) 131 132 def testCreateInstanceMissingParams(self): 133 """Test CreateInstance with optional avd_spec parameters missing.""" 134 expected_metadata = { 135 'android_build_id': self.ANDROID_BUILD_ID, 136 'android_build_target': self.ANDROID_BUILD_TARGET, 137 'avd_type': "cheeps", 138 'betty_image': None, 139 'cvd_01_dpi': str(self.DPI), 140 'cvd_01_x_res': str(self.X_RES), 141 'cvd_01_y_res': str(self.Y_RES), 142 'display': "%sx%s (%s)"%( 143 str(self.X_RES), 144 str(self.Y_RES), 145 str(self.DPI)), 146 } 147 expected_metadata.update(self.METADATA) 148 149 150 avd_spec = mock.MagicMock() 151 avd_spec.hw_property = {constants.HW_X_RES: str(self.X_RES), 152 constants.HW_Y_RES: str(self.Y_RES), 153 constants.HW_ALIAS_DPI: str(self.DPI)} 154 avd_spec.username = None 155 avd_spec.password = None 156 avd_spec.remote_image = { 157 constants.BUILD_ID: self.ANDROID_BUILD_ID, 158 constants.BUILD_TARGET: self.ANDROID_BUILD_TARGET, 159 constants.CHEEPS_BETTY_IMAGE: None, 160 } 161 162 self.cheeps_compute_client.CreateInstance( 163 self.INSTANCE, 164 self.IMAGE, 165 self.IMAGE_PROJECT, 166 avd_spec) 167 # pylint: disable=no-member 168 gcompute_client.ComputeClient.CreateInstance.assert_called_with( 169 self.cheeps_compute_client, 170 instance=self.INSTANCE, 171 image_name=self.IMAGE, 172 image_project=self.IMAGE_PROJECT, 173 disk_args=None, 174 metadata=expected_metadata, 175 machine_type=self.MACHINE_TYPE, 176 network=self.NETWORK, 177 zone=self.ZONE) 178 179if __name__ == "__main__": 180 unittest.main() 181