Esempio n. 1
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. 2
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