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 19import mock 20 21from acloud.internal import constants 22from acloud.internal.lib import cheeps_compute_client 23from acloud.internal.lib import driver_test_lib 24from acloud.internal.lib import gcompute_client 25 26 27class CheepsComputeClientTest(driver_test_lib.BaseDriverTest): 28 """Test CheepsComputeClient.""" 29 30 SSH_PUBLIC_KEY_PATH = "" 31 INSTANCE = "fake-instance" 32 IMAGE = "fake-image" 33 IMAGE_PROJECT = "fake-image-project" 34 MACHINE_TYPE = "fake-machine-type" 35 NETWORK = "fake-network" 36 ZONE = "fake-zone" 37 METADATA = {"metadata_key": "metadata_value"} 38 BOOT_DISK_SIZE_GB = 10 39 ANDROID_BUILD_ID = 123 40 ANDROID_BUILD_TARGET = 'cheese-userdebug' 41 DPI = 320 42 X_RES = 720 43 Y_RES = 1280 44 USER = "test_user" 45 PASSWORD = "test_password" 46 47 def _GetFakeConfig(self): 48 """Create a fake configuration object. 49 50 Returns: 51 A fake configuration mock object. 52 """ 53 54 fake_cfg = mock.MagicMock() 55 fake_cfg.ssh_public_key_path = self.SSH_PUBLIC_KEY_PATH 56 fake_cfg.machine_type = self.MACHINE_TYPE 57 fake_cfg.network = self.NETWORK 58 fake_cfg.zone = self.ZONE 59 fake_cfg.resolution = "{x}x{y}x32x{dpi}".format( 60 x=self.X_RES, y=self.Y_RES, dpi=self.DPI) 61 fake_cfg.metadata_variable = self.METADATA 62 return fake_cfg 63 64 def setUp(self): 65 """Set up the test.""" 66 67 super(CheepsComputeClientTest, self).setUp() 68 self.Patch(cheeps_compute_client.CheepsComputeClient, 69 "InitResourceHandle") 70 self.cheeps_compute_client = ( 71 cheeps_compute_client.CheepsComputeClient(self._GetFakeConfig(), 72 mock.MagicMock())) 73 self.Patch( 74 gcompute_client.ComputeClient, "CompareMachineSize", return_value=1) 75 self.Patch( 76 gcompute_client.ComputeClient, 77 "GetImage", 78 return_value={"diskSizeGb": self.BOOT_DISK_SIZE_GB}) 79 self.Patch(gcompute_client.ComputeClient, "CreateInstance") 80 81 def testCreateInstance(self): 82 """Test CreateInstance.""" 83 84 expected_metadata = { 85 'android_build_id': self.ANDROID_BUILD_ID, 86 'android_build_target': self.ANDROID_BUILD_TARGET, 87 'avd_type': "cheeps", 88 'cvd_01_dpi': str(self.DPI), 89 'cvd_01_x_res': str(self.X_RES), 90 'cvd_01_y_res': str(self.Y_RES), 91 'display': "%sx%s (%s)"%( 92 str(self.X_RES), 93 str(self.Y_RES), 94 str(self.DPI)), 95 'user': self.USER, 96 'password': self.PASSWORD, 97 } 98 expected_metadata.update(self.METADATA) 99 100 avd_spec = mock.MagicMock() 101 avd_spec.hw_property = {constants.HW_X_RES: str(self.X_RES), 102 constants.HW_Y_RES: str(self.Y_RES), 103 constants.HW_ALIAS_DPI: str(self.DPI)} 104 avd_spec.username = self.USER 105 avd_spec.password = self.PASSWORD 106 avd_spec.remote_image = { 107 constants.BUILD_ID: self.ANDROID_BUILD_ID, 108 constants.BUILD_TARGET: self.ANDROID_BUILD_TARGET, 109 } 110 111 self.cheeps_compute_client.CreateInstance( 112 self.INSTANCE, 113 self.IMAGE, 114 self.IMAGE_PROJECT, 115 avd_spec) 116 # pylint: disable=no-member 117 gcompute_client.ComputeClient.CreateInstance.assert_called_with( 118 self.cheeps_compute_client, 119 instance=self.INSTANCE, 120 image_name=self.IMAGE, 121 image_project=self.IMAGE_PROJECT, 122 disk_args=None, 123 metadata=expected_metadata, 124 machine_type=self.MACHINE_TYPE, 125 network=self.NETWORK, 126 zone=self.ZONE) 127 128if __name__ == "__main__": 129 unittest.main() 130