• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2016 - 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 android_compute_client."""
17import unittest
18
19from unittest import mock
20
21from acloud import errors
22from acloud.internal.lib import android_compute_client
23from acloud.internal.lib import driver_test_lib
24from acloud.internal.lib import gcompute_client
25
26
27class AndroidComputeClientTest(driver_test_lib.BaseDriverTest):
28    """Test AndroidComputeClient."""
29
30    PROJECT = "fake-project"
31    SERVICE_ACCOUNT_NAME = "fake@fake.com"
32    PRIVATE_KEY_PATH = "/fake/key/path"
33    SSH_PUBLIC_KEY_PATH = ""
34    IMAGE = "fake-image"
35    GS_IMAGE_SOURCE_URI = "https://storage.googleapis.com/fake-bucket/fake.tar.gz"
36    MACHINE_TYPE = "fake-machine-type"
37    MIN_MACHINE_SIZE = "fake-min-machine-size"
38    METADATA = ("metadata_key", "metadata_value")
39    NETWORK = "fake-network"
40    ZONE = "fake-zone"
41    ORIENTATION = "portrait"
42    DEVICE_RESOLUTION = "1200x1200x1200x1200"
43    TARGET = "aosp_cf_x86_phone-userdebug"
44    BUILD_ID = "2263051"
45    INSTANCE = "fake-instance"
46    BOOT_COMPLETED_MSG = "VIRTUAL_DEVICE_BOOT_COMPLETED"
47    BOOT_STARTED_MSG = "VIRTUAL_DEVICE_BOOT_STARTED"
48    DPI = 160
49    X_RES = 720
50    Y_RES = 1280
51    EXTRA_SCOPES = None
52
53    def _GetFakeConfig(self):
54        """Create a fake configuration object.
55
56        Returns:
57          A fake configuration mock object.
58        """
59        fake_cfg = mock.MagicMock()
60        fake_cfg.project = self.PROJECT
61        fake_cfg.service_account_name = self.SERVICE_ACCOUNT_NAME
62        fake_cfg.service_account_private_key_path = self.PRIVATE_KEY_PATH
63        fake_cfg.ssh_public_key_path = self.SSH_PUBLIC_KEY_PATH
64        fake_cfg.zone = self.ZONE
65        fake_cfg.machine_type = self.MACHINE_TYPE
66        fake_cfg.min_machine_size = self.MIN_MACHINE_SIZE
67        fake_cfg.network = self.NETWORK
68        fake_cfg.orientation = self.ORIENTATION
69        fake_cfg.resolution = self.DEVICE_RESOLUTION
70        fake_cfg.metadata_variable = {self.METADATA[0]: self.METADATA[1]}
71        fake_cfg.extra_scopes = self.EXTRA_SCOPES
72        return fake_cfg
73
74    def setUp(self):
75        """Set up the test."""
76        super(AndroidComputeClientTest, self).setUp()
77        self.Patch(android_compute_client.AndroidComputeClient,
78                   "InitResourceHandle")
79        self.android_compute_client = android_compute_client.AndroidComputeClient(
80            self._GetFakeConfig(), mock.MagicMock())
81
82   # pylint: disable=no-member
83    def testCreateImage(self):
84        """Test CreateImage."""
85        self.Patch(gcompute_client.ComputeClient, "CreateImage")
86        self.Patch(
87            gcompute_client.ComputeClient,
88            "CheckImageExists",
89            return_value=False)
90        image_name = "image-gce-x86-userdebug-2345-abcd"
91        self.android_compute_client.CreateImage(image_name,
92                                                self.GS_IMAGE_SOURCE_URI)
93        super(android_compute_client.AndroidComputeClient,
94              self.android_compute_client).CreateImage.assert_called_with(
95                  image_name, self.GS_IMAGE_SOURCE_URI)
96
97    def testCreateInstance(self):
98        """Test CreateInstance."""
99        image_project = None
100        gpu = None
101        self.Patch(
102            gcompute_client.ComputeClient,
103            "CompareMachineSize",
104            return_value=1)
105        self.Patch(gcompute_client.ComputeClient, "CreateInstance")
106        self.Patch(
107            gcompute_client.ComputeClient,
108            "_GetDiskArgs",
109            return_value=[{
110                "fake_arg": "fake_value"
111            }])
112        instance_name = "gce-x86-userdebug-2345-abcd"
113        extra_disk_name = "gce-x86-userdebug-2345-abcd-data"
114        expected_metadata = {
115            self.METADATA[0]: self.METADATA[1],
116            "cfg_sta_display_resolution": self.DEVICE_RESOLUTION,
117            "t_force_orientation": self.ORIENTATION,
118            "avd_type": "gce",
119            "display": "720x1280 (160)"
120        }
121
122        expected_disk_args = [{
123            "fake_arg": "fake_value"
124        }]
125
126        fake_avd_spec = mock.MagicMock()
127        fake_avd_spec.avd_type = "gce"
128        fake_avd_spec.hw_property = {
129            "x_res" : str(self.X_RES),
130            "y_res" : str(self.Y_RES),
131            "dpi" : str(self.DPI)}
132
133        self.android_compute_client.CreateInstance(instance_name, self.IMAGE,
134                                                   extra_disk_name=extra_disk_name,
135                                                   avd_spec=fake_avd_spec)
136        super(android_compute_client.AndroidComputeClient,
137              self.android_compute_client).CreateInstance.assert_called_with(
138                  instance_name, self.IMAGE, self.MACHINE_TYPE,
139                  expected_metadata, self.NETWORK, self.ZONE,
140                  expected_disk_args, image_project, gpu, extra_disk_name,
141                  extra_scopes=self.EXTRA_SCOPES, tags=None)
142
143    # pylint: disable=invalid-name
144    def testCheckMachineSizeMeetsRequirement(self):
145        """Test CheckMachineSize when machine size meets requirement."""
146        self.Patch(
147            gcompute_client.ComputeClient,
148            "CompareMachineSize",
149            return_value=1)
150        # pylint: disable=protected-access
151        self.android_compute_client._CheckMachineSize()
152        self.android_compute_client.CompareMachineSize.assert_called_with(
153            self.MACHINE_TYPE, self.MIN_MACHINE_SIZE, self.ZONE)
154
155    def testCheckMachineSizeDoesNotMeetRequirement(self):
156        """Test CheckMachineSize when machine size does not meet requirement."""
157        self.Patch(
158            gcompute_client.ComputeClient,
159            "CompareMachineSize",
160            return_value=-1)
161        self.assertRaisesRegexp(
162            errors.DriverError,
163            ".*does not meet the minimum required machine size.*",
164            # pylint: disable=protected-access
165            self.android_compute_client._CheckMachineSize)
166        self.android_compute_client.CompareMachineSize.assert_called_with(
167            self.MACHINE_TYPE, self.MIN_MACHINE_SIZE, self.ZONE)
168
169    def testCheckBoot(self):
170        """Test CheckBoot."""
171        self.Patch(
172            gcompute_client.ComputeClient,
173            "GetSerialPortOutput",
174            return_value="")
175        self.assertFalse(self.android_compute_client.CheckBoot(self.INSTANCE))
176        self.Patch(
177            gcompute_client.ComputeClient,
178            "GetSerialPortOutput",
179            return_value=self.BOOT_COMPLETED_MSG)
180        self.assertTrue(self.android_compute_client.CheckBoot(self.INSTANCE))
181        self.Patch(
182            gcompute_client.ComputeClient,
183            "GetSerialPortOutput",
184            return_value=self.BOOT_STARTED_MSG)
185        self.assertTrue(self.android_compute_client.CheckBoot(self.INSTANCE))
186
187
188if __name__ == "__main__":
189    unittest.main()
190