|
| 1 | +# CrateDB Kubernetes Operator |
| 2 | +# |
| 3 | +# Licensed to Crate.IO GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +import logging |
| 23 | +from asyncio import TimeoutError |
| 24 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 25 | + |
| 26 | +import pytest |
| 27 | +from aiohttp.client_exceptions import ClientConnectorError |
| 28 | + |
| 29 | +from crate.operator.handlers.handle_ping_cratedb_status import ( |
| 30 | + CLUSTER_STATUS_KEY, |
| 31 | + ping_cratedb_status, |
| 32 | +) |
| 33 | +from crate.operator.prometheus import PrometheusClusterStatus |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def mock_cratedb_connection(): |
| 38 | + mock_cursor = AsyncMock() |
| 39 | + mock_cursor.__aenter__.return_value = mock_cursor |
| 40 | + mock_cursor.__aexit__.return_value = None |
| 41 | + |
| 42 | + mock_conn = AsyncMock() |
| 43 | + mock_conn.__aenter__.return_value = mock_conn |
| 44 | + mock_conn.__aexit__.return_value = None |
| 45 | + mock_conn.cursor.return_value = mock_cursor |
| 46 | + |
| 47 | + patcher = patch("crate.operator.cratedb.aiopg.connect", return_value=mock_conn) |
| 48 | + mocked_connect = patcher.start() |
| 49 | + |
| 50 | + yield { |
| 51 | + "mock_connect": mocked_connect, |
| 52 | + "mock_conn": mock_conn, |
| 53 | + "mock_cursor": mock_cursor, |
| 54 | + } |
| 55 | + |
| 56 | + patcher.stop() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_ping_cratedb_status_success(mock_cratedb_connection): |
| 61 | + namespace = "test-ns" |
| 62 | + name = "test-cluster" |
| 63 | + cluster_name = "test-cluster" |
| 64 | + desired_instances = 1 |
| 65 | + |
| 66 | + patch_obj = MagicMock() |
| 67 | + logger = MagicMock(spec=logging.Logger) |
| 68 | + |
| 69 | + mock_api_client = AsyncMock() |
| 70 | + mock_api_client.__aenter__.return_value = mock_api_client |
| 71 | + |
| 72 | + mock_core = MagicMock() |
| 73 | + |
| 74 | + with ( |
| 75 | + patch( |
| 76 | + "crate.operator.handlers.handle_ping_cratedb_status.GlobalApiClient", |
| 77 | + return_value=mock_api_client, |
| 78 | + ), |
| 79 | + patch( |
| 80 | + "crate.operator.handlers.handle_ping_cratedb_status.CoreV1Api", |
| 81 | + return_value=mock_core, |
| 82 | + ), |
| 83 | + patch( |
| 84 | + "crate.operator.handlers.handle_ping_cratedb_status.get_host", |
| 85 | + new_callable=AsyncMock, |
| 86 | + return_value="crate.db.local", |
| 87 | + ), |
| 88 | + patch( |
| 89 | + "crate.operator.handlers.handle_ping_cratedb_status.get_system_user_password", # noqa |
| 90 | + new_callable=AsyncMock, |
| 91 | + return_value="secret-password", |
| 92 | + ), |
| 93 | + patch( |
| 94 | + "crate.operator.handlers.handle_ping_cratedb_status.get_healthiness", |
| 95 | + new_callable=AsyncMock, |
| 96 | + return_value="GREEN", |
| 97 | + ), |
| 98 | + patch( |
| 99 | + "crate.operator.handlers.handle_ping_cratedb_status.report_cluster_status" |
| 100 | + ) as mock_report, |
| 101 | + patch( |
| 102 | + "crate.operator.handlers.handle_ping_cratedb_status.webhook_client.send_notification", # noqa |
| 103 | + new_callable=AsyncMock, |
| 104 | + ), |
| 105 | + ): |
| 106 | + |
| 107 | + await ping_cratedb_status( |
| 108 | + namespace=namespace, |
| 109 | + name=name, |
| 110 | + cluster_name=cluster_name, |
| 111 | + desired_instances=desired_instances, |
| 112 | + patch=patch_obj, |
| 113 | + logger=logger, |
| 114 | + ) |
| 115 | + |
| 116 | + mock_conn = mock_cratedb_connection["mock_conn"] |
| 117 | + mock_cursor = mock_cratedb_connection["mock_cursor"] |
| 118 | + |
| 119 | + mock_conn.__aenter__.assert_awaited_once() |
| 120 | + mock_conn.cursor.assert_called_once() |
| 121 | + mock_cursor.__aenter__.assert_awaited_once() |
| 122 | + |
| 123 | + patch_obj.status.__setitem__.assert_called_once_with( |
| 124 | + CLUSTER_STATUS_KEY, {"health": PrometheusClusterStatus.GREEN.name} |
| 125 | + ) |
| 126 | + |
| 127 | + mock_report.assert_called_once_with( |
| 128 | + name, cluster_name, namespace, PrometheusClusterStatus.GREEN |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +fake_key = MagicMock() |
| 133 | +fake_key.ssl = None |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio |
| 137 | +@pytest.mark.parametrize( |
| 138 | + "side_effect, expected_log_method, expected_log_message", |
| 139 | + [ |
| 140 | + ( |
| 141 | + ClientConnectorError(fake_key, OSError("kaboom")), |
| 142 | + "warning", |
| 143 | + "Transient Kubernetes API connection error during health check", |
| 144 | + ), |
| 145 | + ( |
| 146 | + TimeoutError("timed out"), |
| 147 | + "warning", |
| 148 | + "Timeout while connecting to CrateDB during health check", |
| 149 | + ), |
| 150 | + ( |
| 151 | + Exception("generic failure"), |
| 152 | + "warning", |
| 153 | + "Unexpected error during CrateDB health check", |
| 154 | + ), |
| 155 | + ], |
| 156 | +) |
| 157 | +async def test_ping_cratedb_status_exceptions( |
| 158 | + side_effect, expected_log_method, expected_log_message |
| 159 | +): |
| 160 | + namespace = "test-ns" |
| 161 | + name = "test-cluster" |
| 162 | + cluster_name = "test-cluster" |
| 163 | + desired_instances = 1 |
| 164 | + |
| 165 | + patch_obj = MagicMock() |
| 166 | + logger = MagicMock(spec=logging.Logger) |
| 167 | + |
| 168 | + mock_api_client_cm = AsyncMock() |
| 169 | + mock_core = MagicMock() |
| 170 | + |
| 171 | + with ( |
| 172 | + patch( |
| 173 | + "crate.operator.handlers.handle_ping_cratedb_status.GlobalApiClient", |
| 174 | + return_value=mock_api_client_cm, |
| 175 | + ), |
| 176 | + patch( |
| 177 | + "crate.operator.handlers.handle_ping_cratedb_status.CoreV1Api", |
| 178 | + return_value=mock_core, |
| 179 | + ), |
| 180 | + patch( |
| 181 | + "crate.operator.handlers.handle_ping_cratedb_status.get_host", |
| 182 | + side_effect=side_effect, |
| 183 | + ), |
| 184 | + patch( |
| 185 | + "crate.operator.handlers.handle_ping_cratedb_status.get_system_user_password", # noqa |
| 186 | + new_callable=AsyncMock, |
| 187 | + ), |
| 188 | + patch( |
| 189 | + "crate.operator.handlers.handle_ping_cratedb_status.report_cluster_status" |
| 190 | + ) as mock_report, |
| 191 | + patch( |
| 192 | + "crate.operator.handlers.handle_ping_cratedb_status.webhook_client.send_notification", # noqa |
| 193 | + new_callable=AsyncMock, |
| 194 | + ), |
| 195 | + ): |
| 196 | + |
| 197 | + await ping_cratedb_status( |
| 198 | + namespace=namespace, |
| 199 | + name=name, |
| 200 | + cluster_name=cluster_name, |
| 201 | + desired_instances=desired_instances, |
| 202 | + patch=patch_obj, |
| 203 | + logger=logger, |
| 204 | + ) |
| 205 | + |
| 206 | + # validate patch to status UNREACHABLE |
| 207 | + patch_obj.status.__setitem__.assert_called_once_with( |
| 208 | + CLUSTER_STATUS_KEY, {"health": PrometheusClusterStatus.UNREACHABLE.name} |
| 209 | + ) |
| 210 | + |
| 211 | + # validate the correct logger method was called |
| 212 | + log_method = getattr(logger, expected_log_method) |
| 213 | + log_method.assert_called_once() |
| 214 | + assert expected_log_message in log_method.call_args[0][0] |
| 215 | + |
| 216 | + # check status was reported |
| 217 | + mock_report.assert_called_once_with( |
| 218 | + name, cluster_name, namespace, PrometheusClusterStatus.UNREACHABLE |
| 219 | + ) |
0 commit comments