|
| 1 | +# Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with |
| 4 | +# the License. A copy of the License is located at |
| 5 | +# |
| 6 | +# http://aws.amazon.com/apache2.0/ |
| 7 | +# |
| 8 | +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 9 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import logging |
| 13 | +from datetime import datetime, timezone |
| 14 | +from unittest.mock import MagicMock |
| 15 | + |
| 16 | +import pytest |
| 17 | +from assertpy import assert_that |
| 18 | +from botocore.config import Config |
| 19 | +from botocore.exceptions import ClientError |
| 20 | +from slurm_plugin.cloudwatch_utils import METRICS_NAMESPACE, CloudWatchMetricsPublisher |
| 21 | + |
| 22 | + |
| 23 | +class TestCloudWatchMetricsPublisher: |
| 24 | + """Tests for CloudWatchMetricsPublisher class.""" |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def boto3_config(self): |
| 28 | + return Config(retries={"max_attempts": 1, "mode": "standard"}) |
| 29 | + |
| 30 | + @pytest.fixture |
| 31 | + def metrics_publisher(self, boto3_config): |
| 32 | + return CloudWatchMetricsPublisher( |
| 33 | + region="us-east-1", |
| 34 | + cluster_name="test-cluster", |
| 35 | + instance_id="i-1234567890abcdef0", |
| 36 | + boto3_config=boto3_config, |
| 37 | + ) |
| 38 | + |
| 39 | + def test_init(self, metrics_publisher, boto3_config): |
| 40 | + """Test CloudWatchMetricsPublisher initialization.""" |
| 41 | + assert_that(metrics_publisher._region).is_equal_to("us-east-1") |
| 42 | + assert_that(metrics_publisher._cluster_name).is_equal_to("test-cluster") |
| 43 | + assert_that(metrics_publisher._boto3_config).is_equal_to(boto3_config) |
| 44 | + assert_that(metrics_publisher._instance_id).is_equal_to("i-1234567890abcdef0") |
| 45 | + assert_that(metrics_publisher._cloudwatch_client).is_none() |
| 46 | + |
| 47 | + def test_cloudwatch_client_lazy_initialization(self, metrics_publisher, mocker): |
| 48 | + """Test that CloudWatch client is lazily initialized.""" |
| 49 | + mock_client = MagicMock() |
| 50 | + mock_boto3 = mocker.patch("slurm_plugin.cloudwatch_utils.boto3") |
| 51 | + mock_boto3.client.return_value = mock_client |
| 52 | + |
| 53 | + # First access should create the client |
| 54 | + client = metrics_publisher.cloudwatch_client |
| 55 | + assert_that(client).is_equal_to(mock_client) |
| 56 | + mock_boto3.client.assert_called_once_with( |
| 57 | + "cloudwatch", |
| 58 | + region_name="us-east-1", |
| 59 | + config=metrics_publisher._boto3_config, |
| 60 | + ) |
| 61 | + |
| 62 | + # Second access should return the cached client |
| 63 | + mock_boto3.client.reset_mock() |
| 64 | + client2 = metrics_publisher.cloudwatch_client |
| 65 | + assert_that(client2).is_equal_to(mock_client) |
| 66 | + mock_boto3.client.assert_not_called() |
| 67 | + |
| 68 | + @pytest.mark.parametrize( |
| 69 | + "metric_name, value, unit, additional_dimensions, expected_dimensions", |
| 70 | + [ |
| 71 | + pytest.param( |
| 72 | + "TestMetric", |
| 73 | + 42, |
| 74 | + "Count", |
| 75 | + None, |
| 76 | + [ |
| 77 | + {"Name": "ClusterName", "Value": "test-cluster"}, |
| 78 | + {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, |
| 79 | + ], |
| 80 | + id="basic", |
| 81 | + ), |
| 82 | + pytest.param( |
| 83 | + "HeadNodeDaemonHeartbeat", |
| 84 | + 1, |
| 85 | + "Count", |
| 86 | + [{"Name": "DaemonName", "Value": "clustermgtd"}], |
| 87 | + [ |
| 88 | + {"Name": "ClusterName", "Value": "test-cluster"}, |
| 89 | + {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, |
| 90 | + {"Name": "DaemonName", "Value": "clustermgtd"}, |
| 91 | + ], |
| 92 | + id="with_additional_dimension", |
| 93 | + ), |
| 94 | + pytest.param( |
| 95 | + "LatencyMetric", |
| 96 | + 150.5, |
| 97 | + "Milliseconds", |
| 98 | + None, |
| 99 | + [ |
| 100 | + {"Name": "ClusterName", "Value": "test-cluster"}, |
| 101 | + {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, |
| 102 | + ], |
| 103 | + id="with_custom_unit", |
| 104 | + ), |
| 105 | + pytest.param( |
| 106 | + "CustomMetric", |
| 107 | + 100, |
| 108 | + "Count", |
| 109 | + [ |
| 110 | + {"Name": "DaemonName", "Value": "clustermgtd"}, |
| 111 | + {"Name": "NodeType", "Value": "HeadNode"}, |
| 112 | + ], |
| 113 | + [ |
| 114 | + {"Name": "ClusterName", "Value": "test-cluster"}, |
| 115 | + {"Name": "InstanceId", "Value": "i-1234567890abcdef0"}, |
| 116 | + {"Name": "DaemonName", "Value": "clustermgtd"}, |
| 117 | + {"Name": "NodeType", "Value": "HeadNode"}, |
| 118 | + ], |
| 119 | + id="with_multiple_additional_dimensions", |
| 120 | + ), |
| 121 | + ], |
| 122 | + ) |
| 123 | + def test_put_metric( |
| 124 | + self, |
| 125 | + metrics_publisher, |
| 126 | + mocker, |
| 127 | + metric_name: str, |
| 128 | + value: float, |
| 129 | + unit: str, |
| 130 | + additional_dimensions: list, |
| 131 | + expected_dimensions: list, |
| 132 | + ): |
| 133 | + """Test put_metric with various parameter combinations.""" |
| 134 | + mock_client = MagicMock() |
| 135 | + metrics_publisher._cloudwatch_client = mock_client |
| 136 | + mock_datetime = mocker.patch("slurm_plugin.cloudwatch_utils.datetime") |
| 137 | + fixed_time = datetime(2024, 1, 15, 12, 0, 0, tzinfo=timezone.utc) |
| 138 | + mock_datetime.now.return_value = fixed_time |
| 139 | + |
| 140 | + metrics_publisher.put_metric( |
| 141 | + metric_name=metric_name, |
| 142 | + value=value, |
| 143 | + unit=unit, |
| 144 | + additional_dimensions=additional_dimensions, |
| 145 | + ) |
| 146 | + |
| 147 | + mock_client.put_metric_data.assert_called_once_with( |
| 148 | + Namespace=METRICS_NAMESPACE, |
| 149 | + MetricData=[ |
| 150 | + { |
| 151 | + "MetricName": metric_name, |
| 152 | + "Dimensions": expected_dimensions, |
| 153 | + "Timestamp": fixed_time, |
| 154 | + "Value": value, |
| 155 | + "Unit": unit, |
| 156 | + } |
| 157 | + ], |
| 158 | + ) |
| 159 | + |
| 160 | + def test_put_metric_handles_exception(self, metrics_publisher, caplog): |
| 161 | + """Test that put_metric handles exceptions gracefully.""" |
| 162 | + mock_client = MagicMock() |
| 163 | + mock_client.put_metric_data.side_effect = ClientError( |
| 164 | + {"Error": {"Code": "WHATEVER_CODE", "Message": "WHATEVER_MESSAGE"}}, |
| 165 | + "PutMetricData", |
| 166 | + ) |
| 167 | + metrics_publisher._cloudwatch_client = mock_client |
| 168 | + |
| 169 | + with caplog.at_level(logging.WARNING): |
| 170 | + # Should not raise exception |
| 171 | + metrics_publisher.put_metric(metric_name="WHATEVER_METRIC_NAME", value=1) |
| 172 | + |
| 173 | + assert_that(caplog.text).matches( |
| 174 | + r"Failed to publish metric WHATEVER_METRIC_NAME:.*WHATEVER_CODE.*WHATEVER_MESSAGE" |
| 175 | + ) |
0 commit comments