blob: 694ed6b7b973631ca0d5a33101723a81543aed09 [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
chojoyce98f07c02021-03-17 21:47:38 +080021
22from unittest import mock
Tri Vo29ac1822016-10-01 17:06:29 -070023
herbertxue18c9b262018-07-12 19:14:49 +080024# pylint: disable=no-name-in-module,import-error
Sam Chiu7de3b232018-12-06 19:45:52 +080025from acloud import errors
Tri Vo29ac1822016-10-01 17:06:29 -070026from acloud.internal.proto import internal_config_pb2
27from acloud.internal.proto import user_config_pb2
28from acloud.public import config
Tri Vo29ac1822016-10-01 17:06:29 -070029
30
31class AcloudConfigManagerTest(unittest.TestCase):
32 """Test acloud configuration manager."""
33
34 USER_CONFIG = """
35service_account_name: "fake@developer.gserviceaccount.com"
36service_account_private_key_path: "/path/to/service/account/key"
xingdai8a00d462018-07-30 14:24:48 -070037service_account_json_private_key_path: "/path/to/service/account/json_key"
Tri Vo29ac1822016-10-01 17:06:29 -070038project: "fake-project"
39zone: "us-central1-f"
40machine_type: "n1-standard-1"
41network: "default"
42ssh_private_key_path: "/path/to/ssh/key"
43storage_bucket_name: "fake_bucket"
44orientation: "portrait"
45resolution: "1200x1200x1200x1200"
46client_id: "fake_client_id"
47client_secret: "fake_client_secret"
cyland370db22019-07-17 16:04:00 +080048extra_args_ssh_tunnel: "fake_extra_args_ssh_tunnel"
Tri Vo29ac1822016-10-01 17:06:29 -070049metadata_variable {
50 key: "metadata_1"
51 value: "metadata_value_1"
52}
Sam Chiuc64f3432018-08-17 11:19:06 +080053hw_property: "cpu:3,resolution:1080x1920,dpi:480,memory:4g,disk:10g"
Kevin Chengc330f6f2019-05-13 09:32:42 -070054extra_scopes: "scope1"
55extra_scopes: "scope2"
Shao-Chuan Lee26005262020-09-10 21:51:33 +090056betty_image: "fake_betty_image"
Tri Vo29ac1822016-10-01 17:06:29 -070057"""
58
59 INTERNAL_CONFIG = """
60min_machine_size: "n1-standard-1"
61disk_image_name: "avd-system.tar.gz"
62disk_image_mime_type: "application/x-tar"
63disk_image_extension: ".tar.gz"
64disk_raw_image_name: "disk.raw"
65disk_raw_image_extension: ".img"
66creds_cache_file: ".fake_oauth2.dat"
67user_agent: "fake_user_agent"
Kevin Chengb5963882018-05-09 00:06:27 -070068kernel_build_target: "kernel"
69emulator_build_target: "sdk_tools_linux"
Tri Vo29ac1822016-10-01 17:06:29 -070070
71default_usr_cfg {
72 machine_type: "n1-standard-1"
73 network: "default"
Kevin Chengb5963882018-05-09 00:06:27 -070074 stable_host_image_name: "fake_stable_host_image_name"
75 stable_host_image_project: "fake_stable_host_image_project"
76 stable_goldfish_host_image_name: "fake_stable_goldfish_host_image_name"
77 stable_goldfish_host_image_project: "fake_stable_goldfish_host_image_project"
Kevin Chengf13e8be2019-05-10 14:17:32 -070078 instance_name_pattern: "fake_instance_name_pattern"
Richard Fung97503b22019-02-06 13:43:38 -080079 stable_cheeps_host_image_name: "fake_stable_cheeps_host_image_name"
80 stable_cheeps_host_image_project: "fake_stable_cheeps_host_image_project"
Tri Vo29ac1822016-10-01 17:06:29 -070081 metadata_variable {
82 key: "metadata_1"
83 value: "metadata_value_1"
84 }
85
86 metadata_variable {
87 key: "metadata_2"
88 value: "metadata_value_2"
89 }
90}
91
92device_resolution_map {
93 key: "nexus5"
94 value: "1080x1920x32x480"
95}
96
97device_default_orientation_map {
98 key: "nexus5"
99 value: "portrait"
100}
101
102valid_branch_and_min_build_id {
Kevin Chengb5963882018-05-09 00:06:27 -0700103 key: "aosp-master"
Tri Vo29ac1822016-10-01 17:06:29 -0700104 value: 0
105}
Sam Chiu58dad6e2018-08-27 19:50:33 +0800106
107common_hw_property_map {
108 key: "phone"
Sam Chiuc64f3432018-08-17 11:19:06 +0800109 value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
Sam Chiu58dad6e2018-08-27 19:50:33 +0800110}
herbertxue908bfa82020-11-24 17:49:26 +0800111
112common_hw_property_map {
113 key: "auto"
114 value: "cpu:4,resolution:1280x800,dpi:160,memory:4g"
115}
Tri Vo29ac1822016-10-01 17:06:29 -0700116"""
117
118 def setUp(self):
119 self.config_file = mock.MagicMock()
herbertxue908bfa82020-11-24 17:49:26 +0800120 # initial config with test config.
121 self.config_file.read.return_value = self.INTERNAL_CONFIG
122 internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
123 self.config_file, internal_config_pb2.InternalConfig)
124 self.config_file.read.return_value = self.USER_CONFIG
125 usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
126 self.config_file, user_config_pb2.UserConfig)
127 self.cfg = config.AcloudConfig(usr_cfg, internal_cfg)
Tri Vo29ac1822016-10-01 17:06:29 -0700128
Sam Chiu46ea3112018-05-18 10:47:52 +0800129 # pylint: disable=no-member
Tri Vo29ac1822016-10-01 17:06:29 -0700130 def testLoadUserConfig(self):
131 """Test loading user config."""
132 self.config_file.read.return_value = self.USER_CONFIG
133 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
134 self.config_file, user_config_pb2.UserConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700135 self.assertEqual(cfg.service_account_name,
136 "fake@developer.gserviceaccount.com")
137 self.assertEqual(cfg.service_account_private_key_path,
138 "/path/to/service/account/key")
xingdai8a00d462018-07-30 14:24:48 -0700139 self.assertEqual(cfg.service_account_json_private_key_path,
140 "/path/to/service/account/json_key")
Kevin Chengb5963882018-05-09 00:06:27 -0700141 self.assertEqual(cfg.project, "fake-project")
142 self.assertEqual(cfg.zone, "us-central1-f")
143 self.assertEqual(cfg.machine_type, "n1-standard-1")
144 self.assertEqual(cfg.network, "default")
145 self.assertEqual(cfg.ssh_private_key_path, "/path/to/ssh/key")
146 self.assertEqual(cfg.storage_bucket_name, "fake_bucket")
147 self.assertEqual(cfg.orientation, "portrait")
148 self.assertEqual(cfg.resolution, "1200x1200x1200x1200")
149 self.assertEqual(cfg.client_id, "fake_client_id")
150 self.assertEqual(cfg.client_secret, "fake_client_secret")
cyland370db22019-07-17 16:04:00 +0800151 self.assertEqual(cfg.extra_args_ssh_tunnel, "fake_extra_args_ssh_tunnel")
Sam Chiu46ea3112018-05-18 10:47:52 +0800152 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800153 dict(cfg.metadata_variable.items()),
Sam Chiu46ea3112018-05-18 10:47:52 +0800154 {"metadata_1": "metadata_value_1"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800155 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800156 "cpu:3,resolution:1080x1920,dpi:480,memory:4g,"
157 "disk:10g")
Kevin Chengc330f6f2019-05-13 09:32:42 -0700158 self.assertEqual(cfg.extra_scopes, ["scope1", "scope2"])
Shao-Chuan Lee26005262020-09-10 21:51:33 +0900159 self.assertEqual(cfg.betty_image, "fake_betty_image")
Tri Vo29ac1822016-10-01 17:06:29 -0700160
herbertxue18c9b262018-07-12 19:14:49 +0800161 # pylint: disable=protected-access
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700162 @mock.patch("os.makedirs")
herbertxue18c9b262018-07-12 19:14:49 +0800163 @mock.patch("os.path.exists")
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700164 def testLoadUserConfigLogic(self, mock_file_exist, _mock_makedirs):
herbertxue18c9b262018-07-12 19:14:49 +0800165 """Test load user config logic.
166
167 Load user config with some special design.
168 1. User specified user config:
169 If user config didn't exist: Raise exception.
170 2. User didn't specify user config, use default config:
171 If default config didn't exist: Initialize empty data.
172 """
173 config_specify = config.AcloudConfigManager(self.config_file)
174 self.config_file.read.return_value = self.USER_CONFIG
herbertxue34776bb2018-07-03 21:57:48 +0800175 self.assertEqual(config_specify.user_config_path, self.config_file)
herbertxue18c9b262018-07-12 19:14:49 +0800176 mock_file_exist.return_value = False
177 with self.assertRaises(errors.ConfigError):
178 config_specify.Load()
179 # Test default config
180 config_unspecify = config.AcloudConfigManager(None)
181 cfg = config_unspecify.Load()
herbertxue34776bb2018-07-03 21:57:48 +0800182 self.assertEqual(config_unspecify.user_config_path,
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700183 config.GetDefaultConfigFile())
herbertxue18c9b262018-07-12 19:14:49 +0800184 self.assertEqual(cfg.project, "")
185 self.assertEqual(cfg.zone, "")
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700186
187 # Test default user config exist
herbertxue18c9b262018-07-12 19:14:49 +0800188 mock_file_exist.return_value = True
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700189 # Write the config data into a tmp file and have GetDefaultConfigFile
190 # return that.
191 _, temp_cfg_file_path = tempfile.mkstemp()
192 with open(temp_cfg_file_path, "w") as cfg_file:
herbertxue18c9b262018-07-12 19:14:49 +0800193 cfg_file.writelines(self.USER_CONFIG)
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700194 default_patcher = mock.patch.object(config, "GetDefaultConfigFile",
195 return_value=temp_cfg_file_path)
196 default_patcher.start()
197 try:
198 config_exist = config.AcloudConfigManager(None)
199 cfg = config_exist.Load()
200 self.assertEqual(cfg.project, "fake-project")
201 self.assertEqual(cfg.zone, "us-central1-f")
202 self.assertEqual(cfg.client_id, "fake_client_id")
203 self.assertEqual(cfg.client_secret, "fake_client_secret")
204 finally:
205 # Delete tmp file
206 os.remove(temp_cfg_file_path)
207 default_patcher.stop()
herbertxue18c9b262018-07-12 19:14:49 +0800208
Tri Vo29ac1822016-10-01 17:06:29 -0700209 def testLoadInternalConfig(self):
210 """Test loading internal config."""
211 self.config_file.read.return_value = self.INTERNAL_CONFIG
212 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
213 self.config_file, internal_config_pb2.InternalConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700214 self.assertEqual(cfg.min_machine_size, "n1-standard-1")
215 self.assertEqual(cfg.disk_image_name, "avd-system.tar.gz")
216 self.assertEqual(cfg.disk_image_mime_type, "application/x-tar")
217 self.assertEqual(cfg.disk_image_extension, ".tar.gz")
218 self.assertEqual(cfg.disk_raw_image_name, "disk.raw")
219 self.assertEqual(cfg.disk_raw_image_extension, ".img")
220 self.assertEqual(cfg.creds_cache_file, ".fake_oauth2.dat")
221 self.assertEqual(cfg.user_agent, "fake_user_agent")
222 self.assertEqual(cfg.default_usr_cfg.machine_type, "n1-standard-1")
223 self.assertEqual(cfg.default_usr_cfg.network, "default")
Kevin Chengb5963882018-05-09 00:06:27 -0700224 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800225 dict(cfg.default_usr_cfg.metadata_variable.items()),
morrislin299e3572021-10-20 12:23:10 +0800226 {"metadata_1": "metadata_value_1",
227 "metadata_2": "metadata_value_2"})
228 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800229 dict(cfg.device_resolution_map.items()),
Tri Vo29ac1822016-10-01 17:06:29 -0700230 {"nexus5": "1080x1920x32x480"})
morrislin299e3572021-10-20 12:23:10 +0800231 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800232 dict(cfg.device_default_orientation_map.items()),
morrislin299e3572021-10-20 12:23:10 +0800233 {"nexus5": "portrait"})
234 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800235 dict(cfg.valid_branch_and_min_build_id.items()),
morrislin299e3572021-10-20 12:23:10 +0800236 {"aosp-master": 0})
Sam Chiu46ea3112018-05-18 10:47:52 +0800237 self.assertEqual(cfg.default_usr_cfg.stable_host_image_name,
238 "fake_stable_host_image_name")
239 self.assertEqual(cfg.default_usr_cfg.stable_host_image_project,
240 "fake_stable_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700241 self.assertEqual(cfg.kernel_build_target, "kernel")
242
243 # Emulator related
Sam Chiu46ea3112018-05-18 10:47:52 +0800244 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_name,
245 "fake_stable_goldfish_host_image_name")
246 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_project,
247 "fake_stable_goldfish_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700248 self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux")
Kevin Chengf13e8be2019-05-10 14:17:32 -0700249 self.assertEqual(cfg.default_usr_cfg.instance_name_pattern,
250 "fake_instance_name_pattern")
Tri Vo29ac1822016-10-01 17:06:29 -0700251
Richard Fung97503b22019-02-06 13:43:38 -0800252 # Cheeps related
253 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_name,
254 "fake_stable_cheeps_host_image_name")
255 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_project,
256 "fake_stable_cheeps_host_image_project")
257
Sam Chiu58dad6e2018-08-27 19:50:33 +0800258 # hw property
259 self.assertEqual(
chojoyceb6478442022-03-10 16:44:55 +0800260 dict(cfg.common_hw_property_map.items()),
herbertxue908bfa82020-11-24 17:49:26 +0800261 {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g",
262 "auto": "cpu:4,resolution:1280x800,dpi:160,memory:4g"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800263
Tri Vo29ac1822016-10-01 17:06:29 -0700264 def testLoadConfigFails(self):
265 """Test loading a bad file."""
266 self.config_file.read.return_value = "malformed text"
267 with self.assertRaises(errors.ConfigError):
268 config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
269 self.config_file, internal_config_pb2.InternalConfig)
270
herbertxue93924372021-02-05 14:11:01 +0800271 def testOverrideWithArgs(self):
272 """Test OverrideWithArgs."""
herbertxueffbcbc22020-07-07 14:35:34 +0800273 # test override zone.
herbertxue908bfa82020-11-24 17:49:26 +0800274 self.cfg.zone = "us-central1-f"
herbertxueffbcbc22020-07-07 14:35:34 +0800275 args = mock.MagicMock()
276 args.which = "create"
277 args.flavor = "phone"
278 args.zone = "us-central1-b"
herbertxue908bfa82020-11-24 17:49:26 +0800279 self.cfg.OverrideWithArgs(args)
280 self.assertEqual(self.cfg.zone, "us-central1-b")
281
282 def testGetDefaultHwProperty(self):
283 """Test GetDefaultHwProperty."""
284 # test with "phone" flavor
285 expected = "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
286 self.assertEqual(expected, self.cfg.GetDefaultHwProperty("phone"))
287
288 # test with "auto" flavor
289 expected = "cpu:4,resolution:1280x800,dpi:160,memory:4g"
290 self.assertEqual(expected, self.cfg.GetDefaultHwProperty("auto"))
herbertxueffbcbc22020-07-07 14:35:34 +0800291
herbertxuee3c05da2021-04-27 10:37:18 +0800292 def testGetMissingFields(self):
293 """Test GetMissingFields."""
294 fields = ["project", "zone", "hw_property"]
295 self.cfg.hw_property = ""
296 expected = ["hw_property"]
297 self.assertEqual(expected, self.cfg.GetMissingFields(fields))
298
Tri Vo29ac1822016-10-01 17:06:29 -0700299
300if __name__ == "__main__":
301 unittest.main()