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.public.actions.create_goldfish_actions."""
17import uuid
18import unittest
19import mock
20
21from acloud.internal import constants
22from acloud.internal.lib import android_build_client
23from acloud.internal.lib import android_compute_client
24from acloud.internal.lib import auth
25from acloud.internal.lib import driver_test_lib
26from acloud.internal.lib import goldfish_compute_client
27from acloud.internal.lib import ssh
28from acloud.public.actions import create_goldfish_action
29
30
31class CreateGoldfishActionTest(driver_test_lib.BaseDriverTest):
32    """Tests create_goldfish_action."""
33
34    IP = ssh.IP(external="127.0.0.1", internal="10.0.0.1")
35    INSTANCE = "fake-instance"
36    IMAGE = "fake-image"
37    BUILD_TARGET = "fake-build-target"
38    EMULATOR_BUILD_TARGET = "emu-fake-target"
39    BUILD_ID = "12345"
40    EMULATOR_BUILD_ID = "1234567"
41    GPU = "nvidia-tesla-k80"
42    BRANCH = "fake-branch"
43    EMULATOR_BRANCH = "emu-fake-branch"
44    KERNEL_BRANCH = "fake-kernel-branch"
45    KERNEL_BUILD_ID = "54321"
46    KERNEL_BUILD_TARGET = "kernel"
47    GOLDFISH_HOST_IMAGE_NAME = "fake-stable-host-image-name"
48    GOLDFISH_HOST_IMAGE_PROJECT = "fake-stable-host-image-project"
49    EXTRA_DATA_DISK_GB = 4
50    EXTRA_SCOPES = None
51
52    def setUp(self):
53        """Sets up the test."""
54        super(CreateGoldfishActionTest, self).setUp()
55        self.build_client = mock.MagicMock()
56        self.Patch(
57            android_build_client,
58            "AndroidBuildClient",
59            return_value=self.build_client)
60        self.compute_client = mock.MagicMock()
61        self.Patch(
62            goldfish_compute_client,
63            "GoldfishComputeClient",
64            return_value=self.compute_client)
65        self.Patch(
66            android_compute_client,
67            "AndroidComputeClient",
68            return_value=self.compute_client)
69        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
70        #Initialize new avd_spec
71        self.avd_spec = mock.MagicMock()
72        self.avd_spec.cfg = self._CreateCfg()
73        self.avd_spec.remote_image = {constants.BUILD_ID: self.BUILD_ID,
74                                      constants.BUILD_BRANCH: self.BRANCH,
75                                      constants.BUILD_TARGET: self.BUILD_TARGET}
76        self.avd_spec.emulator_build_id = self.EMULATOR_BUILD_ID
77        self.avd_spec.gpu = self.GPU
78        self.avd_spec.serial_log_file = None
79        self.avd_spec.autoconnect = False
80
81    def _CreateCfg(self):
82        """A helper method that creates a mock configuration object."""
83        cfg = mock.MagicMock()
84        cfg.service_account_name = "fake@service.com"
85        cfg.service_account_private_key_path = "/fake/path/to/key"
86        cfg.zone = "fake_zone"
87        cfg.ssh_private_key_path = ""
88        cfg.ssh_public_key_path = ""
89        cfg.stable_goldfish_host_image_name = self.GOLDFISH_HOST_IMAGE_NAME
90        cfg.stable_goldfish_host_image_project = self.GOLDFISH_HOST_IMAGE_PROJECT
91        cfg.emulator_build_target = self.EMULATOR_BUILD_TARGET
92        cfg.extra_data_disk_size_gb = self.EXTRA_DATA_DISK_GB
93        cfg.extra_scopes = self.EXTRA_SCOPES
94        return cfg
95
96    def testCreateDevices(self):
97        """Tests CreateDevices."""
98        cfg = self._CreateCfg()
99
100        # Mock uuid
101        fake_uuid = mock.MagicMock(hex="1234")
102        self.Patch(uuid, "uuid4", return_value=fake_uuid)
103
104        # Mock compute client methods
105        self.compute_client.GetInstanceIP.return_value = self.IP
106        self.compute_client.GenerateImageName.return_value = self.IMAGE
107        self.compute_client.GenerateInstanceName.return_value = self.INSTANCE
108
109        # Mock build client method
110        self.build_client.GetBuildInfo.side_effect = [
111            android_build_client.BuildInfo(
112                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
113            android_build_client.BuildInfo(
114                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
115                self.EMULATOR_BUILD_TARGET, None),
116            android_build_client.BuildInfo(
117                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
118                self.KERNEL_BUILD_TARGET, None)]
119
120        none_avd_spec = None
121
122        # Call CreateDevices with avd_spec is None
123        report = create_goldfish_action.CreateDevices(
124            none_avd_spec, cfg, build_target=self.BUILD_TARGET,
125            build_id=self.BUILD_ID, emulator_build_id=self.EMULATOR_BUILD_ID,
126            gpu=self.GPU,
127            kernel_branch=self.KERNEL_BRANCH,
128            kernel_build_id=self.KERNEL_BUILD_ID,
129            kernel_build_target=self.KERNEL_BUILD_TARGET)
130
131        # Verify
132        self.compute_client.CreateInstance.assert_called_with(
133            instance=self.INSTANCE,
134            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
135            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
136            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
137            build_target=self.BUILD_TARGET,
138            branch=self.BRANCH,
139            build_id=self.BUILD_ID,
140            emulator_branch=self.EMULATOR_BRANCH,
141            emulator_build_id=self.EMULATOR_BUILD_ID,
142            kernel_branch=self.KERNEL_BRANCH,
143            kernel_build_id=self.KERNEL_BUILD_ID,
144            kernel_build_target=self.KERNEL_BUILD_TARGET,
145            gpu=self.GPU,
146            avd_spec=none_avd_spec,
147            extra_scopes=self.EXTRA_SCOPES,
148            tags=None)
149
150        self.assertEqual(report.data, {
151            "devices": [
152                {
153                    "instance_name": self.INSTANCE,
154                    "ip": self.IP.external,
155                    "branch": self.BRANCH,
156                    "build_id": self.BUILD_ID,
157                    "build_target": self.BUILD_TARGET,
158                    "emulator_branch": self.EMULATOR_BRANCH,
159                    "emulator_build_id": self.EMULATOR_BUILD_ID,
160                    "emulator_build_target": self.EMULATOR_BUILD_TARGET,
161                    "kernel_branch": self.KERNEL_BRANCH,
162                    "kernel_build_id": self.KERNEL_BUILD_ID,
163                    "kernel_build_target": self.KERNEL_BUILD_TARGET,
164                },
165            ],
166        })
167        self.assertEqual(report.command, "create_gf")
168        self.assertEqual(report.status, "SUCCESS")
169
170        # Call CreateDevices with avd_spec
171        self.build_client.GetBranch.side_effect = [
172            self.BRANCH, self.EMULATOR_BRANCH
173        ]
174        # TODO: Break out avd spec testing into its own testcase.
175        # Mock build client method
176        self.build_client.GetBuildInfo.side_effect = [
177            android_build_client.BuildInfo(
178                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
179            android_build_client.BuildInfo(
180                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
181                self.EMULATOR_BUILD_TARGET, None),
182            android_build_client.BuildInfo(
183                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
184                self.KERNEL_BUILD_TARGET, None)]
185
186        report = create_goldfish_action.CreateDevices(avd_spec=self.avd_spec)
187        # Verify
188        self.compute_client.CreateInstance.assert_called_with(
189            instance=self.INSTANCE,
190            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
191            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
192            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
193            build_target=self.BUILD_TARGET,
194            branch=self.BRANCH,
195            build_id=self.BUILD_ID,
196            emulator_branch=self.EMULATOR_BRANCH,
197            emulator_build_id=self.EMULATOR_BUILD_ID,
198            kernel_branch=self.KERNEL_BRANCH,
199            kernel_build_id=self.KERNEL_BUILD_ID,
200            kernel_build_target=self.KERNEL_BUILD_TARGET,
201            gpu=self.GPU,
202            avd_spec=self.avd_spec,
203            extra_scopes=self.EXTRA_SCOPES,
204            tags=None)
205
206    def testCreateDevicesWithoutBuildId(self):
207        """Test CreateDevices when emulator sysimage buildid is not provided."""
208        cfg = self._CreateCfg()
209
210        # Mock uuid
211        fake_uuid = mock.MagicMock(hex="1234")
212        self.Patch(uuid, "uuid4", return_value=fake_uuid)
213
214        # Mock compute client methods
215        self.compute_client.GetInstanceIP.return_value = self.IP
216        self.compute_client.GenerateImageName.return_value = self.IMAGE
217        self.compute_client.GenerateInstanceName.return_value = self.INSTANCE
218
219        # Mock build client method
220        self.build_client.GetBuildInfo.side_effect = [
221            android_build_client.BuildInfo(
222                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
223            android_build_client.BuildInfo(
224                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
225                self.EMULATOR_BUILD_TARGET, None),
226            android_build_client.BuildInfo(
227                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
228                self.KERNEL_BUILD_TARGET, None)]
229
230        # Mock _FetchBuildIdFromFile method
231        self.Patch(
232            create_goldfish_action,
233            "_FetchBuildIdFromFile",
234            return_value=self.BUILD_ID)
235
236        none_avd_spec = None
237        # Call CreateDevices with no avd_spec
238        report = create_goldfish_action.CreateDevices(
239            none_avd_spec,
240            cfg,
241            build_target=self.BUILD_TARGET,
242            build_id=None,
243            emulator_build_id=self.EMULATOR_BUILD_ID,
244            emulator_branch=None,
245            gpu=self.GPU,
246            branch=None,
247            kernel_branch=self.KERNEL_BRANCH,
248            kernel_build_id=self.KERNEL_BUILD_ID,
249            kernel_build_target=self.KERNEL_BUILD_TARGET)
250
251        # Verify
252        self.compute_client.CreateInstance.assert_called_with(
253            instance=self.INSTANCE,
254            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
255            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
256            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
257            build_target=self.BUILD_TARGET,
258            branch=self.BRANCH,
259            build_id=self.BUILD_ID,
260            emulator_branch=self.EMULATOR_BRANCH,
261            emulator_build_id=self.EMULATOR_BUILD_ID,
262            kernel_branch=self.KERNEL_BRANCH,
263            kernel_build_id=self.KERNEL_BUILD_ID,
264            kernel_build_target=self.KERNEL_BUILD_TARGET,
265            gpu=self.GPU,
266            avd_spec=none_avd_spec,
267            extra_scopes=self.EXTRA_SCOPES,
268            tags=None)
269
270        self.assertEqual(report.data, {
271            "devices": [{
272                "instance_name": self.INSTANCE,
273                "ip": self.IP.external,
274                "branch": self.BRANCH,
275                "build_id": self.BUILD_ID,
276                "build_target": self.BUILD_TARGET,
277                "emulator_branch": self.EMULATOR_BRANCH,
278                "emulator_build_id": self.EMULATOR_BUILD_ID,
279                "emulator_build_target": self.EMULATOR_BUILD_TARGET,
280                "kernel_branch": self.KERNEL_BRANCH,
281                "kernel_build_id": self.KERNEL_BUILD_ID,
282                "kernel_build_target": self.KERNEL_BUILD_TARGET,
283            },],
284        })
285        self.assertEqual(report.command, "create_gf")
286        self.assertEqual(report.status, "SUCCESS")
287
288        # Call CreateDevices with avd_spec
289        self.build_client.GetBranch.side_effect = [
290            self.BRANCH, self.EMULATOR_BRANCH
291        ]
292        # TODO: Break out avd spec testing into its own testcase.
293        # Mock build client method
294        self.build_client.GetBuildInfo.side_effect = [
295            android_build_client.BuildInfo(
296                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
297            android_build_client.BuildInfo(
298                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
299                self.EMULATOR_BUILD_TARGET, None),
300            android_build_client.BuildInfo(
301                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
302                self.KERNEL_BUILD_TARGET, None)]
303
304        report = create_goldfish_action.CreateDevices(avd_spec=self.avd_spec)
305        # Verify
306        self.compute_client.CreateInstance.assert_called_with(
307            instance=self.INSTANCE,
308            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
309            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
310            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
311            build_target=self.BUILD_TARGET,
312            branch=self.BRANCH,
313            build_id=self.BUILD_ID,
314            emulator_branch=self.EMULATOR_BRANCH,
315            emulator_build_id=self.EMULATOR_BUILD_ID,
316            kernel_branch=self.KERNEL_BRANCH,
317            kernel_build_id=self.KERNEL_BUILD_ID,
318            kernel_build_target=self.KERNEL_BUILD_TARGET,
319            gpu=self.GPU,
320            avd_spec=self.avd_spec,
321            extra_scopes=self.EXTRA_SCOPES,
322            tags=None)
323
324    #pylint: disable=invalid-name
325    def testCreateDevicesWithoutEmulatorBuildId(self):
326        """Test CreateDevices when emulator build id is not provided."""
327        cfg = self._CreateCfg()
328
329        # Mock uuid
330        fake_uuid = mock.MagicMock(hex="1234")
331        self.Patch(uuid, "uuid4", return_value=fake_uuid)
332
333        # Mock compute client methods
334        self.compute_client.GetInstanceIP.return_value = self.IP
335        self.compute_client.GenerateImageName.return_value = self.IMAGE
336        self.compute_client.GenerateInstanceName.return_value = self.INSTANCE
337
338        # Mock build client method
339        self.build_client.GetBuildInfo.side_effect = [
340            android_build_client.BuildInfo(
341                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
342            android_build_client.BuildInfo(
343                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
344                self.EMULATOR_BUILD_TARGET, None),
345            android_build_client.BuildInfo(
346                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
347                self.KERNEL_BUILD_TARGET, None)]
348
349        # Mock _FetchBuildIdFromFile method
350        self.Patch(
351            create_goldfish_action,
352            "_FetchBuildIdFromFile",
353            return_value=self.EMULATOR_BUILD_ID)
354
355        none_avd_spec = None
356        # Call CreateDevices
357        report = create_goldfish_action.CreateDevices(
358            none_avd_spec, cfg, build_target=self.BUILD_TARGET,
359            build_id=self.BUILD_ID, emulator_build_id=None,
360            gpu=self.GPU)
361
362        # Verify
363        self.compute_client.CreateInstance.assert_called_with(
364            instance=self.INSTANCE,
365            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
366            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
367            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
368            build_target=self.BUILD_TARGET,
369            branch=self.BRANCH,
370            build_id=self.BUILD_ID,
371            emulator_branch=self.EMULATOR_BRANCH,
372            emulator_build_id=self.EMULATOR_BUILD_ID,
373            kernel_branch=self.KERNEL_BRANCH,
374            kernel_build_id=self.KERNEL_BUILD_ID,
375            kernel_build_target=self.KERNEL_BUILD_TARGET,
376            gpu=self.GPU,
377            avd_spec=none_avd_spec,
378            extra_scopes=self.EXTRA_SCOPES,
379            tags=None)
380
381        self.assertEqual(report.data, {
382            "devices": [{
383                "instance_name": self.INSTANCE,
384                "ip": self.IP.external,
385                "branch": self.BRANCH,
386                "build_id": self.BUILD_ID,
387                "build_target": self.BUILD_TARGET,
388                "emulator_branch": self.EMULATOR_BRANCH,
389                "emulator_build_id": self.EMULATOR_BUILD_ID,
390                "emulator_build_target": self.EMULATOR_BUILD_TARGET,
391                "kernel_branch": self.KERNEL_BRANCH,
392                "kernel_build_id": self.KERNEL_BUILD_ID,
393                "kernel_build_target": self.KERNEL_BUILD_TARGET,
394            },],
395        })
396        self.assertEqual(report.command, "create_gf")
397        self.assertEqual(report.status, "SUCCESS")
398
399        # Call CreateDevices with avd_spec
400        self.build_client.GetBranch.side_effect = [
401            self.BRANCH, self.EMULATOR_BRANCH
402        ]
403        # TODO: Break out avd spec testing into its own testcase.
404        # Mock build client method
405        self.build_client.GetBuildInfo.side_effect = [
406            android_build_client.BuildInfo(
407                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
408            android_build_client.BuildInfo(
409                self.EMULATOR_BRANCH, self.EMULATOR_BUILD_ID,
410                self.EMULATOR_BUILD_TARGET, None),
411            android_build_client.BuildInfo(
412                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
413                self.KERNEL_BUILD_TARGET, None)]
414
415        report = create_goldfish_action.CreateDevices(avd_spec=self.avd_spec)
416        # Verify
417        self.compute_client.CreateInstance.assert_called_with(
418            instance=self.INSTANCE,
419            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
420            image_name=self.GOLDFISH_HOST_IMAGE_NAME,
421            image_project=self.GOLDFISH_HOST_IMAGE_PROJECT,
422            build_target=self.BUILD_TARGET,
423            branch=self.BRANCH,
424            build_id=self.BUILD_ID,
425            emulator_branch=self.EMULATOR_BRANCH,
426            emulator_build_id=self.EMULATOR_BUILD_ID,
427            kernel_branch=self.KERNEL_BRANCH,
428            kernel_build_id=self.KERNEL_BUILD_ID,
429            kernel_build_target=self.KERNEL_BUILD_TARGET,
430            gpu=self.GPU,
431            avd_spec=self.avd_spec,
432            extra_scopes=self.EXTRA_SCOPES,
433            tags=None)
434
435
436if __name__ == "__main__":
437    unittest.main()
438