Exemplo n.º 1
0
def test_kill_pod_SSHing():
    label_runner = LabelRunner(NodeInventory(None), None, None,
                               RemoteExecutor())

    #Pactch action of switching to SSHing mode
    k8s_inventory = MagicMock()
    k8s_inventory.delete_pods = False
    label_runner.k8s_inventory = k8s_inventory

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    label_runner.inventory.get_node_by_ip = get_node_by_ip_mock

    # Patch action of choosing container
    execute_mock = MagicMock()
    label_runner.executor.execute = execute_mock

    mock_pod = Pod(name='test', namespace='test')
    mock_pod.container_ids = ["docker://container1"]
    mock_pod.labels = {"seal/force-kill": "false"}
    label_runner.kill_pod(mock_pod)

    mock_pod = Pod(name='test', namespace='test')
    mock_pod.container_ids = ["docker://container1"]
    mock_pod.labels = {}
    label_runner.kill_pod(mock_pod)

    execute_mock.assert_called_with("sudo docker kill -s SIGTERM container1",
                                    nodes=[test_node])
Exemplo n.º 2
0
def test_kill_pod_forced_SSHing():
    label_runner = LabelRunner(NodeInventory(None), None, None, SSHExecutor())

    # patch metrics collector
    label_runner.metric_collector = MagicMock()

    # Patch action of switching to SSHing mode
    k8s_inventory = MagicMock()
    label_runner.k8s_inventory = k8s_inventory

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    label_runner.inventory.get_node_by_ip = get_node_by_ip_mock

    # Patch action of choosing container
    execute_mock = MagicMock()
    label_runner.executor.execute = execute_mock

    mock_pod = Pod(name='test', namespace='test')
    mock_pod.container_ids = ["docker://container1"]
    mock_pod.labels = {"seal/force-kill": "true"}
    label_runner.kill_pod(mock_pod)
    execute_mock.assert_called_once_with(
        "sudo docker kill -s SIGKILL container1", nodes=[test_node])
Exemplo n.º 3
0
def test_kill_pod_APIcalling():

    label_runner = LabelRunner(NodeInventory(None), None, None,
                               RemoteExecutor())

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    label_runner.inventory.get_node_by_ip = get_node_by_ip_mock

    #Pactch action of switching to APIcalling mode
    k8s_inventory = MagicMock()
    k8s_inventory.delete_pods = True
    label_runner.k8s_inventory = k8s_inventory

    # Patch action of choosing container
    k8s_client_mock = MagicMock()
    label_runner.k8s_inventory.k8s_client = k8s_client_mock

    delete_pods_mock = MagicMock()
    label_runner.k8s_inventory.k8s_client.delete_pods = delete_pods_mock

    metric_collector = MagicMock()
    label_runner.metric_collector = metric_collector

    add_pod_killed_metric_mock = MagicMock()
    label_runner.metric_collector.add_pod_killed_metric = add_pod_killed_metric_mock

    mock_pod = Pod(name='test', namespace='test')
    mock_pod.container_ids = ["docker://container1"]
    label_runner.kill_pod(mock_pod)

    delete_pods_mock.assert_called_with([mock_pod])
    add_pod_killed_metric_mock.assert_called_with(mock_pod)
def test_groups(mock_driver):
    inventory = NodeInventory(driver=mock_driver,
                              restrict_to_groups={
                                  "TEST1": ["198.168.1.1"],
                                  "TEST2": ["198.168.1.1", "198.168.2.1"],
                              })
    inventory.sync()
    assert inventory.get_groups() == ["TEST1", "TEST2"]
def test_find(nodes, mock_driver, query, expected_indices):
    inventory = NodeInventory(driver=mock_driver,
                              restrict_to_groups={
                                  "TEST1": ["198.168.1.1"],
                                  "TEST2": ["198.168.1.1", "198.168.2.1"],
                              })
    inventory.sync()
    assert list(
        inventory.find_nodes(query)) == [nodes[x] for x in expected_indices]
def test_sync(nodes, mock_driver):
    inventory = NodeInventory(driver=mock_driver,
                              restrict_to_groups={
                                  "TEST1": ["198.168.1.1"],
                                  "TEST2": ["198.168.1.1", "198.168.2.1"],
                              })
    inventory.sync()
    assert inventory.groups == {"TEST1": nodes[0:1], "TEST2": nodes[0:2]}
    assert inventory.get_azs() == ["AZ1", "AZ2"]
def test_get_by_ip(nodes, mock_driver, ip, should_find, index):
    inventory = NodeInventory(driver=mock_driver,
                              restrict_to_groups={
                                  "TEST1": ["198.168.1.1"],
                                  "TEST2": ["198.168.1.1", "198.168.2.1"],
                              })
    inventory.sync()
    if should_find:
        assert inventory.get_node_by_ip(ip) is nodes[index]
    else:
        assert inventory.get_node_by_ip(ip) == None
Exemplo n.º 8
0
def test_sort_pods():
    pod_1 = MagicMock()
    pod_1.metrics = {'cpu': 10, 'memory': 0}

    pod_2 = MagicMock()
    pod_2.metrics = {'cpu': 5, 'memory': 3}

    pod_3 = MagicMock()
    pod_3.metrics = {'cpu': 5, 'memory': 2}

    demo_runner = DemoRunner(NodeInventory(None), None, None, RemoteExecutor(),
                             None)
    assert demo_runner.sort_pods([pod_2, pod_3,
                                  pod_1]) == [pod_1, pod_2, pod_3]
def test_kill_pod_forced():
    label_runner = LabelRunner(NodeInventory(None), None, None, RemoteExecutor())

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    label_runner.inventory.get_node_by_ip = get_node_by_ip_mock

    # Patch action of choosing container
    execute_mock = MagicMock()
    label_runner.executor.execute = execute_mock

    mock_pod = MagicMock()
    mock_pod.container_ids = ["docker://container1"]
    mock_pod.labels = {"seal/force-kill": "true"}
    label_runner.kill_pod(mock_pod)
    execute_mock.assert_called_once_with("sudo docker kill -s SIGKILL container1", nodes=[test_node])
Exemplo n.º 10
0
def test_kill_pod():
    server_state = ServerState(None, NodeInventory(None), None, None,
                               RemoteExecutor(), None, None, None)

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    server_state.inventory.get_node_by_ip = get_node_by_ip_mock

    # Patch action of choosing container
    execute_mock = MagicMock()
    server_state.executor.execute = execute_mock

    mock_pod = MagicMock()
    mock_pod.container_ids = ["docker://container1"]

    server_state.kill_pod(mock_pod, True)
    execute_mock.assert_called_once_with(
        "sudo docker kill -s SIGKILL container1", nodes=[test_node])
Exemplo n.º 11
0
def test_filter_top():
    first_quintile_runner = DemoRunner(None,
                                       None,
                                       None,
                                       None,
                                       None,
                                       aggressiveness=1)
    assert len(
        first_quintile_runner.filter_top([MagicMock() for _ in range(5)])) == 1
    assert len(
        first_quintile_runner.filter_top([MagicMock()
                                          for _ in range(10)])) == 2
    assert len(
        first_quintile_runner.filter_top([MagicMock() for _ in range(3)])) == 0

    third_quintile_runner = DemoRunner(None,
                                       None,
                                       None,
                                       None,
                                       None,
                                       aggressiveness=3)
    assert len(
        third_quintile_runner.filter_top([MagicMock() for _ in range(5)])) == 3
    assert len(
        third_quintile_runner.filter_top([MagicMock()
                                          for _ in range(10)])) == 6
    assert len(
        third_quintile_runner.filter_top([MagicMock() for _ in range(3)])) == 1

    all_runner = DemoRunner(NodeInventory(None),
                            None,
                            None,
                            None,
                            None,
                            aggressiveness=5)
    assert len(all_runner.filter_top([MagicMock() for _ in range(5)])) == 5
    assert len(all_runner.filter_top([MagicMock() for _ in range(10)])) == 10
    assert len(all_runner.filter_top([MagicMock() for _ in range(3)])) == 3
Exemplo n.º 12
0
def test_kill_pod():
    demo_runner = DemoRunner(NodeInventory(None), None, None, RemoteExecutor(),
                             None)

    # Patch action of getting nodes to execute kill command on
    test_node = Node(1)
    get_node_by_ip_mock = MagicMock(return_value=test_node)
    demo_runner.inventory.get_node_by_ip = get_node_by_ip_mock

    # Patch action of choosing container
    execute_mock = MagicMock()
    demo_runner.executor.execute = execute_mock

    mock_pod = MagicMock()
    mock_pod.container_ids = ["docker://container1"]
    demo_runner.kill_pod(mock_pod)

    mock_pod = MagicMock()
    mock_pod.container_ids = ["docker://container1"]
    demo_runner.kill_pod(mock_pod)

    execute_mock.assert_called_with("sudo docker kill -s SIGTERM container1",
                                    nodes=[test_node])