Пример #1
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)
Пример #2
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])
def test_policy_runner_lifecycle():
    """
    Integration test which starts, checks status, and stops the policy runner
    """
    policy = {
        'config': {
            'minSecondsBetweenRuns': 0,
            'maxSecondsBetweenRuns': 2
        },
        'nodeScenarios': [{
            'name': 'Node Test'
        }],
        'podScenarios': [{
            'name': 'Pod Test'
        }]
    }
    test_inventory = MagicMock()
    test_inventory.sync = MagicMock(return_value=None)

    server_state = ServerState(policy, test_inventory, None, None,
                               RemoteExecutor(), None, None, None)
    assert server_state.is_policy_runner_running() is False

    server_state.start_policy_runner()
    time.sleep(1)
    assert server_state.is_policy_runner_running() is True

    server_state.stop_policy_runner()
    time.sleep(1)
    assert server_state.is_policy_runner_running() is False
Пример #4
0
def test_autonomous_mode_integration(client):
    policy = {
        'config': {
            'minSecondsBetweenRuns': 0,
            'maxSecondsBetweenRuns': 2
        },
        'nodeScenarios': [{
            'name': 'Node Test'
        }],
        'podScenarios': [{
            'name': 'Pod Test'
        }]
    }
    test_inventory = MagicMock()
    test_inventory.sync = MagicMock(return_value=None)

    server_state = ServerState(policy, test_inventory, None, None,
                               RemoteExecutor(), None, None, None)

    # Autonomous mode has not yet started
    result = client.get('/api/autonomous-mode')
    assert json.loads(result.data.decode("utf-8"))['isStarted'] is False

    # Autonomous mode has not yet started so it cannot be stopped
    result = client.post('/api/autonomous-mode',
                         data=json.dumps({'action': 'stop'}),
                         content_type='application/json')
    assert result.status_code == 412

    # Start autonomous mode
    result = client.post('/api/autonomous-mode',
                         data=json.dumps({'action': 'start'}),
                         content_type='application/json')
    assert result.status_code == 200

    # Autonomous mode has started
    result = client.get('/api/autonomous-mode')
    assert json.loads(result.data.decode("utf-8"))['isStarted'] is True

    # Autonomous mode has started so it cannot be started
    result = client.post('/api/autonomous-mode',
                         data=json.dumps({'action': 'start'}),
                         content_type='application/json')
    assert result.status_code == 412

    # Stop autonomous mode
    result = client.post('/api/autonomous-mode',
                         data=json.dumps({'action': 'stop'}),
                         content_type='application/json')
    assert result.status_code == 200

    # Autonomous mode has stopped
    result = client.get('/api/autonomous-mode')
    assert json.loads(result.data.decode("utf-8"))['isStarted'] is False
Пример #5
0
def pod_scenario():
    inventory = MagicMock()
    k8s_inventory = MagicMock()
    executor = RemoteExecutor()
    return PodScenario(
        name="test scenario",
        schema=EXAMPLE_POD_SCHEMA,
        inventory=inventory,
        k8s_inventory=k8s_inventory,
        executor=executor,
    )
Пример #6
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])
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])
Пример #9
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])