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
17"""Tests for create_cuttlefish_action.
18
19Tests for acloud.public.actions.create_cuttlefish_action.
20"""
21
22import uuid
23import unittest
24import mock
25
26from acloud.internal.lib import android_build_client
27from acloud.internal.lib import android_compute_client
28from acloud.internal.lib import auth
29from acloud.internal.lib import cvd_compute_client
30from acloud.internal.lib import cvd_compute_client_multi_stage
31from acloud.internal.lib import driver_test_lib
32from acloud.internal.lib import ssh
33from acloud.public.actions import create_cuttlefish_action
34
35
36class CreateCuttlefishActionTest(driver_test_lib.BaseDriverTest):
37    """Test create_cuttlefish_action."""
38
39    IP = ssh.IP(external="127.0.0.1", internal="10.0.0.1")
40    INSTANCE = "fake-instance"
41    IMAGE = "fake-image"
42    BUILD_TARGET = "fake-build-target"
43    BUILD_ID = "12345"
44    KERNEL_BRANCH = "fake-kernel-branch"
45    KERNEL_BUILD_ID = "54321"
46    KERNEL_BUILD_TARGET = "kernel"
47    BRANCH = "fake-branch"
48    SYSTEM_BRANCH = "fake-system-branch"
49    SYSTEM_BUILD_ID = "23456"
50    SYSTEM_BUILD_TARGET = "fake-system-build-target"
51    STABLE_HOST_IMAGE_NAME = "fake-stable-host-image-name"
52    STABLE_HOST_IMAGE_PROJECT = "fake-stable-host-image-project"
53    EXTRA_DATA_DISK_GB = 4
54    EXTRA_SCOPES = ["scope1", "scope2"]
55
56    def setUp(self):
57        """Set up the test."""
58        super(CreateCuttlefishActionTest, self).setUp()
59        self.build_client = mock.MagicMock()
60        self.Patch(
61            android_build_client,
62            "AndroidBuildClient",
63            return_value=self.build_client)
64        self.compute_client = mock.MagicMock()
65        self.Patch(
66            cvd_compute_client,
67            "CvdComputeClient",
68            return_value=self.compute_client)
69        self.Patch(
70            cvd_compute_client_multi_stage,
71            "CvdComputeClient",
72            return_value=self.compute_client)
73        self.Patch(
74            android_compute_client,
75            "AndroidComputeClient",
76            return_value=self.compute_client)
77        self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
78
79    def _CreateCfg(self):
80        """A helper method that creates a mock configuration object."""
81        cfg = mock.MagicMock()
82        cfg.service_account_name = "fake@service.com"
83        cfg.service_account_private_key_path = "/fake/path/to/key"
84        cfg.zone = "fake_zone"
85        cfg.disk_image_name = "fake_image.tar.gz"
86        cfg.disk_image_mime_type = "fake/type"
87        cfg.ssh_private_key_path = ""
88        cfg.ssh_public_key_path = ""
89        cfg.stable_host_image_name = self.STABLE_HOST_IMAGE_NAME
90        cfg.stable_host_image_project = self.STABLE_HOST_IMAGE_PROJECT
91        cfg.extra_data_disk_size_gb = self.EXTRA_DATA_DISK_GB
92        cfg.kernel_build_target = self.KERNEL_BUILD_TARGET
93        cfg.extra_scopes = self.EXTRA_SCOPES
94        cfg.enable_multi_stage = False
95        return cfg
96
97    def testCreateDevices(self):
98        """Test CreateDevices."""
99        cfg = self._CreateCfg()
100
101        # Mock uuid
102        fake_uuid = mock.MagicMock(hex="1234")
103        self.Patch(uuid, "uuid4", return_value=fake_uuid)
104
105        # Mock compute client methods
106        self.compute_client.GetInstanceIP.return_value = self.IP
107        self.compute_client.GenerateImageName.return_value = self.IMAGE
108        self.compute_client.GenerateInstanceName.return_value = self.INSTANCE
109
110        # Mock build client method
111        self.build_client.GetBuildInfo.side_effect = [
112            android_build_client.BuildInfo(
113                self.BRANCH, self.BUILD_ID, self.BUILD_TARGET, None),
114            android_build_client.BuildInfo(
115                self.KERNEL_BRANCH, self.KERNEL_BUILD_ID,
116                self.KERNEL_BUILD_TARGET, None),
117            android_build_client.BuildInfo(
118                self.SYSTEM_BRANCH, self.SYSTEM_BUILD_ID,
119                self.SYSTEM_BUILD_TARGET, None)]
120
121        # Call CreateDevices
122        report = create_cuttlefish_action.CreateDevices(
123            cfg, self.BUILD_TARGET, self.BUILD_ID, branch=self.BRANCH,
124            kernel_build_id=self.KERNEL_BUILD_ID,
125            system_build_target=self.SYSTEM_BUILD_TARGET,
126            system_branch=self.SYSTEM_BRANCH,
127            system_build_id=self.SYSTEM_BUILD_ID)
128
129        # Verify
130        self.compute_client.CreateInstance.assert_called_with(
131            instance=self.INSTANCE,
132            image_name=self.STABLE_HOST_IMAGE_NAME,
133            image_project=self.STABLE_HOST_IMAGE_PROJECT,
134            build_target=self.BUILD_TARGET,
135            branch=self.BRANCH,
136            build_id=self.BUILD_ID,
137            kernel_branch=self.KERNEL_BRANCH,
138            kernel_build_id=self.KERNEL_BUILD_ID,
139            kernel_build_target=self.KERNEL_BUILD_TARGET,
140            system_branch=self.SYSTEM_BRANCH,
141            system_build_id=self.SYSTEM_BUILD_ID,
142            system_build_target=self.SYSTEM_BUILD_TARGET,
143            blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB,
144            extra_scopes=self.EXTRA_SCOPES)
145
146        self.assertEqual(report.data, {
147            "devices": [
148                {
149                    "branch": self.BRANCH,
150                    "build_id": self.BUILD_ID,
151                    "build_target": self.BUILD_TARGET,
152                    "kernel_branch": self.KERNEL_BRANCH,
153                    "kernel_build_id": self.KERNEL_BUILD_ID,
154                    "kernel_build_target": self.KERNEL_BUILD_TARGET,
155                    "system_branch": self.SYSTEM_BRANCH,
156                    "system_build_id": self.SYSTEM_BUILD_ID,
157                    "system_build_target": self.SYSTEM_BUILD_TARGET,
158                    "instance_name": self.INSTANCE,
159                    "ip": self.IP.external,
160                },
161            ],
162        })
163        self.assertEqual(report.command, "create_cf")
164        self.assertEqual(report.status, "SUCCESS")
165
166
167if __name__ == "__main__":
168    unittest.main()
169