|
1 from mock import patch, Mock |
|
2 from twisted.trial import unittest |
|
3 import signal |
|
4 import io |
|
5 import ConfigParser |
|
6 from iro import config, error |
|
7 from iro.offer.provider import Provider, providers |
|
8 |
|
9 class TestModuleConfig(unittest.TestCase): |
|
10 '''test config class''' |
|
11 |
|
12 def setUp(self): |
|
13 self._reloadList=config.configParser.reloadList |
|
14 config.configParser.reloadList=[] |
|
15 |
|
16 def tearDown(self): |
|
17 config.configParser.reloadList = self._reloadList |
|
18 |
|
19 @patch('iro.config.main') |
|
20 @patch('iro.config.configParser') |
|
21 def testReadConfig(self,pConfig,pMain): |
|
22 pMain._init = True |
|
23 config.readConfig() |
|
24 self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload_","items"]) |
|
25 pConfig.read.assert_called_once_with(config.confFiles) |
|
26 pConfig.items.assert_called_once_with("main") |
|
27 pConfig.reload_.assert_called_once_with() |
|
28 self.assertEqual(pMain.load.called,1) |
|
29 |
|
30 @patch('iro.config.main') |
|
31 @patch('iro.config.configParser') |
|
32 def testReadConfigInit(self,pConfig,pMain): |
|
33 pConfig.items.return_value = [('dburl',''),("port",1000)] |
|
34 pMain._init = False |
|
35 config.readConfig() |
|
36 self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload_","items"]) |
|
37 self.assertEqual([i[0] for i in pMain.method_calls],["same"]) |
|
38 sa = pMain.same.call_args_list[0][0][0] |
|
39 self.assertEqual(sa.port,1000) |
|
40 self.assertEqual(sa.dburl,'') |
|
41 pMain.same.return_value = False |
|
42 self.assertRaises(Exception,config.readConfig,) |
|
43 |
|
44 |
|
45 |
|
46 @patch('signal.signal') |
|
47 @patch('iro.config.readConfig') |
|
48 def testRegisterSignal(self, pReadConfig, pSignal): |
|
49 config.registerSignal() |
|
50 self.assertEqual(pSignal.call_args[0][0],signal.SIGUSR2) |
|
51 self.assertEqual(pReadConfig.called,0) |
|
52 pSignal.call_args[0][1](None, None) |
|
53 pReadConfig.assert_called_once_with() |
|
54 |
|
55 def testRegisterReload(self): |
|
56 def x(): |
|
57 pass |
|
58 config.configParser.registerReload(x) |
|
59 self.assertEqual(config.configParser.reloadList,[x]) |
|
60 |
|
61 def testReload(self): |
|
62 x = Mock() |
|
63 config.configParser.reloadList = [x] |
|
64 config.configParser.reload_() |
|
65 x.assert_called_once_with() |
|
66 |
|
67 |
|
68 class TestRead(unittest.TestCase): |
|
69 |
|
70 def tearDown(self): |
|
71 for s in config.configParser.sections(): |
|
72 config.configParser.remove_section(s) |
|
73 |
|
74 @patch('iro.config.ConfigParser.read') |
|
75 def testMain(self,pRead): |
|
76 sample_config = """[main] |
|
77 port = 8000 |
|
78 dburl = sdfdsafgsfdg |
|
79 """ |
|
80 config.configParser.readfp(io.BytesIO(sample_config)) |
|
81 config.configParser.read([]) |
|
82 pRead.assert_called_once_with(config.configParser,[]) |
|
83 |
|
84 @patch('iro.config.ConfigParser.read') |
|
85 def testMainBadPort(self,pRead): |
|
86 sample_config = """[main] |
|
87 port = -8000 |
|
88 dburl = sadfaserasg |
|
89 """ |
|
90 config.configParser.readfp(io.BytesIO(sample_config)) |
|
91 self.assertRaises(error.ValidateException, config.configParser.read, []) |
|
92 |
|
93 @patch('iro.config.ConfigParser.read') |
|
94 def testMainNoMust(self,pRead): |
|
95 sample_config = """[main] |
|
96 port = 8000 |
|
97 """ |
|
98 config.configParser.readfp(io.BytesIO(sample_config)) |
|
99 self.assertRaises(config.NeededOption, config.configParser.read, []) |
|
100 |
|
101 @patch('iro.config.ConfigParser.read') |
|
102 def testMust(self,pRead): |
|
103 v=Mock() |
|
104 config.main.options["test"] = config.Option(v,default="jehei") |
|
105 try: |
|
106 sample_config = """[main] |
|
107 dburl = sdfawersdf |
|
108 port = 8000 |
|
109 """ |
|
110 config.configParser.readfp(io.BytesIO(sample_config)) |
|
111 config.configParser.read([]) |
|
112 self.assertEqual(v.called,0) |
|
113 config.main.load(config.configParser.items("main")) |
|
114 self.assertEqual(config.main.test,'jehei') |
|
115 sample_config = """[main] |
|
116 dburl = adfgsdftsfg |
|
117 port = 8000 |
|
118 test = foohu |
|
119 """ |
|
120 config.configParser.readfp(io.BytesIO(sample_config)) |
|
121 config.configParser.read([]) |
|
122 v.assert_called_once_with("foohu","test") |
|
123 finally: |
|
124 del(config.main.options["test"]) |
|
125 |
|
126 @patch('iro.config.ConfigParser.read') |
|
127 def testProviders(self, pRead): |
|
128 v=Mock() |
|
129 class TestProvider(Provider): |
|
130 def __init__(self,name): |
|
131 Provider.__init__(self,name) |
|
132 self.options.update({"test":config.Option(v)}) |
|
133 providers["test"]=TestProvider |
|
134 try: |
|
135 sample_config = """[p] |
|
136 """ |
|
137 config.configParser.readfp(io.BytesIO(sample_config)) |
|
138 self.assertRaises(ConfigParser.NoOptionError, config.configParser.read, []) |
|
139 self.assertEqual(v.called,0) |
|
140 sample_config = """[p] |
|
141 typ= test |
|
142 test= foo |
|
143 """ |
|
144 config.configParser.readfp(io.BytesIO(sample_config)) |
|
145 config.configParser.read([]) |
|
146 v.assert_called_once_with("foo","test") |
|
147 finally: |
|
148 del(providers["test"]) |
|
149 |
|
150 |
|
151 class TestConfig(unittest.TestCase): |
|
152 def testSame(self): |
|
153 c1 = config.Config("1") |
|
154 c1.port = 1 |
|
155 c1.dburl = "dburl1" |
|
156 c2 = config.Config("2") |
|
157 c2.port = 1 |
|
158 c2.dburl = "dburl1" |
|
159 self.assertTrue(c1.same(c2)) |
|
160 self.assertTrue(c1.same(c1)) |
|
161 self.assertTrue(c2.same(c1)) |
|
162 c2.port = 2 |
|
163 self.assertFalse(c2.same(c1)) |
|
164 self.assertFalse(c1.same(c2)) |
|
165 c2.port = 1 |
|
166 c2.dburl = "dburl2" |
|
167 self.assertFalse(c2.same(c1)) |
|
168 self.assertFalse(c1.same(c2)) |
|
169 |
|
170 def testSampleConf(self): |
|
171 c1 = config.Config("1") |
|
172 self.assertEqual(c1.sampleConf(),["[1]", |
|
173 "# Connection URL to database", |
|
174 "dburl = ","", |
|
175 "# Port under that twisted is running", |
|
176 "port = ",""]) |
|
177 |
|
178 def testsampleConfDefault(self): |
|
179 c1 = config.Config("1") |
|
180 c1.options["port"].default = 12345 |
|
181 c1.options["port"].must = False |
|
182 c1.options["dburl"].default = True |
|
183 self.assertEqual(c1.sampleConf(),["[1]", |
|
184 "# Connection URL to database", |
|
185 "dburl = True","", |
|
186 "# Port under that twisted is running", |
|
187 "# port = 12345",""]) |