예제 #1
0
def test_memory_worker_run_snapshot_with_image_and_callback(
        mock_docker_client):
    worker = Default.get_employee('worker')
    worker.callback = dummy_callback_function
    assert isinstance(worker.messenger, MemoryMessenger)
    code = Code(add2, image='dummy')
    assert code.get_state() == 'UNKNOWN'
    sn = worker.run_snapshot(code.to_snapshot(kwargs={'a': 3, 'b': 5}))
    assert sn == code.serial_number
    default_config_dir = Config.get_default_config_dir()
    script_dir = os.path.join(default_config_dir, 'scripts')
    mock_docker_client.return_value.containers.run.assert_called_once_with(
        auto_remove=True,
        command=[
            'python', 'run_snapshot.py',
            '%s.json' % sn, '-p', '.', '-l', 'worker-logger', '-c',
            'dummy_callback_function'
        ],
        dns=['8.8.8.8'],
        environment=['CODEPACK_LOGGER_LOG_DIR=/usr/logs'],
        image='dummy',
        name=id(worker.docker_manager),
        volumes=[
            '%s:/usr/src/codepack' % script_dir,
            '%s:/usr/logs' % os.path.abspath(Config.get_log_dir())
        ],
        working_dir='/usr/src/codepack')
    worker.stop()
예제 #2
0
def test_config_path_priority():
    try:
        config = Config()
        _config = config.get_config(section='logger', ignore_error=True)
        assert _config == {
            'log_dir':
            'logs',
            'name':
            'default-logger',
            'config_path':
            os.path.join(config.get_default_config_dir(), 'logging.json')
        }
        with pytest.raises(AssertionError):
            Config(config_path='test.ini')
        os.environ['CODEPACK_CONFIG_DIR'] = 'config'
        config = Config(config_path='test.ini')
        assert config.config_path == os.path.join('config', 'test.ini')
        _config = config.get_config(section='logger')
        ref = {
            'name': 'default-logger',
            'config_path': 'logging.json',
            'log_dir': 'logs'
        }
        assert _config == ref
        os.environ['CODEPACK_CONFIG_PATH'] = 'codepack.ini'
        _config = config.get_config(section='logger')
        assert _config == ref
        _config = config.get_config(section='logger', config_path='test.ini')
        assert _config == ref
    finally:
        os.environ.pop('CODEPACK_CONFIG_PATH', None)
        os.environ.pop('CODEPACK_CONFIG_DIR', None)
예제 #3
0
def test_collect_values_without_anything():
    os_envs = {
        'CODEPACK_WORKER_LOGGER': 'dummy-logger',
        'CODEPACK_WORKER_DUMMY': 'dummy_value'
    }
    try:
        for k, v in os_envs.items():
            os.environ[k] = v
        config = Config()
        ret = config.get_config('worker')
        assert ret == {
            'dummy':
            'dummy_value',
            'background':
            'True',
            'interval':
            '1',
            'logger':
            'dummy-logger',
            'source':
            'memory',
            'topic':
            'codepack',
            'script_path':
            os.path.join(config.get_default_config_dir(),
                         'scripts/run_snapshot.py')
        }
    finally:
        for k in os_envs.keys():
            os.environ.pop(k, None)
예제 #4
0
def test_config_get_value_priority():
    try:
        config = Config()
        _config = config.get_config(section='logger')
        assert _config == {
            'name':
            'default-logger',
            'log_dir':
            'logs',
            'config_path':
            os.path.join(config.get_default_config_dir(), 'logging.json')
        }
        default_value = Config.collect_value(section='logger',
                                             key='name',
                                             config=dict())
        assert default_value == 'default-logger'
        name = Config.collect_value(section='logger',
                                    key='name',
                                    config=_config)
        assert name == 'default-logger'
        os.environ['CODEPACK_LOGGER_NAME'] = 'test-logger'
        name = Config.collect_value(section='logger',
                                    key='name',
                                    config=_config)
        assert name == 'test-logger'
        os.environ.pop('CODEPACK_LOGGER_NAME')
        os.environ['CODEPACK_CONFIG_PATH'] = 'config/test.ini'
        os.environ['CODEPACK_CONFIG_DIR'] = 'config'
        config = Config()
        _config = config.get_config(section='logger')
        assert _config == {
            'name': 'default-logger',
            'config_path': os.path.join('config', 'logging.json'),
            'log_dir': 'logs'
        }
        name = Config.collect_value(section='logger',
                                    key='name',
                                    config=_config)
        assert name == 'default-logger'
        os.environ['CODEPACK_LOGGER_NAME'] = 'test-logger'
        name = Config.collect_value(section='logger',
                                    key='name',
                                    config=_config)
        assert name == 'test-logger'
    finally:
        os.environ.pop('CODEPACK_LOGGER_NAME', None)
        os.environ.pop('CODEPACK_CONFIG_PATH', None)
        os.environ.pop('CODEPACK_CONFIG_DIR', None)
예제 #5
0
def test_memory_worker_run_snapshot_with_env(mock_subprocess_run):
    worker = Default.get_employee('worker')
    assert isinstance(worker.messenger, MemoryMessenger)
    code = Code(add2, env='test_env', image='dummy')
    assert code.get_state() == 'UNKNOWN'
    sn = worker.run_snapshot(code.to_snapshot(kwargs={'a': 3, 'b': 5}))
    assert sn == code.serial_number
    default_config_dir = Config.get_default_config_dir()
    script_dir = os.path.join(default_config_dir, 'scripts')
    mock_subprocess_run.assert_called_once_with([
        os.path.join(worker.interpreter_manager.path, 'test_env', 'bin',
                     'python'),
        os.path.join(script_dir, 'run_snapshot.py'),
        os.path.join(script_dir, '%s.json' % sn), '-p', script_dir, '-l',
        'worker-logger'
    ])
    worker.stop()