blob: 4621d53b16d14ed835e04b96e0002e327ff3fe77 [file] [log] [blame]
herbertxue2625b042018-08-16 23:28:20 +08001# Copyright 2018 - 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 avd_spec."""
15
Sam Chiu96172ae2019-01-31 14:30:30 +080016import glob
17import os
herbertxue2625b042018-08-16 23:28:20 +080018import unittest
19import mock
20
herbertxueb617e8a2018-08-22 10:02:19 +080021from acloud import errors
herbertxue2625b042018-08-16 23:28:20 +080022from acloud.create import avd_spec
23from acloud.internal import constants
herbertxueaf979452019-04-29 09:56:59 +080024from acloud.internal.lib import android_build_client
25from acloud.internal.lib import auth
Sam Chiu96172ae2019-01-31 14:30:30 +080026from acloud.internal.lib import driver_test_lib
27from acloud.internal.lib import utils
herbertxue2625b042018-08-16 23:28:20 +080028
29
30# pylint: disable=invalid-name,protected-access
Sam Chiu96172ae2019-01-31 14:30:30 +080031class AvdSpecTest(driver_test_lib.BaseDriverTest):
Sam Chiuc64f3432018-08-17 11:19:06 +080032 """Test avd_spec methods."""
herbertxue2625b042018-08-16 23:28:20 +080033
34 def setUp(self):
35 """Initialize new avd_spec.AVDSpec."""
Sam Chiu96172ae2019-01-31 14:30:30 +080036 super(AvdSpecTest, self).setUp()
herbertxue2625b042018-08-16 23:28:20 +080037 self.args = mock.MagicMock()
herbertxuea4663c02019-07-21 22:37:17 +080038 self.args.flavor = ""
herbertxue2625b042018-08-16 23:28:20 +080039 self.args.local_image = ""
herbertxue79585f42018-08-28 18:36:45 +080040 self.args.config_file = ""
chojoyce7a361732018-11-26 16:26:13 +080041 self.args.build_target = "fake_build_target"
herbertxue6ef54a52019-05-02 11:38:58 +080042 self.args.adb_port = None
herbertxue2625b042018-08-16 23:28:20 +080043 self.AvdSpec = avd_spec.AVDSpec(self.args)
44
Sam Chiuc64f3432018-08-17 11:19:06 +080045 # pylint: disable=protected-access
Sam Chiu96172ae2019-01-31 14:30:30 +080046 def testProcessLocalImageArgs(self):
herbertxue2625b042018-08-16 23:28:20 +080047 """Test process args.local_image."""
Sam Chiu96172ae2019-01-31 14:30:30 +080048 self.Patch(glob, "glob", return_value=["fake.img"])
49 expected_image_artifact = "/path/cf_x86_phone-img-eng.user.zip"
50 expected_image_dir = "/path-to-image-dir"
51
52 # Specified --local-image to a local zipped image file
53 self.Patch(os.path, "isfile", return_value=True)
54 self.args.local_image = "/path/cf_x86_phone-img-eng.user.zip"
55 self.AvdSpec._avd_type = constants.TYPE_CF
56 self.AvdSpec._instance_type = constants.INSTANCE_TYPE_REMOTE
herbertxue2625b042018-08-16 23:28:20 +080057 self.AvdSpec._ProcessLocalImageArgs(self.args)
Sam Chiu96172ae2019-01-31 14:30:30 +080058 self.assertEqual(self.AvdSpec._local_image_artifact,
59 expected_image_artifact)
herbertxue2625b042018-08-16 23:28:20 +080060
Sam Chiu96172ae2019-01-31 14:30:30 +080061 # Specified --local-image to a dir contains images
62 self.Patch(utils, "GetBuildEnvironmentVariable",
63 return_value="test_environ")
64 self.Patch(os.path, "isfile", return_value=False)
65 self.args.local_image = "/path-to-image-dir"
66 self.AvdSpec._avd_type = constants.TYPE_CF
67 self.AvdSpec._instance_type = constants.INSTANCE_TYPE_REMOTE
68 self.AvdSpec._ProcessLocalImageArgs(self.args)
69 self.assertEqual(self.AvdSpec._local_image_dir, expected_image_dir)
70
71 # Specified local_image without arg
herbertxue2625b042018-08-16 23:28:20 +080072 self.args.local_image = None
Sam Chiu96172ae2019-01-31 14:30:30 +080073 self.Patch(utils, "GetBuildEnvironmentVariable",
74 return_value="test_environ")
75 self.AvdSpec._ProcessLocalImageArgs(self.args)
76 self.assertEqual(self.AvdSpec._local_image_dir, "test_environ")
77 self.assertEqual(self.AvdSpec.local_image_artifact, expected_image_artifact)
herbertxue2625b042018-08-16 23:28:20 +080078
Sam Chiu96172ae2019-01-31 14:30:30 +080079 def testProcessImageArgs(self):
herbertxue2625b042018-08-16 23:28:20 +080080 """Test process image source."""
Sam Chiu96172ae2019-01-31 14:30:30 +080081 self.Patch(glob, "glob", return_value=["fake.img"])
herbertxue2625b042018-08-16 23:28:20 +080082 # No specified local_image, image source is from remote
83 self.args.local_image = ""
84 self.AvdSpec._ProcessImageArgs(self.args)
85 self.assertEqual(self.AvdSpec._image_source, constants.IMAGE_SRC_REMOTE)
cylan1e996c52018-10-01 16:19:50 +080086 self.assertEqual(self.AvdSpec._local_image_dir, None)
herbertxue2625b042018-08-16 23:28:20 +080087
Sam Chiu96172ae2019-01-31 14:30:30 +080088 # Specified local_image with an arg for cf type
89 self.Patch(os.path, "isfile", return_value=True)
90 self.args.local_image = "/test_path/cf_x86_phone-img-eng.user.zip"
91 self.AvdSpec._avd_type = constants.TYPE_CF
92 self.AvdSpec._instance_type = constants.INSTANCE_TYPE_REMOTE
herbertxue2625b042018-08-16 23:28:20 +080093 self.AvdSpec._ProcessImageArgs(self.args)
94 self.assertEqual(self.AvdSpec._image_source, constants.IMAGE_SRC_LOCAL)
Sam Chiu96172ae2019-01-31 14:30:30 +080095 self.assertEqual(self.AvdSpec._local_image_artifact,
96 "/test_path/cf_x86_phone-img-eng.user.zip")
97
98 # Specified local_image with an arg for gce type
99 self.Patch(os.path, "isfile", return_value=False)
100 self.Patch(os.path, "exists", return_value=True)
101 self.args.local_image = "/test_path_to_dir/"
102 self.AvdSpec._avd_type = constants.TYPE_GCE
103 self.AvdSpec._ProcessImageArgs(self.args)
104 self.assertEqual(self.AvdSpec._image_source, constants.IMAGE_SRC_LOCAL)
105 self.assertEqual(self.AvdSpec._local_image_artifact,
106 "/test_path_to_dir/avd-system.tar.gz")
herbertxueb617e8a2018-08-22 10:02:19 +0800107
Kevin Chenge2580452018-10-05 16:33:56 -0700108 @mock.patch.object(avd_spec.AVDSpec, "_GetGitRemote")
herbertxueb617e8a2018-08-22 10:02:19 +0800109 @mock.patch("subprocess.check_output")
Kevin Chenge2580452018-10-05 16:33:56 -0700110 def testGetBranchFromRepo(self, mock_repo, mock_gitremote):
herbertxueb617e8a2018-08-22 10:02:19 +0800111 """Test get branch name from repo info."""
Kevin Chenge2580452018-10-05 16:33:56 -0700112 # Check aosp repo gets proper branch prefix.
113 mock_gitremote.return_value = "aosp"
herbertxueb617e8a2018-08-22 10:02:19 +0800114 mock_repo.return_value = "Manifest branch: master"
115 self.assertEqual(self.AvdSpec._GetBranchFromRepo(), "aosp-master")
116
Kevin Chenge2580452018-10-05 16:33:56 -0700117 # Check default repo gets default branch prefix.
118 mock_gitremote.return_value = ""
119 mock_repo.return_value = "Manifest branch: master"
120 self.assertEqual(self.AvdSpec._GetBranchFromRepo(), "git_master")
121
herbertxuec7379b02019-06-21 15:47:18 +0800122 # Can't get branch from repo info, set it as default branch.
herbertxueb617e8a2018-08-22 10:02:19 +0800123 mock_repo.return_value = "Manifest branch:"
herbertxuec7379b02019-06-21 15:47:18 +0800124 self.assertEqual(self.AvdSpec._GetBranchFromRepo(), "aosp-master")
herbertxueb617e8a2018-08-22 10:02:19 +0800125
herbertxueaf979452019-04-29 09:56:59 +0800126 def testGetBuildBranch(self):
127 """Test GetBuildBranch function"""
128 # Test infer branch from build_id and build_target.
129 build_client = mock.MagicMock()
130 build_id = "fake_build_id"
131 build_target = "fake_build_target"
132 expected_branch = "fake_build_branch"
133 self.Patch(android_build_client, "AndroidBuildClient",
134 return_value=build_client)
135 self.Patch(auth, "CreateCredentials", return_value=mock.MagicMock())
136 self.Patch(build_client, "GetBranch", return_value=expected_branch)
137 self.assertEqual(self.AvdSpec._GetBuildBranch(build_id, build_target),
138 expected_branch)
139 # Infer branch from "repo info" when build_id and build_target is None.
140 self.Patch(self.AvdSpec, "_GetBranchFromRepo", return_value="repo_branch")
141 build_id = None
142 build_target = None
143 expected_branch = "repo_branch"
144 self.assertEqual(self.AvdSpec._GetBuildBranch(build_id, build_target),
145 expected_branch)
146
Kevin Cheng990e2282018-10-08 15:51:37 -0700147 # pylint: disable=protected-access
148 def testGetBuildTarget(self):
herbertxueb617e8a2018-08-22 10:02:19 +0800149 """Test get build target name."""
herbertxue23b2a962019-07-24 22:39:20 +0800150 self.AvdSpec._remote_image[constants.BUILD_BRANCH] = "git_branch"
Kevin Cheng41f5f9b2019-01-11 02:44:59 -0800151 self.AvdSpec._flavor = constants.FLAVOR_IOT
herbertxueb617e8a2018-08-22 10:02:19 +0800152 self.args.avd_type = constants.TYPE_GCE
153 self.assertEqual(
154 self.AvdSpec._GetBuildTarget(self.args),
chojoyce7a361732018-11-26 16:26:13 +0800155 "gce_x86_iot-userdebug")
herbertxueb617e8a2018-08-22 10:02:19 +0800156
herbertxue23b2a962019-07-24 22:39:20 +0800157 self.AvdSpec._remote_image[constants.BUILD_BRANCH] = "aosp-master"
Kevin Cheng41f5f9b2019-01-11 02:44:59 -0800158 self.AvdSpec._flavor = constants.FLAVOR_PHONE
herbertxueb617e8a2018-08-22 10:02:19 +0800159 self.args.avd_type = constants.TYPE_CF
160 self.assertEqual(
161 self.AvdSpec._GetBuildTarget(self.args),
162 "aosp_cf_x86_phone-userdebug")
herbertxue2625b042018-08-16 23:28:20 +0800163
herbertxue23b2a962019-07-24 22:39:20 +0800164 self.AvdSpec._remote_image[constants.BUILD_BRANCH] = "git_branch"
Kevin Cheng41f5f9b2019-01-11 02:44:59 -0800165 self.AvdSpec._flavor = constants.FLAVOR_PHONE
Kevin Chenge2580452018-10-05 16:33:56 -0700166 self.args.avd_type = constants.TYPE_CF
167 self.assertEqual(
168 self.AvdSpec._GetBuildTarget(self.args),
169 "cf_x86_phone-userdebug")
170
Sam Chiuc64f3432018-08-17 11:19:06 +0800171 # pylint: disable=protected-access
172 def testProcessHWPropertyWithInvalidArgs(self):
173 """Test _ProcessHWPropertyArgs with invalid args."""
174 # Checking wrong resolution.
175 args = mock.MagicMock()
176 args.hw_property = "cpu:3,resolution:1280"
177 with self.assertRaises(errors.InvalidHWPropertyError):
178 self.AvdSpec._ProcessHWPropertyArgs(args)
179
180 # Checking property should be int.
181 args = mock.MagicMock()
182 args.hw_property = "cpu:3,dpi:fake"
183 with self.assertRaises(errors.InvalidHWPropertyError):
184 self.AvdSpec._ProcessHWPropertyArgs(args)
185
186 # Checking disk property should be with 'g' suffix.
187 args = mock.MagicMock()
188 args.hw_property = "cpu:3,disk:2"
189 with self.assertRaises(errors.InvalidHWPropertyError):
190 self.AvdSpec._ProcessHWPropertyArgs(args)
191
192 # Checking memory property should be with 'g' suffix.
193 args = mock.MagicMock()
194 args.hw_property = "cpu:3,memory:2"
195 with self.assertRaises(errors.InvalidHWPropertyError):
196 self.AvdSpec._ProcessHWPropertyArgs(args)
197
198 # pylint: disable=protected-access
199 def testParseHWPropertyStr(self):
200 """Test _ParseHWPropertyStr."""
201 expected_dict = {"cpu": "2", "x_res": "1080", "y_res": "1920",
202 "dpi": "240", "memory": "4096", "disk": "4096"}
203 args_str = "cpu:2,resolution:1080x1920,dpi:240,memory:4g,disk:4g"
204 result_dict = self.AvdSpec._ParseHWPropertyStr(args_str)
205 self.assertTrue(expected_dict == result_dict)
206
Sam Chiu96172ae2019-01-31 14:30:30 +0800207 def testGetFlavorFromBuildTargetString(self):
herbertxuefd15dfd2018-12-04 11:26:27 +0800208 """Test _GetFlavorFromLocalImage."""
209 img_path = "/fack_path/cf_x86_tv-img-eng.user.zip"
Sam Chiu96172ae2019-01-31 14:30:30 +0800210 self.assertEqual(self.AvdSpec._GetFlavorFromString(img_path),
211 "tv")
212
213 build_target_str = "aosp_cf_x86_auto"
214 self.assertEqual(self.AvdSpec._GetFlavorFromString(
215 build_target_str), "auto")
herbertxuefd15dfd2018-12-04 11:26:27 +0800216
217 # Flavor is not supported.
218 img_path = "/fack_path/cf_x86_error-img-eng.user.zip"
Sam Chiu96172ae2019-01-31 14:30:30 +0800219 self.assertEqual(self.AvdSpec._GetFlavorFromString(img_path),
220 None)
herbertxuead1c94c2019-01-15 16:00:18 +0800221
chojoyce7a361732018-11-26 16:26:13 +0800222 # pylint: disable=protected-access
223 def testProcessRemoteBuildArgs(self):
224 """Test _ProcessRemoteBuildArgs."""
225 self.args.branch = "git_master"
226 self.args.build_id = "1234"
227
228 # Verify auto-assigned avd_type if build_targe contains "_gce_".
229 self.args.build_target = "aosp_gce_x86_phone-userdebug"
230 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
231 self.assertTrue(self.AvdSpec.avd_type == "gce")
232
233 # Verify auto-assigned avd_type if build_targe contains "gce_".
234 self.args.build_target = "gce_x86_phone-userdebug"
235 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
236 self.assertTrue(self.AvdSpec.avd_type == "gce")
237
238 # Verify auto-assigned avd_type if build_targe contains "_cf_".
239 self.args.build_target = "aosp_cf_x86_phone-userdebug"
240 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
241 self.assertTrue(self.AvdSpec.avd_type == "cuttlefish")
242
243 # Verify auto-assigned avd_type if build_targe contains "cf_".
244 self.args.build_target = "cf_x86_phone-userdebug"
245 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
246 self.assertTrue(self.AvdSpec.avd_type == "cuttlefish")
247
248 # Verify auto-assigned avd_type if build_targe contains "sdk_".
249 self.args.build_target = "sdk_phone_armv7-sdk"
250 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
251 self.assertTrue(self.AvdSpec.avd_type == "goldfish")
252
253 # Verify auto-assigned avd_type if build_targe contains "_sdk_".
254 self.args.build_target = "aosp_sdk_phone_armv7-sdk"
255 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
256 self.assertTrue(self.AvdSpec.avd_type == "goldfish")
257
258 # Verify auto-assigned avd_type if no match, default as cuttlefish.
259 self.args.build_target = "mini_emulator_arm64-userdebug"
260 self.args.avd_type = "cuttlefish"
261 # reset args.avd_type default value as cuttlefish.
262 self.AvdSpec = avd_spec.AVDSpec(self.args)
263 self.AvdSpec._ProcessRemoteBuildArgs(self.args)
264 self.assertTrue(self.AvdSpec.avd_type == "cuttlefish")
265
herbertxuea075bfd2019-01-28 17:59:47 +0800266 def testEscapeAnsi(self):
267 """Test EscapeAnsi."""
268 test_string = "\033[1;32;40m Manifest branch:"
269 expected_result = " Manifest branch:"
270 self.assertEqual(avd_spec.EscapeAnsi(test_string), expected_result)
271
Sam Chiu96172ae2019-01-31 14:30:30 +0800272 def testGetGceLocalImagePath(self):
273 """Test get gce local image path."""
274 self.Patch(os.path, "isfile", return_value=True)
275 # Verify when specify --local-image ~/XXX.tar.gz.
276 fake_image_path = "~/gce_local_image_dir/gce_image.tar.gz"
277 self.Patch(os.path, "exists", return_value=True)
278 self.assertEqual(self.AvdSpec._GetGceLocalImagePath(fake_image_path),
279 "~/gce_local_image_dir/gce_image.tar.gz")
280
281 # Verify when specify --local-image ~/XXX.img.
282 fake_image_path = "~/gce_local_image_dir/gce_image.img"
283 self.assertEqual(self.AvdSpec._GetGceLocalImagePath(fake_image_path),
284 "~/gce_local_image_dir/gce_image.img")
285
286 # Verify if exist argument --local-image as a directory.
287 self.Patch(os.path, "isfile", return_value=False)
288 self.Patch(os.path, "exists", return_value=True)
289 fake_image_path = "~/gce_local_image_dir/"
290 # Default to find */avd-system.tar.gz if exist then return the path.
291 self.assertEqual(self.AvdSpec._GetGceLocalImagePath(fake_image_path),
292 "~/gce_local_image_dir/avd-system.tar.gz")
293
294 # Otherwise choose raw file */android_system_disk_syslinux.img if
295 # exist then return the path.
296 self.Patch(os.path, "exists", side_effect=[False, True])
297 self.assertEqual(self.AvdSpec._GetGceLocalImagePath(fake_image_path),
298 "~/gce_local_image_dir/android_system_disk_syslinux.img")
299
300 # Both _GCE_LOCAL_IMAGE_CANDIDATE could not be found then raise error.
301 self.Patch(os.path, "exists", side_effect=[False, False])
herbertxuee5b98ae2019-06-19 09:26:17 +0800302 self.assertRaises(errors.ImgDoesNotExist,
Sam Chiu96172ae2019-01-31 14:30:30 +0800303 self.AvdSpec._GetGceLocalImagePath, fake_image_path)
304
herbertxue2625b042018-08-16 23:28:20 +0800305
306if __name__ == "__main__":
307 unittest.main()