blob: e6cf6794e750facd5ac1492dc4e26de47dba395a [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"
Tri Vo29ac1822016-10-01 17:06:29 -070073 metadata_variable {
74 key: "metadata_1"
75 value: "metadata_value_1"
76 }
77
78 metadata_variable {
79 key: "metadata_2"
80 value: "metadata_value_2"
81 }
82}
83
84device_resolution_map {
85 key: "nexus5"
86 value: "1080x1920x32x480"
87}
88
89device_default_orientation_map {
90 key: "nexus5"
91 value: "portrait"
92}
93
94valid_branch_and_min_build_id {
Kevin Chengb5963882018-05-09 00:06:27 -070095 key: "aosp-master"
Tri Vo29ac1822016-10-01 17:06:29 -070096 value: 0
97}
Sam Chiu58dad6e2018-08-27 19:50:33 +080098
99common_hw_property_map {
100 key: "phone"
Sam Chiuc64f3432018-08-17 11:19:06 +0800101 value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"
Sam Chiu58dad6e2018-08-27 19:50:33 +0800102}
Tri Vo29ac1822016-10-01 17:06:29 -0700103"""
104
105 def setUp(self):
106 self.config_file = mock.MagicMock()
107
Sam Chiu46ea3112018-05-18 10:47:52 +0800108 # pylint: disable=no-member
Tri Vo29ac1822016-10-01 17:06:29 -0700109 def testLoadUserConfig(self):
110 """Test loading user config."""
111 self.config_file.read.return_value = self.USER_CONFIG
112 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
113 self.config_file, user_config_pb2.UserConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700114 self.assertEqual(cfg.service_account_name,
115 "fake@developer.gserviceaccount.com")
116 self.assertEqual(cfg.service_account_private_key_path,
117 "/path/to/service/account/key")
xingdai8a00d462018-07-30 14:24:48 -0700118 self.assertEqual(cfg.service_account_json_private_key_path,
119 "/path/to/service/account/json_key")
Kevin Chengb5963882018-05-09 00:06:27 -0700120 self.assertEqual(cfg.project, "fake-project")
121 self.assertEqual(cfg.zone, "us-central1-f")
122 self.assertEqual(cfg.machine_type, "n1-standard-1")
123 self.assertEqual(cfg.network, "default")
124 self.assertEqual(cfg.ssh_private_key_path, "/path/to/ssh/key")
125 self.assertEqual(cfg.storage_bucket_name, "fake_bucket")
126 self.assertEqual(cfg.orientation, "portrait")
127 self.assertEqual(cfg.resolution, "1200x1200x1200x1200")
128 self.assertEqual(cfg.client_id, "fake_client_id")
129 self.assertEqual(cfg.client_secret, "fake_client_secret")
Sam Chiu46ea3112018-05-18 10:47:52 +0800130 self.assertEqual(
131 {key: val for key, val in cfg.metadata_variable.iteritems()},
132 {"metadata_1": "metadata_value_1"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800133 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800134 "cpu:3,resolution:1080x1920,dpi:480,memory:4g,"
135 "disk:10g")
Tri Vo29ac1822016-10-01 17:06:29 -0700136
herbertxue18c9b262018-07-12 19:14:49 +0800137 # pylint: disable=protected-access
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700138 @mock.patch("os.makedirs")
herbertxue18c9b262018-07-12 19:14:49 +0800139 @mock.patch("os.path.exists")
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700140 def testLoadUserConfigLogic(self, mock_file_exist, _mock_makedirs):
herbertxue18c9b262018-07-12 19:14:49 +0800141 """Test load user config logic.
142
143 Load user config with some special design.
144 1. User specified user config:
145 If user config didn't exist: Raise exception.
146 2. User didn't specify user config, use default config:
147 If default config didn't exist: Initialize empty data.
148 """
149 config_specify = config.AcloudConfigManager(self.config_file)
150 self.config_file.read.return_value = self.USER_CONFIG
herbertxue34776bb2018-07-03 21:57:48 +0800151 self.assertEqual(config_specify.user_config_path, self.config_file)
herbertxue18c9b262018-07-12 19:14:49 +0800152 mock_file_exist.return_value = False
153 with self.assertRaises(errors.ConfigError):
154 config_specify.Load()
155 # Test default config
156 config_unspecify = config.AcloudConfigManager(None)
157 cfg = config_unspecify.Load()
herbertxue34776bb2018-07-03 21:57:48 +0800158 self.assertEqual(config_unspecify.user_config_path,
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700159 config.GetDefaultConfigFile())
herbertxue18c9b262018-07-12 19:14:49 +0800160 self.assertEqual(cfg.project, "")
161 self.assertEqual(cfg.zone, "")
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700162
163 # Test default user config exist
herbertxue18c9b262018-07-12 19:14:49 +0800164 mock_file_exist.return_value = True
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700165 # Write the config data into a tmp file and have GetDefaultConfigFile
166 # return that.
167 _, temp_cfg_file_path = tempfile.mkstemp()
168 with open(temp_cfg_file_path, "w") as cfg_file:
herbertxue18c9b262018-07-12 19:14:49 +0800169 cfg_file.writelines(self.USER_CONFIG)
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700170 default_patcher = mock.patch.object(config, "GetDefaultConfigFile",
171 return_value=temp_cfg_file_path)
172 default_patcher.start()
173 try:
174 config_exist = config.AcloudConfigManager(None)
175 cfg = config_exist.Load()
176 self.assertEqual(cfg.project, "fake-project")
177 self.assertEqual(cfg.zone, "us-central1-f")
178 self.assertEqual(cfg.client_id, "fake_client_id")
179 self.assertEqual(cfg.client_secret, "fake_client_secret")
180 finally:
181 # Delete tmp file
182 os.remove(temp_cfg_file_path)
183 default_patcher.stop()
herbertxue18c9b262018-07-12 19:14:49 +0800184
Tri Vo29ac1822016-10-01 17:06:29 -0700185 def testLoadInternalConfig(self):
186 """Test loading internal config."""
187 self.config_file.read.return_value = self.INTERNAL_CONFIG
188 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
189 self.config_file, internal_config_pb2.InternalConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700190 self.assertEqual(cfg.min_machine_size, "n1-standard-1")
191 self.assertEqual(cfg.disk_image_name, "avd-system.tar.gz")
192 self.assertEqual(cfg.disk_image_mime_type, "application/x-tar")
193 self.assertEqual(cfg.disk_image_extension, ".tar.gz")
194 self.assertEqual(cfg.disk_raw_image_name, "disk.raw")
195 self.assertEqual(cfg.disk_raw_image_extension, ".img")
196 self.assertEqual(cfg.creds_cache_file, ".fake_oauth2.dat")
197 self.assertEqual(cfg.user_agent, "fake_user_agent")
198 self.assertEqual(cfg.default_usr_cfg.machine_type, "n1-standard-1")
199 self.assertEqual(cfg.default_usr_cfg.network, "default")
200 self.assertEqual({
Tri Vo29ac1822016-10-01 17:06:29 -0700201 key: val
202 for key, val in cfg.default_usr_cfg.metadata_variable.iteritems()
Sam Chiu46ea3112018-05-18 10:47:52 +0800203 }, {
204 "metadata_1": "metadata_value_1",
205 "metadata_2": "metadata_value_2"
206 })
Kevin Chengb5963882018-05-09 00:06:27 -0700207 self.assertEqual(
Sam Chiu46ea3112018-05-18 10:47:52 +0800208 {key: val for key, val in cfg.device_resolution_map.iteritems()},
Tri Vo29ac1822016-10-01 17:06:29 -0700209 {"nexus5": "1080x1920x32x480"})
210 device_resolution = {
211 key: val
212 for key, val in cfg.device_default_orientation_map.iteritems()
213 }
Kevin Chengb5963882018-05-09 00:06:27 -0700214 self.assertEqual(device_resolution, {"nexus5": "portrait"})
Tri Vo29ac1822016-10-01 17:06:29 -0700215 valid_branch_and_min_build_id = {
216 key: val
217 for key, val in cfg.valid_branch_and_min_build_id.iteritems()
218 }
Kevin Chengb5963882018-05-09 00:06:27 -0700219 self.assertEqual(valid_branch_and_min_build_id, {"aosp-master": 0})
Sam Chiu46ea3112018-05-18 10:47:52 +0800220 self.assertEqual(cfg.default_usr_cfg.stable_host_image_name,
221 "fake_stable_host_image_name")
222 self.assertEqual(cfg.default_usr_cfg.stable_host_image_project,
223 "fake_stable_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700224 self.assertEqual(cfg.kernel_build_target, "kernel")
225
226 # Emulator related
Sam Chiu46ea3112018-05-18 10:47:52 +0800227 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_name,
228 "fake_stable_goldfish_host_image_name")
229 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_project,
230 "fake_stable_goldfish_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700231 self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux")
Tri Vo29ac1822016-10-01 17:06:29 -0700232
Sam Chiu58dad6e2018-08-27 19:50:33 +0800233 # hw property
234 self.assertEqual(
235 {key: val for key, val in cfg.common_hw_property_map.iteritems()},
Sam Chiuc64f3432018-08-17 11:19:06 +0800236 {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800237
Tri Vo29ac1822016-10-01 17:06:29 -0700238 def testLoadConfigFails(self):
239 """Test loading a bad file."""
240 self.config_file.read.return_value = "malformed text"
241 with self.assertRaises(errors.ConfigError):
242 config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
243 self.config_file, internal_config_pb2.InternalConfig)
244
Sam Chiu58dad6e2018-08-27 19:50:33 +0800245 def testOverrideWithHWProperty(self):
246 """Test override hw property by flavor type."""
247 # initial config with test config.
248 self.config_file.read.return_value = self.INTERNAL_CONFIG
249 internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
250 self.config_file, internal_config_pb2.InternalConfig)
251 self.config_file.read.return_value = self.USER_CONFIG
252 usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
253 self.config_file, user_config_pb2.UserConfig)
254 cfg = config.AcloudConfig(usr_cfg, internal_cfg)
255
256 # test override with an exist flavor.
257 cfg.hw_property = None
258 args = mock.MagicMock()
259 args.flavor = "phone"
260 args.which = "create"
261 cfg.OverrideWithArgs(args)
262 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800263 "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g")
Sam Chiu58dad6e2018-08-27 19:50:33 +0800264
265 # test override with a nonexistent flavor.
266 cfg.hw_property = None
267 args = mock.MagicMock()
268 args.flavor = "non-exist-flavor"
269 args.which = "create"
270 cfg.OverrideWithArgs(args)
271 self.assertEqual(cfg.hw_property, "")
272
Tri Vo29ac1822016-10-01 17:06:29 -0700273
274if __name__ == "__main__":
275 unittest.main()