Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC2 client describe_images returning default AMIs even with MOTO_AMIS_PATH set #5677

Closed
dblanchette opened this issue Nov 16, 2022 · 3 comments · Fixed by #5697
Closed

EC2 client describe_images returning default AMIs even with MOTO_AMIS_PATH set #5677

dblanchette opened this issue Nov 16, 2022 · 3 comments · Fixed by #5697
Assignees
Labels

Comments

@dblanchette
Copy link

Context

We are setting MOTO_AMIS_PATH in a pytest plugin. Our test uses describe_images on an EC2 client.

Expected

We expect the images set in the file at the path set in the environment variables MOTO_AMIS_PATH to be the only ones returned.

Actual behavior

The images from MOTO_AMIS_PATH are there, but there are also other images that are hard-coded in the library.

Affected versions

Present in 4.0.9, was not present in 3.1.18. moto installed with poetry (reproduced with moto installed manually in a virtual environment)

Seems to have been introduced by #5427

Code here: https://github.com/spulec/moto/blob/6ab2497a12d8f0a5fe9be91387bc6b589d78e295/moto/ec2/models/amis.py#L157

Reproduction steps

Run the following script

import json
import os
from pathlib import Path

test_ami = [
    {
        "ami_id": "ami-760aaa0f760aaa0fe",
        "name": "infra-eks-20211110080547-bionic",
        "description": "An AMI",
        "owner_id": "123456789012",
        "public": False,
        "virtualization_type": "hvm",
        "architecture": "x86_64",
        "state": "available",
        "platform": None,
        "image_type": "machine",
        "hypervisor": "xen",
        "root_device_name": "/dev/sda1",
        "root_device_type": "ebs",
        "sriov": "simple",
        "creation_date": "2021-11-10T08:13:01.000Z"
    }
]

test_ami_path = Path(__file__).parent / "test_ami.json"

with test_ami_path.open("w") as fp:
    json.dump(test_ami, fp)

os.environ["MOTO_AMIS_PATH"] = str(test_ami_path)

import moto
import boto3

with moto.mock_ec2():
    ec2_client = boto3.client("ec2")
    images = ec2_client.describe_images()["Images"]
    print(f"Found {len(images)} images!")
    assert len(images) == 1

With moto 3.1.18:

$ python ./reproduce_step.py 
Found 1 images!

With moto 4.0.9:

$ python ./reproduce_step.py 
Found 18 images!
Traceback (most recent call last):
  File "./reproduce_step.py", line 39, in <module>
    assert len(images) == 1
AssertionError

Conclusion

A workaround is to use the old version for now. Thanks for your help resolving this issue!

@bblommers
Copy link
Collaborator

Thanks for raising this @dblanchette. The documentation is not explicit in whether MOTO_AMIS_PATH should come instead of, or in addition to, the existing AMI's. But considering the previous behaviour was 'instead of', then this is indeed a regression.

@bblommers bblommers added the bug label Nov 17, 2022
@rafcio19
Copy link
Contributor

@bblommers lemme take a look, assign it to me

@dblanchette
Copy link
Author

I'm late in saying this, but thanks for the quick fix, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants