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.""" 18 19import mock 20 21import unittest 22from acloud.internal.proto import internal_config_pb2 23from acloud.internal.proto import user_config_pb2 24from acloud.public import config 25from acloud.public import errors 26 27 28class AcloudConfigManagerTest(unittest.TestCase): 29 """Test acloud configuration manager.""" 30 31 USER_CONFIG = """ 32service_account_name: "fake@developer.gserviceaccount.com" 33service_account_private_key_path: "/path/to/service/account/key" 34project: "fake-project" 35zone: "us-central1-f" 36machine_type: "n1-standard-1" 37network: "default" 38ssh_private_key_path: "/path/to/ssh/key" 39storage_bucket_name: "fake_bucket" 40orientation: "portrait" 41resolution: "1200x1200x1200x1200" 42client_id: "fake_client_id" 43client_secret: "fake_client_secret" 44metadata_variable { 45 key: "metadata_1" 46 value: "metadata_value_1" 47} 48""" 49 50 INTERNAL_CONFIG = """ 51min_machine_size: "n1-standard-1" 52disk_image_name: "avd-system.tar.gz" 53disk_image_mime_type: "application/x-tar" 54disk_image_extension: ".tar.gz" 55disk_raw_image_name: "disk.raw" 56disk_raw_image_extension: ".img" 57creds_cache_file: ".fake_oauth2.dat" 58user_agent: "fake_user_agent" 59 60default_usr_cfg { 61 machine_type: "n1-standard-1" 62 network: "default" 63 metadata_variable { 64 key: "metadata_1" 65 value: "metadata_value_1" 66 } 67 68 metadata_variable { 69 key: "metadata_2" 70 value: "metadata_value_2" 71 } 72} 73 74device_resolution_map { 75 key: "nexus5" 76 value: "1080x1920x32x480" 77} 78 79device_default_orientation_map { 80 key: "nexus5" 81 value: "portrait" 82} 83 84valid_branch_and_min_build_id { 85 key: "git_jb-gce-dev" 86 value: 0 87} 88""" 89 90 def setUp(self): 91 self.config_file = mock.MagicMock() 92 93 def testLoadUserConfig(self): 94 """Test loading user config.""" 95 self.config_file.read.return_value = self.USER_CONFIG 96 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer( 97 self.config_file, user_config_pb2.UserConfig) 98 self.assertEquals(cfg.service_account_name, 99 "fake@developer.gserviceaccount.com") 100 self.assertEquals(cfg.service_account_private_key_path, 101 "/path/to/service/account/key") 102 self.assertEquals(cfg.project, "fake-project") 103 self.assertEquals(cfg.zone, "us-central1-f") 104 self.assertEquals(cfg.machine_type, "n1-standard-1") 105 self.assertEquals(cfg.network, "default") 106 self.assertEquals(cfg.ssh_private_key_path, "/path/to/ssh/key") 107 self.assertEquals(cfg.storage_bucket_name, "fake_bucket") 108 self.assertEquals(cfg.orientation, "portrait") 109 self.assertEquals(cfg.resolution, "1200x1200x1200x1200") 110 self.assertEquals(cfg.client_id, "fake_client_id") 111 self.assertEquals(cfg.client_secret, "fake_client_secret") 112 self.assertEquals({key: val 113 for key, val in cfg.metadata_variable.iteritems()}, 114 {"metadata_1": "metadata_value_1"}) 115 116 def testLoadInternalConfig(self): 117 """Test loading internal config.""" 118 self.config_file.read.return_value = self.INTERNAL_CONFIG 119 cfg = config.AcloudConfigManager.LoadConfigFromProtocolBuffer( 120 self.config_file, internal_config_pb2.InternalConfig) 121 self.assertEquals(cfg.min_machine_size, "n1-standard-1") 122 self.assertEquals(cfg.disk_image_name, "avd-system.tar.gz") 123 self.assertEquals(cfg.disk_image_mime_type, "application/x-tar") 124 self.assertEquals(cfg.disk_image_extension, ".tar.gz") 125 self.assertEquals(cfg.disk_raw_image_name, "disk.raw") 126 self.assertEquals(cfg.disk_raw_image_extension, ".img") 127 self.assertEquals(cfg.creds_cache_file, ".fake_oauth2.dat") 128 self.assertEquals(cfg.user_agent, "fake_user_agent") 129 self.assertEquals(cfg.default_usr_cfg.machine_type, "n1-standard-1") 130 self.assertEquals(cfg.default_usr_cfg.network, "default") 131 self.assertEquals({ 132 key: val 133 for key, val in cfg.default_usr_cfg.metadata_variable.iteritems() 134 }, {"metadata_1": "metadata_value_1", 135 "metadata_2": "metadata_value_2"}) 136 self.assertEquals( 137 {key: val 138 for key, val in cfg.device_resolution_map.iteritems()}, 139 {"nexus5": "1080x1920x32x480"}) 140 device_resolution = { 141 key: val 142 for key, val in cfg.device_default_orientation_map.iteritems() 143 } 144 self.assertEquals(device_resolution, {"nexus5": "portrait"}) 145 valid_branch_and_min_build_id = { 146 key: val 147 for key, val in cfg.valid_branch_and_min_build_id.iteritems() 148 } 149 self.assertEquals(valid_branch_and_min_build_id, {"git_jb-gce-dev": 0}) 150 151 def testLoadConfigFails(self): 152 """Test loading a bad file.""" 153 self.config_file.read.return_value = "malformed text" 154 with self.assertRaises(errors.ConfigError): 155 config.AcloudConfigManager.LoadConfigFromProtocolBuffer( 156 self.config_file, internal_config_pb2.InternalConfig) 157 158 159if __name__ == "__main__": 160 unittest.main() 161