1# Copyright 2020 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for create.""" 15import os 16 17import unittest 18 19from unittest import mock 20 21from acloud import errors 22from acloud.create import create_args 23from acloud.internal import constants 24from acloud.internal.lib import driver_test_lib 25 26 27def _CreateArgs(): 28 """set default pass in arguments.""" 29 mock_args = mock.MagicMock( 30 flavor=None, 31 num=1, 32 adb_port=None, 33 fastboot_port=None, 34 hw_property=None, 35 stable_cheeps_host_image_name=None, 36 stable_cheeps_host_image_project=None, 37 username=None, 38 password=None, 39 cheeps_betty_image=None, 40 cheeps_features=[], 41 local_image=None, 42 local_kernel_image=None, 43 local_system_image=None, 44 local_instance_dir=None, 45 kernel_branch=None, 46 kernel_build_id=None, 47 kernel_build_target="kernel", 48 boot_branch=None, 49 boot_build_id=None, 50 boot_build_target=None, 51 boot_artifact=None, 52 system_branch=None, 53 system_build_id=None, 54 system_build_target=None, 55 local_instance=None, 56 remote_host=None, 57 host_user=constants.GCE_USER, 58 host_ssh_private_key_path=None, 59 emulator_build_id=None, 60 emulator_build_target=None, 61 emulator_zip=None, 62 avd_type=constants.TYPE_CF, 63 autoconnect=constants.INS_KEY_WEBRTC) 64 return mock_args 65 66 67# pylint: disable=invalid-name,protected-access 68class CreateArgsTest(driver_test_lib.BaseDriverTest): 69 """Test create_args functions.""" 70 71 def testVerifyArgs(self): 72 """test VerifyArgs.""" 73 mock_args = _CreateArgs() 74 # Test args default setting shouldn't raise error. 75 self.assertEqual(None, create_args.VerifyArgs(mock_args)) 76 77 def testVerifyArgs_Goldfish(self): 78 """test goldfish arguments.""" 79 # emulator_build_id with wrong avd_type. 80 mock_args = _CreateArgs() 81 mock_args.emulator_build_id = "123456" 82 self.assertRaises(errors.UnsupportedCreateArgs, 83 create_args.VerifyArgs, mock_args) 84 # Valid emulator_build_id. 85 mock_args.avd_type = constants.TYPE_GF 86 create_args.VerifyArgs(mock_args) 87 # emulator_build_target with wrong avd_type. 88 mock_args.avd_type = constants.TYPE_CF 89 mock_args.emulator_build_id = None 90 mock_args.emulator_build_target = "emulator-linux_x64_nolocationui" 91 mock_args.remote_host = "192.0.2.2" 92 self.assertRaises(errors.UnsupportedCreateArgs, 93 create_args.VerifyArgs, mock_args) 94 mock_args.emulator_build_target = None 95 # Incomplete system build info. 96 mock_args.avd_type = constants.TYPE_GF 97 mock_args.system_build_target = "aosp_x86_64-userdebug" 98 mock_args.remote_host = "192.0.2.2" 99 self.assertRaises(errors.UnsupportedCreateArgs, 100 create_args.VerifyArgs, mock_args) 101 mock_args.system_build_target = None 102 # Incomplete boot build info. 103 mock_args.avd_type = constants.TYPE_GF 104 mock_args.boot_build_target = "gki_x86_64-userdebug" 105 mock_args.remote_host = "192.0.2.2" 106 self.assertRaises(errors.UnsupportedCreateArgs, 107 create_args.VerifyArgs, mock_args) 108 mock_args.boot_build_target = None 109 # System build info without remote_host. 110 mock_args.system_branch = "aosp-master" 111 mock_args.system_build_target = "aosp_x86_64-userdebug" 112 mock_args.system_build_id = "123456" 113 mock_args.remote_host = None 114 self.assertRaises(errors.UnsupportedCreateArgs, 115 create_args.VerifyArgs, mock_args) 116 # Valid build info. 117 mock_args.emulator_build_target = "emulator-linux_x64_nolocationui" 118 mock_args.system_branch = "aosp-master" 119 mock_args.system_build_target = "aosp_x86_64-userdebug" 120 mock_args.system_build_id = "123456" 121 mock_args.boot_branch = "aosp-master" 122 mock_args.boot_build_target = "aosp_x86_64-userdebug" 123 mock_args.boot_build_id = "123456" 124 mock_args.boot_artifact = "boot-5.10.img" 125 mock_args.remote_host = "192.0.2.2" 126 create_args.VerifyArgs(mock_args) 127 128 def testVerifyArgs_ConnectWebRTC(self): 129 """test VerifyArgs args.autconnect webrtc. 130 131 WebRTC only apply to remote cuttlefish instance 132 133 """ 134 mock_args = _CreateArgs() 135 mock_args.autoconnect = constants.INS_KEY_WEBRTC 136 # Test remote instance and avd_type cuttlefish(default) 137 # Test args.autoconnect webrtc shouldn't raise error. 138 self.assertEqual(None, create_args.VerifyArgs(mock_args)) 139 140 def testVerifyLocalArgs(self): 141 """Test _VerifyLocalArgs.""" 142 mock_args = _CreateArgs() 143 # verify local image case. 144 mock_args.local_image = "/tmp/local_image_dir" 145 self.Patch(os.path, "exists", return_value=False) 146 self.assertRaises(errors.CheckPathError, 147 create_args._VerifyLocalArgs, mock_args) 148 149 # verify local instance 150 mock_args = _CreateArgs() 151 mock_args.local_instance_dir = "/tmp/local_instance_dir" 152 self.Patch(os.path, "exists", return_value=False) 153 self.assertRaises(errors.CheckPathError, 154 create_args._VerifyLocalArgs, mock_args) 155 156 # verify local system image 157 mock_args = _CreateArgs() 158 mock_args.local_system_image = "/tmp/local_system_image_dir" 159 mock_args.avd_type = "cheeps" 160 self.assertRaises(errors.UnsupportedCreateArgs, 161 create_args._VerifyLocalArgs, mock_args) 162 mock_args.avd_type = "cuttlefish" 163 self.Patch(os.path, "exists", return_value=False) 164 self.assertRaises(errors.CheckPathError, 165 create_args._VerifyLocalArgs, mock_args) 166 167 # unsupport local-image with kernel build 168 mock_args = _CreateArgs() 169 mock_args.local_instance = None 170 mock_args.local_image = "/tmp/local_image_dir" 171 self.Patch(os.path, "exists", return_value=True) 172 mock_args.kernel_branch = "common-android12-5.4" 173 self.assertRaises(errors.UnsupportedCreateArgs, 174 create_args._VerifyLocalArgs, mock_args) 175 mock_args.kernel_branch = None 176 mock_args.kernel_build_id = "fake_kernel_1234567" 177 self.assertRaises(errors.UnsupportedCreateArgs, 178 create_args._VerifyLocalArgs, mock_args) 179 180 181if __name__ == "__main__": 182 unittest.main() 183