Merge pull request #185 from bobbyi/filter_instances_by_tag

Support filtering instances by tag
This commit is contained in:
Steve Pulec 2014-09-08 23:19:43 -04:00
commit 3feaa39bd3
2 changed files with 50 additions and 3 deletions

View file

@ -130,6 +130,47 @@ def test_get_instances_filtering_by_instance_id():
reservations = conn.get_all_instances(filters={'instance-id': 'non-existing-id'})
reservations.should.have.length_of(0)
@mock_ec2
def test_get_instances_filtering_by_tag():
conn = boto.connect_ec2()
reservation = conn.run_instances('ami-1234abcd', min_count=3)
instance1, instance2, instance3 = reservation.instances
instance1.add_tag('tag1', 'value1')
instance1.add_tag('tag2', 'value2')
instance2.add_tag('tag1', 'value1')
instance2.add_tag('tag2', 'wrong value')
instance3.add_tag('tag2', 'value2')
reservations = conn.get_all_instances(filters={'tag:tag0' : 'value0'})
# get_all_instances should return no instances
reservations.should.have.length_of(0)
reservations = conn.get_all_instances(filters={'tag:tag1' : 'value1'})
# get_all_instances should return both instances with this tag value
reservations.should.have.length_of(1)
reservations[0].instances.should.have.length_of(2)
reservations[0].instances[0].id.should.equal(instance1.id)
reservations[0].instances[1].id.should.equal(instance2.id)
reservations = conn.get_all_instances(filters={'tag:tag1' : 'value1', 'tag:tag2' : 'value2'})
# get_all_instances should return the instance with both tag values
reservations.should.have.length_of(1)
reservations[0].instances.should.have.length_of(1)
reservations[0].instances[0].id.should.equal(instance1.id)
reservations = conn.get_all_instances(filters={'tag:tag1' : 'value1', 'tag:tag2' : 'value2'})
# get_all_instances should return the instance with both tag values
reservations.should.have.length_of(1)
reservations[0].instances.should.have.length_of(1)
reservations[0].instances[0].id.should.equal(instance1.id)
reservations = conn.get_all_instances(filters={'tag:tag2' : ['value2', 'bogus']})
# get_all_instances should return both instances with one of the acceptable tag values
reservations.should.have.length_of(1)
reservations[0].instances.should.have.length_of(2)
reservations[0].instances[0].id.should.equal(instance1.id)
reservations[0].instances[1].id.should.equal(instance3.id)
@mock_ec2
def test_instance_start_and_stop():
conn = boto.connect_ec2('the_key', 'the_secret')