|
| 1 | +import json |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from lambda_function import classifier_payload, lambda_handler, parse_key |
| 6 | + |
| 7 | + |
| 8 | +def s3_event(*keys: str) -> dict: |
| 9 | + return { |
| 10 | + "Records": [ |
| 11 | + { |
| 12 | + "s3": { |
| 13 | + "bucket": {"name": "ossci-raw-job-status"}, |
| 14 | + "object": {"key": key}, |
| 15 | + } |
| 16 | + } |
| 17 | + for key in keys |
| 18 | + ] |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | +class TestParseKey(unittest.TestCase): |
| 23 | + def test_bare_id_is_pytorch_pytorch(self): |
| 24 | + self.assertEqual(parse_key("log/123345"), ("pytorch/pytorch", 123345)) |
| 25 | + |
| 26 | + def test_prefixed_key_names_its_repo(self): |
| 27 | + self.assertEqual( |
| 28 | + parse_key("log/pytorch/executorch/999"), ("pytorch/executorch", 999) |
| 29 | + ) |
| 30 | + |
| 31 | + def test_meta_pytorch_repo(self): |
| 32 | + self.assertEqual( |
| 33 | + parse_key("log/meta-pytorch/torchcomms/42"), ("meta-pytorch/torchcomms", 42) |
| 34 | + ) |
| 35 | + |
| 36 | + def test_ignores_other_prefixes(self): |
| 37 | + # The notification is filtered to `log/`, but `classification/` and |
| 38 | + # `logs_something/` must not be mistaken for it if that ever changes. |
| 39 | + self.assertIsNone(parse_key("classification/123")) |
| 40 | + self.assertIsNone(parse_key("log_archive/123")) |
| 41 | + |
| 42 | + def test_ignores_a_non_numeric_id(self): |
| 43 | + self.assertIsNone(parse_key("log/not-a-number")) |
| 44 | + self.assertIsNone(parse_key("log/pytorch/executorch/not-a-number")) |
| 45 | + |
| 46 | + def test_ignores_an_unattributable_depth(self): |
| 47 | + # `log/<owner>/<id>` gives no repo, and anything deeper is not ours. |
| 48 | + self.assertIsNone(parse_key("log/pytorch/123")) |
| 49 | + self.assertIsNone(parse_key("log/a/b/c/123")) |
| 50 | + |
| 51 | + def test_ignores_a_directory_marker(self): |
| 52 | + self.assertIsNone(parse_key("log/")) |
| 53 | + |
| 54 | + |
| 55 | +class TestClassifierPayload(unittest.TestCase): |
| 56 | + def test_is_a_v2_request_the_classifier_can_parse(self): |
| 57 | + payload = classifier_payload("pytorch/executorch", 999) |
| 58 | + # log_classifier builds on lambda_http with only the apigw_http feature, |
| 59 | + # so version 2.0 and requestContext.http are what make it deserialize. |
| 60 | + self.assertEqual(payload["version"], "2.0") |
| 61 | + self.assertIn("http", payload["requestContext"]) |
| 62 | + self.assertEqual( |
| 63 | + payload["queryStringParameters"], |
| 64 | + {"job_id": "999", "repo": "pytorch/executorch"}, |
| 65 | + ) |
| 66 | + self.assertEqual( |
| 67 | + payload["rawQueryString"], "job_id=999&repo=pytorch/executorch" |
| 68 | + ) |
| 69 | + |
| 70 | + def test_is_json_serializable(self): |
| 71 | + json.dumps(classifier_payload("pytorch/pytorch", 1)) |
| 72 | + |
| 73 | + |
| 74 | +class TestLambdaHandler(unittest.TestCase): |
| 75 | + def test_invokes_the_classifier_asynchronously(self): |
| 76 | + with patch("lambda_function.lambda_client") as client: |
| 77 | + lambda_handler(s3_event("log/123345"), None) |
| 78 | + |
| 79 | + kwargs = client.invoke.call_args.kwargs |
| 80 | + self.assertEqual(kwargs["FunctionName"], "log_classifier") |
| 81 | + # Event, not RequestResponse: nothing here reads the classification. |
| 82 | + self.assertEqual(kwargs["InvocationType"], "Event") |
| 83 | + self.assertEqual( |
| 84 | + json.loads(kwargs["Payload"])["queryStringParameters"], |
| 85 | + {"job_id": "123345", "repo": "pytorch/pytorch"}, |
| 86 | + ) |
| 87 | + |
| 88 | + def test_handles_every_record_in_a_batch(self): |
| 89 | + with patch("lambda_function.lambda_client") as client: |
| 90 | + lambda_handler(s3_event("log/1", "log/pytorch/rl/2"), None) |
| 91 | + |
| 92 | + self.assertEqual(client.invoke.call_count, 2) |
| 93 | + |
| 94 | + def test_skips_a_key_it_cannot_attribute(self): |
| 95 | + with patch("lambda_function.lambda_client") as client: |
| 96 | + lambda_handler(s3_event("log/not-a-number"), None) |
| 97 | + |
| 98 | + client.invoke.assert_not_called() |
| 99 | + |
| 100 | + def test_one_failure_does_not_strand_the_batch(self): |
| 101 | + with patch("lambda_function.lambda_client") as client: |
| 102 | + client.invoke.side_effect = [RuntimeError("throttled"), None] |
| 103 | + lambda_handler(s3_event("log/1", "log/2"), None) |
| 104 | + |
| 105 | + self.assertEqual(client.invoke.call_count, 2) |
| 106 | + |
| 107 | + def test_an_empty_event_is_a_noop(self): |
| 108 | + with patch("lambda_function.lambda_client") as client: |
| 109 | + lambda_handler({}, None) |
| 110 | + |
| 111 | + client.invoke.assert_not_called() |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + unittest.main() |
0 commit comments