moto/tests/test_ec2/test_server.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.4 KiB
Python
Raw Normal View History

2013-03-05 13:14:43 +00:00
import re
2021-10-18 19:44:29 +00:00
import sure # noqa # pylint: disable=unused-import
import xmltodict
2013-03-05 13:14:43 +00:00
import moto.server as server
from tests import EXAMPLE_AMI_ID
2013-03-05 13:14:43 +00:00
2019-10-31 15:44:26 +00:00
"""
2013-03-05 13:14:43 +00:00
Test the different server responses
2019-10-31 15:44:26 +00:00
"""
2013-03-05 13:14:43 +00:00
def test_ec2_server_get():
2013-12-29 01:15:37 +00:00
backend = server.create_backend_app("ec2")
test_client = backend.test_client()
res = test_client.get(
"/?Action=RunInstances&ImageId=" + EXAMPLE_AMI_ID,
headers={"Host": "ec2.us-east-1.amazonaws.com"},
)
2013-03-05 13:14:43 +00:00
2017-02-24 02:37:43 +00:00
groups = re.search("<instanceId>(.*)</instanceId>", res.data.decode("utf-8"))
2013-03-05 13:14:43 +00:00
instance_id = groups.groups()[0]
res = test_client.get("/?Action=DescribeInstances")
2014-08-26 17:25:50 +00:00
res.data.decode("utf-8").should.contain(instance_id)
def test_ec2_get_unknown_vpc():
"""
Ensure that this call returns the error format in the right format
Terraform will throw errors when destroying a VPC otherwise
:return:
"""
backend = server.create_backend_app("ec2")
test_client = backend.test_client()
res = test_client.get(
"/?Action=DescribeVpcs&VpcId.1=vpc-unknown",
headers={"Host": "ec2.us-east-1.amazonaws.com"},
)
res.status_code.should.equal(400)
body = xmltodict.parse(res.data.decode("utf-8"), dict_constructor=dict)
body.should.have.key("Response")
body["Response"].should.have.key("Errors")
body["Response"]["Errors"].should.have.key("Error")
error = body["Response"]["Errors"]["Error"]
error["Code"].should.equal("InvalidVpcID.NotFound")
error["Message"].should.equal("VpcID {'vpc-unknown'} does not exist.")
def test_ec2_get_unknown_route_table():
"""
Ensure that this call returns the error format in the right format
Terraform will throw errors when destroying a RouteTable otherwise
:return:
"""
backend = server.create_backend_app("ec2")
test_client = backend.test_client()
res = test_client.get(
"/?Action=DescribeRouteTables&RouteTableId.1=rtb-unknown",
headers={"Host": "ec2.us-east-1.amazonaws.com"},
)
res.status_code.should.equal(400)
body = xmltodict.parse(res.data.decode("utf-8"), dict_constructor=dict)
body.should.have.key("Response")
body["Response"].should.have.key("Errors")
body["Response"]["Errors"].should.have.key("Error")
error = body["Response"]["Errors"]["Error"]
error["Code"].should.equal("InvalidRouteTableID.NotFound")
error["Message"].should.equal("The routeTable ID 'rtb-unknown' does not exist")