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 hw_property=None, 34 stable_cheeps_host_image_name=None, 35 stable_cheeps_host_image_project=None, 36 username=None, 37 password=None, 38 cheeps_betty_image=None, 39 cheeps_features=[], 40 local_image=None, 41 local_kernel_image=None, 42 local_system_image=None, 43 local_instance_dir=None, 44 local_vendor_boot_image=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 remote_image_dir=None, 58 host_user=constants.GCE_USER, 59 host_ssh_private_key_path=None, 60 emulator_build_id=None, 61 emulator_build_target=None, 62 emulator_zip=None, 63 avd_type=constants.TYPE_CF, 64 autoconnect=constants.INS_KEY_WEBRTC) 65 return mock_args 66 67 68# pylint: disable=invalid-name,protected-access 69class CreateArgsTest(driver_test_lib.BaseDriverTest): 70 """Test create_args functions.""" 71 72 def testVerifyArgs(self): 73 """test VerifyArgs.""" 74 mock_args = _CreateArgs() 75 # Test args default setting shouldn't raise error. 76 self.assertEqual(None, create_args.VerifyArgs(mock_args)) 77 78 def testVerifyArgs_Goldfish(self): 79 """test goldfish arguments.""" 80 # emulator_build_id with wrong avd_type. 81 mock_args = _CreateArgs() 82 mock_args.emulator_build_id = "123456" 83 self.assertRaises(errors.UnsupportedCreateArgs, 84 create_args.VerifyArgs, mock_args) 85 # Valid emulator_build_id. 86 mock_args.avd_type = constants.TYPE_GF 87 create_args.VerifyArgs(mock_args) 88 # emulator_build_target with wrong avd_type. 89 mock_args.avd_type = constants.TYPE_CF 90 mock_args.emulator_build_id = None 91 mock_args.emulator_build_target = "emulator-linux_x64_nolocationui" 92 mock_args.remote_host = "192.0.2.2" 93 self.assertRaises(errors.UnsupportedCreateArgs, 94 create_args.VerifyArgs, mock_args) 95 mock_args.emulator_build_target = None 96 # Incomplete system build info. 97 mock_args.avd_type = constants.TYPE_GF 98 mock_args.system_build_target = "aosp_x86_64-userdebug" 99 mock_args.remote_host = "192.0.2.2" 100 self.assertRaises(errors.UnsupportedCreateArgs, 101 create_args.VerifyArgs, mock_args) 102 mock_args.system_build_target = None 103 # Incomplete boot build info. 104 mock_args.avd_type = constants.TYPE_GF 105 mock_args.boot_build_target = "gki_x86_64-userdebug" 106 mock_args.remote_host = "192.0.2.2" 107 self.assertRaises(errors.UnsupportedCreateArgs, 108 create_args.VerifyArgs, mock_args) 109 mock_args.boot_build_target = None 110 # System build info without remote_host. 111 mock_args.system_branch = "aosp-master" 112 mock_args.system_build_target = "aosp_x86_64-userdebug" 113 mock_args.system_build_id = "123456" 114 mock_args.remote_host = None 115 self.assertRaises(errors.UnsupportedCreateArgs, 116 create_args.VerifyArgs, mock_args) 117 # Valid build info. 118 mock_args.emulator_build_target = "emulator-linux_x64_nolocationui" 119 mock_args.system_branch = "aosp-master" 120 mock_args.system_build_target = "aosp_x86_64-userdebug" 121 mock_args.system_build_id = "123456" 122 mock_args.boot_branch = "aosp-master" 123 mock_args.boot_build_target = "aosp_x86_64-userdebug" 124 mock_args.boot_build_id = "123456" 125 mock_args.boot_artifact = "boot-5.10.img" 126 mock_args.remote_host = "192.0.2.2" 127 create_args.VerifyArgs(mock_args) 128 129 def testVerifyArgs_ConnectWebRTC(self): 130 """test VerifyArgs args.autconnect webrtc. 131 132 WebRTC only apply to remote cuttlefish instance 133 134 """ 135 mock_args = _CreateArgs() 136 mock_args.autoconnect = constants.INS_KEY_WEBRTC 137 # Test remote instance and avd_type cuttlefish(default) 138 # Test args.autoconnect webrtc shouldn't raise error. 139 self.assertEqual(None, create_args.VerifyArgs(mock_args)) 140 141 def testVerifyLocalArgs(self): 142 """Test _VerifyLocalArgs.""" 143 mock_args = _CreateArgs() 144 # verify local image case. 145 mock_args.local_image = "/tmp/local_image_dir" 146 self.Patch(os.path, "exists", return_value=False) 147 self.assertRaises(errors.CheckPathError, 148 create_args._VerifyLocalArgs, mock_args) 149 150 # verify local instance 151 mock_args = _CreateArgs() 152 mock_args.local_instance_dir = "/tmp/local_instance_dir" 153 self.Patch(os.path, "exists", return_value=False) 154 self.assertRaises(errors.CheckPathError, 155 create_args._VerifyLocalArgs, mock_args) 156 157 # verify local system image 158 mock_args = _CreateArgs() 159 mock_args.local_system_image = "/tmp/local_system_image_dir" 160 mock_args.avd_type = "cheeps" 161 self.assertRaises(errors.UnsupportedCreateArgs, 162 create_args._VerifyLocalArgs, mock_args) 163 mock_args.avd_type = "cuttlefish" 164 self.Patch(os.path, "exists", return_value=False) 165 self.assertRaises(errors.CheckPathError, 166 create_args._VerifyLocalArgs, mock_args) 167 168 # unsupport local-image with kernel build 169 mock_args = _CreateArgs() 170 mock_args.local_instance = None 171 mock_args.local_image = "/tmp/local_image_dir" 172 self.Patch(os.path, "exists", return_value=True) 173 mock_args.kernel_branch = "common-android12-5.4" 174 self.assertRaises(errors.UnsupportedCreateArgs, 175 create_args._VerifyLocalArgs, mock_args) 176 mock_args.kernel_branch = None 177 mock_args.kernel_build_id = "fake_kernel_1234567" 178 self.assertRaises(errors.UnsupportedCreateArgs, 179 create_args._VerifyLocalArgs, mock_args) 180 181 182if __name__ == "__main__": 183 unittest.main() 184