3 import os
, sys
, unittest
, types
5 from autotest_lib
.client
.common_lib
import global_config
6 from autotest_lib
.client
.common_lib
import autotemp
9 global_config_ini_contents
= """
25 value_1: nobody@localhost
28 shadow_config_ini_contents
= """
30 value_1: somebody@remotehost
34 def create_config_files():
35 global_temp
= autotemp
.tempfile("global", ".ini",
37 os
.write(global_temp
.fd
, global_config_ini_contents
)
39 shadow_temp
= autotemp
.tempfile("shadow", ".ini",
42 os
.write(shadow_temp
.fd
, shadow_config_ini_contents
)
44 return (global_temp
, shadow_temp
)
47 class global_config_test(unittest
.TestCase
):
49 conf
= global_config
.global_config
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
)
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
)
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)
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
):
95 val
= self
.conf
.get_config_value("SECTION_B",
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,
118 self
.assertEquals(val
, "default")
121 # this is so the test can be run in standalone mode
122 if __name__
== '__main__':