Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(ec2): evaluate filters in DescribeVolumes (#1284)

Maximilian Bandlea149078ebe36
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index 9b7e8488..9a9b05b7 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -2655,8 +2655,38 @@ def _delete_volume(p):
return _xml(200, "DeleteVolumeResponse", "<return>true</return>")
+def _volume_matches_filters(vol, filters):
+ """Apply the DescribeVolumes filters, except the tag ones the shared helper handles."""
+ scalars = {
+ "volume-id": vol["VolumeId"],
+ "size": str(vol["Size"]),
+ "status": vol["State"],
+ "volume-type": vol["VolumeType"],
+ "availability-zone": vol["AvailabilityZone"],
+ "snapshot-id": vol.get("SnapshotId") or "",
+ "create-time": vol.get("CreateTime") or "",
+ "encrypted": "true" if vol.get("Encrypted") else "false",
+ "multi-attach-enabled": "true" if vol.get("MultiAttachEnabled") else "false",
+ }
+ for name, value in scalars.items():
+ if filters.get(name) and value not in filters[name]:
+ return False
+ # attachment.* matches when any one attachment matches, like the gateway describes.
+ attachments = vol.get("Attachments", [])
+ for name, key in (("attachment.instance-id", "InstanceId"), ("attachment.device", "Device"),
+ ("attachment.status", "State"), ("attachment.attach-time", "AttachTime")):
+ if filters.get(name) and not any(a.get(key) in filters[name] for a in attachments):
+ return False
+ if filters.get("attachment.delete-on-termination") and not any(
+ ("true" if a.get("DeleteOnTermination") else "false")
+ in filters["attachment.delete-on-termination"] for a in attachments):
+ return False
+ return True
+
+
def _describe_volumes(p):
filter_ids = _parse_member_list(p, "VolumeId")
+ filters = _parse_filters(p)
if filter_ids:
for vid in filter_ids:
if vid not in _volumes:
@@ -2665,6 +2695,10 @@ def _describe_volumes(p):
for vol in _volumes.values():
if filter_ids and vol["VolumeId"] not in filter_ids:
continue
+ if not _resource_matches_tag_filters(vol["VolumeId"], filters):
+ continue
+ if not _volume_matches_filters(vol, filters):
+ continue
items += f"<item>{_volume_inner_xml(vol)}</item>"
return _xml(200, "DescribeVolumesResponse", f"<volumeSet>{items}</volumeSet>")
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index dc6b6169..53711298 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -2747,6 +2747,68 @@ def test_ebs_attach_detach_volume(ec2):
assert desc2["Volumes"][0]["State"] == "available"
assert desc2["Volumes"][0]["Attachments"] == []
+def test_ebs_describe_volumes_honors_filters(ec2):
+ """Filters must narrow the result. Ignoring them returns every volume in the account, so
+ "the volume I tagged for this run" resolves to somebody else's."""
+ tag = _uuid_mod.uuid4().hex[:8]
+ key = f"check-{tag}" # unique, so other tests' volumes cannot match the tag filters
+ zone = ec2.describe_availability_zones()["AvailabilityZones"][0]["ZoneName"]
+ tagged = ec2.create_volume(
+ AvailabilityZone=zone, Size=77, VolumeType="gp3",
+ TagSpecifications=[{"ResourceType": "volume", "Tags": [{"Key": key, "Value": tag}]}],
+ )["VolumeId"]
+ # A second volume differing in size and type: with the filters ignored it comes back too.
+ spare = ec2.create_volume(AvailabilityZone=zone, Size=79, VolumeType="gp2")["VolumeId"]
+ instance_id = ec2.run_instances(ImageId="ami-00000001", MinCount=1, MaxCount=1,
+ )["Instances"][0]["InstanceId"]
+
+ def ids(**kwargs):
+ return sorted(v["VolumeId"] for v in ec2.describe_volumes(**kwargs)["Volumes"])
+
+ try:
+ assert ids(Filters=[{"Name": f"tag:{key}", "Values": [tag]}]) == [tagged]
+ assert ids(Filters=[{"Name": "tag-key", "Values": [key]}]) == [tagged]
+ assert ids(Filters=[{"Name": "size", "Values": ["77"]}]) == [tagged]
+ assert ids(Filters=[{"Name": "volume-id", "Values": [spare]}]) == [spare]
+ assert tagged in ids(Filters=[{"Name": "encrypted", "Values": ["false"]}])
+ # Other tests leave gp2 volumes behind, so this one asserts on membership.
+ by_type = ids(Filters=[{"Name": "volume-type", "Values": ["gp2"]}])
+ assert spare in by_type and tagged not in by_type
+ # Two filter names is an AND, two values for one name an OR.
+ assert ids(Filters=[{"Name": "size", "Values": ["77"]},
+ {"Name": "volume-type", "Values": ["gp2"]}]) == []
+ assert ids(Filters=[{"Name": "volume-id", "Values": [tagged, spare]}]) == sorted(
+ [tagged, spare])
+ # A filter that matches nothing is an empty list, not "everything".
+ assert ids(Filters=[{"Name": "size", "Values": ["4096"]}]) == []
+
+ # attachment.* matches on any one attachment. The instance's own root volume is attached
+ # too, so these assert on membership rather than an exact list -- except the device, which
+ # only this attachment uses.
+ ec2.attach_volume(VolumeId=tagged, InstanceId=instance_id, Device="/dev/xvdz")
+ attached_here = ids(Filters=[{"Name": "attachment.instance-id", "Values": [instance_id]}])
+ assert tagged in attached_here and spare not in attached_here
+ assert ids(Filters=[{"Name": "attachment.device", "Values": ["/dev/xvdz"]}]) == [tagged]
+ in_use = ids(Filters=[{"Name": "status", "Values": ["in-use"]}])
+ assert tagged in in_use and spare not in in_use
+ assert spare in ids(Filters=[{"Name": "status", "Values": ["available"]}])
+ finally:
+ # Cleanup must not mask a failed assertion above: a volume still attached because an
+ # assertion raised first would fail to delete, and that error would be the one reported.
+ try:
+ ec2.detach_volume(VolumeId=tagged)
+ except ClientError:
+ pass
+ try:
+ ec2.terminate_instances(InstanceIds=[instance_id])
+ except ClientError:
+ pass
+ for volume_id in (tagged, spare):
+ try:
+ ec2.delete_volume(VolumeId=volume_id)
+ except ClientError:
+ pass
+
def test_ebs_delete_volume(ec2):
vol = ec2.create_volume(AvailabilityZone="us-east-1a", Size=5, VolumeType="gp2")
vol_id = vol["VolumeId"]