blob: 058acf29133b886d41f5251265ff3f907f17dc5c [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
Tri Vo29ac1822016-10-01 17:06:29 -070024from acloud.internal.proto import internal_config_pb2
25from acloud.internal.proto import user_config_pb2
26from acloud.public import config
27from acloud.public import errors
28
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
138 @mock.patch("os.path.exists")
139 def testLoadUserConfigLogic(self, mock_file_exist):
140 """Test load user config logic.
141
142 Load user config with some special design.
143 1. User specified user config:
144 If user config didn't exist: Raise exception.
145 2. User didn't specify user config, use default config:
146 If default config didn't exist: Initialize empty data.
147 """
148 config_specify = config.AcloudConfigManager(self.config_file)
149 self.config_file.read.return_value = self.USER_CONFIG
herbertxue34776bb2018-07-03 21:57:48 +0800150 self.assertEqual(config_specify.user_config_path, self.config_file)
herbertxue18c9b262018-07-12 19:14:49 +0800151 mock_file_exist.return_value = False
152 with self.assertRaises(errors.ConfigError):
153 config_specify.Load()
154 # Test default config
155 config_unspecify = config.AcloudConfigManager(None)
156 cfg = config_unspecify.Load()
herbertxue34776bb2018-07-03 21:57:48 +0800157 self.assertEqual(config_unspecify.user_config_path,
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700158 config.GetDefaultConfigFile())
herbertxue18c9b262018-07-12 19:14:49 +0800159 self.assertEqual(cfg.project, "")
160 self.assertEqual(cfg.zone, "")
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700161
162 # Test default user config exist
herbertxue18c9b262018-07-12 19:14:49 +0800163 mock_file_exist.return_value = True
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700164 # Write the config data into a tmp file and have GetDefaultConfigFile
165 # return that.
166 _, temp_cfg_file_path = tempfile.mkstemp()
167 with open(temp_cfg_file_path, "w") as cfg_file:
herbertxue18c9b262018-07-12 19:14:49 +0800168 cfg_file.writelines(self.USER_CONFIG)
Kevin Cheng4fce0bc2018-08-13 11:51:34 -0700169 default_patcher = mock.patch.object(config, "GetDefaultConfigFile",
170 return_value=temp_cfg_file_path)
171 default_patcher.start()
172 try:
173 config_exist = config.AcloudConfigManager(None)
174 cfg = config_exist.Load()
175 self.assertEqual(cfg.project, "fake-project")
176 self.assertEqual(cfg.zone, "us-central1-f")
177 self.assertEqual(cfg.client_id, "fake_client_id")
178 self.assertEqual(cfg.client_secret, "fake_client_secret")
179 finally:
180 # Delete tmp file
181 os.remove(temp_cfg_file_path)
182 default_patcher.stop()
herbertxue18c9b262018-07-12 19:14:49 +0800183
Tri Vo29ac1822016-10-01 17:06:29 -0700184 def testLoadInternalConfig(self):
185 """Test loading internal config."""
186 self.config_file.read.return_value = self.INTERNAL_CONFIG
187 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
188 self.config_file, internal_config_pb2.InternalConfig)
Kevin Chengb5963882018-05-09 00:06:27 -0700189 self.assertEqual(cfg.min_machine_size, "n1-standard-1")
190 self.assertEqual(cfg.disk_image_name, "avd-system.tar.gz")
191 self.assertEqual(cfg.disk_image_mime_type, "application/x-tar")
192 self.assertEqual(cfg.disk_image_extension, ".tar.gz")
193 self.assertEqual(cfg.disk_raw_image_name, "disk.raw")
194 self.assertEqual(cfg.disk_raw_image_extension, ".img")
195 self.assertEqual(cfg.creds_cache_file, ".fake_oauth2.dat")
196 self.assertEqual(cfg.user_agent, "fake_user_agent")
197 self.assertEqual(cfg.default_usr_cfg.machine_type, "n1-standard-1")
198 self.assertEqual(cfg.default_usr_cfg.network, "default")
199 self.assertEqual({
Tri Vo29ac1822016-10-01 17:06:29 -0700200 key: val
201 for key, val in cfg.default_usr_cfg.metadata_variable.iteritems()
Sam Chiu46ea3112018-05-18 10:47:52 +0800202 }, {
203 "metadata_1": "metadata_value_1",
204 "metadata_2": "metadata_value_2"
205 })
Kevin Chengb5963882018-05-09 00:06:27 -0700206 self.assertEqual(
Sam Chiu46ea3112018-05-18 10:47:52 +0800207 {key: val for key, val in cfg.device_resolution_map.iteritems()},
Tri Vo29ac1822016-10-01 17:06:29 -0700208 {"nexus5": "1080x1920x32x480"})
209 device_resolution = {
210 key: val
211 for key, val in cfg.device_default_orientation_map.iteritems()
212 }
Kevin Chengb5963882018-05-09 00:06:27 -0700213 self.assertEqual(device_resolution, {"nexus5": "portrait"})
Tri Vo29ac1822016-10-01 17:06:29 -0700214 valid_branch_and_min_build_id = {
215 key: val
216 for key, val in cfg.valid_branch_and_min_build_id.iteritems()
217 }
Kevin Chengb5963882018-05-09 00:06:27 -0700218 self.assertEqual(valid_branch_and_min_build_id, {"aosp-master": 0})
Sam Chiu46ea3112018-05-18 10:47:52 +0800219 self.assertEqual(cfg.default_usr_cfg.stable_host_image_name,
220 "fake_stable_host_image_name")
221 self.assertEqual(cfg.default_usr_cfg.stable_host_image_project,
222 "fake_stable_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700223 self.assertEqual(cfg.kernel_build_target, "kernel")
224
225 # Emulator related
Sam Chiu46ea3112018-05-18 10:47:52 +0800226 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_name,
227 "fake_stable_goldfish_host_image_name")
228 self.assertEqual(cfg.default_usr_cfg.stable_goldfish_host_image_project,
229 "fake_stable_goldfish_host_image_project")
Kevin Chengb5963882018-05-09 00:06:27 -0700230 self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux")
Tri Vo29ac1822016-10-01 17:06:29 -0700231
Sam Chiu58dad6e2018-08-27 19:50:33 +0800232 # hw property
233 self.assertEqual(
234 {key: val for key, val in cfg.common_hw_property_map.iteritems()},
Sam Chiuc64f3432018-08-17 11:19:06 +0800235 {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"})
Sam Chiu58dad6e2018-08-27 19:50:33 +0800236
Tri Vo29ac1822016-10-01 17:06:29 -0700237 def testLoadConfigFails(self):
238 """Test loading a bad file."""
239 self.config_file.read.return_value = "malformed text"
240 with self.assertRaises(errors.ConfigError):
241 config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
242 self.config_file, internal_config_pb2.InternalConfig)
243
Sam Chiu58dad6e2018-08-27 19:50:33 +0800244 def testOverrideWithHWProperty(self):
245 """Test override hw property by flavor type."""
246 # initial config with test config.
247 self.config_file.read.return_value = self.INTERNAL_CONFIG
248 internal_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
249 self.config_file, internal_config_pb2.InternalConfig)
250 self.config_file.read.return_value = self.USER_CONFIG
251 usr_cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer(
252 self.config_file, user_config_pb2.UserConfig)
253 cfg = config.AcloudConfig(usr_cfg, internal_cfg)
254
255 # test override with an exist flavor.
256 cfg.hw_property = None
257 args = mock.MagicMock()
258 args.flavor = "phone"
259 args.which = "create"
260 cfg.OverrideWithArgs(args)
261 self.assertEqual(cfg.hw_property,
Sam Chiuc64f3432018-08-17 11:19:06 +0800262 "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g")
Sam Chiu58dad6e2018-08-27 19:50:33 +0800263
264 # test override with a nonexistent flavor.
265 cfg.hw_property = None
266 args = mock.MagicMock()
267 args.flavor = "non-exist-flavor"
268 args.which = "create"
269 cfg.OverrideWithArgs(args)
270 self.assertEqual(cfg.hw_property, "")
271
Tri Vo29ac1822016-10-01 17:06:29 -0700272
273if __name__ == "__main__":
274 unittest.main()