Reputation: 1441
To test that my application handles state transitions correctly, I'd like control over the lifecycle of moto's fake EC2 instances:
Rather than have instances start immediately in running
, it would be nice to have them start in pending
, let me confirm some things, and then explicitly transition them to running
.
Relatedly, there are some actions I'd like to trigger in my tests when the instances switch to running
.
Is any of this possible? I found InstanceBackend
in moto's code -- is there a way for users to hook into or override methods there?
Upvotes: 1
Views: 282
Reputation: 2123
There are a few feature requests for more control over the transition cycle, but nothing has been implemented yet.
It is possible to use the internal API to set the status directly, as you said, using the InstanceBackend
.
If you only have one instance, you can use the following code:
ec2_backend = moto.ec2.models.ec2_backends[my-region]
list(ec2_backend.reservations.values())[0].instances[0].state = "..."
If you have multiple reservations, you can use the reservation ID like this:
ec2_backend.reservations["r-7df1884b"].instances[0].state = "..."
Note that both AWS and Moto use two properties to track state, state
and state_code
. Only updating state
may result in undefined behaviour, so you may want to update both to ensure they are in sync:
ec2_backend.reservations["r-7df1884b"].instances[0].state_code = ..
Note that this is an internal API, so changes to this data structure may occur without warning.
Upvotes: 1