1# Copyright 2019 - 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.""" 15 16import os 17import subprocess 18import unittest 19 20from unittest import mock 21 22from acloud import errors 23from acloud.create import avd_spec 24from acloud.create import create 25from acloud.create import gce_local_image_remote_instance 26from acloud.internal import constants 27from acloud.internal.lib import driver_test_lib 28from acloud.internal.lib import utils 29from acloud.public import config 30from acloud.setup import gcp_setup_runner 31from acloud.setup import host_setup_runner 32from acloud.setup import setup 33 34 35# pylint: disable=invalid-name,protected-access 36class CreateTest(driver_test_lib.BaseDriverTest): 37 """Test create functions.""" 38 39 def testGetAvdCreatorClass(self): 40 """Test GetAvdCreatorClass.""" 41 # Checking wrong avd arg. 42 avd_type = "unknown type" 43 ins_type = "unknown ins" 44 image_source = "unknown image" 45 with self.assertRaises(errors.UnsupportedInstanceImageType): 46 create.GetAvdCreatorClass(avd_type, ins_type, image_source) 47 48 # Checking right avd arg. 49 avd_creator_class = create.GetAvdCreatorClass( 50 constants.TYPE_GCE, 51 constants.INSTANCE_TYPE_REMOTE, 52 constants.IMAGE_SRC_LOCAL) 53 self.assertEqual(avd_creator_class, 54 gce_local_image_remote_instance.GceLocalImageRemoteInstance) 55 56 # pylint: disable=protected-access 57 def testCheckForAutoconnect(self): 58 """Test CheckForAutoconnect.""" 59 args = mock.MagicMock() 60 args.autoconnect = True 61 args.no_prompt = False 62 63 self.Patch(utils, "InteractWithQuestion", return_value="Y") 64 self.Patch(utils, "FindExecutable", return_value=None) 65 66 # Checking autoconnect should be false if ANDROID_BUILD_TOP is not set. 67 self.Patch(os.environ, "get", return_value=None) 68 create._CheckForAutoconnect(args) 69 self.assertEqual(args.autoconnect, False) 70 71 # checking autoconnect should be True after user make adb from src. 72 args.autoconnect = True 73 self.Patch(subprocess, "check_call", return_value=True) 74 self.Patch(os.environ, "get", return_value="/fake_dir2") 75 create._CheckForAutoconnect(args) 76 self.assertEqual(args.autoconnect, True) 77 78 # checking autoconnect should be False if adb is not built. 79 self.Patch(utils, "InteractWithQuestion", return_value="N") 80 create._CheckForAutoconnect(args) 81 self.assertEqual(args.autoconnect, False) 82 83 # pylint: disable=protected-access,no-member 84 def testCheckForSetup(self): 85 """Test _CheckForSetup.""" 86 args = mock.MagicMock() 87 args.local_instance = None 88 args.args.config_file = "fake_path" 89 self.Patch(gcp_setup_runner.GcpTaskRunner, 90 "ShouldRun", 91 return_value=False) 92 self.Patch(host_setup_runner.HostBasePkgInstaller, 93 "ShouldRun", 94 return_value=False) 95 self.Patch(config, "AcloudConfigManager") 96 self.Patch(config.AcloudConfigManager, "Load") 97 self.Patch(setup, "Run") 98 self.Patch(utils, "InteractWithQuestion", return_value="Y") 99 100 # Checking Setup.Run should not be called if all runner's ShouldRun func 101 # return False 102 create._CheckForSetup(args) 103 gcp_setup_runner.GcpTaskRunner.ShouldRun.assert_called_once() 104 host_setup_runner.HostBasePkgInstaller.ShouldRun.assert_called_once() 105 setup.Run.assert_not_called() 106 107 # Checking Setup.Run should be called if runner's ShouldRun func return 108 # True 109 self.Patch(gcp_setup_runner.GcpTaskRunner, 110 "ShouldRun", 111 return_value=True) 112 create._CheckForSetup(args) 113 setup.Run.assert_called_once() 114 115 # Should or not run gcp_setup or install packages. 116 # Test with remote instance remote image case. 117 self.Patch(gcp_setup_runner.GcpTaskRunner, 118 "ShouldRun") 119 self.Patch(host_setup_runner.AvdPkgInstaller, 120 "ShouldRun") 121 args.local_instance = None 122 args.local_image = None 123 create._CheckForSetup(args) 124 self.assertEqual(gcp_setup_runner.GcpTaskRunner.ShouldRun.call_count, 1) 125 self.assertEqual(host_setup_runner.AvdPkgInstaller.ShouldRun.call_count, 0) 126 gcp_setup_runner.GcpTaskRunner.ShouldRun.reset_mock() 127 host_setup_runner.AvdPkgInstaller.ShouldRun.reset_mock() 128 129 # Test with remote instance local image case. 130 args.local_instance = None 131 args.local_image = "" 132 create._CheckForSetup(args) 133 self.assertEqual(gcp_setup_runner.GcpTaskRunner.ShouldRun.call_count, 1) 134 self.assertEqual(host_setup_runner.AvdPkgInstaller.ShouldRun.call_count, 0) 135 gcp_setup_runner.GcpTaskRunner.ShouldRun.reset_mock() 136 host_setup_runner.AvdPkgInstaller.ShouldRun.reset_mock() 137 138 # Test with local instance remote image case. 139 args.local_instance = 0 140 args.local_image = None 141 create._CheckForSetup(args) 142 self.assertEqual(gcp_setup_runner.GcpTaskRunner.ShouldRun.call_count, 1) 143 self.assertEqual(host_setup_runner.AvdPkgInstaller.ShouldRun.call_count, 1) 144 gcp_setup_runner.GcpTaskRunner.ShouldRun.reset_mock() 145 host_setup_runner.AvdPkgInstaller.ShouldRun.reset_mock() 146 147 # Test with local instance local image case. 148 args.local_instance = 0 149 args.local_image = "" 150 create._CheckForSetup(args) 151 self.assertEqual(gcp_setup_runner.GcpTaskRunner.ShouldRun.call_count, 0) 152 self.assertEqual(host_setup_runner.AvdPkgInstaller.ShouldRun.call_count, 1) 153 gcp_setup_runner.GcpTaskRunner.ShouldRun.reset_mock() 154 host_setup_runner.AvdPkgInstaller.ShouldRun.reset_mock() 155 156 # pylint: disable=no-member 157 def testRun(self): 158 """Test Run.""" 159 args = mock.MagicMock() 160 spec = mock.MagicMock() 161 spec.avd_type = constants.TYPE_GCE 162 spec.instance_type = constants.INSTANCE_TYPE_REMOTE 163 spec.image_source = constants.IMAGE_SRC_LOCAL 164 self.Patch(avd_spec, "AVDSpec", return_value=spec) 165 self.Patch(config, "GetAcloudConfig") 166 self.Patch(create, "PreRunCheck") 167 self.Patch(gce_local_image_remote_instance.GceLocalImageRemoteInstance, 168 "Create") 169 170 # Checking PreRunCheck func should be called if not skip_pre_run_check 171 args.skip_pre_run_check = False 172 create.Run(args) 173 create.PreRunCheck.assert_called_once() 174 175 # Checking PreRunCheck func should not be called if skip_pre_run_check 176 args.skip_pre_run_check = True 177 self.Patch(create, "PreRunCheck") 178 create.Run(args) 179 create.PreRunCheck.assert_not_called() 180 181 182if __name__ == "__main__": 183 unittest.main() 184