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"""Tests for acloud.internal.lib.goldfish_compute_client."""
17import unittest
18import mock
19
20from acloud.internal.lib import driver_test_lib
21from acloud.internal.lib import gcompute_client
22from acloud.internal.lib import goldfish_compute_client
23
24
25class GoldfishComputeClientTest(driver_test_lib.BaseDriverTest):
26    """Test GoldfishComputeClient."""
27
28    SSH_PUBLIC_KEY_PATH = ""
29    INSTANCE = "fake-instance"
30    IMAGE = "fake-image"
31    IMAGE_PROJECT = "fake-iamge-project"
32    MACHINE_TYPE = "fake-machine-type"
33    NETWORK = "fake-network"
34    ZONE = "fake-zone"
35    BRANCH = "fake-branch"
36    TARGET = "sdk_phone_x86_64-sdk"
37    BUILD_ID = "2263051"
38    KERNEL_BRANCH = "kernel-p-dev-android-goldfish-4.14-x86-64"
39    KERNEL_BUILD_ID = "112233"
40    KERNEL_BUILD_TARGET = "kernel_x86_64"
41    KERNEL_BUILD_ARTIFACT = "bzImage"
42    EMULATOR_BRANCH = "aosp-emu-master-dev"
43    EMULATOR_BUILD_ID = "1234567"
44    DPI = 160
45    X_RES = 720
46    Y_RES = 1280
47    METADATA = {"metadata_key": "metadata_value"}
48    EXTRA_DATA_DISK_SIZE_GB = 4
49    BOOT_DISK_SIZE_GB = 10
50    GPU = "nvidia-tesla-k80"
51    EXTRA_SCOPES = "scope1"
52    TAGS = ['http-server']
53
54    def _GetFakeConfig(self):
55        """Create a fake configuration object.
56
57        Returns:
58            A fake configuration mock object.
59        """
60        fake_cfg = mock.MagicMock()
61        fake_cfg.ssh_public_key_path = self.SSH_PUBLIC_KEY_PATH
62        fake_cfg.machine_type = self.MACHINE_TYPE
63        fake_cfg.network = self.NETWORK
64        fake_cfg.zone = self.ZONE
65        fake_cfg.resolution = "{x}x{y}x32x{dpi}".format(
66            x=self.X_RES, y=self.Y_RES, dpi=self.DPI)
67        fake_cfg.metadata_variable = self.METADATA
68        fake_cfg.extra_data_disk_size_gb = self.EXTRA_DATA_DISK_SIZE_GB
69        fake_cfg.extra_scopes = self.EXTRA_SCOPES
70        return fake_cfg
71
72    def setUp(self):
73        """Set up the test."""
74        super(GoldfishComputeClientTest, self).setUp()
75        self.Patch(goldfish_compute_client.GoldfishComputeClient,
76                   "InitResourceHandle")
77        self.goldfish_compute_client = (
78            goldfish_compute_client.GoldfishComputeClient(
79                self._GetFakeConfig(), mock.MagicMock()))
80        self.Patch(
81            gcompute_client.ComputeClient,
82            "CompareMachineSize",
83            return_value=1)
84        self.Patch(
85            gcompute_client.ComputeClient,
86            "GetImage",
87            return_value={"diskSizeGb": 10})
88        self.Patch(gcompute_client.ComputeClient, "CreateInstance")
89        self.Patch(
90            goldfish_compute_client.GoldfishComputeClient,
91            "_GetDiskArgs",
92            return_value=[{
93                "fake_arg": "fake_value"
94            }])
95
96    @mock.patch("getpass.getuser", return_value="fake_user")
97    def testCreateInstance(self, _mock_user):
98        """Test CreateInstance."""
99
100        expected_metadata = {
101            "user": "fake_user",
102            "avd_type": "goldfish",
103            "cvd_01_fetch_android_build_target": self.TARGET,
104            "cvd_01_fetch_android_bid":
105                "{branch}/{build_id}".format(
106                    branch=self.BRANCH, build_id=self.BUILD_ID),
107            "cvd_01_fetch_kernel_bid":
108                "{branch}/{build_id}".format(
109                    branch=self.KERNEL_BRANCH, build_id=self.KERNEL_BUILD_ID),
110            "cvd_01_fetch_kernel_build_target": self.KERNEL_BUILD_TARGET,
111            "cvd_01_fetch_kernel_build_artifact": self.KERNEL_BUILD_ARTIFACT,
112            "cvd_01_use_custom_kernel": "true",
113            "cvd_01_fetch_emulator_bid":
114                "{branch}/{build_id}".format(
115                    branch=self.EMULATOR_BRANCH,
116                    build_id=self.EMULATOR_BUILD_ID),
117            "cvd_01_launch": "1",
118            "cvd_01_dpi": str(self.DPI),
119            "cvd_01_x_res": str(self.X_RES),
120            "cvd_01_y_res": str(self.Y_RES),
121        }
122        expected_metadata.update(self.METADATA)
123        expected_disk_args = [{"fake_arg": "fake_value"}]
124
125        self.goldfish_compute_client.CreateInstance(
126            self.INSTANCE, self.IMAGE, self.IMAGE_PROJECT, self.TARGET,
127            self.BRANCH, self.BUILD_ID,
128            self.KERNEL_BRANCH,
129            self.KERNEL_BUILD_ID,
130            self.KERNEL_BUILD_TARGET,
131            self.EMULATOR_BRANCH,
132            self.EMULATOR_BUILD_ID, self.EXTRA_DATA_DISK_SIZE_GB, self.GPU,
133            extra_scopes=self.EXTRA_SCOPES,
134            tags=self.TAGS)
135
136        # pylint: disable=no-member
137        gcompute_client.ComputeClient.CreateInstance.assert_called_with(
138            self.goldfish_compute_client,
139            instance=self.INSTANCE,
140            image_name=self.IMAGE,
141            image_project=self.IMAGE_PROJECT,
142            disk_args=expected_disk_args,
143            metadata=expected_metadata,
144            machine_type=self.MACHINE_TYPE,
145            network=self.NETWORK,
146            zone=self.ZONE,
147            gpu=self.GPU,
148            tags=self.TAGS,
149            extra_scopes=self.EXTRA_SCOPES)
150
151
152if __name__ == "__main__":
153    unittest.main()
154