示例#1
0
def test_configuration_namespaces():
    class Foo(object):
        bar = config.Option('foo.bar')
    config.load(dict(
        foo = config.Namespace(
            bar = 'foo')
    ))
    assert Foo.bar == 'foo', "Namespaced value not accessible"
示例#2
0
def test_value_changed():
    class Foo(object):
        bar = config.Option('bar')
    config.load(dict(bar='bar'))
    assert config.changed(Foo, 'bar'), "Value should have changed after initial load"
    assert not config.changed(Foo, 'bar'), "Value should not have changed after initial check"
    config.load(dict(bar='foo'))
    assert config.changed(Foo, 'bar'), "Value should have changed after setting"
    assert not config.changed(Foo, 'bar'), "Value should not have changed after previous check"
示例#3
0
def test_value_changed():
    class Foo(object):
        bar = config.Setting('bar')

    config.load(dict(bar='bar'))
    assert config.changed(
        Foo, 'bar'), "Value should have changed after initial load"
    assert not config.changed(
        Foo, 'bar'), "Value should not have changed after initial check"
    config.load(dict(bar='foo'))
    assert config.changed(Foo,
                          'bar'), "Value should have changed after setting"
    assert not config.changed(
        Foo, 'bar'), "Value should not have changed after previous check"
示例#4
0
def test_privileged_configuration():
    from gservice.runner import Runner
    from gservice import config

    uids = {}

    Runner._args = ['run', '-C', 'config', '-u', 'nobody']
    Runner._opener = mock_open({"config": ""})

    runner = Runner()

    # mock out the os getuid/setuid modules
    import os
    props = {'uid': 0, 'gid': 0}
    def getuid(): return props['uid']
    def setuid(uid): props['uid'] = uid
    def getgid(): return props['gid']
    def setgid(gid): props['gid'] = gid

    os.getuid = getuid
    os.setuid = setuid
    os.getgid = getgid
    os.setgid = setgid

    # capture the uid of the service at start and stop time
    def service():
        import gservice.core
        class Service(gservice.core.Service):
            def do_start(self):
                print "asked to do_start"
                uids['start'] = os.getuid()

                def stop(): self.stop()
                gevent.spawn_later(0.1, stop)
            
            def do_stop(self):
                print "asked to do_stop"
                uids['stop'] = os.getuid()
                print "done stopping"

        return Service()

    config.load({'service': service,
                 '_allow_early_gevent_import_for_tests': True})

    print "before action"
    runner.do_action()
    
    assert uids['start'] != runner.uid
    assert uids['stop'] == runner.uid
示例#5
0
def test_configuration_namespaces():
    class Foo(object):
        bar = config.Setting('foo.bar')

    config.load(dict(foo=config.Namespace(bar='foo')))
    assert Foo.bar == 'foo', "Namespaced value not accessible"
示例#6
0
def test_basic_load_and_read_from_option():
    class Foo(object):
        bar = config.Setting('bar')

    config.load(dict(bar='foo'))
    assert Foo.bar == 'foo', "Option value not set properly"
示例#7
0
def test_basic_load_and_read_from_option():
    class Foo(object):
        bar = config.Option('bar')
    config.load(dict(bar='foo'))
    assert Foo.bar == 'foo', "Option value not set properly"
示例#8
0
def test_named_global_services():
    from gservice import config
    from collections import defaultdict
    expected = defaultdict(list)
    lookup = {}


    import os
    props = {'uid': 0, 'gid': 0}
    def getuid(): return props['uid']
    def setuid(uid): props['uid'] = uid
    def getgid(): return props['gid']
    def setgid(gid): props['gid'] = gid

    os.getuid = getuid
    os.setuid = setuid
    os.getgid = getgid
    os.setgid = setgid

    def service():
        import gservice.core
        class MainService(gservice.core.Service):
            def __init__(self, name):
                self.name = name
                
            def do_start(self):
                print "main service starting"
                gevent.spawn_later(0.1, self.stop)
                lookup['named'] = gservice.core.Service('named')
                lookup['named2'] = gservice.core.Service('named2')
                lookup['foo'] = gservice.core.Service('foo')
                expected[self.name].append('start')
            
            def do_stop(self):
                print "asked to do_stop"
                expected[self.name].append('stop')

        class GS(gservice.core.Service):

            def __init__(self, name):
                self.name = name
                
            def do_start(self):
                print self.name, "starting"
                expected[self.name].append('start')

            def do_stop(self):
                print self.name, "stopping"
                expected[self.name].append('stop')

        yield 'named', GS('named')
        yield 'named2', GS('named2')

        yield MainService('main')


    runner = get_runner()

    config.load({'service': service,
                 '_allow_early_gevent_import_for_tests': True})

    print 'do action'
    runner.do_action()

    print 'after action'
    print expected
    print lookup
    for name in 'named', 'named2':
        assert lookup[name].name == name
        assert expected[name] == ['start', 'stop']

    assert expected['main'] == ['start', 'stop']

    assert lookup['foo'].value is None
示例#9
0
def test_named_global_services():
    from gservice import config
    from collections import defaultdict
    expected = defaultdict(list)
    lookup = {}

    import os
    props = {'uid': 0, 'gid': 0}

    def getuid():
        return props['uid']

    def setuid(uid):
        props['uid'] = uid

    def getgid():
        return props['gid']

    def setgid(gid):
        props['gid'] = gid

    os.getuid = getuid
    os.setuid = setuid
    os.getgid = getgid
    os.setgid = setgid

    def service():
        import gservice.core

        class MainService(gservice.core.Service):
            def __init__(self, name):
                self.name = name

            def do_start(self):
                print "main service starting"
                gevent.spawn_later(0.1, self.stop)
                lookup['named'] = gservice.core.Service('named')
                lookup['named2'] = gservice.core.Service('named2')
                lookup['foo'] = gservice.core.Service('foo')
                expected[self.name].append('start')

            def do_stop(self):
                print "asked to do_stop"
                expected[self.name].append('stop')

        class GS(gservice.core.Service):
            def __init__(self, name):
                self.name = name

            def do_start(self):
                print self.name, "starting"
                expected[self.name].append('start')

            def do_stop(self):
                print self.name, "stopping"
                expected[self.name].append('stop')

        yield 'named', GS('named')
        yield 'named2', GS('named2')

        yield MainService('main')

    runner = get_runner()

    config.load({
        'service': service,
        '_allow_early_gevent_import_for_tests': True
    })

    print 'do action'
    runner.do_action()

    print 'after action'
    print expected
    print lookup
    for name in 'named', 'named2':
        assert lookup[name].name == name
        assert expected[name] == ['start', 'stop']

    assert expected['main'] == ['start', 'stop']

    assert lookup['foo'].value is None
示例#10
0
def test_privileged_configuration():
    from gservice.runner import Runner
    from gservice import config

    uids = {}

    Runner._args = ['run', '-C', 'config', '-u', 'nobody']
    Runner._opener = mock_open({"config": ""})

    runner = Runner()

    # mock out the os getuid/setuid modules
    import os
    props = {'uid': 0, 'gid': 0}

    def getuid():
        return props['uid']

    def setuid(uid):
        props['uid'] = uid

    def getgid():
        return props['gid']

    def setgid(gid):
        props['gid'] = gid

    os.getuid = getuid
    os.setuid = setuid
    os.getgid = getgid
    os.setgid = setgid

    # capture the uid of the service at start and stop time
    def service():
        import gservice.core

        class Service(gservice.core.Service):
            def do_start(self):
                print "asked to do_start"
                uids['start'] = os.getuid()

                def stop():
                    self.stop()

                gevent.spawn_later(0.1, stop)

            def do_stop(self):
                print "asked to do_stop"
                uids['stop'] = os.getuid()
                print "done stopping"

        return Service()

    config.load({
        'service': service,
        '_allow_early_gevent_import_for_tests': True
    })

    print "before action"
    runner.do_action()

    assert uids['start'] != runner.uid
    assert uids['stop'] == runner.uid