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