def test_get_new_containers_log_targets(monkeypatch, fx_containers_sync):
    containers, pods, result, _, _ = fx_containers_sync

    get_pod = MagicMock(side_effect=[pod_mock(p) for p in pods])

    monkeypatch.setattr('kube_log_watcher.kube.get_pod', get_pod)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(
        containers,
        CONTAINERS_PATH,
        CLUSTER_ID,
        strict_labels=['application', 'version'])

    assert targets == result
def test_get_new_containers_log_targets(monkeypatch, fx_containers_sync):
    containers, pods, result, _, _ = fx_containers_sync

    get_pods = MagicMock()
    get_pods.return_value = pods

    monkeypatch.setattr('kube_log_watcher.kube.get_pods', get_pods)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(containers,
                                             CONTAINERS_PATH,
                                             CLUSTER_ID,
                                             strict_labels=True)

    assert targets == result
Esempio n. 3
0
def test_get_new_containers_log_targets_not_found_pods(monkeypatch,
                                                       fx_containers_sync):
    containers, pods, _, _, _ = fx_containers_sync

    get_pod = MagicMock()
    get_pod.side_effect = PodNotFound

    monkeypatch.setattr('kube_log_watcher.kube.get_pod', get_pod)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(
        containers,
        CONTAINERS_PATH,
        CLUSTER_ID,
        strict_labels=['application', 'version'])

    assert targets == []
def test_get_new_containers_log_targets_no_strict_labels(
        monkeypatch, fx_containers_sync):
    containers, pods, result_labels, result_no_labels, _ = fx_containers_sync

    result = result_labels + result_no_labels

    get_pod = MagicMock(side_effect=[pod_mock(p) for p in pods])

    monkeypatch.setattr('kube_log_watcher.kube.get_pod', get_pod)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(containers, CONTAINERS_PATH,
                                             CLUSTER_ID)

    assert sorted(targets,
                  key=lambda k: k['id']) == sorted(result,
                                                   key=lambda k: k['id'])
def test_get_new_containers_log_targets_failure(monkeypatch,
                                                fx_containers_sync):
    containers, pods, _, _, _ = fx_containers_sync

    is_pause = MagicMock(side_effect=Exception)

    # Force exception
    monkeypatch.setattr('kube_log_watcher.kube.is_pause_container', is_pause)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(
        containers,
        CONTAINERS_PATH,
        CLUSTER_ID,
        strict_labels=['application', 'version'])

    assert targets == []
def test_get_new_containers_log_targets_no_strict_labels(
        monkeypatch, fx_containers_sync):
    containers, pods, result_labels, result_no_labels, _ = fx_containers_sync

    result = result_labels + result_no_labels

    get_pods = MagicMock()
    get_pods.return_value = pods

    monkeypatch.setattr('kube_log_watcher.kube.get_pods', get_pods)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(containers, CONTAINERS_PATH,
                                             CLUSTER_ID)

    assert sorted(targets,
                  key=lambda k: k['id']) == sorted(result,
                                                   key=lambda k: k['id'])