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