• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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        kernel_branch=None,
45        kernel_build_id=None,
46        kernel_build_target="kernel",
47        kernel_artifact=None,
48        system_branch=None,
49        system_build_id=None,
50        system_build_target=None,
51        local_instance=None,
52        remote_host=None,
53        host_user=constants.GCE_USER,
54        host_ssh_private_key_path=None,
55        emulator_build_id=None,
56        emulator_build_target=None,
57        avd_type=constants.TYPE_CF,
58        autoconnect=constants.INS_KEY_WEBRTC)
59    return mock_args
60
61
62# pylint: disable=invalid-name,protected-access
63class CreateArgsTest(driver_test_lib.BaseDriverTest):
64    """Test create_args functions."""
65
66    def testVerifyArgs(self):
67        """test VerifyArgs."""
68        mock_args = _CreateArgs()
69        # Test args default setting shouldn't raise error.
70        self.assertEqual(None, create_args.VerifyArgs(mock_args))
71
72    def testVerifyArgs_Goldfish(self):
73        """test goldfish arguments."""
74        # emulator_build_id with wrong avd_type.
75        mock_args = _CreateArgs()
76        mock_args.emulator_build_id = 123456
77        self.assertRaises(errors.UnsupportedCreateArgs,
78                          create_args.VerifyArgs, mock_args)
79        # Valid emulator_build_id.
80        mock_args.avd_type = constants.TYPE_GF
81        create_args.VerifyArgs(mock_args)
82        # emulator_build_target with wrong avd_type.
83        mock_args.avd_type = constants.TYPE_CF
84        mock_args.emulator_build_id = None
85        mock_args.emulator_build_target = "sdk_tools_linux"
86        mock_args.remote_host = "192.0.2.2"
87        self.assertRaises(errors.UnsupportedCreateArgs,
88                          create_args.VerifyArgs, mock_args)
89        # emulator_build_target without remote_host.
90        mock_args.avd_type = constants.TYPE_GF
91        mock_args.emulator_build_target = "sdk_tools_linux"
92        mock_args.remote_host = None
93        self.assertRaises(errors.UnsupportedCreateArgs,
94                          create_args.VerifyArgs, mock_args)
95        # Incomplete system build info.
96        mock_args.emulator_build_target = None
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        # System build info without remote_host.
102        mock_args.system_branch = "aosp-master"
103        mock_args.system_build_target = "aosp_x86_64-userdebug"
104        mock_args.system_build_id = "123456"
105        mock_args.remote_host = None
106        self.assertRaises(errors.UnsupportedCreateArgs,
107                          create_args.VerifyArgs, mock_args)
108        # Valid build info.
109        mock_args.emulator_build_target = "sdk_tools_linux"
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.kernel_branch = "aosp-master"
114        mock_args.kernel_build_target = "aosp_x86_64-userdebug"
115        mock_args.kernel_build_id = "123456"
116        mock_args.kernel_artifact = "boot-5.10.img"
117        mock_args.remote_host = "192.0.2.2"
118        create_args.VerifyArgs(mock_args)
119
120    def testVerifyArgs_ConnectWebRTC(self):
121        """test VerifyArgs args.autconnect webrtc.
122
123        WebRTC only apply to remote cuttlefish instance
124
125        """
126        mock_args = _CreateArgs()
127        mock_args.autoconnect = constants.INS_KEY_WEBRTC
128        # Test remote instance and avd_type cuttlefish(default)
129        # Test args.autoconnect webrtc shouldn't raise error.
130        self.assertEqual(None, create_args.VerifyArgs(mock_args))
131
132    def testVerifyLocalArgs(self):
133        """Test _VerifyLocalArgs."""
134        mock_args = _CreateArgs()
135        # verify local image case.
136        mock_args.local_image = "/tmp/local_image_dir"
137        self.Patch(os.path, "exists", return_value=False)
138        self.assertRaises(errors.CheckPathError,
139                          create_args._VerifyLocalArgs, mock_args)
140
141        # verify local instance
142        mock_args = _CreateArgs()
143        mock_args.local_instance_dir = "/tmp/local_instance_dir"
144        self.Patch(os.path, "exists", return_value=False)
145        self.assertRaises(errors.CheckPathError,
146                          create_args._VerifyLocalArgs, mock_args)
147
148        # verify local system image
149        mock_args = _CreateArgs()
150        mock_args.local_system_image = "/tmp/local_system_image_dir"
151        mock_args.avd_type = "cheeps"
152        self.assertRaises(errors.UnsupportedCreateArgs,
153                          create_args._VerifyLocalArgs, mock_args)
154        mock_args.avd_type = "cuttlefish"
155        self.Patch(os.path, "exists", return_value=False)
156        self.assertRaises(errors.CheckPathError,
157                          create_args._VerifyLocalArgs, mock_args)
158
159        # unsupport local-image with kernel build
160        mock_args = _CreateArgs()
161        mock_args.local_instance = None
162        mock_args.local_image = "/tmp/local_image_dir"
163        self.Patch(os.path, "exists", return_value=True)
164        mock_args.kernel_branch = "common-android12-5.4"
165        self.assertRaises(errors.UnsupportedCreateArgs,
166                          create_args._VerifyLocalArgs, mock_args)
167        mock_args.kernel_branch = None
168        mock_args.kernel_build_id = "fake_kernel_1234567"
169        self.assertRaises(errors.UnsupportedCreateArgs,
170                          create_args._VerifyLocalArgs, mock_args)
171
172
173if __name__ == "__main__":
174    unittest.main()
175