Exemple #1
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"
Exemple #2
0
def test_privileged_configuration():
    from ginkgo.runner import Runner
    from ginkgo import config
    import os

    uids = {}

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

    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 ginkgo.core
        class Service(ginkgo.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
Exemple #3
0
def test_named_global_services():
    from ginkgo 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 ginkgo.core
        class MainService(ginkgo.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'] = ginkgo.core.Service('named')
                lookup['named2'] = ginkgo.core.Service('named2')
                lookup['foo'] = ginkgo.core.Service('foo')
                expected[self.name].append('start')
            
            def do_stop(self):
                print "asked to do_stop"
                expected[self.name].append('stop')

        class GS(ginkgo.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
Exemple #4
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"
Exemple #5
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"