33from uuid import uuid4
44
55import pytest
6+ from botocore .exceptions import ClientError
67
78from cwms_batch_events .core .job_logger .cloudwatch import CloudWatchJobLogger
89from cwms_batch_events .core .job_logger .s3 import S3JobLogger
@@ -24,6 +25,24 @@ def test_s3_job_logger_reads_logs_from_bucket():
2425 assert logs == "hello"
2526
2627
28+ def test_s3_job_logger_reports_missing_logs ():
29+ job_id = uuid4 ()
30+ s3_client = mock .Mock ()
31+ s3_client .get_object .side_effect = ClientError (
32+ {"Error" : {"Code" : "NoSuchKey" , "Message" : "not found" }},
33+ "GetObject" ,
34+ )
35+
36+ with mock .patch (
37+ "cwms_batch_events.core.job_logger.s3.boto3.client" ,
38+ return_value = s3_client ,
39+ ), mock .patch ("cwms_batch_events.core.job_logger.s3.S3_BUCKET" , "bucket" ):
40+ logger = S3JobLogger ()
41+
42+ with pytest .raises (FileNotFoundError , match = f"No logs found for job { job_id } " ):
43+ logger .get_logs_for_job (job_id )
44+
45+
2746def test_s3_job_logger_pushes_logs_to_bucket ():
2847 s3_client = mock .Mock ()
2948 job_id = uuid4 ()
@@ -84,6 +103,36 @@ def test_cloudwatch_job_logger_rejects_multiple_batch_jobs():
84103 logger .get_batch_log_name ("ext-123" )
85104
86105
106+ def test_cloudwatch_job_logger_reports_missing_batch_attempts ():
107+ batch_client = mock .Mock ()
108+ batch_client .describe_jobs .return_value = {"jobs" : [{"attempts" : []}]}
109+
110+ with mock .patch (
111+ "cwms_batch_events.core.job_logger.cloudwatch.boto3.client" ,
112+ side_effect = [batch_client , mock .Mock ()],
113+ ):
114+ logger = CloudWatchJobLogger (mock .Mock ())
115+
116+ with pytest .raises (ValueError , match = "No Batch job attempts found" ):
117+ logger .get_batch_log_name ("ext-123" )
118+
119+
120+ def test_cloudwatch_job_logger_reports_missing_log_stream ():
121+ batch_client = mock .Mock ()
122+ batch_client .describe_jobs .return_value = {
123+ "jobs" : [{"attempts" : [{"container" : {}}]}]
124+ }
125+
126+ with mock .patch (
127+ "cwms_batch_events.core.job_logger.cloudwatch.boto3.client" ,
128+ side_effect = [batch_client , mock .Mock ()],
129+ ):
130+ logger = CloudWatchJobLogger (mock .Mock ())
131+
132+ with pytest .raises (ValueError , match = "No log stream found" ):
133+ logger .get_batch_log_name ("ext-123" )
134+
135+
87136def test_cloudwatch_job_logger_requires_external_job_id ():
88137 job_id = uuid4 ()
89138 db = mock .Mock ()
0 commit comments