Beispiel #1
0
def test_add_pod_killed_metric(pod_scenario):
    pod_scenario.schema = {
        "actions": [
            {
                "kill": {
                    "force": False
                }
            },
        ]
    }
    magic_mock = MagicMock(return_value={
        "some ip": {
            "ret_code": 0
        },
    })
    pod_scenario.executor.execute = magic_mock
    mock_item = MagicMock()
    mock_item.container_ids = ["docker://container1"]
    mock_item.namespace = "default"
    mock_item.name = "test_pod1"

    items = [mock_item]

    with mock.patch('powerfulseal.metriccollectors.StdoutCollector.add_pod_killed_metric') \
            as metric_function:
        metric_function.assert_not_called()
        pod_scenario.act(items)
        metric_function.assert_called_with(mock_item)
Beispiel #2
0
def test_doesnt_kill_when_cant_find_node(pod_scenario):
    pod_scenario.schema = {
        "actions": [
            {
                "kill": {
                    "force": True
                }
            },
        ]
    }
    pod_scenario.inventory.get_node_by_ip = MagicMock(return_value=None)
    pod_scenario.logger = MagicMock()
    mock = MagicMock(return_value={
        "some ip": {
            "ret_code": 1
        },
    })
    pod_scenario.executor.execute = mock
    mock_item1 = MagicMock()
    mock_item2 = MagicMock()
    items = [mock_item1, mock_item2]
    pod_scenario.act(items)
    assert mock.call_count == 0
    assert pod_scenario.logger.info.call_args[0] == (
        "Node not found for pod: %s", items[1])
Beispiel #3
0
def test_kills_correctly_with_force(pod_scenario, should_force):
    pod_scenario.schema = {
        "actions": [
            {
                "kill": {
                    "force": should_force
                }
            },
        ]
    }
    mock = MagicMock(return_value={
        "some ip": {
            "ret_code": 1
        },
    })
    pod_scenario.executor.execute = mock
    mock_item1 = MagicMock()
    mock_item1.container_ids = ["docker://container1"]
    mock_item2 = MagicMock()
    mock_item2.container_ids = ["docker://container1"]
    items = [mock_item1, mock_item2]
    pod_scenario.act(items)
    assert mock.call_count == 2
    template = "sudo docker kill -s {signal} {container_id}"
    for i, call in enumerate(mock.call_args_list):
        args, kwargs = call
        cmd = template.format(signal="SIGKILL" if should_force else "SIGTERM",
                              container_id=items[i].container_ids[0].replace(
                                  "docker://", ""))
        assert args[0] == cmd
Beispiel #4
0
def test_kills_with_probability(pod_scenario, prob):
    random.seed(12)
    sample = 10000

    pod_scenario.schema = {
        "actions": [
            {
                "kill": {
                    "probability": prob,
                }
            },
        ]
    }
    mock = MagicMock(return_value={
        "some ip": {
            "ret_code": 0
        },
    })
    pod_scenario.executor.execute = mock
    mock_item1 = MagicMock()
    mock_item1.container_ids = ["docker://container1"]
    items = [mock_item1]

    for _ in range(sample):
        pod_scenario.act(items)

    actual_prob = float(mock.call_count / len(items)) / sample
    assert (actual_prob == pytest.approx(prob, 0.05))