~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/ec2/tests/test_client.py

  • Committer: Duncan McGreggor
  • Date: 2009-11-22 02:20:42 UTC
  • mto: (44.3.2 484858-s3-scripts)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: duncan@canonical.com-20091122022042-4zi231hxni1z53xd
* Updated the LICENSE file with copyright information.
* Updated the README with license information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
from twisted.web.error import Error as TwistedWebError
16
16
from twisted.protocols.policies import WrappingFactory
17
17
 
18
 
from txaws.util import iso8601time
19
18
from txaws.credentials import AWSCredentials
20
19
from txaws.ec2 import client
21
20
from txaws.ec2 import model
40
39
 
41
40
    def test_instance_creation(self):
42
41
        instance = model.Instance(
43
 
            "id1", "running", "type", "id2", "dns1", "dns2", "ip1",
44
 
            "ip2", "key", "ami", "time", "placement",
45
 
            ["prod1", "prod2"], "id3", "id4")
 
42
            "id1", "running", "type", "id2", "dns1", "dns2", "key", "ami",
 
43
            "time", "placement", ["prod1", "prod2"], "id3", "id4")
46
44
        self.assertEquals(instance.instance_id, "id1")
47
45
        self.assertEquals(instance.instance_state, "running")
48
46
        self.assertEquals(instance.instance_type, "type")
49
47
        self.assertEquals(instance.image_id, "id2")
50
48
        self.assertEquals(instance.private_dns_name, "dns1")
51
49
        self.assertEquals(instance.dns_name, "dns2")
52
 
        self.assertEquals(instance.private_ip_address, "ip1")
53
 
        self.assertEquals(instance.ip_address, "ip2")
54
50
        self.assertEquals(instance.key_name, "key")
55
51
        self.assertEquals(instance.ami_launch_index, "ami")
56
52
        self.assertEquals(instance.launch_time, "time")
68
64
        ec2 = client.EC2Client()
69
65
        self.assertNotEqual(None, ec2.creds)
70
66
 
71
 
    def test_post_method(self):
72
 
        """
73
 
        If the method of the endpoint is POST, the parameters are passed in the
74
 
        body.
75
 
        """
76
 
        self.addCleanup(setattr, client.Query, "get_page",
77
 
                        client.Query.get_page)
78
 
 
79
 
        def get_page(query, url, *args, **kwargs):
80
 
            self.assertEquals(args, ())
81
 
            self.assertEquals(
82
 
                kwargs["headers"],
83
 
                {"Content-Type": "application/x-www-form-urlencoded"})
84
 
            self.assertIn("postdata", kwargs)
85
 
            self.assertEquals(kwargs["method"], "POST")
86
 
            self.assertEquals(kwargs["timeout"], 30)
87
 
            return succeed(payload.sample_describe_instances_result)
88
 
 
89
 
        client.Query.get_page = get_page
90
 
 
91
 
        creds = AWSCredentials("foo", "bar")
92
 
        endpoint = AWSServiceEndpoint(uri=EC2_ENDPOINT_US, method="POST")
93
 
        ec2 = client.EC2Client(creds=creds, endpoint=endpoint)
94
 
        return ec2.describe_instances()
95
 
 
96
67
    def test_init_no_creds_non_available_errors(self):
97
68
        self.assertRaises(ValueError, client.EC2Client)
98
69
 
102
73
        self.assertEqual(creds, ec2.creds)
103
74
 
104
75
    def test_describe_availability_zones_single(self):
105
 
 
106
76
        class StubQuery(object):
107
 
 
108
 
            def __init__(stub, action="", creds=None, endpoint=None,
109
 
                         other_params={}):
 
77
            def __init__(stub, action, creds, endpoint, other_params):
110
78
                self.assertEqual(action, "DescribeAvailabilityZones")
111
79
                self.assertEqual(creds.access_key, "foo")
112
80
                self.assertEqual(creds.secret_key, "bar")
113
81
                self.assertEqual(
114
 
                    other_params,
115
 
                    {"ZoneName.1": "us-east-1a"})
116
 
 
 
82
                    {"ZoneName.1": "us-east-1a"},
 
83
                    other_params)
117
84
            def submit(self):
118
85
                return succeed(
119
86
                    payload.sample_describe_availability_zones_single_result)
124
91
            self.assertEquals(zone.name, "us-east-1a")
125
92
            self.assertEquals(zone.state, "available")
126
93
 
 
94
 
127
95
        creds = AWSCredentials("foo", "bar")
128
96
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
129
97
        d = ec2.describe_availability_zones(["us-east-1a"])
131
99
        return d
132
100
 
133
101
    def test_describe_availability_zones_multiple(self):
134
 
 
135
102
        class StubQuery(object):
136
 
 
137
 
            def __init__(stub, action="", creds=None, endpoint=None,
138
 
                         other_params={}):
 
103
            def __init__(stub, action, creds, endpoint, other_params):
139
104
                self.assertEqual(action, "DescribeAvailabilityZones")
140
105
                self.assertEqual(creds.access_key, "foo")
141
106
                self.assertEqual(creds.secret_key, "bar")
142
 
 
143
107
            def submit(self):
144
108
                return succeed(
145
 
                    payload.
146
 
                        sample_describe_availability_zones_multiple_results)
 
109
                    payload.sample_describe_availability_zones_multiple_results)
147
110
 
148
111
        def check_parsed_availability_zones(results):
149
112
            self.assertEquals(len(results), 3)
171
134
        self.assertEquals(reservation.owner_id, "123456789012")
172
135
        # check groups
173
136
        group = reservation.groups[0]
174
 
        self.assertEquals(group[0], "sg-64f9eb08")
175
 
        self.assertEquals(group[1], "default")
 
137
        self.assertEquals(group, "default")
176
138
        # check instance
177
139
        self.assertEquals(instance.instance_id, "i-abcdef01")
178
140
        self.assertEquals(instance.instance_state, "running")
184
146
        self.assertEquals(
185
147
            instance.dns_name,
186
148
            "ec2-75-101-245-65.compute-1.amazonaws.com")
187
 
        self.assertEquals(instance.private_ip_address, "10.0.0.1")
188
 
        self.assertEquals(instance.ip_address, "75.101.245.65")
189
149
        self.assertEquals(instance.key_name, "keyname")
190
150
        self.assertEquals(instance.ami_launch_index, "0")
191
151
        self.assertEquals(instance.launch_time, "2009-04-27T02:23:18.000Z")
202
162
        self.assertEquals(reservation.owner_id, "123456789012")
203
163
        # check groups
204
164
        group = reservation.groups[0]
205
 
        self.assertEquals(group[0], "sg-64f9eb08")
206
 
        self.assertEquals(group[1], "default")
 
165
        self.assertEquals(group, "default")
207
166
        # check instance
208
167
        self.assertEquals(instance.instance_id, "i-abcdef01")
209
168
        self.assertEquals(instance.instance_state, "running")
215
174
        self.assertEquals(
216
175
            instance.dns_name,
217
176
            "ec2-75-101-245-65.compute-1.amazonaws.com")
218
 
        self.assertEquals(instance.private_ip_address, "10.0.0.1")
219
 
        self.assertEquals(instance.ip_address, "75.101.245.65")
220
177
        self.assertEquals(instance.key_name, None)
221
178
        self.assertEquals(instance.ami_launch_index, None)
222
179
        self.assertEquals(instance.launch_time, "2009-04-27T02:23:18.000Z")
228
185
    def test_parse_reservation(self):
229
186
        creds = AWSCredentials("foo", "bar")
230
187
        ec2 = client.EC2Client(creds=creds)
231
 
        results = ec2.parser.describe_instances(
 
188
        results = ec2._parse_describe_instances(
232
189
            payload.sample_describe_instances_result)
233
190
        self.check_parsed_instances(results)
234
191
 
235
192
    def test_describe_instances(self):
236
 
 
237
193
        class StubQuery(object):
238
 
 
239
 
            def __init__(stub, action="", creds=None, endpoint=None,
240
 
                         other_params={}):
 
194
            def __init__(stub, action, creds, endpoint, params):
241
195
                self.assertEqual(action, "DescribeInstances")
242
196
                self.assertEqual(creds.access_key, "foo")
243
197
                self.assertEqual(creds.secret_key, "bar")
244
 
                self.assertEquals(other_params, {})
245
 
 
 
198
                self.assertEquals(params, {})
246
199
            def submit(self):
247
200
                return succeed(payload.sample_describe_instances_result)
248
 
 
249
201
        creds = AWSCredentials("foo", "bar")
250
202
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
251
203
        d = ec2.describe_instances()
253
205
        return d
254
206
 
255
207
    def test_describe_instances_required(self):
256
 
 
257
208
        class StubQuery(object):
258
 
 
259
 
            def __init__(stub, action="", creds=None, endpoint=None,
260
 
                         other_params={}):
 
209
            def __init__(stub, action, creds, endpoint, params):
261
210
                self.assertEqual(action, "DescribeInstances")
262
211
                self.assertEqual(creds.access_key, "foo")
263
212
                self.assertEqual(creds.secret_key, "bar")
264
 
                self.assertEquals(other_params, {})
265
 
 
 
213
                self.assertEquals(params, {})
266
214
            def submit(self):
267
215
                return succeed(
268
216
                    payload.sample_required_describe_instances_result)
269
 
 
270
217
        creds = AWSCredentials("foo", "bar")
271
218
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
272
219
        d = ec2.describe_instances()
274
221
        return d
275
222
 
276
223
    def test_describe_instances_specific_instances(self):
277
 
 
278
224
        class StubQuery(object):
279
 
 
280
 
            def __init__(stub, action="", creds=None, endpoint=None,
281
 
                         other_params={}):
 
225
            def __init__(stub, action, creds, endpoint, params):
282
226
                self.assertEqual(action, "DescribeInstances")
283
227
                self.assertEqual(creds.access_key, "foo")
284
228
                self.assertEqual(creds.secret_key, "bar")
285
229
                self.assertEquals(
286
 
                    other_params,
 
230
                    params,
287
231
                    {"InstanceId.1": "i-16546401",
288
232
                     "InstanceId.2": "i-49873415"})
289
 
 
290
233
            def submit(self):
291
234
                return succeed(
292
235
                    payload.sample_required_describe_instances_result)
293
 
 
294
236
        creds = AWSCredentials("foo", "bar")
295
237
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
296
238
        d = ec2.describe_instances("i-16546401", "i-49873415")
298
240
        return d
299
241
 
300
242
    def test_terminate_instances(self):
301
 
 
302
243
        class StubQuery(object):
303
 
 
304
 
            def __init__(stub, action="", creds=None, endpoint=None,
305
 
                         other_params={}):
 
244
            def __init__(stub, action, creds, endpoint, other_params):
306
245
                self.assertEqual(action, "TerminateInstances")
307
246
                self.assertEqual(creds.access_key, "foo")
308
247
                self.assertEqual(creds.secret_key, "bar")
309
248
                self.assertEqual(
310
 
                    other_params,
311
 
                    {"InstanceId.1": "i-1234", "InstanceId.2": "i-5678"})
312
 
 
 
249
                    {"InstanceId.1": "i-1234", "InstanceId.2": "i-5678"},
 
250
                    other_params)
313
251
            def submit(self):
314
252
                return succeed(payload.sample_terminate_instances_result)
315
 
 
 
253
        creds = AWSCredentials("foo", "bar")
 
254
        endpoint = AWSServiceEndpoint(uri=EC2_ENDPOINT_US)
 
255
        ec2 = client.EC2Client(creds=creds, endpoint=endpoint,
 
256
                               query_factory=StubQuery)
 
257
        d = ec2.terminate_instances("i-1234", "i-5678")
316
258
        def check_transition(changes):
317
259
            self.assertEqual([("i-1234", "running", "shutting-down"),
318
260
                ("i-5678", "shutting-down", "shutting-down")], sorted(changes))
319
 
 
320
 
        creds = AWSCredentials("foo", "bar")
321
 
        endpoint = AWSServiceEndpoint(uri=EC2_ENDPOINT_US)
322
 
        ec2 = client.EC2Client(creds=creds, endpoint=endpoint,
323
 
                               query_factory=StubQuery)
324
 
        d = ec2.terminate_instances("i-1234", "i-5678")
325
261
        d.addCallback(check_transition)
326
262
        return d
327
263
 
333
269
        self.assertEquals(reservation.owner_id, "495219933132")
334
270
        # check groups
335
271
        group = reservation.groups[0]
336
 
        self.assertEquals(group[0], "sg-64f9eb08")
337
 
        self.assertEquals(group[1], "default")
 
272
        self.assertEquals(group, "default")
338
273
        # check instance
339
274
        self.assertEquals(instance.instance_id, "i-2ba64342")
340
275
        self.assertEquals(instance.instance_state, "pending")
354
289
    def test_run_instances(self):
355
290
 
356
291
        class StubQuery(object):
357
 
 
358
 
            def __init__(stub, action="", creds=None, endpoint=None,
359
 
                         other_params={}):
 
292
            def __init__(stub, action, creds, endpoint, params):
360
293
                self.assertEqual(action, "RunInstances")
361
294
                self.assertEqual(creds.access_key, "foo")
362
295
                self.assertEqual(creds.secret_key, "bar")
363
296
                self.assertEquals(
364
 
                    other_params,
 
297
                    params,
365
298
                    {"ImageId": "ami-1234", "MaxCount": "2", "MinCount": "1",
366
299
                     "SecurityGroup.1": u"group1", "KeyName": u"default",
367
300
                     "UserData": "Zm9v", "InstanceType": u"m1.small",
368
301
                     "Placement.AvailabilityZone": u"us-east-1b",
369
302
                     "KernelId": u"k-1234", "RamdiskId": u"r-1234"})
370
 
 
371
303
            def submit(self):
372
304
                return succeed(
373
305
                    payload.sample_run_instances_result)
380
312
            ramdisk_id=u"r-1234")
381
313
        d.addCallback(self.check_parsed_run_instances)
382
314
 
383
 
    def test_run_instances_with_subnet(self):
384
 
        class StubQuery(object):
385
 
            def __init__(stub, action="", creds=None, endpoint=None,
386
 
                         other_params={}):
387
 
                self.assertEqual(action, "RunInstances")
388
 
                self.assertEqual(creds.access_key, "foo")
389
 
                self.assertEqual(creds.secret_key, "bar")
390
 
                self.assertEquals(
391
 
                    other_params,
392
 
                    {"ImageId": "ami-1234", "MaxCount": "2", "MinCount": "1",
393
 
                     "SecurityGroupId.1": u"sg-a72d9f92e", "KeyName": u"default",
394
 
                     "UserData": "Zm9v", "InstanceType": u"m1.small",
395
 
                     "Placement.AvailabilityZone": u"us-east-1b",
396
 
                     "KernelId": u"k-1234", "RamdiskId": u"r-1234",
397
 
                     "SubnetId": "subnet-a72d829f"})
398
 
 
399
 
            def submit(self):
400
 
                return succeed(
401
 
                    payload.sample_run_instances_result)
402
 
 
403
 
        creds = AWSCredentials("foo", "bar")
404
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
405
 
        d = ec2.run_instances("ami-1234", 1, 2, security_group_ids=[u"sg-a72d9f92e"],
406
 
            key_name=u"default", user_data=u"foo", instance_type=u"m1.small",
407
 
            availability_zone=u"us-east-1b", kernel_id=u"k-1234",
408
 
            ramdisk_id=u"r-1234", subnet_id="subnet-a72d829f")
409
 
        d.addCallback(self.check_parsed_run_instances)
410
 
 
411
 
    def test_run_instances_with_subnet_but_without_secgroup_id(self):
412
 
        creds = AWSCredentials("foo", "bar")
413
 
        ec2 = client.EC2Client(creds)
414
 
        error = self.assertRaises(ValueError, ec2.run_instances, "ami-1234", 1, 2,
415
 
            key_name=u"default", user_data=u"foo", instance_type=u"m1.small",
416
 
            availability_zone=u"us-east-1b", kernel_id=u"k-1234",
417
 
            ramdisk_id=u"r-1234", subnet_id="subnet-a72d829f")
418
 
        self.assertEqual(
419
 
            str(error),
420
 
            "You must specify the security_group_ids with the subnet_id"
421
 
        )
422
 
 
423
 
    def test_run_instances_without_subnet_and_secgroups(self):
424
 
        creds = AWSCredentials("foo", "bar")
425
 
        ec2 = client.EC2Client(creds)
426
 
        error = self.assertRaises(ValueError, ec2.run_instances, "ami-1234", 1, 2,
427
 
            key_name=u"default", user_data=u"foo", instance_type=u"m1.small",
428
 
            availability_zone=u"us-east-1b", kernel_id=u"k-1234",
429
 
            ramdisk_id=u"r-1234")
430
 
        self.assertEqual(
431
 
            str(error),
432
 
            ("You must specify either the subnet_id and "
433
 
             "security_group_ids or security_groups")
434
 
        )
435
 
 
436
315
 
437
316
class EC2ClientSecurityGroupsTestCase(TXAWSTestCase):
438
317
 
443
322
        using XML data received from the cloud.
444
323
        """
445
324
        class StubQuery(object):
446
 
 
447
 
            def __init__(stub, action="", creds=None, endpoint=None,
448
 
                         other_params={}):
 
325
            def __init__(stub, action, creds, endpoint, other_params=None):
449
326
                self.assertEqual(action, "DescribeSecurityGroups")
450
327
                self.assertEqual(creds.access_key, "foo")
451
328
                self.assertEqual(creds.secret_key, "bar")
452
 
                self.assertEqual(other_params, {})
453
 
 
 
329
                self.assertEqual(other_params, None)
454
330
            def submit(self):
455
331
                return succeed(payload.sample_describe_security_groups_result)
456
332
 
457
333
        def check_results(security_groups):
458
334
            [security_group] = security_groups
459
 
            self.assertEquals(security_group.id, "sg-a1a1a1")
460
335
            self.assertEquals(security_group.owner_id,
461
336
                              "UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM")
462
337
            self.assertEquals(security_group.name, "WebServers")
479
354
        information about more than one L{SecurityGroup}.
480
355
        """
481
356
        class StubQuery(object):
482
 
 
483
 
            def __init__(stub, action="", creds=None, endpoint=None,
484
 
                         other_params={}):
 
357
            def __init__(stub, action, creds, endpoint, other_params=None):
485
358
                self.assertEqual(action, "DescribeSecurityGroups")
486
359
                self.assertEqual(creds.access_key, "foo")
487
360
                self.assertEqual(creds.secret_key, "bar")
488
 
                self.assertEqual(other_params, {})
489
 
 
 
361
                self.assertEqual(other_params, None)
490
362
            def submit(self):
491
363
                return succeed(
492
364
                    payload.sample_describe_security_groups_multiple_result)
497
369
            security_group = security_groups[0]
498
370
            self.assertEquals(security_group.owner_id,
499
371
                              "UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM")
500
 
            self.assertEquals(security_group.id, "sg-a1a1a1")
501
372
            self.assertEquals(security_group.name, "MessageServers")
502
373
            self.assertEquals(security_group.description, "Message Servers")
503
374
            self.assertEquals(security_group.allowed_groups, [])
509
380
            security_group = security_groups[1]
510
381
            self.assertEquals(security_group.owner_id,
511
382
                              "UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM")
512
 
            self.assertEquals(security_group.id, "sg-c3c3c3")
513
383
            self.assertEquals(security_group.name, "WebServers")
514
384
            self.assertEquals(security_group.description, "Web Servers")
515
385
            self.assertEquals([(pair.user_id, pair.group_name)
516
386
                               for pair in security_group.allowed_groups],
517
 
                              [("group-user-id", "group-name1"),
518
 
                               ("group-user-id", "group-name2")])
 
387
                              [("group-user-id", "group-name")])
519
388
            self.assertEquals(
520
389
                [(ip.ip_protocol, ip.from_port, ip.to_port, ip.cidr_ip)
521
390
                 for ip in security_group.allowed_ips],
526
395
        d = ec2.describe_security_groups()
527
396
        return d.addCallback(check_results)
528
397
 
529
 
    def test_describe_security_groups_with_multiple_groups(self):
530
 
        """
531
 
        Several groups can be contained in a single ip permissions content, and
532
 
        there are recognized by the group parser.
533
 
        """
534
 
        class StubQuery(object):
535
 
 
536
 
            def __init__(stub, action="", creds=None, endpoint=None,
537
 
                         other_params={}):
538
 
                self.assertEqual(action, "DescribeSecurityGroups")
539
 
                self.assertEqual(creds.access_key, "foo")
540
 
                self.assertEqual(creds.secret_key, "bar")
541
 
                self.assertEqual(other_params, {})
542
 
 
543
 
            def submit(self):
544
 
                return succeed(
545
 
                    payload.sample_describe_security_groups_multiple_groups)
546
 
 
547
 
        def check_results(security_groups):
548
 
            self.assertEquals(len(security_groups), 1)
549
 
 
550
 
            security_group = security_groups[0]
551
 
            self.assertEquals(security_group.name, "web/ssh")
552
 
            self.assertEquals([(pair.user_id, pair.group_name)
553
 
                               for pair in security_group.allowed_groups],
554
 
                              [("170723411662", "default"),
555
 
                               ("175723011368", "test1")])
556
 
            self.assertEquals(
557
 
                [(ip.ip_protocol, ip.from_port, ip.to_port, ip.cidr_ip)
558
 
                 for ip in security_group.allowed_ips],
559
 
                [('tcp', 22, 22, '0.0.0.0/0'), ("tcp", 80, 80, "0.0.0.0/0")])
560
 
 
561
 
        creds = AWSCredentials("foo", "bar")
562
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
563
 
        d = ec2.describe_security_groups()
564
 
        return d.addCallback(check_results)
565
 
 
566
398
    def test_describe_security_groups_with_name(self):
567
399
        """
568
400
        L{EC2Client.describe_security_groups} optionally takes a list of
569
401
        security group names to limit results to.
570
402
        """
571
403
        class StubQuery(object):
572
 
 
573
 
            def __init__(stub, action="", creds=None, endpoint=None,
574
 
                         other_params={}):
 
404
            def __init__(stub, action, creds, endpoint, other_params=None):
575
405
                self.assertEqual(action, "DescribeSecurityGroups")
576
406
                self.assertEqual(creds.access_key, "foo")
577
407
                self.assertEqual(creds.secret_key, "bar")
578
408
                self.assertEqual(other_params, {"GroupName.1": "WebServers"})
579
 
 
580
409
            def submit(self):
581
410
                return succeed(payload.sample_describe_security_groups_result)
582
411
 
589
418
        d = ec2.describe_security_groups("WebServers")
590
419
        return d.addCallback(check_result)
591
420
 
592
 
    def test_describe_security_groups_with_openstack(self):
593
 
        """
594
 
        L{EC2Client.describe_security_groups} can work with openstack
595
 
        responses, which may lack proper port information for
596
 
        self-referencing group. Verifying that the response doesn't
597
 
        cause an internal error, workaround for nova launchpad bug
598
 
        #829609.
599
 
        """
600
 
        class StubQuery(object):
601
 
 
602
 
            def __init__(stub, action="", creds=None, endpoint=None,
603
 
                         other_params={}):
604
 
                self.assertEqual(action, "DescribeSecurityGroups")
605
 
                self.assertEqual(creds.access_key, "foo")
606
 
                self.assertEqual(creds.secret_key, "bar")
607
 
                self.assertEqual(other_params, {"GroupName.1": "WebServers"})
608
 
 
609
 
            def submit(self):
610
 
                return succeed(
611
 
                    payload.sample_describe_security_groups_with_openstack)
612
 
 
613
 
        def check_result(security_groups):
614
 
            [security_group] = security_groups
615
 
            self.assertEquals(security_group.name, "WebServers")
616
 
            self.assertEqual(
617
 
                security_group.allowed_groups[0].group_name, "WebServers")
618
 
 
619
 
        creds = AWSCredentials("foo", "bar")
620
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
621
 
        d = ec2.describe_security_groups("WebServers")
622
 
        return d.addCallback(check_result)
623
 
 
624
421
    def test_create_security_group(self):
625
422
        """
626
423
        L{EC2Client.create_security_group} returns a C{Deferred} that
628
425
        operation.
629
426
        """
630
427
        class StubQuery(object):
631
 
 
632
 
            def __init__(stub, action="", creds=None, endpoint=None,
633
 
                         other_params={}):
 
428
            def __init__(stub, action, creds, endpoint, other_params=None):
634
429
                self.assertEqual(action, "CreateSecurityGroup")
635
430
                self.assertEqual(creds.access_key, "foo")
636
431
                self.assertEqual(creds.secret_key, "bar")
638
433
                    "GroupName": "WebServers",
639
434
                    "GroupDescription": "The group for the web server farm.",
640
435
                    })
641
 
 
642
436
            def submit(self):
643
437
                return succeed(payload.sample_create_security_group)
644
438
 
645
 
        def check_result(id):
646
 
            self.assertEquals(id, "sg-1a2b3c4d")
647
 
 
648
439
        creds = AWSCredentials("foo", "bar")
649
440
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
650
441
        d = ec2.create_security_group(
651
442
            "WebServers",
652
443
            "The group for the web server farm.")
653
 
        return d.addCallback(check_result)
654
 
 
655
 
    def test_create_security_group_with_VPC(self):
656
 
        class StubQuery(object):
657
 
 
658
 
            def __init__(stub, action="", creds=None, endpoint=None,
659
 
                         other_params={}):
660
 
                self.assertEqual(action, "CreateSecurityGroup")
661
 
                self.assertEqual(creds.access_key, "foo")
662
 
                self.assertEqual(creds.secret_key, "bar")
663
 
                self.assertEqual(other_params, {
664
 
                    "GroupName": "WebServers",
665
 
                    "GroupDescription": "The group for the web server farm.",
666
 
                    "VpcId": "vpc-a4f2",
667
 
                    })
668
 
 
669
 
            def submit(self):
670
 
                return succeed(payload.sample_create_security_group)
671
 
 
672
 
        def check_result(id):
673
 
            self.assertEquals(id, "sg-1a2b3c4d")
674
 
 
675
 
        creds = AWSCredentials("foo", "bar")
676
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
677
 
        d = ec2.create_security_group(
678
 
            "WebServers",
679
 
            "The group for the web server farm.",
680
 
            "vpc-a4f2")
681
 
        return d.addCallback(check_result)
682
 
 
683
 
    def test_delete_security_group_using_name(self):
 
444
        return self.assertTrue(d)
 
445
 
 
446
    def test_delete_security_group(self):
684
447
        """
685
448
        L{EC2Client.delete_security_group} returns a C{Deferred} that
686
449
        eventually fires with a true value, indicating the success of the
687
450
        operation.
688
451
        """
689
452
        class StubQuery(object):
690
 
 
691
 
            def __init__(stub, action="", creds=None, endpoint=None,
692
 
                         other_params={}):
 
453
            def __init__(stub, action, creds, endpoint, other_params=None):
693
454
                self.assertEqual(action, "DeleteSecurityGroup")
694
455
                self.assertEqual(creds.access_key, "foo")
695
456
                self.assertEqual(creds.secret_key, "bar")
696
457
                self.assertEqual(other_params, {
697
458
                    "GroupName": "WebServers",
698
459
                    })
699
 
 
700
460
            def submit(self):
701
461
                return succeed(payload.sample_delete_security_group)
702
462
 
705
465
        d = ec2.delete_security_group("WebServers")
706
466
        return self.assertTrue(d)
707
467
 
708
 
    def test_delete_security_group_using_id(self):
709
 
        """
710
 
        L{EC2Client.delete_security_group} returns a C{Deferred} that
711
 
        eventually fires with a true value, indicating the success of the
712
 
        operation.
713
 
        """
714
 
        class StubQuery(object):
715
 
 
716
 
            def __init__(stub, action="", creds=None, endpoint=None,
717
 
                         other_params={}):
718
 
                self.assertEqual(action, "DeleteSecurityGroup")
719
 
                self.assertEqual(creds.access_key, "foo")
720
 
                self.assertEqual(creds.secret_key, "bar")
721
 
                self.assertEqual(other_params, {
722
 
                    "GroupId": "sg-a1a1a1",
723
 
                    })
724
 
 
725
 
            def submit(self):
726
 
                return succeed(payload.sample_delete_security_group)
727
 
 
728
 
        creds = AWSCredentials("foo", "bar")
729
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
730
 
        d = ec2.delete_security_group(id="sg-a1a1a1")
731
 
        return self.assertTrue(d)
732
 
 
733
 
    def test_delete_security_group_without_id_and_name(self):
734
 
        creds = AWSCredentials("foo", "bar")
735
 
        ec2 = client.EC2Client(creds)
736
 
        error = self.assertRaises(ValueError, ec2.delete_security_group)
737
 
        self.assertEquals(
738
 
            str(error),
739
 
            "You must provide either the security group name or id",
740
 
        )
741
 
 
742
468
    def test_delete_security_group_failure(self):
743
469
        """
744
470
        L{EC2Client.delete_security_group} returns a C{Deferred} that
746
472
        that another group uses in that other group's policy.
747
473
        """
748
474
        class StubQuery(object):
749
 
 
750
 
            def __init__(stub, action="", creds=None, endpoint=None,
751
 
                         other_params={}):
 
475
            def __init__(stub, action, creds, endpoint, other_params=None):
752
476
                self.assertEqual(action, "DeleteSecurityGroup")
753
477
                self.assertEqual(creds.access_key, "foo")
754
478
                self.assertEqual(creds.secret_key, "bar")
755
479
                self.assertEqual(other_params, {
756
480
                    "GroupName": "GroupReferredTo",
757
481
                    })
758
 
 
759
482
            def submit(self):
760
 
                error = EC2Error(
761
 
                    payload.sample_delete_security_group_failure, 400)
 
483
                error = EC2Error(payload.sample_delete_security_group_failure)
762
484
                return fail(error)
763
485
 
764
486
        def check_error(error):
782
504
        way.
783
505
        """
784
506
        class StubQuery(object):
785
 
 
786
 
            def __init__(stub, action="", creds=None, endpoint=None,
787
 
                         other_params={}):
 
507
            def __init__(stub, action, creds, endpoint, other_params=None):
788
508
                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
789
509
                self.assertEqual(creds.access_key, "foo")
790
510
                self.assertEqual(creds.secret_key, "bar")
793
513
                    "SourceSecurityGroupName": "AppServers",
794
514
                    "SourceSecurityGroupOwnerId": "123456789123",
795
515
                    })
796
 
 
797
 
            def submit(self):
798
 
                return succeed(payload.sample_authorize_security_group)
799
 
 
800
 
        creds = AWSCredentials("foo", "bar")
801
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
802
 
        d = ec2.authorize_security_group(
803
 
            group_name="WebServers", source_group_name="AppServers",
804
 
            source_group_owner_id="123456789123")
805
 
        return self.assertTrue(d)
806
 
 
807
 
    def test_authorize_security_group_using_group_id(self):
808
 
        class StubQuery(object):
809
 
 
810
 
            def __init__(stub, action="", creds=None, endpoint=None,
811
 
                         other_params={}):
812
 
                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
813
 
                self.assertEqual(creds.access_key, "foo")
814
 
                self.assertEqual(creds.secret_key, "bar")
815
 
                self.assertEqual(other_params, {
816
 
                    "GroupId": "sg-a1b2c3d4e5f6",
817
 
                    "SourceSecurityGroupName": "AppServers",
818
 
                    "SourceSecurityGroupOwnerId": "123456789123",
819
 
                    })
820
 
 
821
 
            def submit(self):
822
 
                return succeed(payload.sample_authorize_security_group)
823
 
 
824
 
        creds = AWSCredentials("foo", "bar")
825
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
826
 
        d = ec2.authorize_security_group(
827
 
            group_id="sg-a1b2c3d4e5f6", source_group_name="AppServers",
828
 
            source_group_owner_id="123456789123")
829
 
        return self.assertTrue(d)
830
 
 
831
 
    def test_authorize_security_group_without_group_id_and_group_name(self):
832
 
        creds = AWSCredentials("foo", "bar")
833
 
        ec2 = client.EC2Client(creds)
834
 
        error = self.assertRaises(ValueError, ec2.authorize_security_group,
835
 
                source_group_name="AppServers", source_group_owner_id="123456789123")
836
 
        self.assertEquals(
837
 
            str(error),
838
 
            "You must specify either the group name of the group id.")
 
516
            def submit(self):
 
517
                return succeed(payload.sample_authorize_security_group)
 
518
 
 
519
        creds = AWSCredentials("foo", "bar")
 
520
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
 
521
        d = ec2.authorize_security_group(
 
522
            "WebServers", source_group_name="AppServers",
 
523
            source_group_owner_id="123456789123")
 
524
        return self.assertTrue(d)
839
525
 
840
526
    def test_authorize_security_group_with_ip_permissions(self):
841
527
        """
846
532
        way.
847
533
        """
848
534
        class StubQuery(object):
849
 
 
850
 
            def __init__(stub, action="", creds=None, endpoint=None,
851
 
                         other_params={}):
 
535
            def __init__(stub, action, creds, endpoint, other_params=None):
852
536
                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
853
537
                self.assertEqual(creds.access_key, "foo")
854
538
                self.assertEqual(creds.secret_key, "bar")
857
541
                    "FromPort": "22", "ToPort": "80",
858
542
                    "IpProtocol": "tcp", "CidrIp": "0.0.0.0/0",
859
543
                    })
860
 
 
861
544
            def submit(self):
862
545
                return succeed(payload.sample_authorize_security_group)
863
546
 
864
547
        creds = AWSCredentials("foo", "bar")
865
548
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
866
549
        d = ec2.authorize_security_group(
867
 
            group_name="WebServers", ip_protocol="tcp", from_port="22", to_port="80",
 
550
            "WebServers", ip_protocol="tcp", from_port="22", to_port="80",
868
551
            cidr_ip="0.0.0.0/0")
869
552
        return self.assertTrue(d)
870
553
 
879
562
        """
880
563
        creds = AWSCredentials("foo", "bar")
881
564
        ec2 = client.EC2Client(creds)
882
 
        error = self.assertRaises(ValueError, ec2.authorize_security_group,
883
 
                group_name="WebServers", ip_protocol="tcp", from_port="22")
884
 
        self.assertEquals(
885
 
            str(error),
886
 
            ("You must specify either both group parameters or all the "
887
 
             "ip parameters."))
 
565
        self.assertRaises(ValueError, ec2.authorize_security_group,
 
566
                "WebServers", ip_protocol="tcp", from_port="22")
 
567
        try:
 
568
            ec2.authorize_security_group(
 
569
                "WebServers", ip_protocol="tcp", from_port="22")
 
570
        except Exception, error:
 
571
            self.assertEquals(
 
572
                str(error),
 
573
                ("You must specify either both group parameters or all the "
 
574
                 "ip parameters."))
888
575
 
889
576
    def test_authorize_group_permission(self):
890
577
        """
893
580
        operation.
894
581
        """
895
582
        class StubQuery(object):
896
 
 
897
 
            def __init__(stub, action="", creds=None, endpoint=None,
898
 
                         other_params={}):
 
583
            def __init__(stub, action, creds, endpoint, other_params=None):
899
584
                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
900
585
                self.assertEqual(creds.access_key, "foo")
901
586
                self.assertEqual(creds.secret_key, "bar")
904
589
                    "SourceSecurityGroupName": "AppServers",
905
590
                    "SourceSecurityGroupOwnerId": "123456789123",
906
591
                    })
907
 
 
908
592
            def submit(self):
909
593
                return succeed(payload.sample_authorize_security_group)
910
594
 
922
606
        operation.
923
607
        """
924
608
        class StubQuery(object):
925
 
 
926
 
            def __init__(stub, action="", creds=None, endpoint=None,
927
 
                         other_params={}):
 
609
            def __init__(stub, action, creds, endpoint, other_params=None):
928
610
                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
929
611
                self.assertEqual(creds.access_key, "foo")
930
612
                self.assertEqual(creds.secret_key, "bar")
933
615
                    "FromPort": "22", "ToPort": "80",
934
616
                    "IpProtocol": "tcp", "CidrIp": "0.0.0.0/0",
935
617
                    })
936
 
 
937
618
            def submit(self):
938
619
                return succeed(payload.sample_authorize_security_group)
939
620
 
953
634
        way.
954
635
        """
955
636
        class StubQuery(object):
956
 
 
957
 
            def __init__(stub, action="", creds=None, endpoint=None,
958
 
                         other_params={}):
 
637
            def __init__(stub, action, creds, endpoint, other_params=None):
959
638
                self.assertEqual(action, "RevokeSecurityGroupIngress")
960
639
                self.assertEqual(creds.access_key, "foo")
961
640
                self.assertEqual(creds.secret_key, "bar")
964
643
                    "SourceSecurityGroupName": "AppServers",
965
644
                    "SourceSecurityGroupOwnerId": "123456789123",
966
645
                    })
967
 
 
968
646
            def submit(self):
969
647
                return succeed(payload.sample_revoke_security_group)
970
648
 
975
653
            source_group_owner_id="123456789123")
976
654
        return self.assertTrue(d)
977
655
 
978
 
    def test_revoke_security_group_using_group_id(self):
979
 
        class StubQuery(object):
980
 
 
981
 
            def __init__(stub, action="", creds=None, endpoint=None,
982
 
                         other_params={}):
983
 
                self.assertEqual(action, "RevokeSecurityGroupIngress")
984
 
                self.assertEqual(creds.access_key, "foo")
985
 
                self.assertEqual(creds.secret_key, "bar")
986
 
                self.assertEqual(other_params, {
987
 
                    "GroupId": "sg-a1a1a1",
988
 
                    "SourceSecurityGroupName": "AppServers",
989
 
                    "SourceSecurityGroupOwnerId": "123456789123",
990
 
                    })
991
 
 
992
 
            def submit(self):
993
 
                return succeed(payload.sample_revoke_security_group)
994
 
 
995
 
        creds = AWSCredentials("foo", "bar")
996
 
        ec2 = client.EC2Client(creds, query_factory=StubQuery)
997
 
        d = ec2.revoke_security_group(
998
 
            group_id="sg-a1a1a1", source_group_name="AppServers",
999
 
            source_group_owner_id="123456789123")
1000
 
        return self.assertTrue(d)
1001
 
 
1002
656
    def test_revoke_security_group_with_ip_permissions(self):
1003
657
        """
1004
658
        L{EC2Client.revoke_security_group} returns a C{Deferred} that
1008
662
        way.
1009
663
        """
1010
664
        class StubQuery(object):
1011
 
 
1012
 
            def __init__(stub, action="", creds=None, endpoint=None,
1013
 
                         other_params={}):
 
665
            def __init__(stub, action, creds, endpoint, other_params=None):
1014
666
                self.assertEqual(action, "RevokeSecurityGroupIngress")
1015
667
                self.assertEqual(creds.access_key, "foo")
1016
668
                self.assertEqual(creds.secret_key, "bar")
1019
671
                    "FromPort": "22", "ToPort": "80",
1020
672
                    "IpProtocol": "tcp", "CidrIp": "0.0.0.0/0",
1021
673
                    })
1022
 
 
1023
674
            def submit(self):
1024
675
                return succeed(payload.sample_revoke_security_group)
1025
676
 
1030
681
            cidr_ip="0.0.0.0/0")
1031
682
        return self.assertTrue(d)
1032
683
 
1033
 
    def test_revoke_security_group_without_group_id_and_group_name(self):
1034
 
        creds = AWSCredentials("foo", "bar")
1035
 
        ec2 = client.EC2Client(creds)
1036
 
        error = self.assertRaises(ValueError, ec2.revoke_security_group,
1037
 
                source_group_name="AppServers", source_group_owner_id="123456789123")
1038
 
        self.assertEquals(
1039
 
            str(error),
1040
 
            "You must specify either the group name of the group id.")
1041
 
 
1042
684
    def test_revoke_security_group_with_missing_parameters(self):
1043
685
        """
1044
686
        L{EC2Client.revoke_security_group} returns a C{Deferred} that
1050
692
        """
1051
693
        creds = AWSCredentials("foo", "bar")
1052
694
        ec2 = client.EC2Client(creds)
1053
 
        error = self.assertRaises(ValueError, ec2.revoke_security_group,
1054
 
                group_name="WebServers", ip_protocol="tcp", from_port="22")
1055
 
        self.assertEquals(
1056
 
            str(error),
1057
 
            ("You must specify either both group parameters or all the "
1058
 
             "ip parameters."))
 
695
        self.assertRaises(ValueError, ec2.authorize_security_group,
 
696
                "WebServers", ip_protocol="tcp", from_port="22")
 
697
        try:
 
698
            ec2.authorize_security_group(
 
699
                "WebServers", ip_protocol="tcp", from_port="22")
 
700
        except Exception, error:
 
701
            self.assertEquals(
 
702
                str(error),
 
703
                ("You must specify either both group parameters or all the "
 
704
                 "ip parameters."))
1059
705
 
1060
706
    def test_revoke_group_permission(self):
1061
707
        """
1064
710
        operation.
1065
711
        """
1066
712
        class StubQuery(object):
1067
 
 
1068
 
            def __init__(stub, action="", creds=None, endpoint=None,
1069
 
                         other_params={}):
 
713
            def __init__(stub, action, creds, endpoint, other_params=None):
1070
714
                self.assertEqual(action, "RevokeSecurityGroupIngress")
1071
715
                self.assertEqual(creds.access_key, "foo")
1072
716
                self.assertEqual(creds.secret_key, "bar")
1075
719
                    "SourceSecurityGroupName": "AppServers",
1076
720
                    "SourceSecurityGroupOwnerId": "123456789123",
1077
721
                    })
1078
 
 
1079
722
            def submit(self):
1080
723
                return succeed(payload.sample_revoke_security_group)
1081
724
 
1093
736
        operation.
1094
737
        """
1095
738
        class StubQuery(object):
1096
 
 
1097
 
            def __init__(stub, action="", creds=None, endpoint=None,
1098
 
                         other_params={}):
 
739
            def __init__(stub, action, creds, endpoint, other_params=None):
1099
740
                self.assertEqual(action, "RevokeSecurityGroupIngress")
1100
741
                self.assertEqual(creds.access_key, "foo")
1101
742
                self.assertEqual(creds.secret_key, "bar")
1104
745
                    "FromPort": "22", "ToPort": "80",
1105
746
                    "IpProtocol": "tcp", "CidrIp": "0.0.0.0/0",
1106
747
                    })
1107
 
 
1108
748
            def submit(self):
1109
749
                return succeed(payload.sample_revoke_security_group)
1110
750
 
1144
784
    def test_describe_volumes(self):
1145
785
 
1146
786
        class StubQuery(object):
1147
 
 
1148
 
            def __init__(stub, action="", creds=None, endpoint=None,
1149
 
                         other_params={}):
 
787
            def __init__(stub, action, creds, endpoint, params):
1150
788
                self.assertEqual(action, "DescribeVolumes")
1151
789
                self.assertEqual(self.creds, creds)
1152
790
                self.assertEqual(self.endpoint, endpoint)
1153
 
                self.assertEquals(other_params, {})
 
791
                self.assertEquals(params, {})
1154
792
 
1155
793
            def submit(self):
1156
794
                return succeed(payload.sample_describe_volumes_result)
1164
802
    def test_describe_specified_volumes(self):
1165
803
 
1166
804
        class StubQuery(object):
1167
 
 
1168
 
            def __init__(stub, action="", creds=None, endpoint=None,
1169
 
                         other_params={}):
 
805
            def __init__(stub, action, creds, endpoint, params):
1170
806
                self.assertEqual(action, "DescribeVolumes")
1171
807
                self.assertEqual(self.creds, creds)
1172
808
                self.assertEqual(self.endpoint, endpoint)
1173
809
                self.assertEquals(
1174
 
                    other_params,
 
810
                    params,
1175
811
                    {"VolumeId.1": "vol-4282672b"})
1176
812
 
1177
813
            def submit(self):
1196
832
    def test_describe_snapshots(self):
1197
833
 
1198
834
        class StubQuery(object):
1199
 
 
1200
 
            def __init__(stub, action="", creds=None, endpoint=None,
1201
 
                         other_params={}):
 
835
            def __init__(stub, action, creds, endpoint, params):
1202
836
                self.assertEqual(action, "DescribeSnapshots")
1203
837
                self.assertEqual(self.creds, creds)
1204
838
                self.assertEqual(self.endpoint, endpoint)
1205
 
                self.assertEquals(other_params, {})
 
839
                self.assertEquals(params, {})
1206
840
 
1207
841
            def submit(self):
1208
842
                return succeed(payload.sample_describe_snapshots_result)
1216
850
    def test_describe_specified_snapshots(self):
1217
851
 
1218
852
        class StubQuery(object):
1219
 
 
1220
 
            def __init__(stub, action="", creds=None, endpoint=None,
1221
 
                         other_params={}):
 
853
            def __init__(stub, action, creds, endpoint, params):
1222
854
                self.assertEqual(action, "DescribeSnapshots")
1223
855
                self.assertEqual(self.creds, creds)
1224
856
                self.assertEqual(self.endpoint, endpoint)
1225
857
                self.assertEquals(
1226
 
                    other_params,
 
858
                    params,
1227
859
                    {"SnapshotId.1": "snap-78a54011"})
1228
860
 
1229
861
            def submit(self):
1238
870
    def test_create_volume(self):
1239
871
 
1240
872
        class StubQuery(object):
1241
 
 
1242
 
            def __init__(stub, action="", creds=None, endpoint=None,
1243
 
                         other_params={}):
 
873
            def __init__(stub, action, creds, endpoint, params):
1244
874
                self.assertEqual(action, "CreateVolume")
1245
875
                self.assertEqual(self.creds, creds)
1246
876
                self.assertEqual(self.endpoint, endpoint)
1247
877
                self.assertEqual(
1248
 
                    other_params,
1249
 
                    {"AvailabilityZone": "us-east-1", "Size": "800"})
 
878
                    {"AvailabilityZone": "us-east-1", "Size": "800"},
 
879
                    params)
1250
880
 
1251
881
            def submit(self):
1252
882
                return succeed(payload.sample_create_volume_result)
1267
897
    def test_create_volume_with_snapshot(self):
1268
898
 
1269
899
        class StubQuery(object):
1270
 
 
1271
 
            def __init__(stub, action="", creds=None, endpoint=None,
1272
 
                         other_params={}):
 
900
            def __init__(stub, action, creds, endpoint, params):
1273
901
                self.assertEqual(action, "CreateVolume")
1274
902
                self.assertEqual(self.creds, creds)
1275
903
                self.assertEqual(self.endpoint, endpoint)
1276
904
                self.assertEqual(
1277
 
                    other_params,
1278
905
                    {"AvailabilityZone": "us-east-1",
1279
 
                     "SnapshotId": "snap-12345678"})
 
906
                     "SnapshotId": "snap-12345678"},
 
907
                    params)
1280
908
 
1281
909
            def submit(self):
1282
910
                return succeed(payload.sample_create_volume_result)
1311
939
    def test_delete_volume(self):
1312
940
 
1313
941
        class StubQuery(object):
1314
 
 
1315
 
            def __init__(stub, action="", creds=None, endpoint=None,
1316
 
                         other_params={}):
 
942
            def __init__(stub, action, creds, endpoint, params):
1317
943
                self.assertEqual(action, "DeleteVolume")
1318
944
                self.assertEqual(self.creds, creds)
1319
945
                self.assertEqual(self.endpoint, endpoint)
1320
946
                self.assertEqual(
1321
 
                    other_params,
1322
 
                    {"VolumeId": "vol-4282672b"})
 
947
                    {"VolumeId": "vol-4282672b"},
 
948
                    params)
1323
949
 
1324
950
            def submit(self):
1325
951
                return succeed(payload.sample_delete_volume_result)
1333
959
    def test_create_snapshot(self):
1334
960
 
1335
961
        class StubQuery(object):
1336
 
 
1337
 
            def __init__(stub, action="", creds=None, endpoint=None,
1338
 
                         other_params={}):
 
962
            def __init__(stub, action, creds, endpoint, params):
1339
963
                self.assertEqual(action, "CreateSnapshot")
1340
964
                self.assertEqual(self.creds, creds)
1341
965
                self.assertEqual(self.endpoint, endpoint)
1342
966
                self.assertEqual(
1343
 
                    other_params,
1344
 
                    {"VolumeId": "vol-4d826724"})
 
967
                    {"VolumeId": "vol-4d826724"},
 
968
                    params)
1345
969
 
1346
970
            def submit(self):
1347
971
                return succeed(payload.sample_create_snapshot_result)
1363
987
    def test_delete_snapshot(self):
1364
988
 
1365
989
        class StubQuery(object):
1366
 
 
1367
 
            def __init__(stub, action="", creds=None, endpoint=None,
1368
 
                         other_params={}):
 
990
            def __init__(stub, action, creds, endpoint, params):
1369
991
                self.assertEqual(action, "DeleteSnapshot")
1370
992
                self.assertEqual(self.creds, creds)
1371
993
                self.assertEqual(self.endpoint, endpoint)
1372
994
                self.assertEqual(
1373
 
                    other_params,
1374
 
                    {"SnapshotId": "snap-78a54011"})
 
995
                    {"SnapshotId": "snap-78a54011"},
 
996
                    params)
1375
997
 
1376
998
            def submit(self):
1377
999
                return succeed(payload.sample_delete_snapshot_result)
1385
1007
    def test_attach_volume(self):
1386
1008
 
1387
1009
        class StubQuery(object):
1388
 
 
1389
 
            def __init__(stub, action="", creds=None, endpoint=None,
1390
 
                         other_params={}):
 
1010
            def __init__(stub, action, creds, endpoint, params):
1391
1011
                self.assertEqual(action, "AttachVolume")
1392
1012
                self.assertEqual(self.creds, creds)
1393
1013
                self.assertEqual(self.endpoint, endpoint)
1394
1014
                self.assertEqual(
1395
 
                    other_params,
1396
1015
                    {"VolumeId": "vol-4d826724", "InstanceId": "i-6058a509",
1397
 
                     "Device": "/dev/sdh"})
 
1016
                     "Device": "/dev/sdh"},
 
1017
                    params)
1398
1018
 
1399
1019
            def submit(self):
1400
1020
                return succeed(payload.sample_attach_volume_result)
1422
1042
    def test_single_describe_keypairs(self):
1423
1043
 
1424
1044
        class StubQuery(object):
1425
 
 
1426
 
            def __init__(stub, action="", creds=None, endpoint=None,
1427
 
                         other_params={}):
 
1045
            def __init__(stub, action, creds, endpoint, params):
1428
1046
                self.assertEqual(action, "DescribeKeyPairs")
1429
1047
                self.assertEqual("foo", creds)
1430
 
                self.assertEquals(other_params, {})
 
1048
                self.assertEquals(params, {})
1431
1049
 
1432
1050
            def submit(self):
1433
1051
                return succeed(payload.sample_single_describe_keypairs_result)
1452
1070
                "1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:70")
1453
1071
 
1454
1072
        class StubQuery(object):
1455
 
 
1456
 
            def __init__(stub, action="", creds=None, endpoint=None,
1457
 
                         other_params={}):
 
1073
            def __init__(stub, action, creds, endpoint, params):
1458
1074
                self.assertEqual(action, "DescribeKeyPairs")
1459
1075
                self.assertEqual("foo", creds)
1460
 
                self.assertEquals(other_params, {})
 
1076
                self.assertEquals(params, {})
1461
1077
 
1462
1078
            def submit(self):
1463
1079
                return succeed(
1471
1087
    def test_describe_specified_keypairs(self):
1472
1088
 
1473
1089
        class StubQuery(object):
1474
 
 
1475
 
            def __init__(stub, action="", creds=None, endpoint=None,
1476
 
                         other_params={}):
 
1090
            def __init__(stub, action, creds, endpoint, params):
1477
1091
                self.assertEqual(action, "DescribeKeyPairs")
1478
1092
                self.assertEqual("foo", creds)
1479
1093
                self.assertEquals(
1480
 
                    other_params,
1481
 
                    {"KeyName.1": "gsg-keypair"})
 
1094
                    params,
 
1095
                    {"KeyPair.1": "gsg-keypair"})
1482
1096
 
1483
1097
            def submit(self):
1484
1098
                return succeed(payload.sample_single_describe_keypairs_result)
1502
1116
            self.assertEquals(len(keypair.material), 1670)
1503
1117
 
1504
1118
        class StubQuery(object):
1505
 
 
1506
 
            def __init__(stub, action="", creds=None, endpoint=None,
1507
 
                         other_params={}):
 
1119
            def __init__(stub, action, creds, endpoint, params):
1508
1120
                self.assertEqual(action, "CreateKeyPair")
1509
1121
                self.assertEqual("foo", creds)
1510
1122
                self.assertEquals(
1511
 
                    other_params,
 
1123
                    params,
1512
1124
                    {"KeyName": "example-key-name"})
1513
1125
 
1514
1126
            def submit(self):
1522
1134
    def test_delete_keypair_true_result(self):
1523
1135
 
1524
1136
        class StubQuery(object):
1525
 
 
1526
 
            def __init__(stub, action="", creds=None, endpoint=None,
1527
 
                         other_params={}):
 
1137
            def __init__(stub, action, creds, endpoint, params):
1528
1138
                self.assertEqual(action, "DeleteKeyPair")
1529
1139
                self.assertEqual("foo", creds)
1530
1140
                self.assertEqual("http:///", endpoint.get_uri())
1531
1141
                self.assertEquals(
1532
 
                    other_params,
 
1142
                    params,
1533
1143
                    {"KeyName": "example-key-name"})
1534
1144
 
1535
1145
            def submit(self):
1543
1153
    def test_delete_keypair_false_result(self):
1544
1154
 
1545
1155
        class StubQuery(object):
1546
 
 
1547
 
            def __init__(stub, action="", creds=None, endpoint=None,
1548
 
                         other_params={}):
 
1156
            def __init__(stub, action, creds, endpoint, params):
1549
1157
                self.assertEqual(action, "DeleteKeyPair")
1550
1158
                self.assertEqual("foo", creds)
1551
1159
                self.assertEqual("http:///", endpoint.get_uri())
1552
1160
                self.assertEquals(
1553
 
                    other_params,
 
1161
                    params,
1554
1162
                    {"KeyName": "example-key-name"})
1555
1163
 
1556
1164
            def submit(self):
1564
1172
    def test_delete_keypair_no_result(self):
1565
1173
 
1566
1174
        class StubQuery(object):
1567
 
 
1568
 
            def __init__(stub, action="", creds=None, endpoint=None,
1569
 
                         other_params={}):
 
1175
            def __init__(stub, action, creds, endpoint, params):
1570
1176
                self.assertEqual(action, "DeleteKeyPair")
1571
1177
                self.assertEqual("foo", creds)
1572
1178
                self.assertEqual("http:///", endpoint.get_uri())
1573
1179
                self.assertEquals(
1574
 
                    other_params,
 
1180
                    params,
1575
1181
                    {"KeyName": "example-key-name"})
1576
1182
 
1577
1183
            def submit(self):
1582
1188
        d.addCallback(self.assertFalse)
1583
1189
        return d
1584
1190
 
1585
 
    def test_import_keypair(self):
1586
 
        """
1587
 
        L{client.EC2Client.import_keypair} calls the C{ImportKeyPair} method
1588
 
        with the given arguments, encoding the key material in base64, and
1589
 
        returns a C{Keypair} instance.
1590
 
        """
1591
 
 
1592
 
        def check_parsed_import_keypair(keypair):
1593
 
            self.assertEquals(keypair.name, "example-key-name")
1594
 
            self.assertEquals(
1595
 
                keypair.fingerprint,
1596
 
                "1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f")
1597
 
            self.assertEquals(keypair.material, material)
1598
 
 
1599
 
        class StubQuery(object):
1600
 
 
1601
 
            def __init__(stub, action="", creds=None, endpoint=None,
1602
 
                         other_params={}):
1603
 
                self.assertEqual(action, "ImportKeyPair")
1604
 
                self.assertEqual("foo", creds)
1605
 
                self.assertEquals(
1606
 
                    other_params,
1607
 
                    {"KeyName": "example-key-name",
1608
 
                     "PublicKeyMaterial":
1609
 
                        "c3NoLWRzcyBBQUFBQjNOemFDMWtjM01BQUFDQkFQNmFjakFQeitUR"
1610
 
                        "jJkREtmZGlhcnp2cXBBcjhlbUl6UElBWUp6QXNoTFgvUTJCZ2tWc0"
1611
 
                        "42eGI2QUlIUGE1MUFtWXVieU5PYjMxeVhWS2FRQTF6L213SHZtRld"
1612
 
                        "LQ1ZFQ0wwPSkgdXNlckBob3N0"})
1613
 
 
1614
 
            def submit(self):
1615
 
                return succeed(payload.sample_import_keypair_result)
1616
 
 
1617
 
        ec2 = client.EC2Client(creds="foo", query_factory=StubQuery)
1618
 
        material = (
1619
 
            "ssh-dss AAAAB3NzaC1kc3MAAACBAP6acjAPz+TF2dDKfdiarzvqpAr8emIzPIAY"
1620
 
            "JzAshLX/Q2BgkVsN6xb6AIHPa51AmYubyNOb31yXVKaQA1z/mwHvmFWKCVECL0=)"
1621
 
            " user@host")
1622
 
        d = ec2.import_keypair("example-key-name", material)
1623
 
        d.addCallback(check_parsed_import_keypair)
1624
 
        return d
1625
 
 
1626
1191
 
1627
1192
class EC2ErrorWrapperTestCase(TXAWSTestCase):
1628
1193
 
1649
1214
        self.assertEquals(failure.type, type(error))
1650
1215
        self.assertFalse(isinstance(error, EC2Error))
1651
1216
        self.assertTrue(isinstance(error, Exception))
1652
 
        self.assertEquals(str(error), "found")
 
1217
        self.assertEquals(error.message, "found")
1653
1218
 
1654
1219
    def test_400_error(self):
1655
1220
        failure = self.make_failure(400, TwistedWebError)
1701
1266
        error = self.assertRaises(Exception, client.ec2_error_wrapper, failure)
1702
1267
        self.assertFalse(isinstance(error, EC2Error))
1703
1268
        self.assertTrue(isinstance(error, Exception))
1704
 
        self.assertEquals(str(error), "A server error occurred")
 
1269
        self.assertEquals(error.message, "A server error occurred")
1705
1270
 
1706
1271
    def test_timeout_error(self):
1707
1272
        failure = self.make_failure(type=Exception, message="timeout")
1708
1273
        error = self.assertRaises(Exception, client.ec2_error_wrapper, failure)
1709
1274
        self.assertFalse(isinstance(error, EC2Error))
1710
1275
        self.assertTrue(isinstance(error, Exception))
1711
 
        self.assertEquals(str(error), "timeout")
 
1276
        self.assertEquals(error.message, "timeout")
1712
1277
 
1713
1278
    def test_connection_error(self):
1714
1279
        failure = self.make_failure(type=ConnectionRefusedError)
1720
1285
    def test_response_parse_error(self):
1721
1286
        bad_payload = "<bad></xml>"
1722
1287
        failure = self.make_failure(400, type=TwistedWebError,
1723
 
                                    response=bad_payload)
 
1288
                                   response=bad_payload)
1724
1289
        error = self.assertRaises(Exception, client.ec2_error_wrapper, failure)
1725
1290
        self.assertEquals(str(error), "400 Bad Request")
1726
1291
 
1733
1298
        self.endpoint = AWSServiceEndpoint(uri=EC2_ENDPOINT_US)
1734
1299
 
1735
1300
    def test_init_minimum(self):
1736
 
        query = client.Query(
1737
 
            action="DescribeInstances", creds=self.creds,
1738
 
            endpoint=self.endpoint)
 
1301
        query = client.Query("DescribeInstances", self.creds, self.endpoint)
1739
1302
        self.assertTrue("Timestamp" in query.params)
1740
1303
        del query.params["Timestamp"]
1741
1304
        self.assertEqual(
1742
 
            query.params,
1743
1305
            {"AWSAccessKeyId": "foo",
1744
1306
             "Action": "DescribeInstances",
 
1307
             "SignatureMethod": "HmacSHA256",
1745
1308
             "SignatureVersion": "2",
1746
 
             "Version": "2012-08-15"})
 
1309
             "Version": "2008-12-01"},
 
1310
            query.params)
 
1311
 
 
1312
    def test_init_requires_action(self):
 
1313
        self.assertRaises(TypeError, client.Query)
 
1314
 
 
1315
    def test_init_requires_creds(self):
 
1316
        self.assertRaises(TypeError, client.Query, None)
1747
1317
 
1748
1318
    def test_init_other_args_are_params(self):
1749
 
        query = client.Query(
1750
 
            action="DescribeInstances", creds=self.creds,
1751
 
            endpoint=self.endpoint, other_params={"InstanceId.0": "12345"},
1752
 
            time_tuple=(2007, 11, 12, 13, 14, 15, 0, 0, 0))
 
1319
        query = client.Query("DescribeInstances", self.creds, self.endpoint,
 
1320
            {"InstanceId.0": "12345"},
 
1321
            time_tuple=(2007,11,12,13,14,15,0,0,0))
1753
1322
        self.assertEqual(
1754
 
            query.params,
1755
1323
            {"AWSAccessKeyId": "foo",
1756
1324
             "Action": "DescribeInstances",
1757
1325
             "InstanceId.0": "12345",
 
1326
             "SignatureMethod": "HmacSHA256",
1758
1327
             "SignatureVersion": "2",
1759
1328
             "Timestamp": "2007-11-12T13:14:15Z",
1760
 
             "Version": "2012-08-15"})
1761
 
 
1762
 
    def test_no_timestamp_if_expires_in_other_params(self):
1763
 
        """
1764
 
        If Expires is present in other_params, Timestamp won't be added,
1765
 
        since a request should contain either Expires or Timestamp, but
1766
 
        not both.
1767
 
        """
1768
 
        query = client.Query(
1769
 
            action="DescribeInstances", creds=self.creds,
1770
 
            endpoint=self.endpoint,
1771
 
            other_params={"Expires": "2007-11-12T13:14:15Z"})
1772
 
        self.assertEqual(
1773
 
            query.params,
1774
 
            {"AWSAccessKeyId": "foo",
1775
 
             "Action": "DescribeInstances",
1776
 
             "SignatureVersion": "2",
1777
 
             "Expires": "2007-11-12T13:14:15Z",
1778
 
             "Version": "2012-08-15"})
 
1329
             "Version": "2008-12-01"},
 
1330
            query.params)
 
1331
 
 
1332
    def test_sorted_params(self):
 
1333
        query = client.Query("DescribeInstances", self.creds, self.endpoint,
 
1334
            {"fun": "games"},
 
1335
            time_tuple=(2007,11,12,13,14,15,0,0,0))
 
1336
        self.assertEqual([
 
1337
            ("AWSAccessKeyId", "foo"),
 
1338
            ("Action", "DescribeInstances"),
 
1339
            ("SignatureMethod", "HmacSHA256"),
 
1340
            ("SignatureVersion", "2"),
 
1341
            ("Timestamp", "2007-11-12T13:14:15Z"),
 
1342
            ("Version", "2008-12-01"),
 
1343
            ("fun", "games"),
 
1344
            ], query.sorted_params())
 
1345
 
 
1346
    def test_encode_unreserved(self):
 
1347
        all_unreserved = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
1348
            "abcdefghijklmnopqrstuvwxyz0123456789-_.~")
 
1349
        query = client.Query("DescribeInstances", self.creds, self.endpoint)
 
1350
        self.assertEqual(all_unreserved, query.encode(all_unreserved))
 
1351
 
 
1352
    def test_encode_space(self):
 
1353
        """This may be just "url encode", but the AWS manual isn't clear."""
 
1354
        query = client.Query("DescribeInstances", self.creds, self.endpoint)
 
1355
        self.assertEqual("a%20space", query.encode("a space"))
 
1356
 
 
1357
    def test_canonical_query(self):
 
1358
        query = client.Query("DescribeInstances", self.creds, self.endpoint,
 
1359
            {"fu n": "g/ames", "argwithnovalue":"",
 
1360
             "InstanceId.1": "i-1234"},
 
1361
            time_tuple=(2007,11,12,13,14,15,0,0,0))
 
1362
        expected_query = ("AWSAccessKeyId=foo&Action=DescribeInstances"
 
1363
            "&InstanceId.1=i-1234"
 
1364
            "&SignatureMethod=HmacSHA256&SignatureVersion=2&"
 
1365
            "Timestamp=2007-11-12T13%3A14%3A15Z&Version=2008-12-01&"
 
1366
            "argwithnovalue=&fu%20n=g%2Fames")
 
1367
        self.assertEqual(expected_query, query.canonical_query_params())
 
1368
 
 
1369
    def test_signing_text(self):
 
1370
        query = client.Query("DescribeInstances", self.creds, self.endpoint,
 
1371
            time_tuple=(2007,11,12,13,14,15,0,0,0))
 
1372
        signing_text = ("GET\n%s\n/\n" % self.endpoint.host +
 
1373
            "AWSAccessKeyId=foo&Action=DescribeInstances&"
 
1374
            "SignatureMethod=HmacSHA256&SignatureVersion=2&"
 
1375
            "Timestamp=2007-11-12T13%3A14%3A15Z&Version=2008-12-01")
 
1376
        self.assertEqual(signing_text, query.signing_text())
1779
1377
 
1780
1378
    def test_sign(self):
1781
 
        query = client.Query(
1782
 
            action="DescribeInstances", creds=self.creds,
1783
 
            endpoint=self.endpoint,
 
1379
        query = client.Query("DescribeInstances", self.creds, self.endpoint,
1784
1380
            time_tuple=(2007, 11, 12, 13, 14, 15, 0, 0, 0))
1785
1381
        query.sign()
1786
 
        self.assertEqual("c0gbkemrGEJdqxWOl2UZYaygYiBLVjrpWBs7bTN7Ndo=",
 
1382
        self.assertEqual("aDmLr0Ktjsmt17UJD/EZf6DrfKWT1JW0fq2FDUCOPic=",
1787
1383
            query.params["Signature"])
1788
1384
 
1789
 
    def test_old_sign(self):
1790
 
        query = client.Query(
1791
 
            action="DescribeInstances", creds=self.creds,
1792
 
            endpoint=self.endpoint,
1793
 
            time_tuple=(2007, 11, 12, 13, 14, 15, 0, 0, 0),
1794
 
            other_params={"SignatureVersion": "1"})
1795
 
        query.sign()
1796
 
        self.assertEqual(
1797
 
            "7tWrIC5VYvXOjVE+roVoyDUt2Yw=", query.params["Signature"])
1798
 
 
1799
 
    def test_unsupported_sign(self):
1800
 
        query = client.Query(
1801
 
            action="DescribeInstances", creds=self.creds,
1802
 
            endpoint=self.endpoint,
1803
 
            time_tuple=(2007, 11, 12, 13, 14, 15, 0, 0, 0),
1804
 
            other_params={"SignatureVersion": "0"})
1805
 
        self.assertRaises(RuntimeError, query.sign)
1806
 
 
1807
 
    def test_submit_with_port(self):
1808
 
        """
1809
 
        If the endpoint port differs from the default one, the Host header
1810
 
        of the request will include it.
1811
 
        """
1812
 
        self.addCleanup(setattr, client.Query, "get_page",
1813
 
                        client.Query.get_page)
1814
 
 
1815
 
        def get_page(query, url, **kwargs):
1816
 
            self.assertEqual("example.com:99", kwargs["headers"]["Host"])
1817
 
            return succeed(None)
1818
 
 
1819
 
        client.Query.get_page = get_page
1820
 
        endpoint = AWSServiceEndpoint(uri="http://example.com:99/foo")
1821
 
        query = client.Query(action="SomeQuery", creds=self.creds,
1822
 
                             endpoint=endpoint)
1823
 
 
1824
 
        d = query.submit()
1825
 
        return d
1826
 
 
1827
1385
    def test_submit_400(self):
1828
1386
        """A 4xx response status from EC2 should raise a txAWS EC2Error."""
1829
1387
        status = 400
1843
1401
            self.assertEquals(error.response, payload.sample_ec2_error_message)
1844
1402
 
1845
1403
        query = client.Query(
1846
 
            action='BadQuery', creds=self.creds, endpoint=self.endpoint,
1847
 
            time_tuple=(2009, 8, 15, 13, 14, 15, 0, 0, 0))
 
1404
            'BadQuery', self.creds, self.endpoint,
 
1405
            time_tuple=(2009,8,15,13,14,15,0,0,0))
1848
1406
 
1849
1407
        failure = query.submit()
1850
1408
        d = self.assertFailure(failure, TwistedWebError)
1868
1426
            self.assertEquals(error.status, status)
1869
1427
 
1870
1428
        query = client.Query(
1871
 
            action='BadQuery', creds=self.creds, endpoint=self.endpoint,
1872
 
            time_tuple=(2009, 8, 15, 13, 14, 15, 0, 0, 0))
 
1429
            'BadQuery', self.creds, self.endpoint,
 
1430
            time_tuple=(2009,8,15,13,14,15,0,0,0))
1873
1431
 
1874
1432
        failure = query.submit()
1875
1433
        d = self.assertFailure(failure, TwistedWebError)
1897
1455
                "We encountered an internal error. Please try again.")
1898
1456
 
1899
1457
        query = client.Query(
1900
 
            action='BadQuery', creds=self.creds, endpoint=self.endpoint,
1901
 
            time_tuple=(2009, 8, 15, 13, 14, 15, 0, 0, 0))
 
1458
            'BadQuery', self.creds, self.endpoint,
 
1459
            time_tuple=(2009,8,15,13,14,15,0,0,0))
1902
1460
 
1903
1461
        failure = query.submit()
1904
1462
        d = self.assertFailure(failure, TwistedWebError)
1906
1464
        return d
1907
1465
 
1908
1466
 
1909
 
class SignatureTestCase(TXAWSTestCase):
1910
 
 
1911
 
    def setUp(self):
1912
 
        TXAWSTestCase.setUp(self)
1913
 
        self.creds = AWSCredentials("foo", "bar")
1914
 
        self.endpoint = AWSServiceEndpoint(uri=EC2_ENDPOINT_US)
1915
 
        self.params = {}
1916
 
 
1917
 
    def test_encode_unreserved(self):
1918
 
        all_unreserved = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1919
 
            "abcdefghijklmnopqrstuvwxyz0123456789-_.~")
1920
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1921
 
        self.assertEqual(all_unreserved, signature.encode(all_unreserved))
1922
 
 
1923
 
    def test_encode_space(self):
1924
 
        """This may be just 'url encode', but the AWS manual isn't clear."""
1925
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1926
 
        self.assertEqual("a%20space", signature.encode("a space"))
1927
 
 
1928
 
    def test_encode_unicode(self):
1929
 
        """
1930
 
        L{Signature.encode} accepts unicode strings and encode them un UTF-8.
1931
 
        """
1932
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1933
 
        self.assertEqual(
1934
 
            "f%C3%A9e",
1935
 
            signature.encode(u"f\N{LATIN SMALL LETTER E WITH ACUTE}e"))
1936
 
 
1937
 
    def test_canonical_query(self):
1938
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1939
 
        time_tuple = (2007, 11, 12, 13, 14, 15, 0, 0, 0)
1940
 
        self.params.update({"AWSAccessKeyId": "foo",
1941
 
                            "fu n": "g/ames",
1942
 
                            "argwithnovalue": "",
1943
 
                            "SignatureVersion": "2",
1944
 
                            "Timestamp": iso8601time(time_tuple),
1945
 
                            "Version": "2009-11-30",
1946
 
                            "Action": "DescribeInstances",
1947
 
                            "InstanceId.1": "i-1234"})
1948
 
        expected_params = ("AWSAccessKeyId=foo&Action=DescribeInstances"
1949
 
            "&InstanceId.1=i-1234"
1950
 
            "&SignatureVersion=2&"
1951
 
            "Timestamp=2007-11-12T13%3A14%3A15Z&Version=2009-11-30&"
1952
 
            "argwithnovalue=&fu%20n=g%2Fames")
1953
 
        self.assertEqual(
1954
 
            expected_params, signature.get_canonical_query_params())
1955
 
 
1956
 
    def test_signing_text(self):
1957
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1958
 
        self.params.update({"AWSAccessKeyId": "foo",
1959
 
                            "SignatureVersion": "2",
1960
 
                            "Action": "DescribeInstances"})
1961
 
        signing_text = ("GET\n%s\n/\n" % self.endpoint.host +
1962
 
                        "AWSAccessKeyId=foo&Action=DescribeInstances&" +
1963
 
                        "SignatureVersion=2")
1964
 
        self.assertEqual(signing_text, signature.signing_text())
1965
 
 
1966
 
    def test_signing_text_with_non_default_port(self):
1967
 
        """
1968
 
        The signing text uses the canonical host name, which includes
1969
 
        the port number, if it differs from the default one.
1970
 
        """
1971
 
        endpoint = AWSServiceEndpoint(uri="http://example.com:99/path")
1972
 
        signature = client.Signature(self.creds, endpoint, self.params)
1973
 
        self.params.update({"AWSAccessKeyId": "foo",
1974
 
                            "SignatureVersion": "2",
1975
 
                            "Action": "DescribeInstances"})
1976
 
        signing_text = ("GET\n"
1977
 
                        "example.com:99\n"
1978
 
                        "/path\n"
1979
 
                        "AWSAccessKeyId=foo&"
1980
 
                        "Action=DescribeInstances&"
1981
 
                        "SignatureVersion=2")
1982
 
        self.assertEqual(signing_text, signature.signing_text())
1983
 
 
1984
 
    def test_old_signing_text(self):
1985
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1986
 
        self.params.update({"AWSAccessKeyId": "foo",
1987
 
                            "SignatureVersion": "1",
1988
 
                            "Action": "DescribeInstances"})
1989
 
        signing_text = (
1990
 
            "ActionDescribeInstancesAWSAccessKeyIdfooSignatureVersion1")
1991
 
        self.assertEqual(signing_text, signature.old_signing_text())
1992
 
 
1993
 
    def test_sorted_params(self):
1994
 
        signature = client.Signature(self.creds, self.endpoint, self.params)
1995
 
        self.params.update({"AWSAccessKeyId": "foo",
1996
 
                            "fun": "games",
1997
 
                            "SignatureVersion": "2",
1998
 
                            "Version": "2009-11-30",
1999
 
                            "Action": "DescribeInstances"})
2000
 
 
2001
 
        self.assertEqual([
2002
 
            ("AWSAccessKeyId", "foo"),
2003
 
            ("Action", "DescribeInstances"),
2004
 
            ("SignatureVersion", "2"),
2005
 
            ("Version", "2009-11-30"),
2006
 
            ("fun", "games"),
2007
 
            ], signature.sorted_params())
2008
 
 
2009
 
 
2010
1467
class QueryPageGetterTestCase(TXAWSTestCase):
2011
1468
 
2012
1469
    def setUp(self):
2053
1510
    def test_get_page(self):
2054
1511
        """Copied from twisted.web.test.test_webclient."""
2055
1512
        query = client.Query(
2056
 
            action="DummyQuery", creds=self.creds, endpoint=self.endpoint,
2057
 
            time_tuple=(2009, 8, 15, 13, 14, 15, 0, 0, 0))
 
1513
            'DummyQuery', self.creds, self.endpoint,
 
1514
            time_tuple=(2009,8,17,13,14,15,0,0,0))
2058
1515
        deferred = query.get_page(self.get_url("file"))
2059
1516
        deferred.addCallback(self.assertEquals, "0123456789")
2060
1517
        return deferred
2061
1518
 
2062
1519
 
 
1520
 
2063
1521
class EC2ClientAddressTestCase(TXAWSTestCase):
2064
1522
 
2065
1523
    def setUp(self):
2070
1528
    def test_describe_addresses(self):
2071
1529
 
2072
1530
        class StubQuery(object):
2073
 
 
2074
 
            def __init__(stub, action="", creds=None, endpoint=None,
2075
 
                         other_params={}):
 
1531
            def __init__(stub, action, creds, endpoint, params):
2076
1532
                self.assertEqual(action, "DescribeAddresses")
2077
1533
                self.assertEqual(self.creds, creds)
2078
1534
                self.assertEqual(self.endpoint, endpoint)
2079
 
                self.assertEquals(other_params, {})
 
1535
                self.assertEquals(params, {})
2080
1536
 
2081
1537
            def submit(self):
2082
1538
                return succeed(payload.sample_describe_addresses_result)
2092
1548
    def test_describe_specified_addresses(self):
2093
1549
 
2094
1550
        class StubQuery(object):
2095
 
 
2096
 
            def __init__(stub, action="", creds=None, endpoint=None,
2097
 
                         other_params={}):
 
1551
            def __init__(stub, action, creds, endpoint, params):
2098
1552
                self.assertEqual(action, "DescribeAddresses")
2099
1553
                self.assertEqual(self.creds, creds)
2100
1554
                self.assertEqual(self.endpoint, endpoint)
2101
1555
                self.assertEquals(
2102
 
                    other_params,
 
1556
                    params,
2103
1557
                    {"PublicIp.1": "67.202.55.255"})
2104
1558
 
2105
1559
            def submit(self):
2116
1570
    def test_associate_address(self):
2117
1571
 
2118
1572
        class StubQuery(object):
2119
 
 
2120
 
            def __init__(stub, action="", creds=None, endpoint=None,
2121
 
                         other_params={}):
 
1573
            def __init__(stub, action, creds, endpoint, params):
2122
1574
                self.assertEqual(action, "AssociateAddress")
2123
1575
                self.assertEqual(self.creds, creds)
2124
1576
                self.assertEqual(self.endpoint, endpoint)
2125
1577
                self.assertEquals(
2126
 
                    other_params,
 
1578
                    params,
2127
1579
                    {"InstanceId": "i-28a64341", "PublicIp": "67.202.55.255"})
2128
1580
 
2129
1581
            def submit(self):
2138
1590
    def test_allocate_address(self):
2139
1591
 
2140
1592
        class StubQuery(object):
2141
 
 
2142
 
            def __init__(stub, action="", creds=None, endpoint=None,
2143
 
                         other_params={}):
 
1593
            def __init__(stub, action, creds, endpoint, params):
2144
1594
                self.assertEqual(action, "AllocateAddress")
2145
1595
                self.assertEqual(self.creds, creds)
2146
1596
                self.assertEqual(self.endpoint, endpoint)
2147
 
                self.assertEquals(other_params, {})
 
1597
                self.assertEquals(params, {})
2148
1598
 
2149
1599
            def submit(self):
2150
1600
                return succeed(payload.sample_allocate_address_result)
2158
1608
    def test_release_address(self):
2159
1609
 
2160
1610
        class StubQuery(object):
2161
 
 
2162
 
            def __init__(stub, action="", creds=None, endpoint=None,
2163
 
                         other_params={}):
 
1611
            def __init__(stub, action, creds, endpoint, params):
2164
1612
                self.assertEqual(action, "ReleaseAddress")
2165
1613
                self.assertEqual(self.creds, creds)
2166
1614
                self.assertEqual(self.endpoint, endpoint)
2167
 
                self.assertEquals(other_params, {"PublicIp": "67.202.55.255"})
 
1615
                self.assertEquals(params, {"PublicIp": "67.202.55.255"})
2168
1616
 
2169
1617
            def submit(self):
2170
1618
                return succeed(payload.sample_release_address_result)
2178
1626
    def test_disassociate_address(self):
2179
1627
 
2180
1628
        class StubQuery(object):
2181
 
 
2182
 
            def __init__(stub, action="", creds=None, endpoint=None,
2183
 
                         other_params={}):
 
1629
            def __init__(stub, action, creds, endpoint, params):
2184
1630
                self.assertEqual(action, "DisassociateAddress")
2185
1631
                self.assertEqual(self.creds, creds)
2186
1632
                self.assertEqual(self.endpoint, endpoint)
2187
 
                self.assertEquals(other_params, {"PublicIp": "67.202.55.255"})
 
1633
                self.assertEquals(params, {"PublicIp": "67.202.55.255"})
2188
1634
 
2189
1635
            def submit(self):
2190
1636
                return succeed(payload.sample_disassociate_address_result)
2194
1640
        d = ec2.disassociate_address("67.202.55.255")
2195
1641
        d.addCallback(self.assertTrue)
2196
1642
        return d
2197
 
 
2198
 
 
2199
 
class EC2ParserTestCase(TXAWSTestCase):
2200
 
 
2201
 
    def setUp(self):
2202
 
        self.parser = client.Parser()
2203
 
 
2204
 
    def test_ec2_terminate_instances(self):
2205
 
        """
2206
 
        Given a well formed response from EC2, parse the correct thing.
2207
 
        """
2208
 
        ec2_xml = """<?xml version="1.0" encoding="UTF-8"?>
2209
 
<TerminateInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2008-12-01/">
2210
 
    <requestId>d0adc305-7f97-4652-b7c2-6993b2bb8260</requestId>
2211
 
    <instancesSet>
2212
 
        <item>
2213
 
            <instanceId>i-cab0c1aa</instanceId>
2214
 
            <currentState>
2215
 
                <code>32</code>
2216
 
                <name>shutting-down</name>
2217
 
            </currentState>
2218
 
            <previousState>
2219
 
                <code>16</code>
2220
 
                <name>running</name>
2221
 
            </previousState>
2222
 
        </item>
2223
 
    </instancesSet>
2224
 
</TerminateInstancesResponse>"""
2225
 
        ec2_response = self.parser.terminate_instances(ec2_xml)
2226
 
        self.assertEquals(
2227
 
            [('i-cab0c1aa', 'running', 'shutting-down')], ec2_response)
2228
 
 
2229
 
    def test_nova_terminate_instances(self):
2230
 
        """
2231
 
        Ensure parser can handle the somewhat non-standard response from nova
2232
 
        Note that the bug has been reported in nova here:
2233
 
          https://launchpad.net/bugs/862680
2234
 
        """
2235
 
 
2236
 
        nova_xml = (
2237
 
            '<?xml version="1.0" ?><TerminateInstancesResponse '
2238
 
            'xmlns="http://ec2.amazonaws.com/doc/2008-12-01/"><requestId>'
2239
 
            '4fe6643d-2346-4add-adb7-a1f61f37c043</requestId>'
2240
 
            '<return>true</return></TerminateInstancesResponse>')
2241
 
        nova_response = self.parser.terminate_instances(nova_xml)
2242
 
        self.assertEquals([], nova_response)