2013-05-17 23:35:53 +00:00
|
|
|
import base64
|
|
|
|
|
2013-02-18 21:09:40 +00:00
|
|
|
import boto
|
2013-02-20 04:01:13 +00:00
|
|
|
from boto.ec2.instance import Reservation, InstanceAttribute
|
2013-03-05 13:14:43 +00:00
|
|
|
import sure # flake8: noqa
|
2013-02-18 21:09:40 +00:00
|
|
|
|
|
|
|
from moto import mock_ec2
|
|
|
|
|
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
################ Test Readme ###############
|
|
|
|
def add_servers(ami_id, count):
|
2013-06-27 04:01:33 +00:00
|
|
|
conn = boto.connect_ec2()
|
2013-03-05 13:14:43 +00:00
|
|
|
for index in range(count):
|
|
|
|
conn.run_instances(ami_id)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_add_servers():
|
|
|
|
add_servers('ami-1234abcd', 2)
|
|
|
|
|
2013-06-27 04:01:33 +00:00
|
|
|
conn = boto.connect_ec2()
|
2013-03-05 13:14:43 +00:00
|
|
|
reservations = conn.get_all_instances()
|
|
|
|
assert len(reservations) == 2
|
|
|
|
instance1 = reservations[0].instances[0]
|
|
|
|
assert instance1.image_id == 'ami-1234abcd'
|
|
|
|
|
|
|
|
############################################
|
|
|
|
|
|
|
|
|
2013-02-18 21:09:40 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_instance_launch_and_terminate():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
2013-02-18 21:09:40 +00:00
|
|
|
reservation.should.be.a(Reservation)
|
|
|
|
reservation.instances.should.have.length_of(1)
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances()
|
|
|
|
reservations.should.have.length_of(1)
|
|
|
|
reservations[0].id.should.equal(reservation.id)
|
|
|
|
instances = reservations[0].instances
|
|
|
|
instances.should.have.length_of(1)
|
|
|
|
instances[0].id.should.equal(instance.id)
|
|
|
|
instances[0].state.should.equal('pending')
|
|
|
|
|
2013-02-28 05:08:35 +00:00
|
|
|
conn.terminate_instances([instances[0].id])
|
2013-02-18 21:09:40 +00:00
|
|
|
|
|
|
|
reservations = conn.get_all_instances()
|
|
|
|
instance = reservations[0].instances[0]
|
|
|
|
instance.state.should.equal('shutting-down')
|
2013-02-19 02:56:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_instance_start_and_stop():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd', min_count=2)
|
2013-02-19 02:56:22 +00:00
|
|
|
instances = reservation.instances
|
2013-02-19 04:06:23 +00:00
|
|
|
instances.should.have.length_of(2)
|
2013-02-19 02:56:22 +00:00
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
instance_ids = [instance.id for instance in instances]
|
|
|
|
stopped_instances = conn.stop_instances(instance_ids)
|
2013-02-19 02:56:22 +00:00
|
|
|
|
|
|
|
for instance in stopped_instances:
|
|
|
|
instance.state.should.equal('stopping')
|
|
|
|
|
2013-02-28 05:08:35 +00:00
|
|
|
started_instances = conn.start_instances([instances[0].id])
|
2013-02-19 02:56:22 +00:00
|
|
|
started_instances[0].state.should.equal('pending')
|
2013-02-19 04:06:23 +00:00
|
|
|
|
2013-02-20 04:01:13 +00:00
|
|
|
|
2013-02-20 04:55:01 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_instance_reboot():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
2013-02-20 04:55:01 +00:00
|
|
|
instance = reservation.instances[0]
|
|
|
|
instance.reboot()
|
|
|
|
instance.state.should.equal('pending')
|
|
|
|
|
|
|
|
|
2013-02-20 04:01:13 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_instance_attribute_instance_type():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
2013-02-20 04:01:13 +00:00
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.modify_attribute("instanceType", "m1.small")
|
|
|
|
|
|
|
|
instance_attribute = instance.get_attribute("instanceType")
|
|
|
|
instance_attribute.should.be.a(InstanceAttribute)
|
|
|
|
instance_attribute.get('instanceType').should.equal("m1.small")
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_instance_attribute_user_data():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2013-03-05 13:14:43 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd')
|
2013-02-20 04:01:13 +00:00
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.modify_attribute("userData", "this is my user data")
|
|
|
|
|
|
|
|
instance_attribute = instance.get_attribute("userData")
|
|
|
|
instance_attribute.should.be.a(InstanceAttribute)
|
2013-03-05 13:14:43 +00:00
|
|
|
instance_attribute.get("userData").should.equal("this is my user data")
|
2013-05-17 23:35:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_user_data_with_run_instance():
|
|
|
|
user_data = "some user data"
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', user_data=user_data)
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance_attribute = instance.get_attribute("userData")
|
|
|
|
instance_attribute.should.be.a(InstanceAttribute)
|
|
|
|
decoded_user_data = base64.decodestring(instance_attribute.get("userData"))
|
|
|
|
decoded_user_data.should.equal("some user data")
|