Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
added extra test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ojkelly committed Jul 20, 2018
1 parent bad281c commit 0b477db
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/cloudformation/tasks/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GenerateTemplate(params cloudformation.GenerateParams) {

// GenerateYamlTemplate and return both the raw data as []byte, but also the cloudformation yaml object
func GenerateYamlTemplate(params cloudformation.GenerateParams) ([]byte, cloudformation.YamlCloudformation) {
cf, err := cloudformation.GenerateYamlStack(params)
cf, err := cloudformation.GenerateYamlTemplate(params)
checkError(err)
output, err := yaml.Marshal(cf)
checkError(err)
Expand Down
21 changes: 19 additions & 2 deletions internal/cloudformation/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func init() {
registerYamlTagUnmarshalers()
}

// GenerateYamlStack - generate a cloudformation template
func GenerateYamlStack(params GenerateParams) (compiledTemplate YamlCloudformation, err error) {
// GenerateYamlTemplate - generate a cloudformation template
func GenerateYamlTemplate(params GenerateParams) (compiledTemplate YamlCloudformation, err error) {
// load the config file
var configData []byte

Expand Down Expand Up @@ -74,6 +74,15 @@ func GenerateYamlStack(params GenerateParams) (compiledTemplate YamlCloudformati
// parse the config yaml
data := buf.Bytes()
var config YamlConfig

config.Conditions = make(types.TemplateObject)
config.Metadata = make(types.TemplateObject)
config.Mappings = make(types.TemplateObject)
config.Outputs = make(types.TemplateObject)
config.Parameters = make(types.TemplateObject)
config.Resources = make(map[string]types.CfResource)
config.Transform = make(types.TemplateObject)

if err = yaml.Unmarshal(data, &config); err != nil {
printer.Error(
fmt.Errorf("Failed to unmarshall the template"),
Expand All @@ -95,6 +104,14 @@ func GenerateYamlStack(params GenerateParams) (compiledTemplate YamlCloudformati
resources,
transform map[string]interface{}

conditions = make(map[string]interface{})
metadata = make(map[string]interface{})
mappings = make(map[string]interface{})
outputs = make(map[string]interface{})
parameters = make(map[string]interface{})
resources = make(map[string]interface{})
transform = make(map[string]interface{})

// Process the core and plugin parsers
conditions,
metadata,
Expand Down
162 changes: 162 additions & 0 deletions internal/cloudformation/template_parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,168 @@ import (
"testing"
)

func TestGenerateYamlTemplate(t *testing.T) {
// Prevent the printer from exiting
printer.Test()

tests := []struct {
input GenerateParams
output YamlCloudformation
err error
}{
{
input: GenerateParams{
Filename: "testdata/test.yaml",
},
output: YamlCloudformation{
AWSTemplateFormatVersion: "2010-09-09",
Description: "A Demo Template for testing Kombustion",
Metadata: types.TemplateObject{},
Parameters: types.TemplateObject{
"Environment": map[interface{}]interface{}{
"Type": "String", "Default": "UnknownEnvironment",
},
},
Mappings: types.TemplateObject{},
Conditions: types.TemplateObject{},
Transform: types.TemplateObject{},
Resources: types.TemplateObject{},
Outputs: types.TemplateObject{},
},
err: nil,
},
{
input: GenerateParams{
Filename: "testdata/test.yaml",
GenerateDefaultOutputs: true,
},
output: YamlCloudformation{
AWSTemplateFormatVersion: "2010-09-09",
Description: "A Demo Template for testing Kombustion",
Metadata: types.TemplateObject{},
Parameters: types.TemplateObject{
"Environment": map[interface{}]interface{}{
"Type": "String", "Default": "UnknownEnvironment",
},
},
Mappings: types.TemplateObject{},
Conditions: types.TemplateObject{},
Transform: types.TemplateObject{},
Resources: types.TemplateObject{},
Outputs: types.TemplateObject{
"MyDemoLogGroup3": types.TemplateObject{
"Description": "MyDemoLogGroup3 Object",
"Value": map[string]interface{}{
"Ref": "MyDemoLogGroup3",
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup3",
},
},
},
"MyDemoLogGroup4": types.TemplateObject{
"Description": "MyDemoLogGroup4 Object",
"Value": map[string]interface{}{
"Ref": "MyDemoLogGroup4",
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup4",
},
},
},
"MyDemoLogGroup4Arn": types.TemplateObject{
"Value": map[string]interface{}{"Fn::GetAtt": []string{"MyDemoLogGroup4", "Arn"},
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup4-Arn",
},
},
"Description": "MyDemoLogGroup4 Object",
},
"MyDemoLogGroup": types.TemplateObject{
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup",
},
},
"Description": "MyDemoLogGroup Object",
"Value": map[string]interface{}{
"Ref": "MyDemoLogGroup",
},
},
"MyDemoLogGroupArn": types.TemplateObject{
"Description": "MyDemoLogGroup Object",
"Value": map[string]interface{}{
"Fn::GetAtt": []string{"MyDemoLogGroup", "Arn"},
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup-Arn",
},
},
},
"MyDemoLogGroup2": types.TemplateObject{
"Description": "MyDemoLogGroup2 Object",
"Value": map[string]interface{}{
"Ref": "MyDemoLogGroup2",
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup2",
},
},
},
"MyDemoLogGroup2Arn": types.TemplateObject{
"Description": "MyDemoLogGroup2 Object",
"Value": map[string]interface{}{
"Fn::GetAtt": []string{"MyDemoLogGroup2", "Arn"},
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup2-Arn",
},
},
},
"MyDemoLogGroup3Arn": types.TemplateObject{"Description": "MyDemoLogGroup3 Object",
"Value": map[string]interface{}{
"Fn::GetAtt": []string{"MyDemoLogGroup3", "Arn"},
},
"Export": map[string]interface{}{
"Name": map[string]interface{}{
"Fn::Sub": "${AWS::StackName}-LogsLogGroup-MyDemoLogGroup3-Arn",
},
},
},
},
},
err: nil,
},
}

for i, test := range tests {
assert := assert.New(t)

output, err := GenerateYamlTemplate(
test.input,
)

assert.Equal(
test.err,
err,
fmt.Sprintf("Test: %d", i),
)

assert.Equal(
test.output,
output,
fmt.Sprintf("Test: %d", i),
)
}
}

func TestProcessParsers(t *testing.T) {
// Prevent the printer from exiting
printer.Test()
Expand Down
29 changes: 29 additions & 0 deletions internal/cloudformation/testdata/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: A Demo Template for testing Kombustion

Parameters:
Environment:
Type: String
Default: UnknownEnvironment

Resources:
MyDemoLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Join [ '-', [ "MyDemoLogGroup1",!Ref Environment ] ]
RetentionInDays: 1
MyDemoLogGroup2:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Join [ '-', [ "MyDemoLogGroup2",!Ref Environment ] ]
RetentionInDays: 1
MyDemoLogGroup3:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Join [ '-', [ "MyDemoLogGroup3",!Ref Environment ] ]
RetentionInDays: 1
MyDemoLogGroup4:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Join [ '-', [ "MyDemoLogGroup4",!Ref Environment ] ]
RetentionInDays: 1

0 comments on commit 0b477db

Please sign in to comment.