3
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
5
# Permission is hereby granted, free of charge, to any person obtaining a
6
# copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish, dis-
9
# tribute, sublicense, and/or sell copies of the Software, and to permit
10
# persons to whom the Software is furnished to do so, subject to the fol-
13
# The above copyright notice and this permission notice shall be included
14
# in all copies or substantial portions of the Software.
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
18
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
Some unit tests for the EC2Connection
31
from boto.ec2.connection import EC2Connection
35
class EC2ConnectionTest (unittest.TestCase):
37
def test_1_basic(self):
38
# this is my user_id, if you want to run these tests you should
39
# replace this with yours or they won't work
40
user_id = '963068290131'
41
print '--- running EC2Connection tests ---'
43
# get list of private AMI's
44
rs = c.get_all_images(owners=[user_id])
46
# now pick the first one
48
# temporarily make this image runnable by everyone
49
status = image.set_launch_permissions(group_names=['all'])
51
d = image.get_launch_permissions()
52
assert d.has_key('groups')
53
assert len(d['groups']) > 0
54
# now remove that permission
55
status = image.remove_launch_permissions(group_names=['all'])
57
d = image.get_launch_permissions()
58
assert not d.has_key('groups')
60
# create a new security group
61
group_name = 'test-%d' % int(time.time())
62
group_desc = 'This is a security group created during unit testing'
63
group = c.create_security_group(group_name, group_desc)
64
# now get a listing of all security groups and look for our new one
65
rs = c.get_all_security_groups()
68
if g.name == group_name:
71
# now pass arg to filter results to only our new group
72
rs = c.get_all_security_groups([group_name])
76
# now delete the security group
77
status = c.delete_security_group(group_name)
78
# now make sure it's really gone
79
rs = c.get_all_security_groups()
82
if g.name == group_name:
85
# now create it again for use with the instance test
86
group = c.create_security_group(group_name, group_desc)
88
# now try to launch apache image with our new security group
89
rs = c.get_all_images()
90
img_loc = 'ec2-public-images/fedora-core4-apache.manifest.xml'
92
if image.location == img_loc:
94
reservation = image.run(security_groups=[group.name])
95
instance = reservation.instances[0]
96
while instance.state != 'running':
97
print '\tinstance is %s' % instance.state
100
# instance in now running, try to telnet to port 80
101
t = telnetlib.Telnet()
103
t.open(instance.dns_name, 80)
106
# now open up port 80 and try again, it should work
107
group.authorize('tcp', 80, 80, '0.0.0.0/0')
108
t.open(instance.dns_name, 80)
110
# now revoke authorization and try again
111
group.revoke('tcp', 80, 80, '0.0.0.0/0')
113
t.open(instance.dns_name, 80)
116
# now kill the instance and delete the security group
118
# unfortunately, I can't delete the sg within this script
121
# create a new key pair
122
key_name = 'test-%d' % int(time.time())
123
status = c.create_key_pair(key_name)
125
# now get a listing of all key pairs and look for our new one
126
rs = c.get_all_key_pairs()
129
if k.name == key_name:
132
# now pass arg to filter results to only our new key pair
133
rs = c.get_all_key_pairs([key_name])
136
# now delete the key pair
137
status = c.delete_key_pair(key_name)
138
# now make sure it's really gone
139
rs = c.get_all_key_pairs()
142
if k.name == key_name:
146
# short test around Paid AMI capability
147
demo_paid_ami_id = 'ami-bd9d78d4'
148
demo_paid_ami_product_code = 'A79EC0DB'
149
l = c.get_all_images([demo_paid_ami_id])
151
assert len(l[0].product_codes) == 1
152
assert l[0].product_codes[0] == demo_paid_ami_product_code
154
print '--- tests completed ---'