-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(unit-template): allow generating StackId value #384
- Loading branch information
1 parent
3fa58f5
commit fdd70e3
Showing
4 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
AWSTemplateFormatVersion: 2010-09-09 | ||
Description: "Creates an S3 bucket to store logs." | ||
|
||
Resources: | ||
UniqueBucket: | ||
Type: AWS::S3::Bucket | ||
Properties: | ||
BucketName: !Sub | ||
- 'my-test-${stack_region}-${uniqifier}-bucket' | ||
- # AWS::StackId has this format | ||
# arn:aws:cloudformation:us-west-2:123456789012:stack/teststack/51af3dc0-da77-11e4-872e-1234567db123 | ||
# Trying to capture the last piece after the '-' | ||
# As stack name could contain "-"s, split on the "/"s first | ||
uniqifier: !Select [ 4, !Split [ "-", !Select [ 2, !Split [ "/", !Ref AWS::StackId ] ] ] ] | ||
# Usually you would refer to AWS:::Region, but trying to test StackId creation works as expected | ||
stack_region: !Select [ 3, !Split [":", !Ref AWS::StackId]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Test case that verifies that generation of the value for AWS::StackId works as expected | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from cloud_radar.cf.unit._template import Template | ||
|
||
|
||
@pytest.fixture | ||
def template(): | ||
template_path = Path(__file__).parent / "../../templates/test_stackid.yaml" | ||
|
||
return Template.from_yaml(template_path.resolve(), {}) | ||
|
||
|
||
def test_function_populated_var(template): | ||
expected_value = "arn:aws:cloudformation:us-west-2:123456789012:stack/teststack/51af3dc0-da77-11e4-872e-1234567db123" | ||
Template.StackId = expected_value | ||
|
||
actual_value = template._get_populated_stack_id() | ||
assert actual_value == expected_value | ||
|
||
|
||
def test_function_blank_var(template): | ||
Template.StackId = "" | ||
|
||
actual_value = template._get_populated_stack_id() | ||
# Check all except the UUID | ||
assert actual_value.startswith( | ||
f"arn:{Template.Partition}:cloudformation:{Template.Region}:{Template.AccountId}:stack/{Template.StackName}/" | ||
) | ||
|
||
# Check the UUID part looks UUID like | ||
unique_uuid = actual_value.split("/")[2] | ||
assert 5 == len(unique_uuid.split("-")) | ||
|
||
|
||
def test_template_blank_var_stack_region(template): | ||
Template.StackId = "" | ||
|
||
stack = template.create_stack({}, region="eu-west-1") | ||
|
||
bucket = stack.get_resource("UniqueBucket") | ||
bucket_name = bucket.get_property_value("BucketName") | ||
|
||
assert len(bucket_name) == 37 | ||
assert bucket_name[:18] == "my-test-eu-west-1-" | ||
assert bucket_name[30:] == "-bucket" | ||
|
||
|
||
def test_template_blank_var_global_region(template): | ||
Template.StackId = "" | ||
|
||
stack = template.create_stack({}) | ||
|
||
bucket = stack.get_resource("UniqueBucket") | ||
bucket_name = bucket.get_property_value("BucketName") | ||
|
||
assert len(bucket_name) == 37 | ||
assert bucket_name[:18] == "my-test-us-east-1-" | ||
assert bucket_name[30:] == "-bucket" | ||
|
||
|
||
def test_template_populated_var(template): | ||
Template.StackId = "arn:aws:cloudformation:us-west-2:123456789012:stack/teststack/51af3dc0-da77-11e4-872e-1234567db123" | ||
|
||
stack = template.create_stack({}) | ||
|
||
bucket = stack.get_resource("UniqueBucket") | ||
bucket_name = bucket.get_property_value("BucketName") | ||
|
||
assert "my-test-us-west-2-1234567db123-bucket" == bucket_name |