1#!/usr/bin/python 2 3import collections 4import os 5import mox 6import types 7import unittest 8 9import common 10from autotest_lib.client.common_lib import autotemp 11from autotest_lib.client.common_lib import global_config 12from autotest_lib.client.common_lib import lsbrelease_utils 13 14 15global_config_ini_contents = """ 16[SECTION_A] 17value_1: 6.0 18value_2: hello 19value_3: true 20value_4: FALSE 21value_5: tRuE 22value_6: falsE 23 24[SECTION_B] 25value_1: -5 26value_2: 2.3 27value_3: 0 28value_4: 7 29 30[SECTION_C] 31value_1: nobody@localhost 32 33[SECTION_D] 34value_1: 1 35 36[SECTION_E] 37value_1: 1 38value_2: 2 39value_a: A 40random: 1 41wireless_ssid_1.2.3.4/24: ssid_1 42wireless_ssid_4.3.2.1/16: ssid_2 43""" 44 45moblab_config_ini_contents = """ 46[SECTION_C] 47value_1: moblab@remotehost 48 49[SECTION_D] 50value_1: 2 51""" 52 53shadow_config_ini_contents = """ 54[SECTION_C] 55value_1: somebody@remotehost 56""" 57 58 59def create_config_files(): 60 """Create config files to be used for test.""" 61 global_temp = autotemp.tempfile("global", ".ini", text=True) 62 os.write(global_temp.fd, global_config_ini_contents) 63 64 moblab_temp = autotemp.tempfile("moblab", ".ini", text=True) 65 os.write(moblab_temp.fd, moblab_config_ini_contents) 66 67 shadow_temp = autotemp.tempfile("shadow", ".ini", text=True) 68 os.write(shadow_temp.fd, shadow_config_ini_contents) 69 70 return (global_temp, shadow_temp, moblab_temp) 71 72 73class global_config_test(mox.MoxTestBase): 74 """Test class""" 75 # grab the singelton 76 conf = global_config.global_config 77 78 def setUp(self): 79 """Setup config files for test.""" 80 super(global_config_test, self).setUp() 81 # set the config files to our test files 82 (self.global_temp, self.shadow_temp, 83 self.moblab_temp) = create_config_files() 84 85 self.conf.set_config_files(self.global_temp.name, self.shadow_temp.name, 86 self.moblab_temp.name) 87 88 89 def tearDown(self): 90 """Cleanup and reset config settings.""" 91 self.shadow_temp.clean() 92 self.moblab_temp.clean() 93 self.global_temp.clean() 94 self.conf.set_config_files(global_config.DEFAULT_CONFIG_FILE, 95 global_config.DEFAULT_SHADOW_FILE, 96 global_config.DEFAULT_MOBLAB_FILE) 97 98 99 def test_float(self): 100 """Test converting float value.""" 101 val = self.conf.get_config_value("SECTION_A", "value_1", float) 102 self.assertEquals(type(val), types.FloatType) 103 self.assertEquals(val, 6.0) 104 105 106 def test_int(self): 107 """Test converting int value.""" 108 val = self.conf.get_config_value("SECTION_B", "value_1", int) 109 self.assertEquals(type(val), types.IntType) 110 self.assertTrue(val < 0) 111 val = self.conf.get_config_value("SECTION_B", "value_3", int) 112 self.assertEquals(val, 0) 113 val = self.conf.get_config_value("SECTION_B", "value_4", int) 114 self.assertTrue(val > 0) 115 116 117 def test_string(self): 118 """Test converting string value.""" 119 val = self.conf.get_config_value("SECTION_A", "value_2") 120 self.assertEquals(type(val),types.StringType) 121 self.assertEquals(val, "hello") 122 123 124 def setIsMoblab(self, is_moblab): 125 """Set lsbrelease_utils.is_moblab result. 126 127 @param is_moblab: Value to have lsbrelease_utils.is_moblab to return. 128 """ 129 self.mox.StubOutWithMock(lsbrelease_utils, 'is_moblab') 130 lsbrelease_utils.is_moblab().AndReturn(is_moblab) 131 132 133 def test_override_non_moblab(self): 134 """Test value overriding works in non-moblab setup.""" 135 self.setIsMoblab(False) 136 self.mox.ReplayAll() 137 138 self.conf.reset_config_values() 139 140 # Confirm shadow config overrides global config. 141 val = self.conf.get_config_value("SECTION_C", "value_1") 142 self.assertEquals(val, "somebody@remotehost") 143 144 # Confirm moblab config should be ignored in non-moblab environment.. 145 val = self.conf.get_config_value("SECTION_D", "value_1") 146 self.assertEquals(val, "1") 147 148 149 def test_override_moblab(self): 150 """Test value overriding works in moblab setup.""" 151 self.setIsMoblab(True) 152 self.mox.ReplayAll() 153 154 self.conf.reset_config_values() 155 156 # Confirm shadow config overrides both moblab and global config. 157 val = self.conf.get_config_value("SECTION_C", "value_1") 158 self.assertEquals(val, "somebody@remotehost") 159 160 # Confirm moblab config should override global config in moblab. 161 val = self.conf.get_config_value("SECTION_D", "value_1") 162 self.assertEquals(val, "2") 163 164 165 def test_exception(self): 166 """Test exception to be raised on invalid config value.""" 167 error = 0 168 try: 169 val = self.conf.get_config_value("SECTION_B", "value_2", int) 170 except: 171 error = 1 172 self.assertEquals(error, 1) 173 174 175 def test_boolean(self): 176 """Test converting boolean value.""" 177 val = self.conf.get_config_value("SECTION_A", "value_3", bool) 178 self.assertEquals(val, True) 179 val = self.conf.get_config_value("SECTION_A", "value_4", bool) 180 self.assertEquals(val, False) 181 val = self.conf.get_config_value("SECTION_A", "value_5", bool) 182 self.assertEquals(val, True) 183 val = self.conf.get_config_value("SECTION_A", "value_6", bool) 184 self.assertEquals(val, False) 185 186 187 def test_defaults(self): 188 """Test default value works.""" 189 val = self.conf.get_config_value("MISSING", "foo", float, 3.6) 190 self.assertEquals(val, 3.6) 191 val = self.conf.get_config_value("SECTION_A", "novalue", str, "default") 192 self.assertEquals(val, "default") 193 194 195 def test_fallback_key(self): 196 """Test fallback value works.""" 197 val = self.conf.get_config_value_with_fallback( 198 "SECTION_A", "value_3", "value_4", bool) 199 self.assertEquals(val, True) 200 201 val = self.conf.get_config_value_with_fallback( 202 "SECTION_A", "not_existing", "value_4", bool) 203 self.assertEquals(val, False) 204 205 val = self.conf.get_config_value_with_fallback( 206 "SECTION_A", "not_existing", "value_4", 207 fallback_section='SECTION_B') 208 self.assertEquals(val, '7') 209 210 self.assertRaises( 211 Exception, self.conf.get_config_value_with_fallback, 212 "SECTION_A", "not_existing", "also_not_existing", bool) 213 214 215 def test_get_config_value_regex(self): 216 """Test get_config_value_regex works.""" 217 configs = self.conf.get_config_value_regex('SECTION_E', 'value_\d+', 218 int) 219 self.assertEquals(configs, {'value_1': 1, 'value_2': 2}) 220 configs = self.conf.get_config_value_regex('SECTION_E', 'value_.*') 221 self.assertEquals(configs, {'value_1': '1', 'value_2': '2', 222 'value_a': 'A'}) 223 configs = self.conf.get_config_value_regex('SECTION_E', 224 'wireless_ssid_.*') 225 self.assertEquals(configs, {'wireless_ssid_1.2.3.4/24': 'ssid_1', 226 'wireless_ssid_4.3.2.1/16': 'ssid_2'}) 227 228 229 def test_get_section_as_dict(self): 230 """Test get_section_as_dict.""" 231 got = self.conf.get_section_as_dict('SECTION_D') 232 self.assertEqual(got, collections.OrderedDict([('value_1', '1')])) 233 234 235 def test_get_section_as_dict_missing(self): 236 """Test get_section_as_dict with missing section.""" 237 got = self.conf.get_section_as_dict('FOOBAR') 238 self.assertEqual(got, collections.OrderedDict()) 239 240 241# this is so the test can be run in standalone mode 242if __name__ == '__main__': 243 """Main""" 244 unittest.main() 245