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

Find a way to add extensions (e.g. callbacks) to moto #7126

Open
retpolanne opened this issue Dec 15, 2023 · 4 comments
Open

Find a way to add extensions (e.g. callbacks) to moto #7126

retpolanne opened this issue Dec 15, 2023 · 4 comments

Comments

@retpolanne
Copy link

I have an environment where I want to test the whole EC2 creation flow, from the EC2 api request to a locally-simulated cloud VM with cloud-init.

I created this tool - https://github.com/retpolanne/cloud-vm-sim - that has an API endpoint that receives a userdata and creates a QEMU VM that can reach to this userdata and run cloud-init. I've extended the EC2 code to call this service on the mock and it works nicely :)

However, I wanted this to be cleaner - I don't want moto to have this code as this is useful just for me. I wanted to be able to specify a plugins directory and load them and call whatever is in my plugin before the EC2 response.

@retpolanne
Copy link
Author

My code for cloud-sim call is here - 882e010

@retpolanne retpolanne changed the title Find a way to add extensions (i.e. callbacks) to moto Find a way to add extensions (e.g. callbacks) to moto Dec 15, 2023
@bblommers
Copy link
Collaborator

Hi @retpolanne! Having an extensions/plugin system is quite the implementation/maintenance effort, and I'm not sure there would be enough usage to justify this.

As an alternative solution:
Would it work to use mock Moto, and extend the functionality that way? That looks like a simpler solution, and achieves the same result:

original_add_instance = InstanceBackend.add_instances
def add_instance_and_run_code(self, image_id, min_count, user_data, security_group_names, **kwargs):
    print("call vm as desired")
    return original_add_instance(self, image_id, min_count, user_data, security_group_names, **kwargs)


@mock_ec2
@mock.patch.object(InstanceBackend, "add_instances", add_instance_and_run_code)
def test_instance_launch_and_terminate():
    client = boto3.client("ec2", region_name="us-east-1")
    # whatever regular test you have...

@retpolanne
Copy link
Author

@bblommers thanks! In my case, I'm using moto as a standalone server for apps running in another language, so that wouldn't really work :c

@bblommers
Copy link
Collaborator

Ah, I understand. That makes a bit more difficult.

I'll mark it as a feature request, let's see if we can come up with a solution for this.

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

No branches or pull requests

2 participants