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 driver_test_lib 31from acloud.internal.lib import gcompute_client 32from acloud.public.actions import create_cuttlefish_action 33 34 35class CreateCuttlefishActionTest(driver_test_lib.BaseDriverTest): 36 """Test create_cuttlefish_action.""" 37 38 IP = gcompute_client.IP(external="127.0.0.1", internal="10.0.0.1") 39 INSTANCE = "fake-instance" 40 IMAGE = "fake-image" 41 BUILD_TARGET = "fake-build-target" 42 BUILD_ID = "12345" 43 KERNEL_BRANCH = "fake-kernel-branch" 44 KERNEL_BUILD_ID = "54321" 45 KERNEL_BUILD_TARGET = "kernel" 46 BRANCH = "fake-branch" 47 STABLE_HOST_IMAGE_NAME = "fake-stable-host-image-name" 48 STABLE_HOST_IMAGE_PROJECT = "fake-stable-host-image-project" 49 EXTRA_DATA_DISK_GB = 4 50 EXTRA_SCOPES = ["scope1", "scope2"] 51 52 def setUp(self): 53 """Set up the test.""" 54 super(CreateCuttlefishActionTest, 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 cvd_compute_client, 63 "CvdComputeClient", 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 71 def _CreateCfg(self): 72 """A helper method that creates a mock configuration object.""" 73 cfg = mock.MagicMock() 74 cfg.service_account_name = "fake@service.com" 75 cfg.service_account_private_key_path = "/fake/path/to/key" 76 cfg.zone = "fake_zone" 77 cfg.disk_image_name = "fake_image.tar.gz" 78 cfg.disk_image_mime_type = "fake/type" 79 cfg.ssh_private_key_path = "" 80 cfg.ssh_public_key_path = "" 81 cfg.stable_host_image_name = self.STABLE_HOST_IMAGE_NAME 82 cfg.stable_host_image_project = self.STABLE_HOST_IMAGE_PROJECT 83 cfg.extra_data_disk_size_gb = self.EXTRA_DATA_DISK_GB 84 cfg.kernel_build_target = self.KERNEL_BUILD_TARGET 85 cfg.extra_scopes = self.EXTRA_SCOPES 86 return cfg 87 88 def testCreateDevices(self): 89 """Test CreateDevices.""" 90 cfg = self._CreateCfg() 91 92 # Mock uuid 93 fake_uuid = mock.MagicMock(hex="1234") 94 self.Patch(uuid, "uuid4", return_value=fake_uuid) 95 96 # Mock compute client methods 97 self.compute_client.GetInstanceIP.return_value = self.IP 98 self.compute_client.GenerateImageName.return_value = self.IMAGE 99 self.compute_client.GenerateInstanceName.return_value = self.INSTANCE 100 101 # Mock build client method 102 self.build_client.GetBranch.side_effect = [self.BRANCH, 103 self.KERNEL_BRANCH] 104 105 # Setup avd_spec as None to use cfg to create devices 106 none_avd_spec = None 107 108 # Call CreateDevices 109 report = create_cuttlefish_action.CreateDevices( 110 none_avd_spec, cfg, self.BUILD_TARGET, self.BUILD_ID, self.KERNEL_BUILD_ID) 111 112 # Verify 113 self.compute_client.CreateInstance.assert_called_with( 114 instance=self.INSTANCE, 115 image_name=self.STABLE_HOST_IMAGE_NAME, 116 image_project=self.STABLE_HOST_IMAGE_PROJECT, 117 build_target=self.BUILD_TARGET, 118 branch=self.BRANCH, 119 build_id=self.BUILD_ID, 120 kernel_branch=self.KERNEL_BRANCH, 121 kernel_build_id=self.KERNEL_BUILD_ID, 122 blank_data_disk_size_gb=self.EXTRA_DATA_DISK_GB, 123 avd_spec=none_avd_spec, 124 extra_scopes=self.EXTRA_SCOPES) 125 126 self.assertEquals(report.data, { 127 "devices": [ 128 { 129 "branch": self.BRANCH, 130 "build_id": self.BUILD_ID, 131 "build_target": self.BUILD_TARGET, 132 "kernel_branch": self.KERNEL_BRANCH, 133 "kernel_build_id": self.KERNEL_BUILD_ID, 134 "kernel_build_target": self.KERNEL_BUILD_TARGET, 135 "instance_name": self.INSTANCE, 136 "ip": self.IP.external, 137 }, 138 ], 139 }) 140 self.assertEquals(report.command, "create_cf") 141 self.assertEquals(report.status, "SUCCESS") 142 143 144if __name__ == "__main__": 145 unittest.main() 146