2014-08-27 15:17:06 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-08-25 22:09:38 +00:00
|
|
|
# Ensure 'assert_raises' context manager support for Python 2.6
|
|
|
|
import tests.backport_assert_raises
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
2013-02-22 04:13:01 +00:00
|
|
|
import boto
|
2014-08-26 17:25:50 +00:00
|
|
|
import six
|
2013-08-03 21:21:25 +00:00
|
|
|
import sure # noqa
|
2013-02-22 04:13:01 +00:00
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
from boto.exception import EC2ResponseError
|
2017-02-16 03:35:45 +00:00
|
|
|
from moto import mock_ec2_deprecated
|
2013-02-22 04:13:01 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 10:08:00 +00:00
|
|
|
def test_key_pairs_empty():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
assert len(conn.get_all_key_pairs()) == 0
|
2014-02-24 10:24:46 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-08-25 17:54:47 +00:00
|
|
|
def test_key_pairs_invalid_id():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
|
|
|
|
with assert_raises(EC2ResponseError) as cm:
|
|
|
|
conn.get_all_key_pairs('foo')
|
|
|
|
cm.exception.code.should.equal('InvalidKeyPair.NotFound')
|
|
|
|
cm.exception.status.should.equal(400)
|
|
|
|
cm.exception.request_id.should_not.be.none
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 10:24:46 +00:00
|
|
|
def test_key_pairs_create():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
with assert_raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
kp = conn.create_key_pair('foo', dry_run=True)
|
2017-02-24 00:43:48 +00:00
|
|
|
ex.exception.error_code.should.equal('DryRunOperation')
|
2016-10-15 13:08:44 +00:00
|
|
|
ex.exception.status.should.equal(400)
|
2017-02-24 02:37:43 +00:00
|
|
|
ex.exception.message.should.equal(
|
|
|
|
'An error occurred (DryRunOperation) when calling the CreateKeyPair operation: Request would have succeeded, but DryRun flag is set')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-02-24 10:24:46 +00:00
|
|
|
kp = conn.create_key_pair('foo')
|
2014-02-24 11:03:26 +00:00
|
|
|
assert kp.material.startswith('---- BEGIN RSA PRIVATE KEY ----')
|
2014-02-24 11:20:47 +00:00
|
|
|
kps = conn.get_all_key_pairs()
|
|
|
|
assert len(kps) == 1
|
|
|
|
assert kps[0].name == 'foo'
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 11:20:47 +00:00
|
|
|
def test_key_pairs_create_two():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
kp = conn.create_key_pair('foo')
|
|
|
|
kp = conn.create_key_pair('bar')
|
|
|
|
assert kp.material.startswith('---- BEGIN RSA PRIVATE KEY ----')
|
|
|
|
kps = conn.get_all_key_pairs()
|
2017-01-12 01:37:57 +00:00
|
|
|
kps.should.have.length_of(2)
|
|
|
|
[i.name for i in kps].should.contain('foo')
|
|
|
|
[i.name for i in kps].should.contain('bar')
|
2014-02-24 13:22:08 +00:00
|
|
|
kps = conn.get_all_key_pairs('foo')
|
2017-01-12 01:37:57 +00:00
|
|
|
kps.should.have.length_of(1)
|
|
|
|
kps[0].name.should.equal('foo')
|
2014-02-24 11:06:53 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 11:06:53 +00:00
|
|
|
def test_key_pairs_create_exist():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
kp = conn.create_key_pair('foo')
|
|
|
|
assert kp.material.startswith('---- BEGIN RSA PRIVATE KEY ----')
|
2014-02-24 11:20:47 +00:00
|
|
|
assert len(conn.get_all_key_pairs()) == 1
|
2014-08-25 17:54:47 +00:00
|
|
|
|
|
|
|
with assert_raises(EC2ResponseError) as cm:
|
|
|
|
conn.create_key_pair('foo')
|
|
|
|
cm.exception.code.should.equal('InvalidKeyPair.Duplicate')
|
|
|
|
cm.exception.status.should.equal(400)
|
|
|
|
cm.exception.request_id.should_not.be.none
|
2014-02-24 11:34:39 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 11:34:39 +00:00
|
|
|
def test_key_pairs_delete_no_exist():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
assert len(conn.get_all_key_pairs()) == 0
|
|
|
|
r = conn.delete_key_pair('foo')
|
|
|
|
r.should.be.ok
|
2014-02-24 11:35:42 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2014-02-24 11:35:42 +00:00
|
|
|
def test_key_pairs_delete_exist():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
conn.create_key_pair('foo')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
with assert_raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
r = conn.delete_key_pair('foo', dry_run=True)
|
2017-02-24 00:43:48 +00:00
|
|
|
ex.exception.error_code.should.equal('DryRunOperation')
|
2016-10-15 13:08:44 +00:00
|
|
|
ex.exception.status.should.equal(400)
|
2017-02-24 02:37:43 +00:00
|
|
|
ex.exception.message.should.equal(
|
|
|
|
'An error occurred (DryRunOperation) when calling the DeleteKeyPair operation: Request would have succeeded, but DryRun flag is set')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2014-02-24 11:35:42 +00:00
|
|
|
r = conn.delete_key_pair('foo')
|
|
|
|
r.should.be.ok
|
|
|
|
assert len(conn.get_all_key_pairs()) == 0
|
2015-09-24 15:25:49 +00:00
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2015-09-24 15:25:49 +00:00
|
|
|
def test_key_pairs_import():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
with assert_raises(EC2ResponseError) as ex:
|
2016-10-15 13:08:44 +00:00
|
|
|
kp = conn.import_key_pair('foo', b'content', dry_run=True)
|
2017-02-24 00:43:48 +00:00
|
|
|
ex.exception.error_code.should.equal('DryRunOperation')
|
2016-10-15 13:08:44 +00:00
|
|
|
ex.exception.status.should.equal(400)
|
2017-02-24 02:37:43 +00:00
|
|
|
ex.exception.message.should.equal(
|
|
|
|
'An error occurred (DryRunOperation) when calling the ImportKeyPair operation: Request would have succeeded, but DryRun flag is set')
|
2016-10-15 13:08:44 +00:00
|
|
|
|
2015-09-24 15:25:49 +00:00
|
|
|
kp = conn.import_key_pair('foo', b'content')
|
|
|
|
assert kp.name == 'foo'
|
|
|
|
kps = conn.get_all_key_pairs()
|
|
|
|
assert len(kps) == 1
|
|
|
|
assert kps[0].name == 'foo'
|
|
|
|
|
|
|
|
|
2017-02-16 03:35:45 +00:00
|
|
|
@mock_ec2_deprecated
|
2015-09-24 15:25:49 +00:00
|
|
|
def test_key_pairs_import_exist():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
kp = conn.import_key_pair('foo', b'content')
|
|
|
|
assert kp.name == 'foo'
|
|
|
|
assert len(conn.get_all_key_pairs()) == 1
|
|
|
|
|
|
|
|
with assert_raises(EC2ResponseError) as cm:
|
|
|
|
conn.create_key_pair('foo')
|
|
|
|
cm.exception.code.should.equal('InvalidKeyPair.Duplicate')
|
|
|
|
cm.exception.status.should.equal(400)
|
|
|
|
cm.exception.request_id.should_not.be.none
|
2017-09-14 09:29:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2_deprecated
|
|
|
|
def test_key_pair_filters():
|
|
|
|
conn = boto.connect_ec2('the_key', 'the_secret')
|
|
|
|
|
|
|
|
_ = conn.create_key_pair('kpfltr1')
|
|
|
|
kp2 = conn.create_key_pair('kpfltr2')
|
|
|
|
kp3 = conn.create_key_pair('kpfltr3')
|
|
|
|
|
|
|
|
kp_by_name = conn.get_all_key_pairs(
|
|
|
|
filters={'key-name': 'kpfltr2'})
|
|
|
|
set([kp.name for kp in kp_by_name]
|
|
|
|
).should.equal(set([kp2.name]))
|
|
|
|
|
|
|
|
kp_by_name = conn.get_all_key_pairs(
|
|
|
|
filters={'fingerprint': kp3.fingerprint})
|
|
|
|
set([kp.name for kp in kp_by_name]
|
|
|
|
).should.equal(set([kp3.name]))
|