Repository: getmoto/moto
Pull request #7137 (2023-12-18 21:28:19)

Techdebt: RedshiftData test fixtures should be mocked
Following our own examples in the docs: The `mock_..`-decorators should be used inside the fixture, otherwise the client isn't mocked properly.

Currently, these tests actually fail on master, when run on their own (`TEST_SERVER_MODE=true pytest tests/test_redshiftdata/`). They only pass in conjunction with other tests (`TEST_SERVER_MODE=true pytest tests/test_core/ tests/test_redshiftdata/`) - presumably because that alters the import order somehow.

With this change, the tests do succeed.


Patch:
diff --git a/moto/ec2/models/launch_templates.py b/moto/ec2/models/launch_templates.py
index 1e4a63f34..16db8d79d 100644
--- a/moto/ec2/models/launch_templates.py
+++ b/moto/ec2/models/launch_templates.py
@@ -13,6 +13,7 @@ from ..utils import (
     convert_tag_spec,
     generic_filter,
     random_launch_template_id,
+    random_launch_template_name,
     utc_date_and_time,
 )
 from .core import TaggedEC2Resource
@@ -141,6 +142,9 @@ class LaunchTemplate(TaggedEC2Resource, CloudFormationModel):
             properties.get("TagSpecifications", {}), tag_key="Tags"
         )
 
+        if name is None:
+            name = random_launch_template_name()
+
         launch_template = backend.create_launch_template(
             name, description, data, tag_spec
         )
diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py
index 08638ac37..821712af8 100644
--- a/moto/ec2/utils.py
+++ b/moto/ec2/utils.py
@@ -233,6 +233,10 @@ def random_launch_template_id() -> str:
     return random_id(prefix=EC2_RESOURCE_TO_PREFIX["launch-template"], size=17)
 
 
+def random_launch_template_name() -> str:
+    return f"LaunchTemplate_{random_resource_id(size=12)}"
+
+
 def random_iam_instance_profile_association_id() -> str:
     return random_id(prefix=EC2_RESOURCE_TO_PREFIX["iam-instance-profile-association"])
 


Test patch:
diff --git a/tests/test_ec2/test_launch_templates_cloudformation.py b/tests/test_ec2/test_launch_templates_cloudformation.py
index 71ce94a98..ede997e20 100644
--- a/tests/test_ec2/test_launch_templates_cloudformation.py
+++ b/tests/test_ec2/test_launch_templates_cloudformation.py
@@ -245,3 +245,55 @@ def test_asg_with_default_launch_template_version():
         == launch_template_name
     )
     assert autoscaling_group["LaunchTemplate"]["Version"] == "1"
+
+
+@mock_autoscaling
+@mock_cloudformation
+@mock_ec2
+def test_two_launch_templates():
+    cf_client = boto3.client("cloudformation", region_name="us-west-1")
+    ec2_client = boto3.client("ec2", region_name="us-west-1")
+
+    stack_name = str(uuid4())
+
+    template_json = json.dumps(
+        {
+            "AWSTemplateFormatVersion": "2010-09-09",
+            "Description": "AWS CloudFormation Template to create two LaunchTemplate",
+            "Resources": {
+                "LaunchTemplate0": {
+                    "Type": "AWS::EC2::LaunchTemplate",
+                    "Properties": {
+                        "LaunchTemplateData": {
+                            "ImageId": EXAMPLE_AMI_ID,
+                            "InstanceType": "t3.small",
+                            "UserData": "",
+                        },
+                    },
+                },
+                "LaunchTemplate1": {
+                    "Type": "AWS::EC2::LaunchTemplate",
+                    "Properties": {
+                        "LaunchTemplateData": {
+                            "ImageId": EXAMPLE_AMI_ID,
+                            "InstanceType": "t3.medium",
+                            "UserData": "",
+                        },
+                    },
+                },
+            },
+        }
+    )
+
+    cf_client.create_stack(
+        StackName=stack_name,
+        TemplateBody=template_json,
+        Capabilities=["CAPABILITY_NAMED_IAM"],
+        OnFailure="DELETE",
+    )
+
+    launch_templates = ec2_client.describe_launch_templates()
+    assert (
+        launch_templates["LaunchTemplates"][0]["LaunchTemplateName"]
+        != launch_templates["LaunchTemplates"][1]["LaunchTemplateName"]
+    )
diff --git a/tests/test_redshiftdata/test_redshiftdata.py b/tests/test_redshiftdata/test_redshiftdata.py
index 50a35d0ff..ae6181e25 100644
--- a/tests/test_redshiftdata/test_redshiftdata.py
+++ b/tests/test_redshiftdata/test_redshiftdata.py
@@ -15,10 +15,10 @@ RESOURCE_NOT_FOUND_ERROR_MESSAGE = "Query does not exist."
 
 @pytest.fixture(autouse=True, name="client")
 def fixture_client():
-    yield boto3.client("redshift-data", region_name=REGION)
+    with mock_redshiftdata():
+        yield boto3.client("redshift-data", region_name=REGION)
 
 
-@mock_redshiftdata
 def test_cancel_statement_throws_exception_when_uuid_invalid(client):
     statement_id = "test"
 
@@ -30,7 +30,6 @@ def test_cancel_statement_throws_exception_when_uuid_invalid(client):
     )
 
 
-@mock_redshiftdata
 def test_cancel_statement_throws_exception_when_statement_not_found(client):
     statement_id = "890f1253-595b-4608-a0d1-73f933ccd0a0"
 
@@ -42,7 +41,6 @@ def test_cancel_statement_throws_exception_when_statement_not_found(client):
     )
 
 
-@mock_redshiftdata
 def test_describe_statement_throws_exception_when_uuid_invalid(client):
     statement_id = "test"
 
@@ -54,7 +52,6 @@ def test_describe_statement_throws_exception_when_uuid_invalid(client):
     )
 
 
-@mock_redshiftdata
 def test_describe_statement_throws_exception_when_statement_not_found(client):
     statement_id = "890f1253-595b-4608-a0d1-73f933ccd0a0"
 
@@ -66,7 +63,6 @@ def test_describe_statement_throws_exception_when_statement_not_found(client):
     )
 
 
-@mock_redshiftdata
 def test_get_statement_result_throws_exception_when_uuid_invalid(client):
     statement_id = "test"
 
@@ -78,7 +74,6 @@ def test_get_statement_result_throws_exception_when_uuid_invalid(client):
     )
 
 
-@mock_redshiftdata
 def test_get_statement_result_throws_exception_when_statement_not_found(client):
     statement_id = "890f1253-595b-4608-a0d1-73f933ccd0a0"
 
@@ -90,7 +85,6 @@ def test_get_statement_result_throws_exception_when_statement_not_found(client):
     )
 
 
-@mock_redshiftdata
 def test_execute_statement_and_cancel_statement(client):
     cluster_identifier = "cluster_identifier"
     database = "database"
@@ -115,7 +109,6 @@ def test_execute_statement_and_cancel_statement(client):
     assert cancel_response["Status"] is True
 
 
-@mock_redshiftdata
 def test_execute_statement_and_describe_statement(client):
     cluster_identifier = "cluster_identifier"
     database = "database"
@@ -146,7 +139,6 @@ def test_execute_statement_and_describe_statement(client):
     assert describe_response["Status"] == "STARTED"
 
 
-@mock_redshiftdata
 def test_execute_statement_and_get_statement_result(client):
     cluster_identifier = "cluster_identifier"
     database = "database"