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