|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from kubeflow.katib import report_metrics |
| 5 | +from kubeflow.katib.constants import constants |
| 6 | + |
| 7 | +TEST_RESULT_SUCCESS = "success" |
| 8 | +ENV_VARIABLE_EMPTY = True |
| 9 | +ENV_VARIABLE_NOT_EMPTY = False |
| 10 | + |
| 11 | + |
| 12 | +def report_observation_log_response(*args, **kwargs): |
| 13 | + if kwargs.get("timeout") == 0: |
| 14 | + raise TimeoutError |
| 15 | + |
| 16 | + |
| 17 | +test_report_metrics_data = [ |
| 18 | + ( |
| 19 | + "valid metrics with float type", |
| 20 | + {"metrics": {"result": 0.99}, "timeout": constants.DEFAULT_TIMEOUT}, |
| 21 | + TEST_RESULT_SUCCESS, |
| 22 | + ENV_VARIABLE_NOT_EMPTY, |
| 23 | + ), |
| 24 | + ( |
| 25 | + "valid metrics with string type", |
| 26 | + {"metrics": {"result": "0.99"}, "timeout": constants.DEFAULT_TIMEOUT}, |
| 27 | + TEST_RESULT_SUCCESS, |
| 28 | + ENV_VARIABLE_NOT_EMPTY, |
| 29 | + ), |
| 30 | + ( |
| 31 | + "valid metrics with int type", |
| 32 | + {"metrics": {"result": 1}, "timeout": constants.DEFAULT_TIMEOUT}, |
| 33 | + TEST_RESULT_SUCCESS, |
| 34 | + ENV_VARIABLE_NOT_EMPTY, |
| 35 | + ), |
| 36 | + ( |
| 37 | + "ReportObservationLog timeout error", |
| 38 | + {"metrics": {"result": 0.99}, "timeout": 0}, |
| 39 | + RuntimeError, |
| 40 | + ENV_VARIABLE_NOT_EMPTY, |
| 41 | + ), |
| 42 | + ( |
| 43 | + "invalid metrics with type string", |
| 44 | + {"metrics": {"result": "abc"}, "timeout": constants.DEFAULT_TIMEOUT}, |
| 45 | + ValueError, |
| 46 | + ENV_VARIABLE_NOT_EMPTY, |
| 47 | + ), |
| 48 | + ( |
| 49 | + "Trial name is not passed to env variables", |
| 50 | + {"metrics": {"result": 0.99}, "timeout": constants.DEFAULT_TIMEOUT}, |
| 51 | + ValueError, |
| 52 | + ENV_VARIABLE_EMPTY, |
| 53 | + ), |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def mock_getenv(request): |
| 59 | + with patch("os.getenv") as mock: |
| 60 | + if request.param is ENV_VARIABLE_EMPTY: |
| 61 | + mock.side_effect = ValueError |
| 62 | + else: |
| 63 | + mock.return_value = "example" |
| 64 | + yield mock |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def mock_get_current_k8s_namespace(): |
| 69 | + with patch("kubeflow.katib.utils.utils.get_current_k8s_namespace") as mock: |
| 70 | + mock.return_value = "test" |
| 71 | + yield mock |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def mock_report_observation_log(): |
| 76 | + with patch("kubeflow.katib.katib_api_pb2_grpc.DBManagerStub") as mock: |
| 77 | + mock_instance = mock.return_value |
| 78 | + mock_instance.ReportObservationLog.side_effect = report_observation_log_response |
| 79 | + yield mock_instance |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "test_name,kwargs,expected_output,mock_getenv", |
| 84 | + test_report_metrics_data, |
| 85 | + indirect=["mock_getenv"], |
| 86 | +) |
| 87 | +def test_report_metrics( |
| 88 | + test_name, |
| 89 | + kwargs, |
| 90 | + expected_output, |
| 91 | + mock_getenv, |
| 92 | + mock_get_current_k8s_namespace, |
| 93 | + mock_report_observation_log, |
| 94 | +): |
| 95 | + """ |
| 96 | + test report_metrics function |
| 97 | + """ |
| 98 | + print("\n\nExecuting test:", test_name) |
| 99 | + try: |
| 100 | + report_metrics(**kwargs) |
| 101 | + assert expected_output == TEST_RESULT_SUCCESS |
| 102 | + except Exception as e: |
| 103 | + assert type(e) is expected_output |
| 104 | + print("test execution complete") |
0 commit comments