Repository: getmoto/moto
Pull request #6679 (2023-08-16 18:14:04)

Techdebt: Replace sure with regular assertions in Polly
See https://github.com/getmoto/moto/issues/6035


Patch:
diff --git a/moto/ec2/models/instances.py b/moto/ec2/models/instances.py
index 19a2dac0b..8463ed9c3 100644
--- a/moto/ec2/models/instances.py
+++ b/moto/ec2/models/instances.py
@@ -120,6 +120,7 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
         in_ec2_classic = not bool(self.subnet_id)
         self.key_name = kwargs.get("key_name")
         self.ebs_optimized = kwargs.get("ebs_optimized", False)
+        self.monitoring_state = kwargs.get("monitoring_state", "disabled")
         self.source_dest_check = "true"
         self.launch_time = utc_date_and_time()
         self.ami_launch_index = kwargs.get("ami_launch_index", 0)
diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py
index e1d2e0bcf..c1ec8fc92 100644
--- a/moto/ec2/responses/instances.py
+++ b/moto/ec2/responses/instances.py
@@ -84,6 +84,9 @@ class InstanceResponse(EC2BaseResponse):
             or None,
             "iam_instance_profile_arn": self._get_param("IamInstanceProfile.Arn")
             or None,
+            "monitoring_state": "enabled"
+            if self._get_param("Monitoring.Enabled") == "true"
+            else "disabled",
         }
         if len(kwargs["nics"]) and kwargs["subnet_id"]:
             raise InvalidParameterCombination(
@@ -472,7 +475,7 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
             <tenancy>default</tenancy>
           </placement>
           <monitoring>
-            <state>enabled</state>
+            <state> {{ instance.monitoring_state }} </state>
           </monitoring>
           {% if instance.subnet_id %}
             <subnetId>{{ instance.subnet_id }}</subnetId>
@@ -632,7 +635,7 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns="http://ec2.amazona
                     <platform>{{ instance.platform }}</platform>
                     {% endif %}
                     <monitoring>
-                      <state>disabled</state>
+                      <state>{{ instance.monitoring_state }}</state>
                     </monitoring>
                     {% if instance.subnet_id %}
                       <subnetId>{{ instance.subnet_id }}</subnetId>


Test patch:
diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py
index 919630cfd..b45d52d89 100644
--- a/tests/test_ec2/test_instances.py
+++ b/tests/test_ec2/test_instances.py
@@ -2664,3 +2664,35 @@ def test_run_multiple_instances_with_single_nic_template():
     instance_1_ip = enis[1]["PrivateIpAddresses"][0]["PrivateIpAddress"]
 
     assert instance_0_ip != instance_1_ip
+
+
+@mock_ec2
+def test_describe_instance_without_enhanced_monitoring():
+    conn = boto3.client("ec2", region_name="us-west-1")
+
+    instance = conn.run_instances(
+        ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, Monitoring={"Enabled": True}
+    )
+    assert instance["Instances"][0]["Monitoring"] == {"State": "enabled"}
+
+    result = conn.describe_instances(
+        InstanceIds=[instance["Instances"][0]["InstanceId"]]
+    )["Reservations"][0]["Instances"]
+
+    assert result[0]["Monitoring"] == {"State": "enabled"}
+
+
+@mock_ec2
+def test_describe_instance_with_enhanced_monitoring():
+    conn = boto3.client("ec2", region_name="us-west-1")
+
+    instance = conn.run_instances(
+        ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, Monitoring={"Enabled": False}
+    )
+    assert instance["Instances"][0]["Monitoring"] == {"State": "disabled"}
+
+    result = conn.describe_instances(
+        InstanceIds=[instance["Instances"][0]["InstanceId"]]
+    )["Reservations"][0]["Instances"]
+
+    assert result[0]["Monitoring"] == {"State": "disabled"}
diff --git a/tests/test_polly/test_polly.py b/tests/test_polly/test_polly.py
index 1700dfeb9..c02788905 100644
--- a/tests/test_polly/test_polly.py
+++ b/tests/test_polly/test_polly.py
@@ -1,267 +1,263 @@
-from botocore.exceptions import ClientError
-import boto3
-import sure  # noqa # pylint: disable=unused-import
-from moto import mock_polly
-
-# Polly only available in a few regions
-DEFAULT_REGION = "eu-west-1"
-
-LEXICON_XML = """<?xml version="1.0" encoding="UTF-8"?>
-<lexicon version="1.0"
-      xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
-      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-      xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
-        http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
-      alphabet="ipa"
-      xml:lang="en-US">
-  <lexeme>
-    <grapheme>W3C</grapheme>
-    <alias>World Wide Web Consortium</alias>
-  </lexeme>
-</lexicon>"""
-
-
-@mock_polly
-def test_describe_voices():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-
-    resp = client.describe_voices()
-    len(resp["Voices"]).should.be.greater_than(1)
-
-    resp = client.describe_voices(LanguageCode="en-GB")
-    len(resp["Voices"]).should.equal(4)
-
-    try:
-        client.describe_voices(LanguageCode="SOME_LANGUAGE")
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("400")
-    else:
-        raise RuntimeError("Should have raised an exception")
-
-
-@mock_polly
-def test_put_list_lexicon():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-
-    # Return nothing
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    resp = client.list_lexicons()
-    len(resp["Lexicons"]).should.equal(1)
-
-
-@mock_polly
-def test_put_get_lexicon():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-
-    # Return nothing
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    resp = client.get_lexicon(Name="test")
-    resp.should.contain("Lexicon")
-    resp.should.contain("LexiconAttributes")
-
-
-@mock_polly
-def test_put_lexicon_bad_name():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-
-    try:
-        client.put_lexicon(Name="test-invalid", Content=LEXICON_XML)
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("InvalidParameterValue")
-    else:
-        raise RuntimeError("Should have raised an exception")
-
-
-@mock_polly
-def test_synthesize_speech():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-
-    # Return nothing
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    tests = (("pcm", "audio/pcm"), ("mp3", "audio/mpeg"), ("ogg_vorbis", "audio/ogg"))
-    for output_format, content_type in tests:
-        resp = client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat=output_format,
-            SampleRate="16000",
-            Text="test1234",
-            TextType="text",
-            VoiceId="Astrid",
-        )
-        resp["ContentType"].should.equal(content_type)
-
-
-@mock_polly
-def test_synthesize_speech_bad_lexicon():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test2"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="text",
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("LexiconNotFoundException")
-    else:
-        raise RuntimeError("Should have raised LexiconNotFoundException")
-
-
-@mock_polly
-def test_synthesize_speech_bad_output_format():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="invalid",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="text",
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("InvalidParameterValue")
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_bad_sample_rate():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="18000",
-            Text="test1234",
-            TextType="text",
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("InvalidSampleRateException")
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_bad_text_type():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="invalid",
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("InvalidParameterValue")
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_bad_voice_id():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="text",
-            VoiceId="Luke",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("InvalidParameterValue")
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_text_too_long():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234" * 376,  # = 3008 characters
-            TextType="text",
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal("TextLengthExceededException")
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_bad_speech_marks1():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="text",
-            SpeechMarkTypes=["word"],
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal(
-            "MarksNotSupportedForFormatException"
-        )
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_synthesize_speech_bad_speech_marks2():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-
-    try:
-        client.synthesize_speech(
-            LexiconNames=["test"],
-            OutputFormat="pcm",
-            SampleRate="16000",
-            Text="test1234",
-            TextType="ssml",
-            SpeechMarkTypes=["word"],
-            VoiceId="Astrid",
-        )
-    except ClientError as err:
-        err.response["Error"]["Code"].should.equal(
-            "MarksNotSupportedForFormatException"
-        )
-    else:
-        raise RuntimeError("Should have raised ")
-
-
-@mock_polly
-def test_update_lexicon():
-    client = boto3.client("polly", region_name=DEFAULT_REGION)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
-    client.put_lexicon(Name="test", Content=LEXICON_XML)
+import boto3
+from botocore.exceptions import ClientError
+
+from moto import mock_polly
+
+# Polly only available in a few regions
+DEFAULT_REGION = "eu-west-1"
+
+LEXICON_XML = """<?xml version="1.0" encoding="UTF-8"?>
+<lexicon version="1.0"
+      xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
+      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+      xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
+        http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
+      alphabet="ipa"
+      xml:lang="en-US">
+  <lexeme>
+    <grapheme>W3C</grapheme>
+    <alias>World Wide Web Consortium</alias>
+  </lexeme>
+</lexicon>"""
+
+
+@mock_polly
+def test_describe_voices():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+
+    resp = client.describe_voices()
+    assert len(resp["Voices"]) >= 1
+
+    resp = client.describe_voices(LanguageCode="en-GB")
+    assert len(resp["Voices"]) == 4
+
+    try:
+        client.describe_voices(LanguageCode="SOME_LANGUAGE")
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "400"
+    else:
+        raise RuntimeError("Should have raised an exception")
+
+
+@mock_polly
+def test_put_list_lexicon():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+
+    # Return nothing
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    resp = client.list_lexicons()
+    assert len(resp["Lexicons"]) == 1
+
+
+@mock_polly
+def test_put_get_lexicon():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+
+    # Return nothing
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    resp = client.get_lexicon(Name="test")
+    assert "Lexicon" in resp
+    assert "LexiconAttributes" in resp
+
+
+@mock_polly
+def test_put_lexicon_bad_name():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+
+    try:
+        client.put_lexicon(Name="test-invalid", Content=LEXICON_XML)
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "InvalidParameterValue"
+    else:
+        raise RuntimeError("Should have raised an exception")
+
+
+@mock_polly
+def test_synthesize_speech():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+
+    # Return nothing
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    tests = (("pcm", "audio/pcm"), ("mp3", "audio/mpeg"), ("ogg_vorbis", "audio/ogg"))
+    for output_format, content_type in tests:
+        resp = client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat=output_format,
+            SampleRate="16000",
+            Text="test1234",
+            TextType="text",
+            VoiceId="Astrid",
+        )
+        assert resp["ContentType"] == content_type
+
+
+@mock_polly
+def test_synthesize_speech_bad_lexicon():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test2"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="text",
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "LexiconNotFoundException"
+    else:
+        raise RuntimeError("Should have raised LexiconNotFoundException")
+
+
+@mock_polly
+def test_synthesize_speech_bad_output_format():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="invalid",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="text",
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "InvalidParameterValue"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_bad_sample_rate():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="18000",
+            Text="test1234",
+            TextType="text",
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "InvalidSampleRateException"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_bad_text_type():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="invalid",
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "InvalidParameterValue"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_bad_voice_id():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="text",
+            VoiceId="Luke",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "InvalidParameterValue"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_text_too_long():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234" * 376,  # = 3008 characters
+            TextType="text",
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "TextLengthExceededException"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_bad_speech_marks1():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="text",
+            SpeechMarkTypes=["word"],
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "MarksNotSupportedForFormatException"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_synthesize_speech_bad_speech_marks2():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+
+    try:
+        client.synthesize_speech(
+            LexiconNames=["test"],
+            OutputFormat="pcm",
+            SampleRate="16000",
+            Text="test1234",
+            TextType="ssml",
+            SpeechMarkTypes=["word"],
+            VoiceId="Astrid",
+        )
+    except ClientError as err:
+        assert err.response["Error"]["Code"] == "MarksNotSupportedForFormatException"
+    else:
+        raise RuntimeError("Should have raised ")
+
+
+@mock_polly
+def test_update_lexicon():
+    client = boto3.client("polly", region_name=DEFAULT_REGION)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
+    client.put_lexicon(Name="test", Content=LEXICON_XML)
diff --git a/tests/test_polly/test_server.py b/tests/test_polly/test_server.py
index 1668b280a..8db2b148f 100644
--- a/tests/test_polly/test_server.py
+++ b/tests/test_polly/test_server.py
@@ -1,12 +1,7 @@
-import sure  # noqa # pylint: disable=unused-import
-
+"""Test the different server responses."""
 import moto.server as server
 from moto import mock_polly
 
-"""
-Test the different server responses
-"""
-
 
 @mock_polly
 def test_polly_list():
@@ -14,4 +9,4 @@ def test_polly_list():
     test_client = backend.test_client()
 
     res = test_client.get("/v1/lexicons")
-    res.status_code.should.equal(200)
+    assert res.status_code == 200