Exemplo n.º 1
0
def test_capacity_check_crit(capfd):
    mock_result = mock.MagicMock()
    mock_result.result = lambda: [{
        'groupings': {
            'foo': 'baz'
        },
        'cpus': {
            'total': 2,
            'free': 1,
            'used': 1
        },
        'mem': {
            'total': 2,
            'free': 1,
            'used': 1
        },
        'disk': {
            'total': 2,
            'free': 1,
            'used': 1
        },
    }]
    mock_api_client = mock.MagicMock()
    mock_api_client.resources.resources = lambda groupings: mock_result

    for t in check_types:
        options = mock.MagicMock()
        options.type = t
        options.overrides = None
        options.cluster = 'fake_cluster'
        options.attributes = 'foo'
        options.warn = 45
        options.crit = 49

        with mock.patch(
                'paasta_tools.monitoring.check_capacity.parse_capacity_check_options',
                autospec=True,
                return_value=options,
        ), mock.patch(
                'paasta_tools.monitoring.check_capacity.load_system_paasta_config',
                autospec=True,
        ), mock.patch(
                'paasta_tools.monitoring.check_capacity.get_paasta_api_client',
                autospec=True,
                return_value=mock_api_client,
        ):
            with pytest.raises(SystemExit) as error:
                run_capacity_check()
            out, err = capfd.readouterr()
            assert error.value.code == 2, out
            assert 'CRITICAL' in out
            assert 'fake_cluster' in out
            assert t in out
def test_capacity_check_crit(capfd):
    mock_result = mock.MagicMock()
    mock_result.result = lambda: [{
        "groupings": {
            "foo": "baz"
        },
        "cpus": {
            "total": 2,
            "free": 1,
            "used": 1
        },
        "mem": {
            "total": 2,
            "free": 1,
            "used": 1
        },
        "disk": {
            "total": 2,
            "free": 1,
            "used": 1
        },
    }]
    mock_api_client = mock.MagicMock()
    mock_api_client.resources.resources = lambda groupings: mock_result

    for t in check_types:
        options = mock.MagicMock()
        options.type = t
        options.overrides = None
        options.cluster = "fake_cluster"
        options.attributes = "foo"
        options.warn = 45
        options.crit = 49

        with mock.patch(
                "paasta_tools.monitoring.check_capacity.parse_capacity_check_options",
                autospec=True,
                return_value=options,
        ), mock.patch(
                "paasta_tools.monitoring.check_capacity.load_system_paasta_config",
                autospec=True,
        ), mock.patch(
                "paasta_tools.monitoring.check_capacity.get_paasta_api_client",
                autospec=True,
                return_value=mock_api_client,
        ):
            with pytest.raises(SystemExit) as error:
                run_capacity_check()
            out, err = capfd.readouterr()
            assert error.value.code == 2, out
            assert "CRITICAL" in out
            assert "fake_cluster" in out
            assert t in out
Exemplo n.º 3
0
def test_capacity_check_overrides(capfd):
    mock_api_client = mock.MagicMock()
    mock_api_client.resources.resources.return_value.value = [
        {
            "groupings": {
                "foo": "bar"
            },
            "cpus": {
                "total": 2,
                "free": 1,
                "used": 1
            },
            "mem": {
                "total": 2,
                "free": 1,
                "used": 1
            },
            "disk": {
                "total": 2,
                "free": 1,
                "used": 1
            },
        },
        {
            "groupings": {
                "foo": "baz"
            },
            "cpus": {
                "total": 2,
                "free": 1,
                "used": 1
            },
            "mem": {
                "total": 2,
                "free": 1,
                "used": 1
            },
            "disk": {
                "total": 2,
                "free": 1,
                "used": 1
            },
        },
    ]

    mock_overrides = [{
        "groupings": {
            "foo": "bar"
        },
        "warn": {
            "cpus": 99,
            "mem": 99,
            "disk": 99
        },
        "crit": {
            "cpus": 10,
            "mem": 10,
            "disk": 10
        },
    }]

    for t in check_types:
        options = mock.MagicMock()
        options.type = t
        options.overrides = "/fake/file.json"
        options.cluster = "fake_cluster"
        options.attributes = "foo"
        options.warn = 99
        options.crit = 99

        with mock.patch(
                "paasta_tools.monitoring.check_capacity.parse_capacity_check_options",
                autospec=True,
                return_value=options,
        ), mock.patch(
                "paasta_tools.monitoring.check_capacity.load_system_paasta_config",
                autospec=True,
        ), mock.patch(
                "paasta_tools.monitoring.check_capacity.get_paasta_oapi_client",
                autospec=True,
                return_value=mock_api_client,
        ), mock.patch(
                "paasta_tools.monitoring.check_capacity.read_overrides",
                autospec=True,
                return_value=mock_overrides,
        ):
            with pytest.raises(SystemExit) as error:
                run_capacity_check()
            out, err = capfd.readouterr()
            assert error.value.code == 2, out
            assert "CRITICAL" in out
            assert "fake_cluster" in out
            assert t in out
            assert "baz" not in out