Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 1 | #!/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 Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 18 | import unittest |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 19 | import os |
Kevin Cheng | 4fce0bc | 2018-08-13 11:51:34 -0700 | [diff] [blame] | 20 | import tempfile |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 21 | import mock |
| 22 | |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 23 | # pylint: disable=no-name-in-module,import-error |
Sam Chiu | 7de3b23 | 2018-12-06 19:45:52 +0800 | [diff] [blame] | 24 | from acloud import errors |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 25 | from acloud.internal.proto import internal_config_pb2 |
| 26 | from acloud.internal.proto import user_config_pb2 |
| 27 | from acloud.public import config |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class AcloudConfigManagerTest(unittest.TestCase): |
| 31 | """Test acloud configuration manager.""" |
| 32 | |
| 33 | USER_CONFIG = """ |
| 34 | service_account_name: "fake@developer.gserviceaccount.com" |
| 35 | service_account_private_key_path: "/path/to/service/account/key" |
xingdai | 8a00d46 | 2018-07-30 14:24:48 -0700 | [diff] [blame] | 36 | service_account_json_private_key_path: "/path/to/service/account/json_key" |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 37 | project: "fake-project" |
| 38 | zone: "us-central1-f" |
| 39 | machine_type: "n1-standard-1" |
| 40 | network: "default" |
| 41 | ssh_private_key_path: "/path/to/ssh/key" |
| 42 | storage_bucket_name: "fake_bucket" |
| 43 | orientation: "portrait" |
| 44 | resolution: "1200x1200x1200x1200" |
| 45 | client_id: "fake_client_id" |
| 46 | client_secret: "fake_client_secret" |
| 47 | metadata_variable { |
| 48 | key: "metadata_1" |
| 49 | value: "metadata_value_1" |
| 50 | } |
Sam Chiu | c64f343 | 2018-08-17 11:19:06 +0800 | [diff] [blame] | 51 | hw_property: "cpu:3,resolution:1080x1920,dpi:480,memory:4g,disk:10g" |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 52 | """ |
| 53 | |
| 54 | INTERNAL_CONFIG = """ |
| 55 | min_machine_size: "n1-standard-1" |
| 56 | disk_image_name: "avd-system.tar.gz" |
| 57 | disk_image_mime_type: "application/x-tar" |
| 58 | disk_image_extension: ".tar.gz" |
| 59 | disk_raw_image_name: "disk.raw" |
| 60 | disk_raw_image_extension: ".img" |
| 61 | creds_cache_file: ".fake_oauth2.dat" |
| 62 | user_agent: "fake_user_agent" |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 63 | kernel_build_target: "kernel" |
| 64 | emulator_build_target: "sdk_tools_linux" |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 65 | |
| 66 | default_usr_cfg { |
| 67 | machine_type: "n1-standard-1" |
| 68 | network: "default" |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 69 | 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 Fung | 97503b2 | 2019-02-06 13:43:38 -0800 | [diff] [blame^] | 73 | stable_cheeps_host_image_name: "fake_stable_cheeps_host_image_name" |
| 74 | stable_cheeps_host_image_project: "fake_stable_cheeps_host_image_project" |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 75 | 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 | |
| 86 | device_resolution_map { |
| 87 | key: "nexus5" |
| 88 | value: "1080x1920x32x480" |
| 89 | } |
| 90 | |
| 91 | device_default_orientation_map { |
| 92 | key: "nexus5" |
| 93 | value: "portrait" |
| 94 | } |
| 95 | |
| 96 | valid_branch_and_min_build_id { |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 97 | key: "aosp-master" |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 98 | value: 0 |
| 99 | } |
Sam Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 100 | |
| 101 | common_hw_property_map { |
| 102 | key: "phone" |
Sam Chiu | c64f343 | 2018-08-17 11:19:06 +0800 | [diff] [blame] | 103 | value: "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g" |
Sam Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 104 | } |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 105 | """ |
| 106 | |
| 107 | def setUp(self): |
| 108 | self.config_file = mock.MagicMock() |
| 109 | |
Sam Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 110 | # pylint: disable=no-member |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 111 | 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 Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 116 | 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") |
xingdai | 8a00d46 | 2018-07-30 14:24:48 -0700 | [diff] [blame] | 120 | self.assertEqual(cfg.service_account_json_private_key_path, |
| 121 | "/path/to/service/account/json_key") |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 122 | 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 Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 132 | self.assertEqual( |
| 133 | {key: val for key, val in cfg.metadata_variable.iteritems()}, |
| 134 | {"metadata_1": "metadata_value_1"}) |
Sam Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 135 | self.assertEqual(cfg.hw_property, |
Sam Chiu | c64f343 | 2018-08-17 11:19:06 +0800 | [diff] [blame] | 136 | "cpu:3,resolution:1080x1920,dpi:480,memory:4g," |
| 137 | "disk:10g") |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 138 | |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 139 | # pylint: disable=protected-access |
Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 140 | @mock.patch("os.makedirs") |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 141 | @mock.patch("os.path.exists") |
Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 142 | def testLoadUserConfigLogic(self, mock_file_exist, _mock_makedirs): |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 143 | """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 |
herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 153 | self.assertEqual(config_specify.user_config_path, self.config_file) |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 154 | 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() |
herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 160 | self.assertEqual(config_unspecify.user_config_path, |
Kevin Cheng | 4fce0bc | 2018-08-13 11:51:34 -0700 | [diff] [blame] | 161 | config.GetDefaultConfigFile()) |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 162 | self.assertEqual(cfg.project, "") |
| 163 | self.assertEqual(cfg.zone, "") |
Kevin Cheng | 4fce0bc | 2018-08-13 11:51:34 -0700 | [diff] [blame] | 164 | |
| 165 | # Test default user config exist |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 166 | mock_file_exist.return_value = True |
Kevin Cheng | 4fce0bc | 2018-08-13 11:51:34 -0700 | [diff] [blame] | 167 | # 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: |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 171 | cfg_file.writelines(self.USER_CONFIG) |
Kevin Cheng | 4fce0bc | 2018-08-13 11:51:34 -0700 | [diff] [blame] | 172 | 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() |
herbertxue | 18c9b26 | 2018-07-12 19:14:49 +0800 | [diff] [blame] | 186 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 187 | 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 Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 192 | 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 Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 203 | key: val |
| 204 | for key, val in cfg.default_usr_cfg.metadata_variable.iteritems() |
Sam Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 205 | }, { |
| 206 | "metadata_1": "metadata_value_1", |
| 207 | "metadata_2": "metadata_value_2" |
| 208 | }) |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 209 | self.assertEqual( |
Sam Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 210 | {key: val for key, val in cfg.device_resolution_map.iteritems()}, |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 211 | {"nexus5": "1080x1920x32x480"}) |
| 212 | device_resolution = { |
| 213 | key: val |
| 214 | for key, val in cfg.device_default_orientation_map.iteritems() |
| 215 | } |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 216 | self.assertEqual(device_resolution, {"nexus5": "portrait"}) |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 217 | valid_branch_and_min_build_id = { |
| 218 | key: val |
| 219 | for key, val in cfg.valid_branch_and_min_build_id.iteritems() |
| 220 | } |
Kevin Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 221 | self.assertEqual(valid_branch_and_min_build_id, {"aosp-master": 0}) |
Sam Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 222 | 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 Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 226 | self.assertEqual(cfg.kernel_build_target, "kernel") |
| 227 | |
| 228 | # Emulator related |
Sam Chiu | 46ea311 | 2018-05-18 10:47:52 +0800 | [diff] [blame] | 229 | 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 Cheng | b596388 | 2018-05-09 00:06:27 -0700 | [diff] [blame] | 233 | self.assertEqual(cfg.emulator_build_target, "sdk_tools_linux") |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 234 | |
Richard Fung | 97503b2 | 2019-02-06 13:43:38 -0800 | [diff] [blame^] | 235 | # 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 Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 241 | # hw property |
| 242 | self.assertEqual( |
| 243 | {key: val for key, val in cfg.common_hw_property_map.iteritems()}, |
Sam Chiu | c64f343 | 2018-08-17 11:19:06 +0800 | [diff] [blame] | 244 | {"phone": "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g"}) |
Sam Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 245 | |
Tri Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 246 | 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 Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 253 | 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 Chiu | c64f343 | 2018-08-17 11:19:06 +0800 | [diff] [blame] | 271 | "cpu:2,resolution:1080x1920,dpi:420,memory:4g,disk:8g") |
Sam Chiu | 58dad6e | 2018-08-27 19:50:33 +0800 | [diff] [blame] | 272 | |
| 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 Vo | 29ac182 | 2016-10-01 17:06:29 -0700 | [diff] [blame] | 281 | |
| 282 | if __name__ == "__main__": |
| 283 | unittest.main() |