Esempio n. 1
0
def test_get_current_log_paths(monkeypatch, scalyr_key_file, config, result):
    patch_env(monkeypatch, scalyr_key_file, ENVS[0])

    mock_open, mock_fp = patch_open(monkeypatch)

    load = MagicMock(side_effect=[config])
    monkeypatch.setattr('json.load', load)

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False, True])
    monkeypatch.setattr('os.path.exists', exists)

    get_template = MagicMock()
    monkeypatch.setattr(env, 'get_template', get_template)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })

    assert_agent(agent)

    res = agent._get_current_log_paths()

    assert res == result

    mock_open.assert_called_with(os.path.join(agent.config_path))
Esempio n. 2
0
def test_remove_log_target(monkeypatch, scalyr_key_file, exc):
    patch_env(monkeypatch, scalyr_key_file, ENVS[0])
    patch_os(monkeypatch)

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False, True])
    monkeypatch.setattr('os.path.exists', exists)

    rmtree = MagicMock()
    if exc:
        rmtree.side_effect = exc
    monkeypatch.setattr('shutil.rmtree', rmtree)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })

    assert_agent(agent)

    container_id = 'container-1'
    agent.remove_log_target(container_id)

    rmtree.assert_called_with(os.path.join(agent.dest_path, container_id))
Esempio n. 3
0
def test_add_log_target_no_src(monkeypatch, env, fx_scalyr):
    patch_os(monkeypatch)
    patch_env(monkeypatch, env)

    target = fx_scalyr['target']

    exists = MagicMock()
    exists.side_effect = (True, True, False)
    monkeypatch.setattr('os.path.exists', exists)

    agent = ScalyrAgent(CLUSTER_ID, load_template)

    assert_agent(agent, env)

    agent.add_log_target(target)

    assert agent.logs == []
Esempio n. 4
0
def test_add_log_target(monkeypatch, scalyr_env, fx_scalyr):
    target = fx_scalyr['target']
    kwargs = fx_scalyr['kwargs']

    assert_fx_sanity(kwargs)

    # adjust kwargs
    kwargs['monitor_journald'] = SCALYR_MONITOR_JOURNALD if os.environ.get(
        'WATCHER_SCALYR_JOURNALD') else {}

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False, True])
    monkeypatch.setattr('os.path.exists', exists)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    current_targets = MagicMock(return_value=set())
    monkeypatch.setattr(ScalyrAgent, '_get_current_log_paths', current_targets)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })
    assert_agent(agent)

    mock_open, mock_fp = patch_open(monkeypatch)

    with agent:
        agent.add_log_target(target)
        if kwargs['logs'][0]['attributes']['parser'] == SCALYR_DEFAULT_PARSER:
            assert 'parser' not in agent.logs[target['id']]['attributes']
        else:
            assert agent.logs[target['id']]['attributes']['parser'] == kwargs[
                'logs'][0]['attributes']['parser']

    log_path = kwargs['logs'][0]['path']

    makedirs.assert_called_with(os.path.dirname(log_path))
    symlink.assert_called_with(target['kwargs']['log_file_path'], log_path)

    mock_open.assert_called_with(agent.config_path, 'w')
    mock_fp.write.assert_called_once()

    assert agent.first_run is False
Esempio n. 5
0
def test_add_log_target_with_sampling(monkeypatch, scalyr_env, fx_scalyr):
    target = fx_scalyr['target']

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False])
    monkeypatch.setattr('os.path.exists', exists)

    patch_os(monkeypatch)

    agent = ScalyrAgent({
        'cluster_id':
        CLUSTER_ID,
        'scalyr_sampling_rules': [
            {
                'application':
                'app-2',
                'value':
                '[{"container": "app-1-container-1", "sampling-rules":[{ "match_expression": "WARNING", "sampling_rate": 0 }]}]',  # noqa: E501
            },
            {
                'application':
                'app-1',
                'component':
                'main',
                'value':
                '[{"container": "app-1-container-1", "sampling-rules":[{ "match_expression": "INFO", "sampling_rate": 0 }]}]',  # noqa: E501
            },
            {
                'application':
                'app-1',
                'value':
                '[{"container": "app-1-container-1", "sampling-rules":[{ "match_expression": "DEBUG", "sampling_rate": 0 }]}]',  # noqa: E501
            },
        ],
    })
    assert_agent(agent)

    agent.add_log_target(target)

    assert agent.logs[target['id']]['sampling_rules'] == [{
        'match_expression': 'INFO',
        'sampling_rate': 0
    }]
Esempio n. 6
0
def test_parse_scalyr_sampling_rules(monkeypatch, scalyr_env, fx_scalyr):
    patch_os(monkeypatch)

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[False])
    monkeypatch.setattr('os.path.exists', exists)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
        'scalyr_sampling_rules': SCALYR_SAMPLING_RULES,
    })

    assert agent.scalyr_sampling_rules == [
        {
            'application': 'app-1',
            'component': 'comp-1',
            'probability': 0,
            'value': '{"annotation": 1}'
        },
        {
            'application': 'app-1',
            'component': 'comp-1',
            'probability': 0.5,
            'value': '{"annotation": 2}'
        },
        {
            'application': 'app-1',
            'component': 'comp-2',
            'probability': 1,
            'value': '{"annotation": 3}'
        },
        {
            'application': 'app-1',
            'value': '{"annotation": 4}'
        },
        {
            'application': 'app-1',
            'component': 'comp-3',
            'value': '{"annotation": 6}'
        },
        {
            'application': 'app-2',
            'component': 'comp-1',
            'value': '{"annotation": 7}'
        },
        {
            'component': 'comp-5',
            'value': '{"annotation": 8}'
        },
        {
            'application': 'app-2',
            'value': '{"annotation": 9}'
        },
    ]
Esempio n. 7
0
def test_initialization_failure(monkeypatch, env, exists):
    patch_env(monkeypatch, env)
    patch_os(monkeypatch)

    exists = MagicMock()
    exists.side_effect = exists
    monkeypatch.setattr('os.path.exists', exists)

    with pytest.raises(RuntimeError):
        ScalyrAgent(CLUSTER_ID, load_template)
Esempio n. 8
0
def test_add_log_target_no_change(monkeypatch, env, fx_scalyr):
    patch_env(monkeypatch, env)

    target = fx_scalyr['target']
    kwargs = fx_scalyr['kwargs']

    assert_fx_sanity(kwargs)

    # adjust kwargs
    kwargs['monitor_journald'] = {} if not env.get(
        'WATCHER_SCALYR_JOURNALD') else SCALYR_MONITOR_JOURNALD

    exists = MagicMock()
    exists.side_effect = (True, True, True, False, False, True)
    monkeypatch.setattr('os.path.exists', exists)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    log_path = kwargs['logs'][0]['path']

    # targets did not change
    current_targets = MagicMock()
    current_targets.return_value = [log_path]
    monkeypatch.setattr(ScalyrAgent, '_get_current_log_paths', current_targets)

    agent = ScalyrAgent(CLUSTER_ID, load_template)

    assert_agent(agent, env)

    mock_open, mock_fp = patch_open(monkeypatch)

    # assuming not the first run
    agent._first_run = False

    with agent:
        agent.add_log_target(target)

    makedirs.assert_called_with(os.path.dirname(log_path))
    symlink.assert_called_with(target['kwargs']['log_file_path'], log_path)

    mock_fp.write.assert_not_called()

    assert agent.first_run is False
Esempio n. 9
0
def test_add_log_target_no_src(monkeypatch, scalyr_env, fx_scalyr):
    patch_os(monkeypatch)

    target = fx_scalyr['target']

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[False])
    monkeypatch.setattr('os.path.exists', exists)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })

    assert_agent(agent)

    agent.add_log_target(target)

    assert agent.logs == {}
Esempio n. 10
0
def test_remove_log_target(monkeypatch, env, exc):
    patch_os(monkeypatch)
    patch_env(monkeypatch, env)

    exists = MagicMock()
    exists.side_effect = (True, True, True, False, False, True)
    monkeypatch.setattr('os.path.exists', exists)

    rmtree = MagicMock()
    if exc:
        rmtree.side_effect = exc
    monkeypatch.setattr('shutil.rmtree', rmtree)

    agent = ScalyrAgent(CLUSTER_ID, load_template)

    assert_agent(agent, env)

    container_id = 'container-1'
    agent.remove_log_target(container_id)

    rmtree.assert_called_with(os.path.join(agent.dest_path, container_id))
Esempio n. 11
0
def test_initialization_failure(monkeypatch, scalyr_key_file, env, isdir):
    patch_env(monkeypatch, scalyr_key_file, env)
    patch_os(monkeypatch)

    isdir = MagicMock(side_effect=isdir)
    monkeypatch.setattr('os.path.isdir', isdir)

    with pytest.raises(RuntimeError):
        ScalyrAgent({
            'cluster_id': CLUSTER_ID,
            'scalyr_sampling_rules': None,
        })
Esempio n. 12
0
def test_flush_failure(monkeypatch, scalyr_env, fx_scalyr):
    target = fx_scalyr['target']
    kwargs = fx_scalyr['kwargs']

    assert_fx_sanity(kwargs)

    # adjust kwargs
    kwargs['monitor_journald'] = SCALYR_MONITOR_JOURNALD if os.environ.get(
        'WATCHER_SCALYR_JOURNALD') else {}

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False, True])
    monkeypatch.setattr('os.path.exists', exists)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    log_path = kwargs['logs'][0]['path']

    current_targets = MagicMock(return_value=set())
    monkeypatch.setattr(ScalyrAgent, '_get_current_log_paths', current_targets)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })

    assert_agent(agent)

    mock_open, mock_fp = patch_open(monkeypatch, Exception)

    with agent:
        agent.add_log_target(target)

    makedirs.assert_called_with(os.path.dirname(log_path))
    symlink.assert_called_with(target['kwargs']['log_file_path'], log_path)

    assert agent.first_run is False
Esempio n. 13
0
def test_get_current_log_paths(monkeypatch, env, config, result):
    patch_env(monkeypatch, env)
    mock_open, mock_fp = patch_open(monkeypatch)

    load = MagicMock()
    load.return_value = config
    monkeypatch.setattr('json.load', load)

    exists = MagicMock()
    exists.side_effect = (True, True, True, False, False, True)
    monkeypatch.setattr('os.path.exists', exists)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    agent = ScalyrAgent(CLUSTER_ID, load_template)

    assert_agent(agent, env)

    res = agent._get_current_log_paths()

    assert res == result

    mock_open.assert_called_with(os.path.join(agent.config_path))
Esempio n. 14
0
def test_add_log_target_no_change(monkeypatch, scalyr_env, fx_scalyr):
    target = fx_scalyr['target']
    kwargs = fx_scalyr['kwargs']

    assert_fx_sanity(kwargs)

    # adjust kwargs
    kwargs['monitor_journald'] = SCALYR_MONITOR_JOURNALD if os.environ.get(
        'WATCHER_SCALYR_JOURNALD') else {}

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[True, False, False, True])
    monkeypatch.setattr('os.path.exists', exists)

    makedirs, symlink, listdir = patch_os(monkeypatch)

    log_path = kwargs['logs'][0]['path']

    # targets did not change
    current_targets = MagicMock(return_value={log_path})
    monkeypatch.setattr(ScalyrAgent, '_get_current_log_paths', current_targets)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
    })

    assert_agent(agent)

    mock_open, mock_fp = patch_open(monkeypatch)

    # assuming not the first run
    agent._first_run = False
    agent.api_key = SCALYR_KEY
    mock_fp.read.side_effect = lambda: SCALYR_KEY

    with agent:
        agent.add_log_target(target)

    makedirs.assert_called_with(os.path.dirname(log_path))
    symlink.assert_called_with(target['kwargs']['log_file_path'], log_path)

    mock_fp.write.assert_not_called()

    assert agent.first_run is False
Esempio n. 15
0
def test_get_scalyr_sampling_rule(monkeypatch, scalyr_env, fx_scalyr):
    patch_os(monkeypatch)

    isdir = MagicMock(side_effect=[True, True])
    monkeypatch.setattr('os.path.isdir', isdir)

    exists = MagicMock(side_effect=[False])
    monkeypatch.setattr('os.path.exists', exists)

    agent = ScalyrAgent({
        'cluster_id': CLUSTER_ID,
        'scalyr_sampling_rules': SCALYR_SAMPLING_RULES,
    })

    # Component is not applied because of `probability`
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-1',
        'component':
        'comp-1',
        'container_id':
        '472de5194b88bc3302721ac28dcfe3a9fdc58350d0a8dcafab2f24683bca50f8',
    })
    assert rule == '{"annotation": 2}'

    # Component is applied because of `probability`
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-1',
        'component':
        'comp-1',
        'container_id':
        'f2c88e81c4a4dd91023e5725bae3f743caef6e6bd727e255c7c6949a8bf56978',
    })
    assert rule == '{"annotation": 4}'

    # Get rule for component
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-1',
        'component':
        'comp-2',
        'container_id':
        '472de5194b88bc3302721ac28dcfe3a9fdc58350d0a8dcafab2f24683bca50f8',
    })
    assert rule == '{"annotation": 3}'

    # Component rule is lower than application rule - use application's one
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-1',
        'component':
        'comp-3',
        'container_id':
        '472de5194b88bc3302721ac28dcfe3a9fdc58350d0a8dcafab2f24683bca50f8',
    })
    assert rule == '{"annotation": 4}'

    # No rule for component - use application's rule
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-1',
        'component':
        'comp-4',
        'container_id':
        '472de5194b88bc3302721ac28dcfe3a9fdc58350d0a8dcafab2f24683bca50f8',
    })
    assert rule == '{"annotation": 4}'

    # Get rule by component
    rule = agent.get_scalyr_sampling_rule({
        'application':
        'app-5',
        'component':
        'comp-5',
        'container_id':
        '472de5194b88bc3302721ac28dcfe3a9fdc58350d0a8dcafab2f24683bca50f8',
    })
    assert rule == '{"annotation": 8}'