Improve parsing of string values that represents booleans during block device mapping construction (#3073)
* convert str into bool * Fix python2 * Fix python2 * pylint
This commit is contained in:
parent
5988e5efaa
commit
610bf36f3b
@ -13,6 +13,7 @@ from moto.elbv2 import elbv2_backends
|
||||
from moto.core import ACCOUNT_ID
|
||||
|
||||
from copy import deepcopy
|
||||
import six
|
||||
|
||||
|
||||
class InstanceResponse(BaseResponse):
|
||||
@ -283,15 +284,15 @@ class InstanceResponse(BaseResponse):
|
||||
device_template["Ebs"]["VolumeSize"] = device_mapping.get(
|
||||
"ebs._volume_size"
|
||||
)
|
||||
device_template["Ebs"]["DeleteOnTermination"] = device_mapping.get(
|
||||
"ebs._delete_on_termination", False
|
||||
device_template["Ebs"]["DeleteOnTermination"] = self._convert_to_bool(
|
||||
device_mapping.get("ebs._delete_on_termination", False)
|
||||
)
|
||||
device_template["Ebs"]["VolumeType"] = device_mapping.get(
|
||||
"ebs._volume_type"
|
||||
)
|
||||
device_template["Ebs"]["Iops"] = device_mapping.get("ebs._iops")
|
||||
device_template["Ebs"]["Encrypted"] = device_mapping.get(
|
||||
"ebs._encrypted", False
|
||||
device_template["Ebs"]["Encrypted"] = self._convert_to_bool(
|
||||
device_mapping.get("ebs._encrypted", False)
|
||||
)
|
||||
mappings.append(device_template)
|
||||
|
||||
@ -308,6 +309,16 @@ class InstanceResponse(BaseResponse):
|
||||
):
|
||||
raise MissingParameterError("size or snapshotId")
|
||||
|
||||
@staticmethod
|
||||
def _convert_to_bool(bool_str):
|
||||
if isinstance(bool_str, bool):
|
||||
return bool_str
|
||||
|
||||
if isinstance(bool_str, six.text_type):
|
||||
return str(bool_str).lower() == "true"
|
||||
|
||||
return False
|
||||
|
||||
|
||||
BLOCK_DEVICE_MAPPING_TEMPLATE = {
|
||||
"VirtualName": None,
|
||||
|
@ -128,7 +128,35 @@ def test_instance_terminate_discard_volumes():
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_instance_terminate_keep_volumes():
|
||||
def test_instance_terminate_keep_volumes_explicit():
|
||||
|
||||
ec2_resource = boto3.resource("ec2", "us-west-1")
|
||||
|
||||
result = ec2_resource.create_instances(
|
||||
ImageId="ami-d3adb33f",
|
||||
MinCount=1,
|
||||
MaxCount=1,
|
||||
BlockDeviceMappings=[
|
||||
{
|
||||
"DeviceName": "/dev/sda1",
|
||||
"Ebs": {"VolumeSize": 50, "DeleteOnTermination": False},
|
||||
}
|
||||
],
|
||||
)
|
||||
instance = result[0]
|
||||
|
||||
instance_volume_ids = []
|
||||
for volume in instance.volumes.all():
|
||||
instance_volume_ids.append(volume.volume_id)
|
||||
|
||||
instance.terminate()
|
||||
instance.wait_until_terminated()
|
||||
|
||||
assert len(list(ec2_resource.volumes.all())) == 1
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_instance_terminate_keep_volumes_implicit():
|
||||
ec2_resource = boto3.resource("ec2", "us-west-1")
|
||||
|
||||
result = ec2_resource.create_instances(
|
||||
|
Loading…
Reference in New Issue
Block a user