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