tests/config.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 23 Feb 2012 16:58:58 +0100
branchdevel
changeset 184 6b0ff82dff81
parent 179 af65fcbd59d5
child 186 b381eaa774ab
permissions -rw-r--r--
testing Must argument and Provider section

from mock import patch, Mock
from twisted.trial import unittest
import signal
import io
import ConfigParser
from  iro import config 
from iro.offer.provider import Provider, providers

class TestConfig(unittest.TestCase):
    '''test config class'''
    
    def setUp(self):
        self._reloadList=config.config.reloadList
        config.config.reloadList=[]

    def tearDown(self):
        config.config.reloadList = self._reloadList

    @patch('iro.config.config')
    def testReadConfig(self,pConfig):
        config.readConfig()
        self.assertEqual([i[0] for i in pConfig.method_calls],["read","reload"])
        pConfig.read.assert_called_once_with(config.confFiles)
        pConfig.reload.assert_called_once_with()

    @patch('signal.signal')
    @patch('iro.config.readConfig')
    def testRegisterSignal(self, pReadConfig, pSignal):
        config.registerSignal()
        self.assertEqual(pSignal.call_args[0][0],signal.SIGUSR2)
        self.assertEqual(pReadConfig.called,0)
        pSignal.call_args[0][1](None, None)
        pReadConfig.assert_called_once_with()

    def testRegisterReload(self):
        def x():
            pass
        config.config.registerReload(x)
        self.assertEqual(config.config.reloadList,[x])

    def testReload(self):
        x = Mock()
        config.config.reloadList = [x] 
        config.config.reload_()
        x.assert_called_once_with()


class TestRead(unittest.TestCase):

    def tearDown(self):
        for s in config.config.sections():
            config.config.remove_section(s)

    @patch('iro.config.ConfigParser.read')
    def testMain(self,pRead):
        sample_config = """[main]
hostname = localhost
port = 8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        config.config.read([])
        pRead.assert_called_once_with(config.config,[])

    @patch('iro.config.ConfigParser.read')
    def testMainBadPort(self,pRead):
        sample_config = """[main]
hostname = localhost
port = -8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        self.assertRaises(config.ValidateException, config.config.read, [])

    @patch('iro.config.ConfigParser.read')
    def testMainNoMust(self,pRead):
        sample_config = """[main]
port = 8000
"""
        config.config.readfp(io.BytesIO(sample_config))
        self.assertRaises(ConfigParser.NoOptionError, config.config.read, [])

    @patch('iro.config.ConfigParser.read')
    def testMust(self,pRead):
        v=Mock()
        config.main["test"] = config.Option(v)
        try:
            sample_config = """[main]
hostname = localhost
port = 8000
"""
            config.config.readfp(io.BytesIO(sample_config))
            config.config.read([])
            self.assertEqual(v.called,0)
            sample_config = """[main]
hostname = localhost
port = 8000
test = foohu
    """
            config.config.readfp(io.BytesIO(sample_config))
            config.config.read([])
            v.assert_called_once_with("foohu","test")
        finally:
            del(config.main["test"])

    @patch('iro.config.ConfigParser.read')
    def testProviders(self, pRead):
        v=Mock()
        class TestProvider(Provider):
            def __init__(self,name,c):
                Provider.__init__(self,name,c)
                self.options.update({"test":config.Option(v)})
                self.loadConfig()
        providers["test"]=TestProvider
        try:
            sample_config = """[p]
"""
            config.config.readfp(io.BytesIO(sample_config))
            self.assertRaises(ConfigParser.NoOptionError, config.config.read, [])  
            self.assertEqual(v.called,0)
            sample_config = """[p]
typ= test
test= foo
"""
            config.config.readfp(io.BytesIO(sample_config))
            config.config.read([])
            v.assert_called_once_with("foo","test")
        finally:
            del(providers["test"])