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
61        self.Patch(utils, "InteractWithQuestion", return_value="Y")
62        self.Patch(utils, "FindExecutable", return_value=None)
63
64        # Checking autoconnect should be false if ANDROID_BUILD_TOP is not set.
65        self.Patch(os.environ, "get", return_value=None)
66        create._CheckForAutoconnect(args)
67        self.assertEqual(args.autoconnect, False)
68
69        # checking autoconnect should be True after user make adb from src.
70        args.autoconnect = True
71        self.Patch(subprocess, "check_call", return_value=True)
72        self.Patch(os.environ, "get", return_value="/fake_dir2")
73        create._CheckForAutoconnect(args)
74        self.assertEqual(args.autoconnect, True)
75
76        # checking autoconnect should be False if adb is not built.
77        self.Patch(utils, "InteractWithQuestion", return_value="N")
78        create._CheckForAutoconnect(args)
79        self.assertEqual(args.autoconnect, False)
80
81    # pylint: disable=protected-access,no-member
82    def testCheckForSetup(self):
83        """Test _CheckForSetup."""
84        args = mock.MagicMock()
85        args.local_instance = None
86        args.args.config_file = "fake_path"
87        self.Patch(gcp_setup_runner.GcpTaskRunner,
88                   "ShouldRun",
89                   return_value=False)
90        self.Patch(host_setup_runner.HostBasePkgInstaller,
91                   "ShouldRun",
92                   return_value=False)
93        self.Patch(config, "AcloudConfigManager")
94        self.Patch(config.AcloudConfigManager, "Load")
95        self.Patch(setup, "Run")
96        self.Patch(utils, "InteractWithQuestion", return_value="Y")
97
98        # Checking Setup.Run should not be called if all runner's ShouldRun func
99        # return False
100        create._CheckForSetup(args)
101        gcp_setup_runner.GcpTaskRunner.ShouldRun.assert_called_once()
102        host_setup_runner.HostBasePkgInstaller.ShouldRun.assert_called_once()
103        setup.Run.assert_not_called()
104
105        # Checking Setup.Run should be called if runner's ShouldRun func return
106        # True
107        self.Patch(gcp_setup_runner.GcpTaskRunner,
108                   "ShouldRun",
109                   return_value=True)
110        create._CheckForSetup(args)
111        setup.Run.assert_called_once()
112
113    # pylint: disable=no-member
114    def testRun(self):
115        """Test Run."""
116        args = mock.MagicMock()
117        spec = mock.MagicMock()
118        spec.avd_type = constants.TYPE_GCE
119        spec.instance_type = constants.INSTANCE_TYPE_REMOTE
120        spec.image_source = constants.IMAGE_SRC_LOCAL
121        self.Patch(avd_spec, "AVDSpec", return_value=spec)
122        self.Patch(config, "GetAcloudConfig")
123        self.Patch(create, "PreRunCheck")
124        self.Patch(gce_local_image_remote_instance.GceLocalImageRemoteInstance,
125                   "Create")
126
127        # Checking PreRunCheck func should be called if not skip_pre_run_check
128        args.skip_pre_run_check = False
129        create.Run(args)
130        create.PreRunCheck.assert_called_once()
131
132        # Checking PreRunCheck func should not be called if skip_pre_run_check
133        args.skip_pre_run_check = True
134        self.Patch(create, "PreRunCheck")
135        create.Run(args)
136        create.PreRunCheck.assert_not_called()
137
138
139if __name__ == "__main__":
140    unittest.main()
141