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