blob: be5787079a4e216c5fcd92d338a1b8f4433dcb2e [file] [log] [blame]
Tri Vo29ac1822016-10-01 17:06:29 -07001#!/usr/bin/env python
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tests for acloud.public.config."""
Sam Chiu46ea3112018-05-18 10:47:52 +080018import unittest
herbertxue18c9b262018-07-12 19:14:49 +080019import os
Kevin Cheng4fce0bc2018-08-13 11:51:34 -070020import tempfile
Tri Vo29ac1822016-10-01 17:06:29 -070021import mock
22
chojoyce35556da2019-10-25 16:02:36 +080023import six
24
herbertxue18c9b262018-07-12 19:14:49 +080025# pylint: disable=no-name-in-module,import-error
Sam Chiu7de3b232018-12-06 19:45:52 +080026from acloud import errors
Tri Vo29ac1822016-10-01 17:06:29 -070027from acloud.internal.proto import internal_config_pb2
28from acloud.internal.proto import user_config_pb2
29from acloud.public import config
Tri Vo29ac1822016-10-01 17:06:29 -070030
31
32class AcloudConfigManagerTest(unittest.TestCase):
33 """Test acloud configuration manager."""
34
35 USER_CONFIG = """
36service_account_name: "fake@developer.gserviceaccount.com"
37service_account_private_key_path: "/path/to/service/account/key"
xingdai8a00d462018-07-30 14:24:48 -070038service_account_json_private_key_path: "/path/to/service/account/json_key"
Tri Vo29ac1822016-10-01 17:06:29 -070039project: "fake-project"
40zone: "us-central1-f"
41machine_type: "n1-standard-1"
42network: "default"
43ssh_private_key_path: "/path/to/ssh/key"
44storage_bucket_name: "fake_bucket"
45orientation: "portrait"
46resolution: "1200x1200x1200x1200"
47client_id: "fake_client_id"
48client_secret: "fake_client_secret"
cyland370db22019-07-17 16:04:00 +080049extra_args_ssh_tunnel: "fake_extra_args_ssh_tunnel"
Tri Vo29ac1822016-10-01 17:06:29 -070050metadata_variable {
51 key: "metadata_1"
52 value: "metadata_value_1"
53}
Sam Chiuc64f3432018-08-17 11:19:06 +080054hw_property: "cpu:3,resolution:1080x1920,dpi:480,memory:4g,disk:10g"
Kevin Chengc330f6f2019-05-13 09:32:42 -070055extra_scopes: "scope1"
56extra_scopes: "scope2"
Shao-Chuan Lee26005262020-09-10 21:51:33 +090057betty_image: "fake_betty_image"
Tri Vo29ac1822016-10-01 17:06:29 -070058"""
59
60 INTERNAL_CONFIG = """
61min_machine_size: "n1-standard-1"
62disk_image_name: "avd-system.tar.gz"
63disk_image_mime_type: "application/x-tar"
64disk_image_extension: ".tar.gz"
65disk_raw_image_name: "disk.raw"
66disk_raw_image_extension: ".img"
67creds_cache_file: ".fake_oauth2.dat"
68user_agent: "fake_user_agent"
Kevin Chengb5963882018-05-09 00:06:27 -070069kernel_build_target: "kernel"
70emulator_build_target: "sdk_tools_linux"
Tri Vo29ac1822016-10-01 17:06:29 -070071
72default_usr_cfg {
73 machine_type: "n1-standard-1"
74 network: "default"
Kevin Chengb5963882018-05-09 00:06:27 -070075 stable_host_image_name: "fake_stable_host_image_name"
76 stable_host_image_project: "fake_stable_host_image_project"
77 stable_goldfish_host_image_name: "fake_stable_goldfish_host_image_name"
78 stable_goldfish_host_image_project: "fake_stable_goldfish_host_image_project"
Kevin Chengf13e8be2019-05-10 14:17:32 -070079 instance_name_pattern: "fake_instance_name_pattern"
Richard Fung97503b22019-02-06 13:43:38 -080080 stable_cheeps_host_image_name: "fake_stable_cheeps_host_image_name"
81 stable_cheeps_host_image_project: "fake_stable_cheeps_host_image_project"
Tri Vo29ac1822016-10-01 17:06:29 -070082 metadata_variable {
83 key: "metadata_1"
84 value: "metadata_value_1"
85 }
86
87 metadata_variable {
88 key: "metadata_2"
89 value: "metadata_value_2"
90 }
91}
92
93device_resolution_map {
94 key: "nexus5"
95 value: "1080x1920x32x480"
96}
97
98device_default_orientation_map {
99 key: "nexus5"
100 value: "portrait"
101}
102
103valid_branch_and_min_build_id {
Kevin Chengb5963882018-05-09 00:06:27 -0700104 key: "aosp-master"
Tri Vo29ac1822016-10-01 17:06:29 -0700105 value: 0
106}
Sam Chiu58dad6e2018-08-27 19:50:33 +0800107
108common_hw_property_map {
109 key: "phone"
Sam Chiuc64f3432018-08-17 11:19:06 +0800110 value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
Sam Chiu58dad6e2018-08-27 19:50:33 +0800111}
herbertxue908bfa82020-11-24 17:49:26 +0800112
113common_hw_property_map {
114 key: "auto"
115 value: "cpu:4,resolution:1280x800,dpi:160,memory:4g"
116}
Tri Vo29ac1822016-10-01 17:06:29 -0700117"""
118
119 def setUp(self):
120 self.config_file = mock.MagicMock()
herbertxue908bfa82020-11-24 17:49:26 +0800121 # initial config with test config.
122 self.config_file.read.return_value = self.INTERNAL_CONFIG
123 internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
124 self.config_file, internal_config_pb2.InternalConfig)
125 self.config_file.read.return_value = self.USER_CONFIG
126 usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
127 self.config_file, user_config_pb2.UserConfig)
128 self.cfg = config.AcloudConfig(usr_cfg, internal_cfg)
Tri Vo29ac1822016-10-01 17:06:29 -0700129
Sam Chiu46ea3112018-05-18 10:47:52 +0800130 # pylint: disable=no-member
Tri Vo29ac1822016-10-01 17:06:29 -0700131 def testLoadUserConfig(self):
132 """Test loading user config."""
133 self.config_file.read.return_value = self.USER_CONFIG
134 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
135 self.config_file, user_config_pb2.UserConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700136 self.assertEqual(cfg.service_account_name,
137 "fake@developer.gserviceaccount.com")
138 self.assertEqual(cfg.service_account_private_key_path,
139 "/path/to/service/account/key")
xingdai8a00d462018-07-30 14:24:48 -0700140 self.assertEqual(cfg.service_account_json_private_key_path,
141 "/path/to/service/account/json_key")
Kevin Chengb5963882018-05-09 00:06:27 -0700142 self.assertEqual(cfg.project, "fake-project")
143 self.assertEqual(cfg.zone, "us-central1-f")
144 self.assertEqual(cfg.machine_type, "n1-standard-1")
145 self.assertEqual(cfg.network, "default")
146 self.assertEqual(cfg.ssh_private_key_path, "/path/to/ssh/key")
147 self.assertEqual(cfg.storage_bucket_name, "fake_bucket")
148 self.assertEqual(cfg.orientation, "portrait")
149 self.assertEqual(cfg.resolution, "1200x1200x1200x1200")
150 self.assertEqual(cfg.client_id, "fake_client_id")
151 self.assertEqual(cfg.client_secret, "fake_client_secret")
cyland370db22019-07-17 16:04:00 +0800152 self.assertEqual(cfg.extra_args_ssh_tunnel, "fake_extra_args_ssh_tunnel")
Sam Chiu46ea3112018-05-18 10:47:52 +0800153 self.assertEqual(
chojoyce35556da2019-10-25 16:02:36 +0800154 {key: val for key, val in six.iteritems(cfg.metadata_variable)},
Sam Chiu46ea3112018-05-18 10:47:52 +0800155 {"metadata_1": "metadata_value_1"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800156 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800157 "cpu:3,resolution:1080x1920,dpi:480,memory:4g,"
158 "disk:10g")
Kevin Chengc330f6f2019-05-13 09:32:42 -0700159 self.assertEqual(cfg.extra_scopes, ["scope1", "scope2"])
Shao-Chuan Lee26005262020-09-10 21:51:33 +0900160 self.assertEqual(cfg.betty_image, "fake_betty_image")
Tri Vo29ac1822016-10-01 17:06:29 -0700161
herbertxue18c9b262018-07-12 19:14:49 +0800162 # pylint: disable=protected-access
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700163 @mock.patch("os.makedirs")
herbertxue18c9b262018-07-12 19:14:49 +0800164 @mock.patch("os.path.exists")
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700165 def testLoadUserConfigLogic(self, mock_file_exist, _mock_makedirs):
herbertxue18c9b262018-07-12 19:14:49 +0800166 """Test load user config logic.
167
168 Load user config with some special design.
169 1. User specified user config:
170 If user config didn't exist: Raise exception.
171 2. User didn't specify user config, use default config:
172 If default config didn't exist: Initialize empty data.
173 """
174 config_specify = config.AcloudConfigManager(self.config_file)
175 self.config_file.read.return_value = self.USER_CONFIG
herbertxue34776bb2018-07-03 21:57:48 +0800176 self.assertEqual(config_specify.user_config_path, self.config_file)
herbertxue18c9b262018-07-12 19:14:49 +0800177 mock_file_exist.return_value = False
178 with self.assertRaises(errors.ConfigError):
179 config_specify.Load()
180 # Test default config
181 config_unspecify = config.AcloudConfigManager(None)
182 cfg = config_unspecify.Load()
herbertxue34776bb2018-07-03 21:57:48 +0800183 self.assertEqual(config_unspecify.user_config_path,
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700184 config.GetDefaultConfigFile())
herbertxue18c9b262018-07-12 19:14:49 +0800185 self.assertEqual(cfg.project, "")
186 self.assertEqual(cfg.zone, "")
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700187
188 # Test default user config exist
herbertxue18c9b262018-07-12 19:14:49 +0800189 mock_file_exist.return_value = True
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700190 # Write the config data into a tmp file and have GetDefaultConfigFile
191 # return that.
192 _, temp_cfg_file_path = tempfile.mkstemp()
193 with open(temp_cfg_file_path, "w") as cfg_file:
herbertxue18c9b262018-07-12 19:14:49 +0800194 cfg_file.writelines(self.USER_CONFIG)
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700195 default_patcher = mock.patch.object(config, "GetDefaultConfigFile",
196 return_value=temp_cfg_file_path)
197 default_patcher.start()
198 try:
199 config_exist = config.AcloudConfigManager(None)
200 cfg = config_exist.Load()
201 self.assertEqual(cfg.project, "fake-project")
202 self.assertEqual(cfg.zone, "us-central1-f")
203 self.assertEqual(cfg.client_id, "fake_client_id")
204 self.assertEqual(cfg.client_secret, "fake_client_secret")
205 finally:
206 # Delete tmp file
207 os.remove(temp_cfg_file_path)
208 default_patcher.stop()
herbertxue18c9b262018-07-12 19:14:49 +0800209
Tri Vo29ac1822016-10-01 17:06:29 -0700210 def testLoadInternalConfig(self):
211 """Test loading internal config."""
212 self.config_file.read.return_value = self.INTERNAL_CONFIG
213 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
214 self.config_file, internal_config_pb2.InternalConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700215 self.assertEqual(cfg.min_machine_size, "n1-standard-1")
216 self.assertEqual(cfg.disk_image_name, "avd-system.tar.gz")
217 self.assertEqual(cfg.disk_image_mime_type, "application/x-tar")
218 self.assertEqual(cfg.disk_image_extension, ".tar.gz")
219 self.assertEqual(cfg.disk_raw_image_name, "disk.raw")
220 self.assertEqual(cfg.disk_raw_image_extension, ".img")
221 self.assertEqual(cfg.creds_cache_file, ".fake_oauth2.dat")
222 self.assertEqual(cfg.user_agent, "fake_user_agent")
223 self.assertEqual(cfg.default_usr_cfg.machine_type, "n1-standard-1")
224 self.assertEqual(cfg.default_usr_cfg.network, "default")
225 self.assertEqual({
Tri Vo29ac1822016-10-01 17:06:29 -0700226 key: val
chojoyce35556da2019-10-25 16:02:36 +0800227 for key, val in six.iteritems(cfg.default_usr_cfg.metadata_variable)
Sam Chiu46ea3112018-05-18 10:47:52 +0800228 }, {
229 "metadata_1": "metadata_value_1",
230 "metadata_2": "metadata_value_2"
231 })
Kevin Chengb5963882018-05-09 00:06:27 -0700232 self.assertEqual(
chojoyce35556da2019-10-25 16:02:36 +0800233 {key: val for key, val in six.iteritems(cfg.device_resolution_map)},
Tri Vo29ac1822016-10-01 17:06:29 -0700234 {"nexus5": "1080x1920x32x480"})
235 device_resolution = {
236 key: val
chojoyce35556da2019-10-25 16:02:36 +0800237 for key, val in six.iteritems(cfg.device_default_orientation_map)
Tri Vo29ac1822016-10-01 17:06:29 -0700238 }
Kevin Chengb5963882018-05-09 00:06:27 -0700239 self.assertEqual(device_resolution, {"nexus5": "portrait"})
Tri Vo29ac1822016-10-01 17:06:29 -0700240 valid_branch_and_min_build_id = {
241 key: val
chojoyce35556da2019-10-25 16:02:36 +0800242 for key, val in six.iteritems(cfg.valid_branch_and_min_build_id)
Tri Vo29ac1822016-10-01 17:06:29 -0700243 }
Kevin Chengb5963882018-05-09 00:06:27 -0700244 self.assertEqual(valid_branch_and_min_build_id, {"aosp-master": 0})
Sam Chiu46ea3112018-05-18 10:47:52 +0800245 self.assertEqual(cfg.default_usr_cfg.stable_host_image_name,
246 "fake_stable_host_image_name")
247 self.assertEqual(cfg.default_usr_cfg.stable_host_image_project,
248 "fake_stable_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700249 self.assertEqual(cfg.kernel_build_target, "kernel")
250
251 # Emulator related
Sam Chiu46ea3112018-05-18 10:47:52 +0800252 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_name,
253 "fake_stable_goldfish_host_image_name")
254 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_project,
255 "fake_stable_goldfish_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700256 self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux")
Kevin Chengf13e8be2019-05-10 14:17:32 -0700257 self.assertEqual(cfg.default_usr_cfg.instance_name_pattern,
258 "fake_instance_name_pattern")
Tri Vo29ac1822016-10-01 17:06:29 -0700259
Richard Fung97503b22019-02-06 13:43:38 -0800260 # Cheeps related
261 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_name,
262 "fake_stable_cheeps_host_image_name")
263 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_project,
264 "fake_stable_cheeps_host_image_project")
265
Sam Chiu58dad6e2018-08-27 19:50:33 +0800266 # hw property
267 self.assertEqual(
chojoyce35556da2019-10-25 16:02:36 +0800268 {key: val for key, val in six.iteritems(cfg.common_hw_property_map)},
herbertxue908bfa82020-11-24 17:49:26 +0800269 {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g",
270 "auto": "cpu:4,resolution:1280x800,dpi:160,memory:4g"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800271
Tri Vo29ac1822016-10-01 17:06:29 -0700272 def testLoadConfigFails(self):
273 """Test loading a bad file."""
274 self.config_file.read.return_value = "malformed text"
275 with self.assertRaises(errors.ConfigError):
276 config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
277 self.config_file, internal_config_pb2.InternalConfig)
278
herbertxue93924372021-02-05 14:11:01 +0800279 def testOverrideWithArgs(self):
280 """Test OverrideWithArgs."""
herbertxueffbcbc22020-07-07 14:35:34 +0800281 # test override zone.
herbertxue908bfa82020-11-24 17:49:26 +0800282 self.cfg.zone = "us-central1-f"
herbertxueffbcbc22020-07-07 14:35:34 +0800283 args = mock.MagicMock()
284 args.which = "create"
285 args.flavor = "phone"
286 args.zone = "us-central1-b"
herbertxue908bfa82020-11-24 17:49:26 +0800287 self.cfg.OverrideWithArgs(args)
288 self.assertEqual(self.cfg.zone, "us-central1-b")
289
290 def testGetDefaultHwProperty(self):
291 """Test GetDefaultHwProperty."""
292 # test with "phone" flavor
293 expected = "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
294 self.assertEqual(expected, self.cfg.GetDefaultHwProperty("phone"))
295
296 # test with "auto" flavor
297 expected = "cpu:4,resolution:1280x800,dpi:160,memory:4g"
298 self.assertEqual(expected, self.cfg.GetDefaultHwProperty("auto"))
herbertxueffbcbc22020-07-07 14:35:34 +0800299
Tri Vo29ac1822016-10-01 17:06:29 -0700300
301if __name__ == "__main__":
302 unittest.main()