virt.virt_test_utils: run_autotest - 'tar' needs relative paths to strip the leading '/'
[autotest-zwu.git] / client / common_lib / global_config_unittest.py
blobb3ad857c8e88b5be58d573be9a80bf52a396e78a
1 #!/usr/bin/python
3 import os, sys, unittest, types
4 import common
5 from autotest_lib.client.common_lib import global_config
6 from autotest_lib.client.common_lib import autotemp
9 global_config_ini_contents = """
10 [SECTION_A]
11 value_1: 6.0
12 value_2: hello
13 value_3: true
14 value_4: FALSE
15 value_5: tRuE
16 value_6: falsE
18 [SECTION_B]
19 value_1: -5
20 value_2: 2.3
21 value_3: 0
22 value_4: 7
24 [SECTION_C]
25 value_1: nobody@localhost
26 """
28 shadow_config_ini_contents = """
29 [SECTION_C]
30 value_1: somebody@remotehost
31 """
34 def create_config_files():
35 global_temp = autotemp.tempfile("global", ".ini",
36 text=True)
37 os.write(global_temp.fd, global_config_ini_contents)
39 shadow_temp = autotemp.tempfile("shadow", ".ini",
40 text=True)
41 fd = shadow_temp.fd
42 os.write(shadow_temp.fd, shadow_config_ini_contents)
44 return (global_temp, shadow_temp)
47 class global_config_test(unittest.TestCase):
48 # grab the singelton
49 conf = global_config.global_config
51 def setUp(self):
52 # set the config files to our test files
53 (self.global_temp, self.shadow_temp) = create_config_files()
55 self.conf.set_config_files(self.global_temp.name, self.shadow_temp.name)
58 def tearDown(self):
59 self.shadow_temp.clean()
60 self.global_temp.clean()
61 self.conf.set_config_files(global_config.DEFAULT_CONFIG_FILE,
62 global_config.DEFAULT_SHADOW_FILE)
65 def test_float(self):
66 val = self.conf.get_config_value("SECTION_A", "value_1", float)
67 self.assertEquals(type(val), types.FloatType)
68 self.assertEquals(val, 6.0)
71 def test_int(self):
72 val = self.conf.get_config_value("SECTION_B", "value_1", int)
73 self.assertEquals(type(val), types.IntType)
74 self.assertTrue(val < 0)
75 val = self.conf.get_config_value("SECTION_B", "value_3", int)
76 self.assertEquals(val, 0)
77 val = self.conf.get_config_value("SECTION_B", "value_4", int)
78 self.assertTrue(val > 0)
81 def test_string(self):
82 val = self.conf.get_config_value("SECTION_A", "value_2")
83 self.assertEquals(type(val),types.StringType)
84 self.assertEquals(val, "hello")
87 def test_override(self):
88 val = self.conf.get_config_value("SECTION_C", "value_1")
89 self.assertEquals(val, "somebody@remotehost")
92 def test_exception(self):
93 error = 0
94 try:
95 val = self.conf.get_config_value("SECTION_B",
96 "value_2", int)
97 except:
98 error = 1
99 self.assertEquals(error, 1)
102 def test_boolean(self):
103 val = self.conf.get_config_value("SECTION_A", "value_3", bool)
104 self.assertEquals(val, True)
105 val = self.conf.get_config_value("SECTION_A", "value_4", bool)
106 self.assertEquals(val, False)
107 val = self.conf.get_config_value("SECTION_A", "value_5", bool)
108 self.assertEquals(val, True)
109 val = self.conf.get_config_value("SECTION_A", "value_6", bool)
110 self.assertEquals(val, False)
113 def test_defaults(self):
114 val = self.conf.get_config_value("MISSING", "foo", float, 3.6)
115 self.assertEquals(val, 3.6)
116 val = self.conf.get_config_value("SECTION_A", "novalue", str,
117 "default")
118 self.assertEquals(val, "default")
121 # this is so the test can be run in standalone mode
122 if __name__ == '__main__':
123 unittest.main()