2014-08-25 22:09:38 +00:00
|
|
|
# Ensure 'assert_raises' context manager support for Python 2.6
|
|
|
|
import tests.backport_assert_raises
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
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-07-09 01:18:05 +00:00
|
|
|
from boto.exception import EC2ResponseError
|
2013-08-03 21:21:25 +00:00
|
|
|
import sure # 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]
|
2013-08-28 14:19:12 +00:00
|
|
|
instance.state.should.equal('pending')
|
2013-02-18 21:09:40 +00:00
|
|
|
|
|
|
|
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)
|
2013-08-28 14:19:12 +00:00
|
|
|
instances[0].state.should.equal('running')
|
2013-02-18 21:09:40 +00:00
|
|
|
|
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]
|
2013-08-28 14:19:12 +00:00
|
|
|
instance.state.should.equal('terminated')
|
2013-02-19 02:56:22 +00:00
|
|
|
|
|
|
|
|
2013-07-08 23:25:47 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_get_instances_by_id():
|
|
|
|
conn = boto.connect_ec2()
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', min_count=2)
|
|
|
|
instance1, instance2 = reservation.instances
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances(instance_ids=[instance1.id])
|
|
|
|
reservations.should.have.length_of(1)
|
|
|
|
reservation = reservations[0]
|
|
|
|
reservation.instances.should.have.length_of(1)
|
|
|
|
reservation.instances[0].id.should.equal(instance1.id)
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances(instance_ids=[instance1.id, instance2.id])
|
|
|
|
reservations.should.have.length_of(1)
|
|
|
|
reservation = reservations[0]
|
|
|
|
reservation.instances.should.have.length_of(2)
|
|
|
|
instance_ids = [instance.id for instance in reservation.instances]
|
|
|
|
instance_ids.should.equal([instance1.id, instance2.id])
|
|
|
|
|
2013-07-09 01:18:05 +00:00
|
|
|
# Call get_all_instances with a bad id should raise an error
|
2014-08-25 17:54:47 +00:00
|
|
|
with assert_raises(EC2ResponseError) as cm:
|
|
|
|
conn.get_all_instances(instance_ids=[instance1.id, "i-1234abcd"])
|
|
|
|
cm.exception.code.should.equal('InvalidInstanceID.NotFound')
|
|
|
|
cm.exception.status.should.equal(400)
|
|
|
|
cm.exception.request_id.should_not.be.none
|
2013-07-09 01:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_get_instances_filtering_by_state():
|
|
|
|
conn = boto.connect_ec2()
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', min_count=3)
|
|
|
|
instance1, instance2, instance3 = reservation.instances
|
|
|
|
|
|
|
|
conn.terminate_instances([instance1.id])
|
|
|
|
|
2013-08-28 14:19:12 +00:00
|
|
|
reservations = conn.get_all_instances(filters={'instance-state-name': 'running'})
|
2013-07-09 01:18:05 +00:00
|
|
|
reservations.should.have.length_of(1)
|
|
|
|
# Since we terminated instance1, only instance2 and instance3 should be returned
|
|
|
|
instance_ids = [instance.id for instance in reservations[0].instances]
|
|
|
|
set(instance_ids).should.equal(set([instance2.id, instance3.id]))
|
|
|
|
|
2013-08-28 14:19:12 +00:00
|
|
|
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'running'})
|
2013-07-09 01:18:05 +00:00
|
|
|
reservations.should.have.length_of(1)
|
|
|
|
instance_ids = [instance.id for instance in reservations[0].instances]
|
|
|
|
instance_ids.should.equal([instance2.id])
|
|
|
|
|
2013-08-28 14:19:12 +00:00
|
|
|
reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'terminated'})
|
2013-07-09 02:25:25 +00:00
|
|
|
list(reservations).should.equal([])
|
|
|
|
|
2013-07-09 02:20:55 +00:00
|
|
|
# get_all_instances should still return all 3
|
|
|
|
reservations = conn.get_all_instances()
|
|
|
|
reservations[0].instances.should.have.length_of(3)
|
|
|
|
|
2013-07-09 01:18:05 +00:00
|
|
|
conn.get_all_instances.when.called_with(filters={'not-implemented-filter': 'foobar'}).should.throw(NotImplementedError)
|
|
|
|
|
2014-06-05 09:12:42 +00:00
|
|
|
@mock_ec2
|
|
|
|
def test_get_instances_filtering_by_instance_id():
|
|
|
|
conn = boto.connect_ec2()
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', min_count=3)
|
|
|
|
instance1, instance2, instance3 = reservation.instances
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances(filters={'instance-id': instance1.id})
|
|
|
|
# get_all_instances should return just instance1
|
|
|
|
reservations[0].instances.should.have.length_of(1)
|
|
|
|
reservations[0].instances[0].id.should.equal(instance1.id)
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances(filters={'instance-id': [instance1.id, instance2.id]})
|
|
|
|
# get_all_instances should return two
|
|
|
|
reservations[0].instances.should.have.length_of(2)
|
|
|
|
|
|
|
|
reservations = conn.get_all_instances(filters={'instance-id': 'non-existing-id'})
|
|
|
|
reservations.should.have.length_of(0)
|
2013-07-08 23:25:47 +00:00
|
|
|
|
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")
|
2014-03-22 18:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
2014-05-07 13:16:28 +00:00
|
|
|
def test_run_instance_with_security_group_name():
|
2014-03-22 18:02:47 +00:00
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
group = conn.create_security_group('group1', "some description")
|
|
|
|
|
2014-05-07 13:16:28 +00:00
|
|
|
reservation = conn.run_instances('ami-1234abcd',
|
|
|
|
security_groups=['group1'])
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.groups[0].id.should.equal(group.id)
|
|
|
|
instance.groups[0].name.should.equal("group1")
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_run_instance_with_security_group_id():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
group = conn.create_security_group('group1', "some description")
|
|
|
|
|
|
|
|
reservation = conn.run_instances('ami-1234abcd',
|
|
|
|
security_group_ids=[group.id])
|
2014-03-22 18:02:47 +00:00
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.groups[0].id.should.equal(group.id)
|
|
|
|
instance.groups[0].name.should.equal("group1")
|
2014-05-07 12:36:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_run_instance_with_instance_type():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', instance_type="t1.micro")
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.instance_type.should.equal("t1.micro")
|
2014-05-07 12:47:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_run_instance_with_subnet():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
reservation = conn.run_instances('ami-1234abcd',
|
|
|
|
subnet_id="subnet-abcd1234")
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.subnet_id.should.equal("subnet-abcd1234")
|
2014-05-07 12:54:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_run_instance_with_keypair():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', key_name="keypair_name")
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
instance.key_name.should.equal("keypair_name")
|
2014-08-20 17:52:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_describe_instance_status_no_instances():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
all_status = conn.get_all_instance_status()
|
|
|
|
len(all_status).should.equal(0)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_describe_instance_status_with_instances():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
conn.run_instances('ami-1234abcd', key_name="keypair_name")
|
|
|
|
|
|
|
|
all_status = conn.get_all_instance_status()
|
|
|
|
len(all_status).should.equal(1)
|
|
|
|
all_status[0].instance_status.status.should.equal('ok')
|
|
|
|
all_status[0].system_status.status.should.equal('ok')
|
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
def test_describe_instance_status_with_instance_filter():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
|
|
|
|
# We want to filter based on this one
|
|
|
|
reservation = conn.run_instances('ami-1234abcd', key_name="keypair_name")
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
|
|
|
|
# This is just to setup the test
|
|
|
|
conn.run_instances('ami-1234abcd', key_name="keypair_name")
|
|
|
|
|
|
|
|
all_status = conn.get_all_instance_status(instance_ids=[instance.id])
|
|
|
|
len(all_status).should.equal(1)
|
2014-08-25 20:43:23 +00:00
|
|
|
all_status[0].id.should.equal(instance.id)
|
2014-08-25 21:00:35 +00:00
|
|
|
|
|
|
|
# Call get_all_instance_status with a bad id should raise an error
|
|
|
|
with assert_raises(EC2ResponseError) as cm:
|
|
|
|
conn.get_all_instance_status(instance_ids=[instance.id, "i-1234abcd"])
|
|
|
|
cm.exception.code.should.equal('InvalidInstanceID.NotFound')
|
|
|
|
cm.exception.status.should.equal(400)
|
|
|
|
cm.exception.request_id.should_not.be.none
|