1 | vows = require 'vows'
|
2 | assert = require 'assert'
|
3 | fs = require 'fs'
|
4 | path = require 'path'
|
5 | coffin = require '../lib/coffin'
|
6 | commandHelper = require '../lib/commandHelper'
|
7 |
|
8 | @assertListsEqual = (actualList, expectedList) ->
|
9 | assert.equal actualList.length, expectedList.length
|
10 | for key, val of expectedList
|
11 | for k, v of val.Properties
|
12 | assert.deepEqual actualList[key].Properties[k], v
|
13 | expected = {}
|
14 | expected[key] = val
|
15 | actual = {}
|
16 | actual[key] = actualList[key]
|
17 | assert.deepEqual actual, expected
|
18 | assert.deepEqual expectedList, actualList
|
19 |
|
20 | suite = vows.describe 'Template Test Suite'
|
21 | suite.addBatch
|
22 | 'when parsing WordPressTemplate.coffee':
|
23 | topic: ->
|
24 | raw = ''
|
25 | input = fs.createReadStream path.normalize '../examples/WordPress-1.0.0.template.original'
|
26 | input.on 'data', (d) =>
|
27 | raw += "#{d}"
|
28 | input.on 'end', =>
|
29 | generatedTemplate = require('../examples/WordPress-1.0.0.template.coffee')
|
30 | originalTemplate = JSON.parse(raw)
|
31 |
|
32 | @callback originalTemplate, generatedTemplate
|
33 | return
|
34 | 'Descriptions are the same': (originalTemplate, generatedTemplate) =>
|
35 | return if originalTemplate is null or generatedTemplate is null
|
36 | assert.equal generatedTemplate.Description, originalTemplate.Description
|
37 | 'Parameters are the same': (originalTemplate, generatedTemplate) =>
|
38 | return if originalTemplate is null or generatedTemplate is null
|
39 | @assertListsEqual generatedTemplate.Parameters, originalTemplate.Parameters
|
40 | 'Mappings are the same': (originalTemplate, generatedTemplate) =>
|
41 | return if originalTemplate is null or generatedTemplate is null
|
42 | @assertListsEqual generatedTemplate.Mappings, originalTemplate.Mappings
|
43 | 'Resources are the same': (originalTemplate, generatedTemplate) =>
|
44 | return if originalTemplate is null or generatedTemplate is null
|
45 | @assertListsEqual generatedTemplate.Resources, originalTemplate.Resources
|
46 | 'Outputs are the same': (originalTemplate, generatedTemplate) =>
|
47 | return if originalTemplate is null or generatedTemplate is null
|
48 | @assertListsEqual generatedTemplate.Outputs, originalTemplate.Outputs
|
49 | 'when using resource types':
|
50 | topic: ->
|
51 | coffin ->
|
52 | @AWS.AutoScaling.ScalingPolicy 'scalePolicy'
|
53 | @Param.String 'shortParam'
|
54 | 'it does not break': (topic) ->
|
55 | assert.ok topic?
|
56 | assert.ok topic.Parameters?
|
57 | assert.ok topic.Resources?
|
58 | assert.ok topic.Outputs?
|
59 | 'scaling policy is good': (topic) ->
|
60 | assert.ok topic.Resources.scalePolicy?
|
61 | 'short param is good': (topic) ->
|
62 | assert.ok topic.Parameters.shortParam?
|
63 | 'when using a blank template':
|
64 | topic: ->
|
65 | coffin ->
|
66 | 'it is not null': (topic) ->
|
67 | assert.ok topic?
|
68 | 'there is no description': (topic) ->
|
69 | assert.ok Object.keys(topic).indexOf('Description') is -1
|
70 | 'there is no mappings block': (topic) ->
|
71 | assert.ok Object.keys(topic).indexOf('Mappings') is -1
|
72 | 'there is a Parameters block': (topic) ->
|
73 | assert.ok topic.Parameters?
|
74 | 'there is a Resources block': (topic) ->
|
75 | assert.ok topic.Resources?
|
76 | 'there is a Outputs block': (topic) ->
|
77 | assert.ok topic.Outputs?
|
78 | 'when using mappings':
|
79 | topic: ->
|
80 | coffin ->
|
81 | @Mapping 'AWSRegionArch2AMI',
|
82 | 'us-east-1':
|
83 | 32: "ami-f417e49d"
|
84 | 64: "ami-f617e49f"
|
85 | 'mappings block exists': (topic) ->
|
86 | assert.ok topic.Mappings?
|
87 | 'values exist': (topic) ->
|
88 | assert.equal 'ami-f417e49d', topic.Mappings.AWSRegionArch2AMI['us-east-1']['32']
|
89 | assert.equal 'ami-f617e49f', topic.Mappings.AWSRegionArch2AMI['us-east-1']['64']
|
90 | 'when using tags':
|
91 | topic: ->
|
92 | coffin ->
|
93 | @AWS.EC2.Instance 'instance',
|
94 | Tags: [ @Tag('Name', 'someInstance'), @Tag('Environment', 'someEnvironment') ]
|
95 | 'tags are correct': (topic) ->
|
96 | assert.equal 'Name', topic.Resources.instance.Properties.Tags[0].Key
|
97 | assert.equal 'someInstance', topic.Resources.instance.Properties.Tags[0].Value
|
98 | assert.equal 'Environment', topic.Resources.instance.Properties.Tags[1].Key
|
99 | assert.equal 'someEnvironment', topic.Resources.instance.Properties.Tags[1].Value
|
100 | 'when using the join function':
|
101 | topic: ->
|
102 | myArray = ['x', 'y', 'z']
|
103 | coffin ->
|
104 | @AWS.EC2.Instance 'a',
|
105 | UserData: @Join '', 'x', 'y', 'z'
|
106 | @AWS.EC2.Instance 'b',
|
107 | UserData: @Join '', myArray
|
108 | 'it works with varargs': (topic) ->
|
109 | assert.equal topic.Resources.a.Properties.UserData['Fn::Join'][0], ''
|
110 | assert.deepEqual topic.Resources.a.Properties.UserData['Fn::Join'][1], ['x', 'y', 'z']
|
111 | 'it works with an array arg': (topic) ->
|
112 | assert.equal topic.Resources.b.Properties.UserData['Fn::Join'][0], ''
|
113 | assert.deepEqual topic.Resources.b.Properties.UserData['Fn::Join'][1], ['x', 'y', 'z']
|
114 | 'when using metadata and top level properties':
|
115 | topic: ->
|
116 | coffin ->
|
117 | @AWS.EC2.Instance 'a',
|
118 | Metadata:
|
119 | meta: 'data'
|
120 | Properties:
|
121 | a: 'b'
|
122 | 'metadata block is correct': (topic) ->
|
123 | assert.ok topic.Resources.a.Metadata?
|
124 | assert.equal topic.Resources.a.Metadata.meta, 'data'
|
125 | 'properties block is correct': (topic) ->
|
126 | assert.ok topic.Resources.a.Properties?
|
127 | assert.equal topic.Resources.a.Properties.a, 'b'
|
128 | 'type is in the right place': (topic) ->
|
129 | assert.ok topic.Resources.a.Type is 'AWS::EC2::Instance'
|
130 | 'when using @InitScript':
|
131 | topic: ->
|
132 | coffin ->
|
133 | environment = 'test'
|
134 | @AWS.EC2.Instance 'a',
|
135 | UserData: @InitScript """
|
136 | #!/bin/bash
|
137 | echo "hey %{@Region}"
|
138 | """
|
139 | @AWS.EC2.Instance 'b',
|
140 | UserData: @InitScript 'init_script.sh'
|
141 | 'we are able to embed a nice shell script inline': (topic) ->
|
142 | assert.ok topic.Resources.a.Properties.UserData?["Fn::Base64"]?["Fn::Join"]?
|
143 | assert.equal topic.Resources.a.Properties.UserData["Fn::Base64"]["Fn::Join"][1].length, 3
|
144 | assert.equal topic.Resources.a.Properties.UserData["Fn::Base64"]["Fn::Join"][1][0], "#!/bin/bash\necho \"hey "
|
145 | assert.equal topic.Resources.a.Properties.UserData["Fn::Base64"]["Fn::Join"][1][1].Ref, 'AWS::Region'
|
146 | assert.equal topic.Resources.a.Properties.UserData["Fn::Base64"]["Fn::Join"][1][2], "\""
|
147 |
|
148 | 'we are able to include a nice shell script file': (topic) ->
|
149 | assert.ok topic.Resources.b.Properties.UserData?["Fn::Base64"]?["Fn::Join"]?
|
150 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1].length, 5
|
151 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1][0].indexOf("#!/bin/bash\necho \"hey\"\necho "), 0
|
152 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1][1].Ref, 'param1'
|
153 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1][2], "\necho "
|
154 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1][3].Ref, 'AWS::Region'
|
155 | assert.equal topic.Resources.b.Properties.UserData["Fn::Base64"]["Fn::Join"][1][4], "\necho \"ho\"\n"
|
156 |
|
157 | 'when creating a stack':
|
158 | topic: ->
|
159 | coffin ->
|
160 | describeAnyResourceStatement =
|
161 | Effect:"Allow"
|
162 | Action:"cloudformation:DescribeStackResource"
|
163 | Resource:"*"
|
164 | rootPolicy =
|
165 | PolicyName: "root"
|
166 | PolicyDocument:
|
167 | Statement:[ describeAnyResourceStatement ]
|
168 | @AWS.IAM.User 'CfnUser',
|
169 | Path: "/"
|
170 | Policies: [ rootPolicy ]
|
171 |
|
172 | 'we recognize that an IAM resource was used (so we can add the proper capabilities flag to the stack commands': (topic) ->
|
173 | assert.ok commandHelper.doesTemplateReferenceIAM JSON.stringify topic
|
174 |
|
175 | suite.run()
|