Adding server mode
This commit is contained in:
parent
c6f5afff75
commit
a728b2581a
31 changed files with 489 additions and 66 deletions
19
tests/test_core/test_server.py
Normal file
19
tests/test_core/test_server.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from mock import patch
|
||||
import sure # flake8: noqa
|
||||
|
||||
from moto.server import main
|
||||
|
||||
|
||||
def test_wrong_arguments():
|
||||
try:
|
||||
main(["name", "test1", "test2"])
|
||||
assert False, ("main() when called with the incorrect number of args"
|
||||
" should raise a system exit")
|
||||
except SystemExit:
|
||||
pass
|
||||
|
||||
|
||||
@patch('moto.server.app.run')
|
||||
def test_right_arguments(app_run):
|
||||
main(["name", "s3"])
|
||||
app_run.assert_called_once_with()
|
||||
20
tests/test_core/test_url_mapping.py
Normal file
20
tests/test_core/test_url_mapping.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import sure # flake8: noqa
|
||||
|
||||
from moto.core.utils import convert_regex_to_flask_path
|
||||
|
||||
|
||||
def test_flask_path_converting_simple():
|
||||
convert_regex_to_flask_path("/").should.equal("/")
|
||||
convert_regex_to_flask_path("/$").should.equal("/")
|
||||
|
||||
convert_regex_to_flask_path("/foo").should.equal("/foo")
|
||||
|
||||
convert_regex_to_flask_path("/foo/bar/").should.equal("/foo/bar/")
|
||||
|
||||
|
||||
def test_flask_path_converting_regex():
|
||||
convert_regex_to_flask_path("/(?P<key_name>\w+)").should.equal('/<regex("\w+"):key_name>')
|
||||
|
||||
convert_regex_to_flask_path("(?P<account_id>\d+)/(?P<queue_name>.*)$").should.equal(
|
||||
'<regex("\d+"):account_id>/<regex(".*"):queue_name>'
|
||||
)
|
||||
18
tests/test_dynamodb/test_server.py
Normal file
18
tests/test_dynamodb/test_server.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import sure # flake8: noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
Test the different server responses
|
||||
'''
|
||||
server.configure_urls("dynamodb")
|
||||
|
||||
|
||||
def test_table_list():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.get('/')
|
||||
res.status_code.should.equal(404)
|
||||
|
||||
headers = {'X-Amz-Target': 'TestTable.ListTables'}
|
||||
res = test_client.get('/', headers=headers)
|
||||
res.data.should.contain('TableNames')
|
||||
|
|
@ -9,7 +9,7 @@ from moto import mock_ec2
|
|||
@mock_ec2
|
||||
def test_ami_create_and_delete():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
image = conn.create_image(instance.id, "test-ami", "this is a test ami")
|
||||
|
||||
|
|
@ -23,13 +23,14 @@ def test_ami_create_and_delete():
|
|||
@mock_ec2
|
||||
def test_ami_create_from_missing_instance():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
conn.create_image.when.called_with("i-abcdefg", "test-ami", "this is a test ami").should.throw(EC2ResponseError)
|
||||
args = ["i-abcdefg", "test-ami", "this is a test ami"]
|
||||
conn.create_image.when.called_with(*args).should.throw(EC2ResponseError)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_ami_pulls_attributes_from_instance():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
instance.modify_attribute("kernel", "test-kernel")
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import boto
|
||||
from boto.exception import EC2ResponseError
|
||||
|
||||
from sure import expect
|
||||
import sure # flake8: noqa
|
||||
|
||||
from moto import mock_ec2
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ def test_create_and_delete_volume():
|
|||
@mock_ec2
|
||||
def test_volume_attach_and_detach():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
volume = conn.create_volume(80, "us-east-1a")
|
||||
|
||||
|
|
@ -47,7 +46,8 @@ def test_volume_attach_and_detach():
|
|||
volume.update()
|
||||
volume.volume_state().should.equal('available')
|
||||
|
||||
conn.detach_volume.when.called_with(volume.id, instance.id, "/dev/sdh").should.throw(EC2ResponseError)
|
||||
conn.detach_volume.when.called_with(
|
||||
volume.id, instance.id, "/dev/sdh").should.throw(EC2ResponseError)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
|
|
|
|||
|
|
@ -1,14 +1,34 @@
|
|||
import boto
|
||||
from boto.ec2.instance import Reservation, InstanceAttribute
|
||||
from sure import expect
|
||||
import sure # flake8: noqa
|
||||
|
||||
from moto import mock_ec2
|
||||
|
||||
|
||||
################ Test Readme ###############
|
||||
def add_servers(ami_id, count):
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
for index in range(count):
|
||||
conn.run_instances(ami_id)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_add_servers():
|
||||
add_servers('ami-1234abcd', 2)
|
||||
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservations = conn.get_all_instances()
|
||||
assert len(reservations) == 2
|
||||
instance1 = reservations[0].instances[0]
|
||||
assert instance1.image_id == 'ami-1234abcd'
|
||||
|
||||
############################################
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_instance_launch_and_terminate():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
reservation.should.be.a(Reservation)
|
||||
reservation.instances.should.have.length_of(1)
|
||||
instance = reservation.instances[0]
|
||||
|
|
@ -31,11 +51,12 @@ def test_instance_launch_and_terminate():
|
|||
@mock_ec2
|
||||
def test_instance_start_and_stop():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>', min_count=2)
|
||||
reservation = conn.run_instances('ami-1234abcd', min_count=2)
|
||||
instances = reservation.instances
|
||||
instances.should.have.length_of(2)
|
||||
|
||||
stopped_instances = conn.stop_instances([instance.id for instance in instances])
|
||||
instance_ids = [instance.id for instance in instances]
|
||||
stopped_instances = conn.stop_instances(instance_ids)
|
||||
|
||||
for instance in stopped_instances:
|
||||
instance.state.should.equal('stopping')
|
||||
|
|
@ -47,7 +68,7 @@ def test_instance_start_and_stop():
|
|||
@mock_ec2
|
||||
def test_instance_reboot():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
instance.reboot()
|
||||
instance.state.should.equal('pending')
|
||||
|
|
@ -56,7 +77,7 @@ def test_instance_reboot():
|
|||
@mock_ec2
|
||||
def test_instance_attribute_instance_type():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
|
||||
instance.modify_attribute("instanceType", "m1.small")
|
||||
|
|
@ -69,11 +90,11 @@ def test_instance_attribute_instance_type():
|
|||
@mock_ec2
|
||||
def test_instance_attribute_user_data():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
|
||||
instance.modify_attribute("userData", "this is my user data")
|
||||
|
||||
instance_attribute = instance.get_attribute("userData")
|
||||
instance_attribute.should.be.a(InstanceAttribute)
|
||||
expect(instance_attribute.get("userData")).should.equal("this is my user data")
|
||||
instance_attribute.get("userData").should.equal("this is my user data")
|
||||
|
|
|
|||
20
tests/test_ec2/test_server.py
Normal file
20
tests/test_ec2/test_server.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import re
|
||||
import sure # flake8: noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
Test the different server responses
|
||||
'''
|
||||
server.configure_urls("ec2")
|
||||
|
||||
|
||||
def test_ec2_server_get():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.get('/?Action=RunInstances&ImageId=ami-60a54009')
|
||||
|
||||
groups = re.search("<instanceId>(.*)</instanceId>", res.data)
|
||||
instance_id = groups.groups()[0]
|
||||
|
||||
res = test_client.get('/?Action=DescribeInstances')
|
||||
res.data.should.contain(instance_id)
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import boto
|
||||
from sure import expect
|
||||
import sure # flake8: noqa
|
||||
|
||||
from moto import mock_ec2
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ from moto import mock_ec2
|
|||
@mock_ec2
|
||||
def test_instance_launch_and_terminate():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>')
|
||||
reservation = conn.run_instances('ami-1234abcd')
|
||||
instance = reservation.instances[0]
|
||||
|
||||
instance.add_tag("a key", "some value")
|
||||
|
|
|
|||
35
tests/test_s3/test_server.py
Normal file
35
tests/test_s3/test_server.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import sure # flake8: noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
Test the different server responses
|
||||
'''
|
||||
server.configure_urls("s3")
|
||||
|
||||
|
||||
def test_s3_server_get():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.get('/')
|
||||
|
||||
res.data.should.contain('ListAllMyBucketsResult')
|
||||
|
||||
|
||||
def test_s3_server_bucket_create():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.put('/', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
|
||||
res = test_client.get('/')
|
||||
res.data.should.contain('<Name>foobar</Name>')
|
||||
|
||||
res = test_client.get('/', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
res.data.should.contain("ListBucketResult")
|
||||
|
||||
res = test_client.put('/bar', 'http://foobar.localhost:5000/', data='test value')
|
||||
res.status_code.should.equal(200)
|
||||
|
||||
res = test_client.get('/bar', 'http://foobar.localhost:5000/')
|
||||
res.status_code.should.equal(200)
|
||||
res.data.should.equal("test value")
|
||||
14
tests/test_ses/test_server.py
Normal file
14
tests/test_ses/test_server.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import sure # flake8: noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
Test the different server responses
|
||||
'''
|
||||
server.configure_urls("ses")
|
||||
|
||||
|
||||
def test_ses_list_identities():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.get('/?Action=ListIdentities')
|
||||
res.data.should.contain("ListIdentitiesResponse")
|
||||
26
tests/test_sqs/test_server.py
Normal file
26
tests/test_sqs/test_server.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import base64
|
||||
import re
|
||||
import sure # flake8: noqa
|
||||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
Test the different server responses
|
||||
'''
|
||||
server.configure_urls("sqs")
|
||||
|
||||
|
||||
def test_ses_list_identities():
|
||||
test_client = server.app.test_client()
|
||||
res = test_client.get('/?Action=ListQueues')
|
||||
res.data.should.contain("ListQueuesResponse")
|
||||
|
||||
res = test_client.put('/?Action=CreateQueue&QueueName=testqueue')
|
||||
|
||||
res = test_client.put(
|
||||
'/123/testqueue?MessageBody=test-message&Action=SendMessage')
|
||||
|
||||
res = test_client.get(
|
||||
'/123/testqueue?Action=ReceiveMessage&MaxNumberOfMessages=1')
|
||||
message = re.search("<Body>(.*?)</Body>", res.data).groups()[0]
|
||||
base64.decodestring(message).should.equal('test-message')
|
||||
Loading…
Add table
Add a link
Reference in a new issue