blob: 15e612f1ab17af2af4d2e00dba389ecdcff04aea [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
herbertxue18c9b262018-07-12 19:14:49 +080023# pylint: disable=no-name-in-module,import-error
Sam Chiu7de3b232018-12-06 19:45:52 +080024from acloud import errors
Tri Vo29ac1822016-10-01 17:06:29 -070025from acloud.internal.proto import internal_config_pb2
26from acloud.internal.proto import user_config_pb2
27from acloud.public import config
Tri Vo29ac1822016-10-01 17:06:29 -070028
29
30class AcloudConfigManagerTest(unittest.TestCase):
31 """Test acloud configuration manager."""
32
33 USER_CONFIG = """
34service_account_name: "fake@developer.gserviceaccount.com"
35service_account_private_key_path: "/path/to/service/account/key"
xingdai8a00d462018-07-30 14:24:48 -070036service_account_json_private_key_path: "/path/to/service/account/json_key"
Tri Vo29ac1822016-10-01 17:06:29 -070037project: "fake-project"
38zone: "us-central1-f"
39machine_type: "n1-standard-1"
40network: "default"
41ssh_private_key_path: "/path/to/ssh/key"
42storage_bucket_name: "fake_bucket"
43orientation: "portrait"
44resolution: "1200x1200x1200x1200"
45client_id: "fake_client_id"
46client_secret: "fake_client_secret"
47metadata_variable {
48 key: "metadata_1"
49 value: "metadata_value_1"
50}
Sam Chiuc64f3432018-08-17 11:19:06 +080051hw_property: "cpu:3,resolution:1080x1920,dpi:480,memory:4g,disk:10g"
Tri Vo29ac1822016-10-01 17:06:29 -070052"""
53
54 INTERNAL_CONFIG = """
55min_machine_size: "n1-standard-1"
56disk_image_name: "avd-system.tar.gz"
57disk_image_mime_type: "application/x-tar"
58disk_image_extension: ".tar.gz"
59disk_raw_image_name: "disk.raw"
60disk_raw_image_extension: ".img"
61creds_cache_file: ".fake_oauth2.dat"
62user_agent: "fake_user_agent"
Kevin Chengb5963882018-05-09 00:06:27 -070063kernel_build_target: "kernel"
64emulator_build_target: "sdk_tools_linux"
Tri Vo29ac1822016-10-01 17:06:29 -070065
66default_usr_cfg {
67 machine_type: "n1-standard-1"
68 network: "default"
Kevin Chengb5963882018-05-09 00:06:27 -070069 stable_host_image_name: "fake_stable_host_image_name"
70 stable_host_image_project: "fake_stable_host_image_project"
71 stable_goldfish_host_image_name: "fake_stable_goldfish_host_image_name"
72 stable_goldfish_host_image_project: "fake_stable_goldfish_host_image_project"
Richard Fung97503b22019-02-06 13:43:38 -080073 stable_cheeps_host_image_name: "fake_stable_cheeps_host_image_name"
74 stable_cheeps_host_image_project: "fake_stable_cheeps_host_image_project"
Tri Vo29ac1822016-10-01 17:06:29 -070075 metadata_variable {
76 key: "metadata_1"
77 value: "metadata_value_1"
78 }
79
80 metadata_variable {
81 key: "metadata_2"
82 value: "metadata_value_2"
83 }
84}
85
86device_resolution_map {
87 key: "nexus5"
88 value: "1080x1920x32x480"
89}
90
91device_default_orientation_map {
92 key: "nexus5"
93 value: "portrait"
94}
95
96valid_branch_and_min_build_id {
Kevin Chengb5963882018-05-09 00:06:27 -070097 key: "aosp-master"
Tri Vo29ac1822016-10-01 17:06:29 -070098 value: 0
99}
Sam Chiu58dad6e2018-08-27 19:50:33 +0800100
101common_hw_property_map {
102 key: "phone"
Sam Chiuc64f3432018-08-17 11:19:06 +0800103 value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
Sam Chiu58dad6e2018-08-27 19:50:33 +0800104}
Tri Vo29ac1822016-10-01 17:06:29 -0700105"""
106
107 def setUp(self):
108 self.config_file = mock.MagicMock()
109
Sam Chiu46ea3112018-05-18 10:47:52 +0800110 # pylint: disable=no-member
Tri Vo29ac1822016-10-01 17:06:29 -0700111 def testLoadUserConfig(self):
112 """Test loading user config."""
113 self.config_file.read.return_value = self.USER_CONFIG
114 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
115 self.config_file, user_config_pb2.UserConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700116 self.assertEqual(cfg.service_account_name,
117 "fake@developer.gserviceaccount.com")
118 self.assertEqual(cfg.service_account_private_key_path,
119 "/path/to/service/account/key")
xingdai8a00d462018-07-30 14:24:48 -0700120 self.assertEqual(cfg.service_account_json_private_key_path,
121 "/path/to/service/account/json_key")
Kevin Chengb5963882018-05-09 00:06:27 -0700122 self.assertEqual(cfg.project, "fake-project")
123 self.assertEqual(cfg.zone, "us-central1-f")
124 self.assertEqual(cfg.machine_type, "n1-standard-1")
125 self.assertEqual(cfg.network, "default")
126 self.assertEqual(cfg.ssh_private_key_path, "/path/to/ssh/key")
127 self.assertEqual(cfg.storage_bucket_name, "fake_bucket")
128 self.assertEqual(cfg.orientation, "portrait")
129 self.assertEqual(cfg.resolution, "1200x1200x1200x1200")
130 self.assertEqual(cfg.client_id, "fake_client_id")
131 self.assertEqual(cfg.client_secret, "fake_client_secret")
Sam Chiu46ea3112018-05-18 10:47:52 +0800132 self.assertEqual(
133 {key: val for key, val in cfg.metadata_variable.iteritems()},
134 {"metadata_1": "metadata_value_1"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800135 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800136 "cpu:3,resolution:1080x1920,dpi:480,memory:4g,"
137 "disk:10g")
Tri Vo29ac1822016-10-01 17:06:29 -0700138
herbertxue18c9b262018-07-12 19:14:49 +0800139 # pylint: disable=protected-access
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700140 @mock.patch("os.makedirs")
herbertxue18c9b262018-07-12 19:14:49 +0800141 @mock.patch("os.path.exists")
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700142 def testLoadUserConfigLogic(self, mock_file_exist, _mock_makedirs):
herbertxue18c9b262018-07-12 19:14:49 +0800143 """Test load user config logic.
144
145 Load user config with some special design.
146 1. User specified user config:
147 If user config didn't exist: Raise exception.
148 2. User didn't specify user config, use default config:
149 If default config didn't exist: Initialize empty data.
150 """
151 config_specify = config.AcloudConfigManager(self.config_file)
152 self.config_file.read.return_value = self.USER_CONFIG
herbertxue34776bb2018-07-03 21:57:48 +0800153 self.assertEqual(config_specify.user_config_path, self.config_file)
herbertxue18c9b262018-07-12 19:14:49 +0800154 mock_file_exist.return_value = False
155 with self.assertRaises(errors.ConfigError):
156 config_specify.Load()
157 # Test default config
158 config_unspecify = config.AcloudConfigManager(None)
159 cfg = config_unspecify.Load()
herbertxue34776bb2018-07-03 21:57:48 +0800160 self.assertEqual(config_unspecify.user_config_path,
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700161 config.GetDefaultConfigFile())
herbertxue18c9b262018-07-12 19:14:49 +0800162 self.assertEqual(cfg.project, "")
163 self.assertEqual(cfg.zone, "")
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700164
165 # Test default user config exist
herbertxue18c9b262018-07-12 19:14:49 +0800166 mock_file_exist.return_value = True
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700167 # Write the config data into a tmp file and have GetDefaultConfigFile
168 # return that.
169 _, temp_cfg_file_path = tempfile.mkstemp()
170 with open(temp_cfg_file_path, "w") as cfg_file:
herbertxue18c9b262018-07-12 19:14:49 +0800171 cfg_file.writelines(self.USER_CONFIG)
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700172 default_patcher = mock.patch.object(config, "GetDefaultConfigFile",
173 return_value=temp_cfg_file_path)
174 default_patcher.start()
175 try:
176 config_exist = config.AcloudConfigManager(None)
177 cfg = config_exist.Load()
178 self.assertEqual(cfg.project, "fake-project")
179 self.assertEqual(cfg.zone, "us-central1-f")
180 self.assertEqual(cfg.client_id, "fake_client_id")
181 self.assertEqual(cfg.client_secret, "fake_client_secret")
182 finally:
183 # Delete tmp file
184 os.remove(temp_cfg_file_path)
185 default_patcher.stop()
herbertxue18c9b262018-07-12 19:14:49 +0800186
Tri Vo29ac1822016-10-01 17:06:29 -0700187 def testLoadInternalConfig(self):
188 """Test loading internal config."""
189 self.config_file.read.return_value = self.INTERNAL_CONFIG
190 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
191 self.config_file, internal_config_pb2.InternalConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700192 self.assertEqual(cfg.min_machine_size, "n1-standard-1")
193 self.assertEqual(cfg.disk_image_name, "avd-system.tar.gz")
194 self.assertEqual(cfg.disk_image_mime_type, "application/x-tar")
195 self.assertEqual(cfg.disk_image_extension, ".tar.gz")
196 self.assertEqual(cfg.disk_raw_image_name, "disk.raw")
197 self.assertEqual(cfg.disk_raw_image_extension, ".img")
198 self.assertEqual(cfg.creds_cache_file, ".fake_oauth2.dat")
199 self.assertEqual(cfg.user_agent, "fake_user_agent")
200 self.assertEqual(cfg.default_usr_cfg.machine_type, "n1-standard-1")
201 self.assertEqual(cfg.default_usr_cfg.network, "default")
202 self.assertEqual({
Tri Vo29ac1822016-10-01 17:06:29 -0700203 key: val
204 for key, val in cfg.default_usr_cfg.metadata_variable.iteritems()
Sam Chiu46ea3112018-05-18 10:47:52 +0800205 }, {
206 "metadata_1": "metadata_value_1",
207 "metadata_2": "metadata_value_2"
208 })
Kevin Chengb5963882018-05-09 00:06:27 -0700209 self.assertEqual(
Sam Chiu46ea3112018-05-18 10:47:52 +0800210 {key: val for key, val in cfg.device_resolution_map.iteritems()},
Tri Vo29ac1822016-10-01 17:06:29 -0700211 {"nexus5": "1080x1920x32x480"})
212 device_resolution = {
213 key: val
214 for key, val in cfg.device_default_orientation_map.iteritems()
215 }
Kevin Chengb5963882018-05-09 00:06:27 -0700216 self.assertEqual(device_resolution, {"nexus5": "portrait"})
Tri Vo29ac1822016-10-01 17:06:29 -0700217 valid_branch_and_min_build_id = {
218 key: val
219 for key, val in cfg.valid_branch_and_min_build_id.iteritems()
220 }
Kevin Chengb5963882018-05-09 00:06:27 -0700221 self.assertEqual(valid_branch_and_min_build_id, {"aosp-master": 0})
Sam Chiu46ea3112018-05-18 10:47:52 +0800222 self.assertEqual(cfg.default_usr_cfg.stable_host_image_name,
223 "fake_stable_host_image_name")
224 self.assertEqual(cfg.default_usr_cfg.stable_host_image_project,
225 "fake_stable_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700226 self.assertEqual(cfg.kernel_build_target, "kernel")
227
228 # Emulator related
Sam Chiu46ea3112018-05-18 10:47:52 +0800229 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_name,
230 "fake_stable_goldfish_host_image_name")
231 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_project,
232 "fake_stable_goldfish_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700233 self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux")
Tri Vo29ac1822016-10-01 17:06:29 -0700234
Richard Fung97503b22019-02-06 13:43:38 -0800235 # Cheeps related
236 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_name,
237 "fake_stable_cheeps_host_image_name")
238 self.assertEqual(cfg.default_usr_cfg.stable_cheeps_host_image_project,
239 "fake_stable_cheeps_host_image_project")
240
Sam Chiu58dad6e2018-08-27 19:50:33 +0800241 # hw property
242 self.assertEqual(
243 {key: val for key, val in cfg.common_hw_property_map.iteritems()},
Sam Chiuc64f3432018-08-17 11:19:06 +0800244 {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800245
Tri Vo29ac1822016-10-01 17:06:29 -0700246 def testLoadConfigFails(self):
247 """Test loading a bad file."""
248 self.config_file.read.return_value = "malformed text"
249 with self.assertRaises(errors.ConfigError):
250 config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
251 self.config_file, internal_config_pb2.InternalConfig)
252
Sam Chiu58dad6e2018-08-27 19:50:33 +0800253 def testOverrideWithHWProperty(self):
254 """Test override hw property by flavor type."""
255 # initial config with test config.
256 self.config_file.read.return_value = self.INTERNAL_CONFIG
257 internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
258 self.config_file, internal_config_pb2.InternalConfig)
259 self.config_file.read.return_value = self.USER_CONFIG
260 usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
261 self.config_file, user_config_pb2.UserConfig)
262 cfg = config.AcloudConfig(usr_cfg, internal_cfg)
263
264 # test override with an exist flavor.
265 cfg.hw_property = None
266 args = mock.MagicMock()
267 args.flavor = "phone"
268 args.which = "create"
269 cfg.OverrideWithArgs(args)
270 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800271 "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g")
Sam Chiu58dad6e2018-08-27 19:50:33 +0800272
273 # test override with a nonexistent flavor.
274 cfg.hw_property = None
275 args = mock.MagicMock()
276 args.flavor = "non-exist-flavor"
277 args.which = "create"
278 cfg.OverrideWithArgs(args)
279 self.assertEqual(cfg.hw_property, "")
280
Tri Vo29ac1822016-10-01 17:06:29 -0700281
282if __name__ == "__main__":
283 unittest.main()