1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
from __future__ import annotations
import gzip
import inspect
import json
import sys
import unittest
from pathlib import Path
from typing import Any, Dict
from unittest import mock
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(REPO_ROOT))
from tools.stats.upload_metrics import add_global_metric, emit_metric, global_metrics
from tools.stats.upload_stats_lib import get_s3_resource, remove_nan_inf
sys.path.remove(str(REPO_ROOT))
# default values
REPO = "some/repo"
BUILD_ENV = "cuda-10.2"
TEST_CONFIG = "test-config"
WORKFLOW = "some-workflow"
JOB = "some-job"
RUN_ID = 56
RUN_NUMBER = 123
RUN_ATTEMPT = 3
PR_NUMBER = 6789
JOB_ID = 234
JOB_NAME = "some-job-name"
@mock.patch("boto3.resource")
class TestUploadStats(unittest.TestCase):
emitted_metric: Dict[str, Any] = {"did_not_emit": True}
def mock_put_item(self, **kwargs: Any) -> None:
# Utility for mocking putting items into s3. THis will save the emitted
# metric so tests can check it
self.emitted_metric = json.loads(
gzip.decompress(kwargs["Body"]).decode("utf-8")
)
# Before each test, set the env vars to their default values
def setUp(self) -> None:
get_s3_resource.cache_clear()
global_metrics.clear()
mock.patch.dict(
"os.environ",
{
"CI": "true",
"BUILD_ENVIRONMENT": BUILD_ENV,
"TEST_CONFIG": TEST_CONFIG,
"GITHUB_REPOSITORY": REPO,
"GITHUB_WORKFLOW": WORKFLOW,
"GITHUB_JOB": JOB,
"GITHUB_RUN_ID": str(RUN_ID),
"GITHUB_RUN_NUMBER": str(RUN_NUMBER),
"GITHUB_RUN_ATTEMPT": str(RUN_ATTEMPT),
"JOB_ID": str(JOB_ID),
"JOB_NAME": str(JOB_NAME),
},
clear=True, # Don't read any preset env vars
).start()
def test_emits_default_and_given_metrics(self, mock_resource: Any) -> None:
metric = {
"some_number": 123,
"float_number": 32.34,
}
# Querying for this instead of hard coding it b/c this will change
# based on whether we run this test directly from python or from
# pytest
current_module = inspect.getmodule(inspect.currentframe()).__name__ # type: ignore[union-attr]
emit_should_include = {
"metric_name": "metric_name",
"calling_file": "test_upload_stats_lib.py",
"calling_module": current_module,
"calling_function": "test_emits_default_and_given_metrics",
"repo": REPO,
"workflow": WORKFLOW,
"build_environment": BUILD_ENV,
"job": JOB,
"test_config": TEST_CONFIG,
"run_id": RUN_ID,
"run_number": RUN_NUMBER,
"run_attempt": RUN_ATTEMPT,
"job_id": JOB_ID,
"job_name": JOB_NAME,
"info": metric,
}
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertEqual(
self.emitted_metric,
{**self.emitted_metric, **emit_should_include},
)
def test_when_global_metric_specified_then_it_emits_it(
self, mock_resource: Any
) -> None:
metric = {
"some_number": 123,
}
global_metric_name = "global_metric"
global_metric_value = "global_value"
add_global_metric(global_metric_name, global_metric_value)
emit_should_include = {
**metric,
global_metric_name: global_metric_value,
}
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertEqual(
self.emitted_metric,
{**self.emitted_metric, "info": emit_should_include},
)
def test_when_local_and_global_metric_specified_then_global_is_overridden(
self, mock_resource: Any
) -> None:
global_metric_name = "global_metric"
global_metric_value = "global_value"
local_override = "local_override"
add_global_metric(global_metric_name, global_metric_value)
metric = {
"some_number": 123,
global_metric_name: local_override,
}
emit_should_include = {
**metric,
global_metric_name: local_override,
}
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertEqual(
self.emitted_metric,
{**self.emitted_metric, "info": emit_should_include},
)
def test_when_optional_envvar_set_to_actual_value_then_emit_vars_emits_it(
self, mock_resource: Any
) -> None:
metric = {
"some_number": 123,
}
emit_should_include = {
"info": {**metric},
"pr_number": PR_NUMBER,
}
mock.patch.dict(
"os.environ",
{
"PR_NUMBER": str(PR_NUMBER),
},
).start()
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertEqual(
self.emitted_metric,
{**self.emitted_metric, **emit_should_include},
)
def test_when_optional_envvar_set_to_a_empty_str_then_emit_vars_ignores_it(
self, mock_resource: Any
) -> None:
metric = {"some_number": 123}
emit_should_include: dict[str, Any] = metric.copy()
# Github Actions defaults some env vars to an empty string
default_val = ""
mock.patch.dict(
"os.environ",
{
"PR_NUMBER": default_val,
},
).start()
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertEqual(
self.emitted_metric,
{**self.emitted_metric, "info": emit_should_include},
f"Metrics should be emitted when an option parameter is set to '{default_val}'",
)
self.assertFalse(
self.emitted_metric.get("pr_number"),
f"Metrics should not include optional item 'pr_number' when it's envvar is set to '{default_val}'",
)
def test_no_metrics_emitted_if_required_env_var_not_set(
self, mock_resource: Any
) -> None:
metric = {"some_number": 123}
mock.patch.dict(
"os.environ",
{
"CI": "true",
"BUILD_ENVIRONMENT": BUILD_ENV,
},
clear=True,
).start()
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertTrue(self.emitted_metric["did_not_emit"])
def test_no_metrics_emitted_if_required_env_var_set_to_empty_string(
self, mock_resource: Any
) -> None:
metric = {"some_number": 123}
mock.patch.dict(
"os.environ",
{
"GITHUB_JOB": "",
},
).start()
mock_resource.return_value.Object.return_value.put = self.mock_put_item
emit_metric("metric_name", metric)
self.assertTrue(self.emitted_metric["did_not_emit"])
def test_remove_nan_inf(self, _mocked_resource: Any) -> None:
checks = [
(float("inf"), '"inf"', "Infinity"),
(float("nan"), '"nan"', "NaN"),
({1: float("inf")}, '{"1": "inf"}', '{"1": Infinity}'),
([float("nan")], '["nan"]', "[NaN]"),
({1: [float("nan")]}, '{"1": ["nan"]}', '{"1": [NaN]}'),
]
for input, clean, unclean in checks:
clean_output = json.dumps(remove_nan_inf(input))
unclean_output = json.dumps(input)
self.assertEqual(
clean_output,
clean,
f"Expected {clean} when input is {unclean}, got {clean_output}",
)
self.assertEqual(
unclean_output,
unclean,
f"Expected {unclean} when input is {unclean}, got {unclean_output}",
)
if __name__ == "__main__":
unittest.main()
|