def mock_k8s_client(mocker):
    list_pods_val = dict_to_simple_namespace({'items': [
        {'metadata': {
            'uid': 'aaa',
            'name': 'test_pod1',
            'namespace': 'test_ns',
            'annotations': None,
            'owner_references': None
        }
        }, {'metadata': {
            'uid': 'bbb',
            'name': 'test_pod2',
            'namespace': 'test_ns',
            'annotations': None,
            'owner_references': None
        }
        }
    ]})
    empty_list_pods_val = dict_to_simple_namespace({'items': []})

    list_node_val = dict_to_simple_namespace({'items': [{'metadata': {'name': 'test_node'}}]})

    mock_api = mocker.Mock(**{'list_pod_for_all_namespaces.side_effect': [list_pods_val, empty_list_pods_val],
                              'list_node.return_value': list_node_val,
                              'patch_node.return_value': mocker.Mock()}
                           )

    class Configuration:

        def __init__(self):
            self.api_key = {}
            self.api_key_prefix = {}

    return mocker.Mock(**{'CoreV1Api.return_value': mock_api,
                          'Configuration.return_value': Configuration()})
def test_remove_all_pods(mocker):
    list_pods_val = dict_to_simple_namespace({
        'items': [{
            'metadata': {
                'uid': 'aaa',
                'name': 'test_pod1',
                'namespace': 'test_ns',
                'annotations': None,
                'owner_references': None
            }
        }, {
            'metadata': {
                'uid': 'bbb',
                'name': 'test_pod2',
                'namespace': 'test_ns',
                'annotations': None,
                'owner_references': None
            }
        }]
    })
    empty_list_pods_val = dict_to_simple_namespace({'items': []})

    mock_api = mocker.Mock(
        **{
            'list_pod_for_all_namespaces.side_effect':
            [list_pods_val, empty_list_pods_val]
        })

    remove_all_pods(mock_api, 'test_node')

    mock_api.list_pod_for_all_namespaces.assert_called_with(
        watch=False,
        include_uninitialized=True,
        field_selector='spec.nodeName=test_node')

    mock_arg = {
        'apiVersion': 'policy/v1beta1',
        'kind': 'Eviction',
        'deleteOptions': {},
        'metadata': {
            'name': 'test_pod1',
            'namespace': 'test_ns'
        }
    }

    mock_arg1 = {
        'apiVersion': 'policy/v1beta1',
        'kind': 'Eviction',
        'deleteOptions': {},
        'metadata': {
            'name': 'test_pod2',
            'namespace': 'test_ns'
        }
    }

    mock_api.create_namespaced_pod_eviction.assert_any_call(
        'test_pod1', 'test_ns', mock_arg)
    mock_api.create_namespaced_pod_eviction.assert_any_call(
        'test_pod2', 'test_ns', mock_arg1)
def mock_k8s_client_pods_exception(mocker):
    list_node_val = dict_to_simple_namespace(
        {'items': [{
            'metadata': {
                'name': 'test_node'
            }
        }]})

    mock_api = mocker.Mock(
        **{
            'list_pod_for_all_namespaces.side_effect': ApiException(),
            'list_node.return_value': list_node_val,
            'patch_node.return_value': mocker.Mock()
        })

    class Configuration:
        def __init__(self):
            self.api_key = {}
            self.api_key_prefix = {}

    return mocker.Mock(
        **{
            'CoreV1Api.return_value': mock_api,
            'Configuration.return_value': Configuration()
        })
def test_node_exists(mocker):
    list_nodes_val = dict_to_simple_namespace(
        {'items': [{
            'metadata': {
                'name': 'test_node'
            }
        }]})
    mock_api = mocker.Mock(**{'list_node.return_value': list_nodes_val})

    assert node_exists(mock_api, 'test_node') is True
    assert node_exists(mock_api, 'nope') is False
def mock_k8s_client_no_nodes(mocker):

    list_node_val = dict_to_simple_namespace({'items': []})

    mock_api = mocker.Mock(**{'list_node.return_value': list_node_val})

    class Configuration:

        def __init__(self):
            self.api_key = {}
            self.api_key_prefix = {}

    return mocker.Mock(**{'CoreV1Api.return_value': mock_api,
                          'Configuration.return_value': Configuration()})
Exemplo n.º 6
0
def test_remove_all_pods(mocker):
    list_pods_val = dict_to_simple_namespace({
        'items': [{
            'metadata': {
                'name': 'test_pod1',
                'namespace': 'test_ns'
            }
        }, {
            'metadata': {
                'name': 'test_pod2',
                'namespace': 'test_ns'
            }
        }]
    })

    mock_api = mocker.Mock(
        **{'list_pod_for_all_namespaces.return_value': list_pods_val})

    remove_all_pods(mock_api, 'test_node')

    mock_api.list_pod_for_all_namespaces.assert_called_with(
        watch=False, field_selector='spec.nodeName=test_node')

    mock_arg = {
        'apiVersion': 'policy/v1beta1',
        'kind': 'Eviction',
        'metadata': {
            'name': 'test_pod1',
            'namespace': 'test_ns'
        }
    }

    mock_arg1 = {
        'apiVersion': 'policy/v1beta1',
        'kind': 'Eviction',
        'metadata': {
            'name': 'test_pod2',
            'namespace': 'test_ns'
        }
    }

    mock_api.create_namespaced_pod_eviction.assert_any_call(
        'test_pod1-eviction', 'test_ns', mock_arg)
    mock_api.create_namespaced_pod_eviction.assert_any_call(
        'test_pod2-eviction', 'test_ns', mock_arg1)