1 from mock import patch, Mock |
1 from mock import patch, Mock |
2 from twisted.trial import unittest |
2 from twisted.trial import unittest |
3 import signal |
3 import signal |
4 import io |
4 import io |
5 import ConfigParser |
5 import ConfigParser |
6 from iro import config |
6 from iro import config, error |
7 from iro.offer.provider import Provider, providers |
7 from iro.offer.provider import Provider, providers |
8 |
8 |
9 class TestConfig(unittest.TestCase): |
9 class TestConfig(unittest.TestCase): |
10 '''test config class''' |
10 '''test config class''' |
11 |
11 |
12 def setUp(self): |
12 def setUp(self): |
13 self._reloadList=config.config.reloadList |
13 self._reloadList=config.configParser.reloadList |
14 config.config.reloadList=[] |
14 config.configParser.reloadList=[] |
15 |
15 |
16 def tearDown(self): |
16 def tearDown(self): |
17 config.config.reloadList = self._reloadList |
17 config.configParser.reloadList = self._reloadList |
18 |
18 |
19 @patch('iro.config.config') |
19 @patch('iro.config.main') |
20 def testReadConfig(self,pConfig): |
20 @patch('iro.config.configParser') |
|
21 def testReadConfig(self,pConfig,pMain): |
|
22 pMain.read = False |
21 config.readConfig() |
23 config.readConfig() |
22 self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload"]) |
24 self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload","items"]) |
23 pConfig.read.assert_called_once_with(config.confFiles) |
25 pConfig.read.assert_called_once_with(config.confFiles) |
|
26 pConfig.items.assert_called_once_with("main") |
24 pConfig.reload.assert_called_once_with() |
27 pConfig.reload.assert_called_once_with() |
|
28 self.assertEqual(pMain.load.called,1) |
25 |
29 |
26 @patch('signal.signal') |
30 @patch('signal.signal') |
27 @patch('iro.config.readConfig') |
31 @patch('iro.config.readConfig') |
28 def testRegisterSignal(self, pReadConfig, pSignal): |
32 def testRegisterSignal(self, pReadConfig, pSignal): |
29 config.registerSignal() |
33 config.registerSignal() |
33 pReadConfig.assert_called_once_with() |
37 pReadConfig.assert_called_once_with() |
34 |
38 |
35 def testRegisterReload(self): |
39 def testRegisterReload(self): |
36 def x(): |
40 def x(): |
37 pass |
41 pass |
38 config.config.registerReload(x) |
42 config.configParser.registerReload(x) |
39 self.assertEqual(config.config.reloadList,[x]) |
43 self.assertEqual(config.configParser.reloadList,[x]) |
40 |
44 |
41 def testReload(self): |
45 def testReload(self): |
42 x = Mock() |
46 x = Mock() |
43 config.config.reloadList = [x] |
47 config.configParser.reloadList = [x] |
44 config.config.reload_() |
48 config.configParser.reload_() |
45 x.assert_called_once_with() |
49 x.assert_called_once_with() |
46 |
50 |
47 |
51 |
48 class TestRead(unittest.TestCase): |
52 class TestRead(unittest.TestCase): |
49 |
53 |
50 def tearDown(self): |
54 def tearDown(self): |
51 for s in config.config.sections(): |
55 for s in config.configParser.sections(): |
52 config.config.remove_section(s) |
56 config.configParser.remove_section(s) |
53 |
57 |
54 @patch('iro.config.ConfigParser.read') |
58 @patch('iro.config.ConfigParser.read') |
55 def testMain(self,pRead): |
59 def testMain(self,pRead): |
56 sample_config = """[main] |
60 sample_config = """[main] |
57 hostname = localhost |
61 hostname = localhost |
58 port = 8000 |
62 port = 8000 |
|
63 dburl = sdfdsafgsfdg |
59 """ |
64 """ |
60 config.config.readfp(io.BytesIO(sample_config)) |
65 config.configParser.readfp(io.BytesIO(sample_config)) |
61 config.config.read([]) |
66 config.configParser.read([]) |
62 pRead.assert_called_once_with(config.config,[]) |
67 pRead.assert_called_once_with(config.configParser,[]) |
63 |
68 |
64 @patch('iro.config.ConfigParser.read') |
69 @patch('iro.config.ConfigParser.read') |
65 def testMainBadPort(self,pRead): |
70 def testMainBadPort(self,pRead): |
66 sample_config = """[main] |
71 sample_config = """[main] |
67 hostname = localhost |
72 hostname = localhost |
68 port = -8000 |
73 port = -8000 |
|
74 dburl = sadfaserasg |
69 """ |
75 """ |
70 config.config.readfp(io.BytesIO(sample_config)) |
76 config.configParser.readfp(io.BytesIO(sample_config)) |
71 self.assertRaises(config.ValidateException, config.config.read, []) |
77 self.assertRaises(error.ValidateException, config.configParser.read, []) |
72 |
78 |
73 @patch('iro.config.ConfigParser.read') |
79 @patch('iro.config.ConfigParser.read') |
74 def testMainNoMust(self,pRead): |
80 def testMainNoMust(self,pRead): |
75 sample_config = """[main] |
81 sample_config = """[main] |
76 port = 8000 |
82 port = 8000 |
|
83 dburl = asdfgdsrg |
77 """ |
84 """ |
78 config.config.readfp(io.BytesIO(sample_config)) |
85 config.configParser.readfp(io.BytesIO(sample_config)) |
79 self.assertRaises(ConfigParser.NoOptionError, config.config.read, []) |
86 self.assertRaises(config.NeededOption, config.configParser.read, []) |
80 |
87 |
81 @patch('iro.config.ConfigParser.read') |
88 @patch('iro.config.ConfigParser.read') |
82 def testMust(self,pRead): |
89 def testMust(self,pRead): |
83 v=Mock() |
90 v=Mock() |
84 config.main["test"] = config.Option(v) |
91 config.main.options["test"] = config.Option(v) |
85 try: |
92 try: |
86 sample_config = """[main] |
93 sample_config = """[main] |
87 hostname = localhost |
94 hostname = localhost |
|
95 dburl = sdfawersdf |
88 port = 8000 |
96 port = 8000 |
89 """ |
97 """ |
90 config.config.readfp(io.BytesIO(sample_config)) |
98 config.configParser.readfp(io.BytesIO(sample_config)) |
91 config.config.read([]) |
99 config.configParser.read([]) |
92 self.assertEqual(v.called,0) |
100 self.assertEqual(v.called,0) |
93 sample_config = """[main] |
101 sample_config = """[main] |
94 hostname = localhost |
102 hostname = localhost |
|
103 dburl = adfgsdftsfg |
95 port = 8000 |
104 port = 8000 |
96 test = foohu |
105 test = foohu |
97 """ |
106 """ |
98 config.config.readfp(io.BytesIO(sample_config)) |
107 config.configParser.readfp(io.BytesIO(sample_config)) |
99 config.config.read([]) |
108 config.configParser.read([]) |
100 v.assert_called_once_with("foohu","test") |
109 v.assert_called_once_with("foohu","test") |
101 finally: |
110 finally: |
102 del(config.main["test"]) |
111 del(config.main.options["test"]) |
103 |
112 |
104 @patch('iro.config.ConfigParser.read') |
113 @patch('iro.config.ConfigParser.read') |
105 def testProviders(self, pRead): |
114 def testProviders(self, pRead): |
106 v=Mock() |
115 v=Mock() |
107 class TestProvider(Provider): |
116 class TestProvider(Provider): |
108 def __init__(self,name,c): |
117 def __init__(self,name): |
109 Provider.__init__(self,name,c) |
118 Provider.__init__(self,name) |
110 self.options.update({"test":config.Option(v)}) |
119 self.options.update({"test":config.Option(v)}) |
111 self.loadConfig() |
|
112 providers["test"]=TestProvider |
120 providers["test"]=TestProvider |
113 try: |
121 try: |
114 sample_config = """[p] |
122 sample_config = """[p] |
115 """ |
123 """ |
116 config.config.readfp(io.BytesIO(sample_config)) |
124 config.configParser.readfp(io.BytesIO(sample_config)) |
117 self.assertRaises(ConfigParser.NoOptionError, config.config.read, []) |
125 self.assertRaises(ConfigParser.NoOptionError, config.configParser.read, []) |
118 self.assertEqual(v.called,0) |
126 self.assertEqual(v.called,0) |
119 sample_config = """[p] |
127 sample_config = """[p] |
120 typ= test |
128 typ= test |
121 test= foo |
129 test= foo |
122 """ |
130 """ |
123 config.config.readfp(io.BytesIO(sample_config)) |
131 config.configParser.readfp(io.BytesIO(sample_config)) |
124 config.config.read([]) |
132 config.configParser.read([]) |
125 v.assert_called_once_with("foo","test") |
133 v.assert_called_once_with("foo","test") |
126 finally: |
134 finally: |
127 del(providers["test"]) |
135 del(providers["test"]) |