Ejemplo n.º 1
0
def test_service_generator():

    import gservice.core

    class MyService(gservice.core.Service):
        pass

    expected_children = [('hi', MyService()), ('also named', MyService())]
    expected_main = MyService()

    def service():
        for name, child in expected_children:
            yield name, child
        yield expected_main

    from gservice.runner import Runner

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

    runner = Runner()

    service_gen = service()

    children, main_service = runner._expand_service_generators(service_gen)

    print children, main_service
    assert children == expected_children
    assert main_service == expected_main
Ejemplo n.º 2
0
def test_service_generator():
    
    import gservice.core
    class MyService(gservice.core.Service): pass

    expected_children = [('hi', MyService()), ('also named', MyService())]
    expected_main = MyService()

    def service():
        for name, child in expected_children:
            yield name, child
        yield expected_main
        
    from gservice.runner import Runner
    
    Runner._args = ['run', '-C', 'config', '-u', 'nobody']
    Runner._opener = mock_open({"config": ""})

    runner = Runner()
    
    service_gen = service()
        
    children, main_service = runner._expand_service_generators(service_gen)

    print children, main_service
    assert children == expected_children
    assert main_service == expected_main
Ejemplo n.º 3
0
def test_runner_no_daemonize():
    from gservice.runner import Runner
    class EmptyRunner(Runner):
        def run(self): pass
    EmptyRunner._args = ['start', '-C', 'config', '-n']
    EmptyRunner._opener = mock_open({"config": "", "serviced.log": "", "serviced.pid": ""})
    with silencer():
        EmptyRunner()
Ejemplo n.º 4
0
def get_runner():
    from gservice.runner import Runner
    
    Runner._args = ['run', '-C', 'config', '-u', 'nobody']
    Runner._opener = mock_open({"config": ""})

    runner = Runner()
    return runner
Ejemplo n.º 5
0
def get_runner():
    from gservice.runner import Runner

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

    runner = Runner()
    return runner
Ejemplo n.º 6
0
def test_extend_file_config_option():
    from gservice.runner import Runner
    from gservice.config import Setting

    class ConfigCheckRunner(Runner):
        foo = Setting('foo')
        def run(self):
            assert self.foo == 'bar2'

    ConfigCheckRunner._args = ['run', '-C', 'config1', '-X', "foo = 'bar2'"]
    ConfigCheckRunner._opener = mock_open({"config1": "foo = 'bar1'"})

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 7
0
def test_command_line_override_config():
    """ Test that specifying an option on the command line override any
        value set in a config file"""
    from gservice.runner import Runner

    class ConfigCheckRunner(Runner):
        def run(self):
            assert self.pidfile_path == 'mypidfile.pid'

    ConfigCheckRunner._args = ['run', '-C', 'config', '-p', 'mypidfile.pid']
    ConfigCheckRunner._opener = mock_open({"config": "pidfile = 'configpidfile.pid'"})

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def test_read_config_option():
    from gservice.runner import Runner
    from gservice.config import Option

    class ConfigCheckRunner(Runner):
        foo = Option('foo')
        def run(self):
            assert self.foo == 'bar'

    ConfigCheckRunner._args = ['run', '-C', 'config']
    ConfigCheckRunner._opener = mock_open({"config": "foo = 'bar'"})

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 10
0
def test_command_line_override_config():
    """ Test that specifying an option on the command line override any
        value set in a config file"""
    from gservice.runner import Runner

    class ConfigCheckRunner(Runner):
        def run(self):
            assert self.pidfile_path == 'mypidfile.pid'

    ConfigCheckRunner._args = ['run', '-C', 'config', '-p', 'mypidfile.pid']
    ConfigCheckRunner._opener = mock_open(
        {"config": "pidfile = 'configpidfile.pid'"})

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 11
0
def test_read_config_option():
    from gservice.runner import Runner
    from gservice.config import Setting

    class ConfigCheckRunner(Runner):
        foo = Setting('foo')

        def run(self):
            assert self.foo == 'bar'

    ConfigCheckRunner._args = ['run', '-C', 'config']
    ConfigCheckRunner._opener = mock_open({"config": "foo = 'bar'"})

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 12
0
def test_runner_no_daemonize():
    from gservice.runner import Runner

    class EmptyRunner(Runner):
        def run(self):
            pass

    EmptyRunner._args = ['start', '-C', 'config', '-n']
    EmptyRunner._opener = mock_open({
        "config": "",
        "serviced.log": "",
        "serviced.pid": ""
    })
    with silencer():
        EmptyRunner()
Ejemplo n.º 13
0
def test_extend_file_config_option():
    from gservice.runner import Runner
    from gservice.config import Setting

    class ConfigCheckRunner(Runner):
        foo = Setting('foo')

        def run(self):
            assert self.foo == 'bar2'

    ConfigCheckRunner._args = ['run', '-C', 'config1', '-X', 'config2']
    ConfigCheckRunner._opener = mock_open({
        "config1": "foo = 'bar1'",
        "config2": "foo = 'bar2'"
    })

    with silencer():
        ConfigCheckRunner().do_action()
Ejemplo n.º 14
0
def test_runner_reads_config():
    from gservice.runner import Runner
    Runner._args = ['start', '-C', 'config']
    Runner._opener = mock_open({"config": "", "serviced.log": ""})
    with silencer():
        Runner()
Ejemplo n.º 15
0
def test_runner_reads_config():
    from gservice.runner import Runner
    Runner._args = ['start', '-C', 'config']
    Runner._opener = mock_open({"config": "", "serviced.log": ""})
    with silencer():
        Runner()
Ejemplo n.º 16
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