Techdebt: Improve/fix exception handling in tests (#7011)

This commit is contained in:
Bert Blommers 2023-11-09 22:17:46 -01:00 committed by GitHub
parent 3ca46c3c24
commit 90e8bb1313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 73 additions and 53 deletions

View File

@ -578,8 +578,8 @@ class ECRBackend(BaseBackend):
parsed_image_manifest["imageManifest"] = image_manifest_mediatype parsed_image_manifest["imageManifest"] = image_manifest_mediatype
else: else:
if "mediaType" not in parsed_image_manifest: if "mediaType" not in parsed_image_manifest:
raise Exception( raise InvalidParameterException(
"image manifest mediatype not provided in manifest or parameter" message="image manifest mediatype not provided in manifest or parameter"
) )
else: else:
image_manifest_mediatype = parsed_image_manifest["mediaType"] image_manifest_mediatype = parsed_image_manifest["mediaType"]

View File

@ -1836,7 +1836,10 @@ class EC2ContainerServiceBackend(BaseBackend):
if container_instance is None: if container_instance is None:
raise Exception("{0} is not a container id in the cluster") raise Exception("{0} is not a container id in the cluster")
if not force and container_instance.running_tasks_count > 0: if not force and container_instance.running_tasks_count > 0:
raise Exception("Found running tasks on the instance.") raise JsonRESTError(
error_type="InvalidParameter",
message="Found running tasks on the instance.",
)
# Currently assume that people might want to do something based around deregistered instances # Currently assume that people might want to do something based around deregistered instances
# with tasks left running on them - but nothing if no tasks were running already # with tasks left running on them - but nothing if no tasks were running already
elif force and container_instance.running_tasks_count > 0: elif force and container_instance.running_tasks_count > 0:

View File

@ -3,6 +3,7 @@ import datetime
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from moto.core import BaseBackend, BackendDict, BaseModel from moto.core import BaseBackend, BackendDict, BaseModel
from moto.core.exceptions import JsonRESTError
from moto.utilities.utils import md5_hash from moto.utilities.utils import md5_hash
from .utils import get_job_id from .utils import get_job_id
@ -189,6 +190,8 @@ class GlacierBackend(BaseBackend):
self.vaults: Dict[str, Vault] = {} self.vaults: Dict[str, Vault] = {}
def get_vault(self, vault_name: str) -> Vault: def get_vault(self, vault_name: str) -> Vault:
if vault_name not in self.vaults:
raise JsonRESTError(error_type="VaultNotFound", message="Vault not found")
return self.vaults[vault_name] return self.vaults[vault_name]
def create_vault(self, vault_name: str) -> None: def create_vault(self, vault_name: str) -> None:

View File

@ -730,6 +730,7 @@ class SQSResponse(BaseResponse):
template = self.response_template(UNTAG_QUEUE_RESPONSE) template = self.response_template(UNTAG_QUEUE_RESPONSE)
return template.render() return template.render()
@jsonify_error
def list_queue_tags(self) -> str: def list_queue_tags(self) -> str:
queue_name = self._get_queue_name() queue_name = self._get_queue_name()

View File

@ -1846,7 +1846,7 @@ def test_describe_continuous_backups_errors():
client = boto3.client("dynamodb", region_name="us-east-1") client = boto3.client("dynamodb", region_name="us-east-1")
# when # when
with pytest.raises(Exception) as e: with pytest.raises(ClientError) as e:
client.describe_continuous_backups(TableName="not-existing-table") client.describe_continuous_backups(TableName="not-existing-table")
# then # then
@ -1930,7 +1930,7 @@ def test_update_continuous_backups_errors():
client = boto3.client("dynamodb", region_name="us-east-1") client = boto3.client("dynamodb", region_name="us-east-1")
# when # when
with pytest.raises(Exception) as e: with pytest.raises(ClientError) as e:
client.update_continuous_backups( client.update_continuous_backups(
TableName="not-existing-table", TableName="not-existing-table",
PointInTimeRecoverySpecification={"PointInTimeRecoveryEnabled": True}, PointInTimeRecoverySpecification={"PointInTimeRecoveryEnabled": True},

View File

@ -663,6 +663,7 @@ def test_describe_flow_logs_filtering():
)["FlowLogs"] )["FlowLogs"]
assert len(fl_by_tag_key) == 0 assert len(fl_by_tag_key) == 0
# NotYetImplemented
with pytest.raises(Exception): with pytest.raises(Exception):
client.describe_flow_logs(Filters=[{"Name": "unknown", "Values": ["foobar"]}]) client.describe_flow_logs(Filters=[{"Name": "unknown", "Values": ["foobar"]}])

View File

@ -362,12 +362,17 @@ def test_put_image_without_mediatype():
image_manifest = _create_image_manifest() image_manifest = _create_image_manifest()
_ = image_manifest.pop("mediaType") _ = image_manifest.pop("mediaType")
with pytest.raises(Exception): with pytest.raises(ClientError) as exc:
client.put_image( client.put_image(
repositoryName="test_repository", repositoryName="test_repository",
imageManifest=json.dumps(image_manifest), imageManifest=json.dumps(image_manifest),
imageTag="latest", imageTag="latest",
) )
err = exc.value.response["Error"]
assert (
err["Message"]
== "image manifest mediatype not provided in manifest or parameter"
)
@mock_ecr @mock_ecr

View File

@ -1484,7 +1484,7 @@ def test_deregister_container_instance():
cluster=test_cluster_name, instanceIdentityDocument=instance_id_document cluster=test_cluster_name, instanceIdentityDocument=instance_id_document
) )
container_instance_id = response["containerInstance"]["containerInstanceArn"] container_instance_id = response["containerInstance"]["containerInstanceArn"]
response = ecs_client.deregister_container_instance( ecs_client.deregister_container_instance(
cluster=test_cluster_name, containerInstance=container_instance_id cluster=test_cluster_name, containerInstance=container_instance_id
) )
container_instances_response = ecs_client.list_container_instances( container_instances_response = ecs_client.list_container_instances(
@ -1520,12 +1520,12 @@ def test_deregister_container_instance():
containerInstances=[container_instance_id], containerInstances=[container_instance_id],
startedBy="moto", startedBy="moto",
) )
with pytest.raises(Exception): with pytest.raises(ClientError) as exc:
ecs_client.deregister_container_instance( ecs_client.deregister_container_instance(
cluster=test_cluster_name, containerInstance=container_instance_id cluster=test_cluster_name, containerInstance=container_instance_id
) )
# TODO: Return correct error format err = exc.value.response["Error"]
# should.contain("Found running tasks on the instance") assert err["Message"] == "Found running tasks on the instance."
container_instances_response = ecs_client.list_container_instances( container_instances_response = ecs_client.list_container_instances(
cluster=test_cluster_name cluster=test_cluster_name

View File

@ -3,6 +3,7 @@ import os
import pytest import pytest
from moto import mock_glacier from moto import mock_glacier
from botocore.exceptions import ClientError
@mock_glacier @mock_glacier
@ -48,8 +49,8 @@ def test_delete_archive():
delete = client.delete_archive(vaultName="asdf", archiveId=archive["archiveId"]) delete = client.delete_archive(vaultName="asdf", archiveId=archive["archiveId"])
assert delete["ResponseMetadata"]["HTTPStatusCode"] == 204 assert delete["ResponseMetadata"]["HTTPStatusCode"] == 204
with pytest.raises(Exception): with pytest.raises(ClientError) as exc:
# Not ideal - but this will throw an error if the archvie does not exist # Not ideal - but this will throw an error if the archive does not exist
# Which is a good indication that the deletion went through # Which is a good indication that the deletion went through
client.initiate_job( client.initiate_job(
vaultName="myname", vaultName="myname",
@ -58,3 +59,5 @@ def test_delete_archive():
"Type": "archive-retrieval", "Type": "archive-retrieval",
}, },
) )
err = exc.value.response["Error"]
assert err["Code"] == "VaultNotFound"

View File

@ -1,6 +1,7 @@
import boto3 import boto3
import pytest import pytest
from botocore.exceptions import ClientError
from moto import mock_glacier from moto import mock_glacier
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from uuid import uuid4 from uuid import uuid4
@ -31,8 +32,10 @@ def test_delete_vault_boto3():
client.delete_vault(vaultName="myvault") client.delete_vault(vaultName="myvault")
with pytest.raises(Exception): with pytest.raises(ClientError) as exc:
client.describe_vault(vaultName="myvault") client.describe_vault(vaultName="myvault")
err = exc.value.response["Error"]
assert err["Code"] == "VaultNotFound"
@mock_glacier @mock_glacier

View File

@ -1,7 +1,9 @@
import boto3 import boto3
from freezegun import freeze_time
import pytest import pytest
from botocore.exceptions import ClientError
from freezegun import freeze_time
from moto import mock_opsworks from moto import mock_opsworks
@ -32,12 +34,12 @@ def test_create_app_response():
assert "AppId" in response assert "AppId" in response
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_app(StackId=stack_id, Type="other", Name="TestApp") client.create_app(StackId=stack_id, Type="other", Name="TestApp")
assert r'already an app named "TestApp"' in exc.value.response["Error"]["Message"] assert r'already an app named "TestApp"' in exc.value.response["Error"]["Message"]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_app(StackId="nothere", Type="other", Name="TestApp") client.create_app(StackId="nothere", Type="other", Name="TestApp")
assert exc.value.response["Error"]["Message"] == "nothere" assert exc.value.response["Error"]["Message"] == "nothere"
@ -61,20 +63,20 @@ def test_describe_apps():
assert rv1["Apps"][0]["Name"] == "TestApp" assert rv1["Apps"][0]["Name"] == "TestApp"
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_apps(StackId=stack_id, AppIds=[app_id]) client.describe_apps(StackId=stack_id, AppIds=[app_id])
assert exc.value.response["Error"]["Message"] == ( assert exc.value.response["Error"]["Message"] == (
"Please provide one or more app IDs or a stack ID" "Please provide one or more app IDs or a stack ID"
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_apps(StackId="nothere") client.describe_apps(StackId="nothere")
assert exc.value.response["Error"]["Message"] == ( assert exc.value.response["Error"]["Message"] == (
"Unable to find stack with ID nothere" "Unable to find stack with ID nothere"
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_apps(AppIds=["nothere"]) client.describe_apps(AppIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere" assert exc.value.response["Error"]["Message"] == "nothere"

View File

@ -1,6 +1,7 @@
import boto3 import boto3
import pytest import pytest
from botocore.exceptions import ClientError
from moto import mock_opsworks from moto import mock_opsworks
from moto import mock_ec2 from moto import mock_ec2
from tests import EXAMPLE_AMI_ID from tests import EXAMPLE_AMI_ID
@ -43,7 +44,7 @@ def test_create_instance():
assert "InstanceId" in response assert "InstanceId" in response
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_instance( client.create_instance(
StackId="nothere", LayerIds=[layer_id], InstanceType="t2.micro" StackId="nothere", LayerIds=[layer_id], InstanceType="t2.micro"
) )
@ -51,14 +52,14 @@ def test_create_instance():
"Unable to find stack with ID nothere" "Unable to find stack with ID nothere"
) )
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_instance( client.create_instance(
StackId=stack_id, LayerIds=["nothere"], InstanceType="t2.micro" StackId=stack_id, LayerIds=["nothere"], InstanceType="t2.micro"
) )
assert exc.value.response["Error"]["Message"] == "nothere" assert exc.value.response["Error"]["Message"] == "nothere"
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_instance( client.create_instance(
StackId=stack_id, LayerIds=[second_layer_id], InstanceType="t2.micro" StackId=stack_id, LayerIds=[second_layer_id], InstanceType="t2.micro"
) )
@ -67,7 +68,7 @@ def test_create_instance():
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.start_instance(InstanceId="nothere") client.start_instance(InstanceId="nothere")
assert exc.value.response["Error"]["Message"] == ( assert exc.value.response["Error"]["Message"] == (
"Unable to find instance with ID nothere" "Unable to find instance with ID nothere"
@ -160,22 +161,22 @@ def test_describe_instances():
assert S2L1_i1 not in [i["InstanceId"] for i in response] assert S2L1_i1 not in [i["InstanceId"] for i in response]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_instances(StackId=S1, LayerId=S1L1) client.describe_instances(StackId=S1, LayerId=S1L1)
assert "Please provide either one or more" in exc.value.response["Error"]["Message"] assert "Please provide either one or more" in exc.value.response["Error"]["Message"]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_instances(StackId="nothere") client.describe_instances(StackId="nothere")
assert "nothere" in exc.value.response["Error"]["Message"] assert "nothere" in exc.value.response["Error"]["Message"]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_instances(LayerId="nothere") client.describe_instances(LayerId="nothere")
assert "nothere" in exc.value.response["Error"]["Message"] assert "nothere" in exc.value.response["Error"]["Message"]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_instances(InstanceIds=["nothere"]) client.describe_instances(InstanceIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere" assert exc.value.response["Error"]["Message"] == "nothere"

View File

@ -1,7 +1,9 @@
import boto3 import boto3
from freezegun import freeze_time
import pytest import pytest
from botocore.exceptions import ClientError
from freezegun import freeze_time
from moto import mock_opsworks from moto import mock_opsworks
@ -42,7 +44,7 @@ def test_create_layer_response():
assert "LayerId" in response assert "LayerId" in response
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_layer( client.create_layer(
StackId=stack_id, Type="custom", Name="TestLayer", Shortname="_" StackId=stack_id, Type="custom", Name="TestLayer", Shortname="_"
) )
@ -51,7 +53,7 @@ def test_create_layer_response():
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_layer( client.create_layer(
StackId=stack_id, Type="custom", Name="_", Shortname="TestLayerShortName" StackId=stack_id, Type="custom", Name="_", Shortname="TestLayerShortName"
) )
@ -60,7 +62,7 @@ def test_create_layer_response():
) in exc.value.response["Error"]["Message"] ) in exc.value.response["Error"]["Message"]
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.create_layer( client.create_layer(
StackId="nothere", Type="custom", Name="TestLayer", Shortname="_" StackId="nothere", Type="custom", Name="TestLayer", Shortname="_"
) )
@ -91,20 +93,20 @@ def test_describe_layers():
assert rv1["Layers"][0]["Name"] == "TestLayer" assert rv1["Layers"][0]["Name"] == "TestLayer"
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_layers(StackId=stack_id, LayerIds=[layer_id]) client.describe_layers(StackId=stack_id, LayerIds=[layer_id])
assert exc.value.response["Error"]["Message"] == ( assert exc.value.response["Error"]["Message"] == (
"Please provide one or more layer IDs or a stack ID" "Please provide one or more layer IDs or a stack ID"
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_layers(StackId="nothere") client.describe_layers(StackId="nothere")
assert exc.value.response["Error"]["Message"] == ( assert exc.value.response["Error"]["Message"] == (
"Unable to find stack with ID nothere" "Unable to find stack with ID nothere"
) )
# ClientError # ClientError
with pytest.raises(Exception) as exc: with pytest.raises(ClientError) as exc:
client.describe_layers(LayerIds=["nothere"]) client.describe_layers(LayerIds=["nothere"])
assert exc.value.response["Error"]["Message"] == "nothere" assert exc.value.response["Error"]["Message"] == "nothere"

View File

@ -599,10 +599,10 @@ def test_cannot_restore_standard_class_object():
bucket.create() bucket.create()
key = bucket.put_object(Key="the-key", Body=b"somedata") key = bucket.put_object(Key="the-key", Body=b"somedata")
with pytest.raises(Exception) as err: with pytest.raises(ClientError) as exc:
key.restore_object(RestoreRequest={"Days": 1}) key.restore_object(RestoreRequest={"Days": 1})
err = err.value.response["Error"] err = exc.value.response["Error"]
assert err["Code"] == "InvalidObjectState" assert err["Code"] == "InvalidObjectState"
assert err["StorageClass"] == "STANDARD" assert err["StorageClass"] == "STANDARD"
assert err["Message"] == ( assert err["Message"] == (

View File

@ -582,11 +582,11 @@ def test_s3_abort_multipart_data_with_invalid_upload_and_key():
client.create_bucket(Bucket="blah") client.create_bucket(Bucket="blah")
with pytest.raises(Exception) as err: with pytest.raises(ClientError) as exc:
client.abort_multipart_upload( client.abort_multipart_upload(
Bucket="blah", Key="foobar", UploadId="dummy_upload_id" Bucket="blah", Key="foobar", UploadId="dummy_upload_id"
) )
err = err.value.response["Error"] err = exc.value.response["Error"]
assert err["Code"] == "NoSuchUpload" assert err["Code"] == "NoSuchUpload"
assert err["Message"] == ( assert err["Message"] == (
"The specified upload does not exist. The upload ID may be invalid, " "The specified upload does not exist. The upload ID may be invalid, "

View File

@ -2831,13 +2831,11 @@ def test_send_message_to_fifo_without_message_group_id():
Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"}, Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"},
) )
with pytest.raises(Exception) as e: with pytest.raises(ClientError) as exc:
queue.send_message(MessageBody="message-1") queue.send_message(MessageBody="message-1")
ex = e.value err = exc.value.response["Error"]
assert ex.response["Error"]["Code"] == "MissingParameter" assert err["Code"] == "MissingParameter"
assert ex.response["Error"]["Message"] == ( assert err["Message"] == "The request must contain the parameter MessageGroupId."
"The request must contain the parameter MessageGroupId."
)
@mock_sqs @mock_sqs
@ -2848,13 +2846,11 @@ def test_send_messages_to_fifo_without_message_group_id():
Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"}, Attributes={"FifoQueue": "true", "ContentBasedDeduplication": "true"},
) )
with pytest.raises(Exception) as e: with pytest.raises(ClientError) as exc:
queue.send_messages(Entries=[{"Id": "id_1", "MessageBody": "body_1"}]) queue.send_messages(Entries=[{"Id": "id_1", "MessageBody": "body_1"}])
ex = e.value err = exc.value.response["Error"]
assert ex.response["Error"]["Code"] == "MissingParameter" assert err["Code"] == "MissingParameter"
assert ex.response["Error"]["Message"] == ( assert err["Message"] == "The request must contain the parameter MessageGroupId."
"The request must contain the parameter MessageGroupId."
)
@mock_sqs @mock_sqs
@ -2862,10 +2858,10 @@ def test_maximum_message_size_attribute_default():
sqs = boto3.resource("sqs", region_name="eu-west-3") sqs = boto3.resource("sqs", region_name="eu-west-3")
queue = sqs.create_queue(QueueName=str(uuid4())) queue = sqs.create_queue(QueueName=str(uuid4()))
assert int(queue.attributes["MaximumMessageSize"]) == MAXIMUM_MESSAGE_LENGTH assert int(queue.attributes["MaximumMessageSize"]) == MAXIMUM_MESSAGE_LENGTH
with pytest.raises(Exception) as e: with pytest.raises(ClientError) as exc:
queue.send_message(MessageBody="a" * (MAXIMUM_MESSAGE_LENGTH + 1)) queue.send_message(MessageBody="a" * (MAXIMUM_MESSAGE_LENGTH + 1))
ex = e.value err = exc.value.response["Error"]
assert ex.response["Error"]["Code"] == "InvalidParameterValue" assert err["Code"] == "InvalidParameterValue"
@mock_sqs @mock_sqs