Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix diff with local yaml templates #541

Merged
merged 3 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions stacker/actions/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,27 @@ def diff_parameters(old_params, new_params):
return diff


def normalize_json(template):
"""Normalize our template for diffing.

Args:
template(str): string representing the template

Returns:
list: json representation of the parameters
"""
obj = parse_cloudformation_template(template)
json_str = json.dumps(obj, sort_keys=True, indent=4)
result = []
lines = json_str.split("\n")
for line in lines:
result.append(line + "\n")
return result


def print_stack_changes(stack_name, new_stack, old_stack, new_params,
old_params):
"""Prints out the paramters (if changed) and stack diff"""
"""Prints out the parameters (if changed) and stack diff"""
from_file = "old_%s" % (stack_name,)
to_file = "new_%s" % (stack_name,)
lines = difflib.context_diff(
Expand All @@ -156,9 +174,10 @@ def print_stack_changes(stack_name, new_stack, old_stack, new_params,
template_changes = list(lines)
if not template_changes:
print "*** No changes to template ***"
else:
param_diffs = diff_parameters(old_params, new_params)
param_diffs = diff_parameters(old_params, new_params)
if param_diffs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I noticed this bug recently as well. Thanks for fixing!

print format_params_diff(param_diffs)
if template_changes:
print "".join(template_changes)


Expand All @@ -174,23 +193,6 @@ class Action(build.Action):
config.
"""

def _normalize_json(self, template):
"""Normalizes our template for diffing

Args:
template(str): json string representing the template

Returns:
list: json representation of the parameters
"""
obj = json.loads(template)
json_str = json.dumps(obj, sort_keys=True, indent=4)
result = []
lines = json_str.split("\n")
for line in lines:
result.append(line + "\n")
return result

def _print_new_stack(self, stack, parameters):
"""Prints out the parameters & stack contents of a new stack"""
print "New template parameters:"
Expand Down Expand Up @@ -229,7 +231,7 @@ def _diff_stack(self, stack, **kwargs):
for p in parameters:
new_params[p['ParameterKey']] = p['ParameterValue']
new_template = stack.blueprint.rendered
new_stack = self._normalize_json(new_template)
new_stack = normalize_json(new_template)

print "============== Stack: %s ==============" % (stack.name,)
# If this is a completely new template dump our params & stack
Expand All @@ -244,7 +246,7 @@ def _diff_stack(self, stack, **kwargs):
# ->
# AWSTemplateFormatVersion: "2010-09-09"
old_template = parse_cloudformation_template(old_template)
old_stack = self._normalize_json(
old_stack = normalize_json(
json.dumps(old_template,
sort_keys=True,
indent=4)
Expand Down
57 changes: 57 additions & 0 deletions stacker/tests/actions/test_diff.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import unittest

from operator import attrgetter
from stacker.actions.diff import (
diff_dictionaries,
diff_parameters,
normalize_json,
DictValue
)

Expand Down Expand Up @@ -81,3 +83,58 @@ def test_diff_parameters_no_changes(self):

param_diffs = diff_parameters(old_params, new_params)
self.assertEquals(param_diffs, [])


class TestDiffFunctions(unittest.TestCase):
"""Test functions in diff."""

def test_normalize_json(self):
"""Ensure normalize_json parses yaml correctly."""
with open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), # noqa
'fixtures',
'cfn_template.yaml'), 'r') as yamlfile:
template = yamlfile.read()
normalized_template = [
'{\n',
' "AWSTemplateFormatVersion": "2010-09-09", \n',
' "Description": "TestTemplate", \n',
' "Outputs": {\n',
' "DummyId": {\n',
' "Value": "dummy-1234"\n',
' }\n',
' }, \n',
' "Parameters": {\n',
' "Param1": {\n',
' "Type": "String"\n',
' }, \n',
' "Param2": {\n',
' "Default": "default", \n',
' "Type": "CommaDelimitedList"\n',
' }\n',
' }, \n',
' "Resources": {\n',
' "Bucket": {\n',
' "Properties": {\n',
' "BucketName": {\n',
' "Fn::Join": [\n',
' "-", \n',
' [\n',
' {\n',
' "Ref": "AWS::StackName"\n',
' }, \n',
' {\n',
' "Ref": "AWS::Region"\n',
' }\n',
' ]\n',
' ]\n',
' }\n',
' }, \n',
' "Type": "AWS::S3::Bucket"\n',
' }, \n',
' "Dummy": {\n',
' "Type": "AWS::CloudFormation::WaitConditionHandle"\n',
' }\n',
' }\n',
'}\n'
]
self.assertEquals(normalized_template, normalize_json(template))
39 changes: 39 additions & 0 deletions tests/suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,45 @@ EOF
assert_has_line "${STACKER_NAMESPACE}-vpc: complete (stack destroyed)"
}

@test "stacker diff - raw template" {
needs_aws

config1() {
cat <<EOF
namespace: ${STACKER_NAMESPACE}
stacks:
- name: vpc
template_path: ../stacker/tests/fixtures/cfn_template.yaml
variables:
Param1: foobar
EOF
}

config2() {
cat <<EOF
namespace: ${STACKER_NAMESPACE}
stacks:
- name: vpc
template_path: ../stacker/tests/fixtures/cfn_template.yaml
variables:
Param1: newbar
EOF
}

teardown() {
stacker destroy --force <(config1)
}

# Create the new stacks.
stacker build <(config1)
assert "$status" -eq 0

stacker diff <(config2)
assert "$status" -eq 0
assert_has_line "\-Param1 = foobar"
assert_has_line "+Param1 = newbar"
}

@test "stacker build - no parallelism" {
needs_aws

Expand Down