/
Template_UT.scala
191 lines (160 loc) · 6.04 KB
/
Template_UT.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.monsanto.arch.cloudformation.model
import com.monsanto.arch.cloudformation.model.resource._
import com.monsanto.arch.cloudformation.model.simple._
import org.scalatest.{Matchers, FlatSpec}
import spray.json.DefaultJsonProtocol._
class Template_UT extends FlatSpec with Matchers with VPC with Subnet with AvailabilityZone with SecurityGroup with EC2 {
val parameter1 = StringParameter(name = "param1")
val parameter2 = StringParameter(name = "param2")
val condition1 = Condition(name = "cond1", function = "fn1")
val condition2 = Condition(name = "cond2", function = "fn2")
val mapping1 = Mapping[String](name = "map1", Map())
val mapping2 = Mapping[String](name = "map2", Map())
val resource1 = `AWS::S3::Bucket`(name = "bucket1", None)
val resource2 = `AWS::S3::Bucket`(name = "bucket2", None)
val output1 = Output("output1", "", `Fn::Join`("", Seq()))
val output2 = Output("output2", "", `Fn::Join`("", Seq()))
it should "throw an exception if some resources have the same name" in {
val thrown = the[IllegalArgumentException] thrownBy Template.collapse(Seq(
`AWS::EC2::VPC`("repeat", CidrBlock(10, 10, 10, 10, 16), Seq(AmazonTag("a", "B"))),
`AWS::EC2::VPC`("repeat", CidrBlock(10, 10, 10, 12, 16), Seq(AmazonTag("c", "C")))
))
thrown.getMessage should include("repeat")
}
it should "be able to add a parameter" in {
val template = Template.EMPTY ++ parameter1
template should equal(Template.EMPTY.copy(Parameters = Some(Seq(parameter1))))
}
it should "be able to add parameters" in {
val params = Seq(parameter1, parameter2)
val template = Template.EMPTY ++ params
template should equal(Template.EMPTY.copy(Parameters = Some(params)))
}
it should "be able to add a condition" in {
val template = Template.EMPTY ++ condition1
template should equal(Template.EMPTY.copy(Conditions = Some(Seq(condition1))))
}
it should "be able to add conditions" in {
val conditions = Seq(condition1, condition2)
val template = Template.EMPTY ++ conditions
template should equal(Template.EMPTY.copy(Conditions = Some(conditions)))
}
it should "be able to add a mapping" in {
val template = Template.EMPTY ++ mapping1
template should equal(Template.EMPTY.copy(Mappings = Some(Seq(mapping1))))
}
it should "be able to add mappings" in {
val mappings = Seq(mapping1, mapping2)
val template = Template.EMPTY ++ mappings
template should equal(Template.EMPTY.copy(Mappings = Some(mappings)))
}
it should "be able to add a resource" in {
val template = Template.EMPTY ++ resource1
template should equal(Template.EMPTY.copy(Resources = Some(Seq(resource1))))
}
it should "be able to add resources" in {
val resources = Seq(resource1, resource2)
val template = Template.EMPTY ++ resources
template should equal(Template.EMPTY.copy(Resources = Some(resources)))
}
val cidr = CidrBlock(10,1,1,1,32)
it should "be able to add a security group routable" in {
withVpc(cidr) { implicit vpc =>
withAZ("") { implicit az =>
withSubnet("subnet", cidr) { implicit subnet =>
val sg = securityGroup("sg", "")
val sgr = ec2("instance", "", "", AMIId(""), Seq(sg), Seq())
val template = Template.EMPTY ++ sgr
template should equal(Template.EMPTY.copy(Routables = Seq(sgr), Resources = sgr.resources))
template
}
}
}
}
it should "be able to add security group routables" in {
withVpc(cidr) { implicit vpc =>
withAZ("") { implicit az =>
withSubnet("subnet", cidr) { implicit subnet =>
val sg = securityGroup("sg", "")
val sgr1 = ec2("instance1", "", "", AMIId(""), Seq(sg), Seq())
val sgr2 = ec2("instance2", "", "", AMIId(""), Seq(sg), Seq())
val sgrs = Seq(sgr1, sgr2)
val template = Template.EMPTY ++ sgrs
template should equal(Template.EMPTY.copy(Routables = sgrs, Resources = sgrs.flatMap(_.resources)))
template
}
}
}
}
it should "be able to add a output" in {
val template = Template.EMPTY ++ output1
template should equal(Template.EMPTY.copy(Outputs = Some(Seq(output1))))
}
it should "be able to add an output of type Token[String]" in new JsonWritingMatcher {
import spray.json._
val token : Token[String] = ResourceRef(resource1)
val output = Output(
name = "out1",
Description = "test",
Value = token
)
output.toJson shouldMatch
s"""
|{
| "Description": "test",
| "Value": {
| "Ref": "${resource1.name}"
| }
|}
""".stripMargin
}
it should "be able to add an output of type Token[String] with a static export" in new JsonWritingMatcher {
import spray.json._
val token : Token[String] = ResourceRef(resource1)
val output = Output(
name = "out1",
Description = "test",
Value = token,
Export = Some("export-test")
)
output.toJson shouldMatch
s"""
|{
| "Description": "test",
| "Value": {
| "Ref": "${resource1.name}"
| },
| "Export": {
| "Name": "export-test"
| }
|}
""".stripMargin
}
it should "be able to add an output of type Token[String] with a function export" in new JsonWritingMatcher {
import spray.json._
val token : Token[String] = ResourceRef(resource1)
val output = Output(
name = "out1",
Description = "test",
Value = token,
Export = Some(`Fn::Sub`(s"$${AWS::StackName}-test-export"))
)
output.toJson shouldMatch
s"""
|{
| "Description": "test",
| "Value": {
| "Ref": "${resource1.name}"
| },
| "Export": {
| "Name": {"Fn::Sub": "$${AWS::StackName}-test-export"}
| }
|}
""".stripMargin
}
it should "be able to add outputs" in {
val outputs = Seq(output1, output2)
val template = Template.EMPTY ++ outputs
template should equal(Template.EMPTY.copy(Outputs = Some(outputs)))
}
}