From 3fab3f572f3da3470be1032775a3cf77dd7582f7 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 17 Mar 2020 16:09:42 +0000 Subject: [PATCH] #2773 - CloudFormation - Set CreationDate --- moto/cloudformation/models.py | 6 ++++++ moto/cloudformation/responses.py | 4 ++-- .../test_cloudformation_stack_crud_boto3.py | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/moto/cloudformation/models.py b/moto/cloudformation/models.py index b32d63b32..8136e353d 100644 --- a/moto/cloudformation/models.py +++ b/moto/cloudformation/models.py @@ -8,6 +8,7 @@ from boto3 import Session from moto.compat import OrderedDict from moto.core import BaseBackend, BaseModel +from moto.core.utils import iso_8601_datetime_without_milliseconds from .parsing import ResourceMap, OutputMap from .utils import ( @@ -240,6 +241,7 @@ class FakeStack(BaseModel): self.output_map = self._create_output_map() self._add_stack_event("CREATE_COMPLETE") self.status = "CREATE_COMPLETE" + self.creation_time = datetime.utcnow() def _create_resource_map(self): resource_map = ResourceMap( @@ -259,6 +261,10 @@ class FakeStack(BaseModel): output_map.create() return output_map + @property + def creation_time_iso_8601(self): + return iso_8601_datetime_without_milliseconds(self.creation_time) + def _add_stack_event( self, resource_status, resource_status_reason=None, resource_properties=None ): diff --git a/moto/cloudformation/responses.py b/moto/cloudformation/responses.py index 77a3051fd..782d68946 100644 --- a/moto/cloudformation/responses.py +++ b/moto/cloudformation/responses.py @@ -662,7 +662,7 @@ DESCRIBE_STACKS_TEMPLATE = """ {{ stack.name }} {{ stack.stack_id }} - 2010-07-27T22:28:28Z + {{ stack.creation_time_iso_8601 }} {{ stack.status }} {% if stack.notification_arns %} @@ -803,7 +803,7 @@ LIST_STACKS_RESPONSE = """ {{ stack.stack_id }} {{ stack.status }} {{ stack.name }} - 2011-05-23T15:47:44Z + {{ stack.creation_time_iso_8601 }} {{ stack.description }} {% endfor %} diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py index b7e86a1d5..5444c2278 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py +++ b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals import json from collections import OrderedDict +from datetime import datetime, timedelta +import pytz import boto3 from botocore.exceptions import ClientError @@ -911,6 +913,10 @@ def test_describe_stack_by_name(): stack = cf_conn.describe_stacks(StackName="test_stack")["Stacks"][0] stack["StackName"].should.equal("test_stack") + two_secs_ago = datetime.now(tz=pytz.UTC) - timedelta(seconds=2) + assert ( + two_secs_ago < stack["CreationTime"] < datetime.now(tz=pytz.UTC) + ), "Stack should have been created recently" @mock_cloudformation