Skip to content

Commit 38b9efd

Browse files
committed
Update with status checking for cancellation, and simplified integration test.
1 parent c8a5605 commit 38b9efd

File tree

5 files changed

+58
-441
lines changed

5 files changed

+58
-441
lines changed

python/example_code/s3/scenarios/batch/s3_batch_wrapper.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,28 @@ def cancel_job(self, job_id: str, account_id: str) -> None:
316316
account_id (str): AWS account ID
317317
"""
318318
try:
319-
self.s3control_client.update_job_status(
319+
response = self.s3control_client.describe_job(
320320
AccountId=account_id,
321-
JobId=job_id,
322-
RequestedJobStatus='Cancelled'
321+
JobId=job_id
323322
)
324-
print(f"Job {job_id} was successfully canceled.")
323+
current_status = response['Job']['Status']
324+
print(f"Current job status: {current_status}")
325+
326+
if current_status in ['Ready', 'Suspended', 'Active']:
327+
self.s3control_client.update_job_status(
328+
AccountId=account_id,
329+
JobId=job_id,
330+
RequestedJobStatus='Cancelled'
331+
)
332+
print(f"Job {job_id} was successfully canceled.")
333+
elif current_status in ['Completing', 'Complete']:
334+
print(f"Job is in '{current_status}' state - cannot be cancelled")
335+
if current_status == 'Completing':
336+
print("Job is finishing up and will complete soon.")
337+
elif current_status == 'Complete':
338+
print("Job has already completed successfully.")
339+
else:
340+
print(f"Job is in '{current_status}' state - cancel not allowed")
325341
except ClientError as e:
326342
print(f"Error canceling job: {e}")
327343
raise
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
"""Shared test fixtures for S3 batch operations tests."""
4+
"""
5+
Contains common test fixtures used to run unit tests.
6+
"""
57

6-
import boto3
7-
import pytest
8-
from moto import mock_s3, mock_s3control, mock_sts
8+
import sys
99

10-
from test_s3_batch_stubber import S3BatchStubber
11-
from s3_batch_wrapper import S3BatchWrapper
12-
from cloudformation_helper import CloudFormationHelper
10+
# This is needed so Python can find test_tools on the path.
11+
sys.path.append("../../../..")
12+
from test_tools.fixtures.common import *
1313

1414

15-
class ScenarioData:
16-
"""Holds data for scenario tests."""
17-
18-
def __init__(self, wrapper, cfn_helper, stubber):
19-
self.wrapper = wrapper
20-
self.cfn_helper = cfn_helper
21-
self.stubber = stubber
22-
23-
24-
@pytest.fixture
25-
def scenario_data(make_stubber):
26-
"""Create scenario data with stubbed clients."""
27-
s3_client = boto3.client("s3", region_name="us-east-1")
28-
s3control_client = boto3.client("s3control", region_name="us-east-1")
29-
sts_client = boto3.client("sts", region_name="us-east-1")
30-
cfn_client = boto3.client("cloudformation", region_name="us-east-1")
31-
32-
wrapper = S3BatchWrapper(s3_client, s3control_client, sts_client)
33-
cfn_helper = CloudFormationHelper(cfn_client)
34-
stubber = make_stubber(S3BatchStubber, s3_client, s3control_client, sts_client)
35-
36-
return ScenarioData(wrapper, cfn_helper, stubber)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import pytest
5+
6+
import boto3
7+
8+
from s3_batch_wrapper import S3BatchWrapper
9+
from cloudformation_helper import CloudFormationHelper
10+
from s3_batch_scenario import S3BatchScenario
11+
12+
13+
@pytest.mark.integ
14+
def test_run_batch_wrapper_scenario_integ(input_mocker, capsys):
15+
s3_client = boto3.client('s3')
16+
s3control_client = boto3.client('s3control')
17+
sts_client = boto3.client('sts')
18+
cfn_client = boto3.client('cloudformation')
19+
20+
batch_wrapper = S3BatchWrapper(s3_client, s3control_client, sts_client)
21+
cf_helper = CloudFormationHelper(cfn_client)
22+
scenario = S3BatchScenario(batch_wrapper, cf_helper)
23+
24+
input_mocker.mock_answers(
25+
["y", "y", "y", "n", "y", "y", "y", "y", "y", "y", "y"] # yes to proceed, no to cancel, yes to cleanup
26+
)
27+
28+
scenario.run_scenario()
29+
30+
capt = capsys.readouterr()
31+
assert "has successfully completed" in capt.out

python/example_code/s3/scenarios/batch/test/test_s3_batch_stubbed.py

Lines changed: 0 additions & 218 deletions
This file was deleted.

0 commit comments

Comments
 (0)