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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet_spec/compiler'
require 'puppet/parser/collector'
describe Puppet::Parser::Collector do
include PuppetSpec::Compiler
def expect_the_message_to_be(expected_messages, code, node = Puppet::Node.new('the node'))
catalog = compile_to_catalog(code, node)
messages = catalog.resources.find_all { |resource| resource.type == 'Notify' }.
collect { |notify| notify[:message] }
messages.should include(*expected_messages)
end
shared_examples_for "virtual resource collection" do
it "matches everything when no query given" do
expect_the_message_to_be(["the other message", "the message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
@notify { "other": message => "the other message" }
Notify <| |>
MANIFEST
end
it "matches regular resources " do
expect_the_message_to_be(["changed", "changed"], <<-MANIFEST)
notify { "testing": message => "the message" }
notify { "other": message => "the other message" }
Notify <| |> { message => "changed" }
MANIFEST
end
it "matches on tags" do
expect_the_message_to_be(["wanted"], <<-MANIFEST)
@notify { "testing": tag => ["one"], message => "wanted" }
@notify { "other": tag => ["two"], message => "unwanted" }
Notify <| tag == one |>
MANIFEST
end
it "matches on title" do
expect_the_message_to_be(["the message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
Notify <| title == "testing" |>
MANIFEST
end
it "matches on other parameters" do
expect_the_message_to_be(["the message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
@notify { "other testing": message => "the wrong message" }
Notify <| message == "the message" |>
MANIFEST
end
it "matches against elements of an array valued parameter" do
expect_the_message_to_be([["the", "message"]], <<-MANIFEST)
@notify { "testing": message => ["the", "message"] }
@notify { "other testing": message => ["not", "here"] }
Notify <| message == "message" |>
MANIFEST
end
it "allows criteria to be combined with 'and'" do
expect_the_message_to_be(["the message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
@notify { "other": message => "the message" }
Notify <| title == "testing" and message == "the message" |>
MANIFEST
end
it "allows criteria to be combined with 'or'" do
expect_the_message_to_be(["the message", "other message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
@notify { "other": message => "other message" }
@notify { "yet another": message => "different message" }
Notify <| title == "testing" or message == "other message" |>
MANIFEST
end
it "allows criteria to be combined with 'or'" do
expect_the_message_to_be(["the message", "other message"], <<-MANIFEST)
@notify { "testing": message => "the message" }
@notify { "other": message => "other message" }
@notify { "yet another": message => "different message" }
Notify <| title == "testing" or message == "other message" |>
MANIFEST
end
it "allows criteria to be grouped with parens" do
expect_the_message_to_be(["the message", "different message"], <<-MANIFEST)
@notify { "testing": message => "different message", withpath => true }
@notify { "other": message => "the message" }
@notify { "yet another": message => "the message", withpath => true }
Notify <| (title == "testing" or message == "the message") and withpath == true |>
MANIFEST
end
it "does not do anything if nothing matches" do
expect_the_message_to_be([], <<-MANIFEST)
@notify { "testing": message => "different message" }
Notify <| title == "does not exist" |>
MANIFEST
end
it "excludes items with inequalities" do
expect_the_message_to_be(["good message"], <<-MANIFEST)
@notify { "testing": message => "good message" }
@notify { "the wrong one": message => "bad message" }
Notify <| title != "the wrong one" |>
MANIFEST
end
it "does not exclude resources with unequal arrays" do
expect_the_message_to_be(["message", ["not this message", "or this one"]], <<-MANIFEST)
@notify { "testing": message => "message" }
@notify { "the wrong one": message => ["not this message", "or this one"] }
Notify <| message != "not this message" |>
MANIFEST
end
it "does not exclude tags with inequalities" do
expect_the_message_to_be(["wanted message", "the way it works"], <<-MANIFEST)
@notify { "testing": tag => ["wanted"], message => "wanted message" }
@notify { "other": tag => ["why"], message => "the way it works" }
Notify <| tag != "why" |>
MANIFEST
end
it "does not collect classes" do
node = Puppet::Node.new('the node')
expect do
catalog = compile_to_catalog(<<-MANIFEST, node)
class theclass {
@notify { "testing": message => "good message" }
}
Class <| |>
MANIFEST
end.to raise_error(/Classes cannot be collected/)
end
context "overrides" do
it "modifies an existing array" do
expect_the_message_to_be([["original message", "extra message"]], <<-MANIFEST)
@notify { "testing": message => ["original message"] }
Notify <| |> {
message +> "extra message"
}
MANIFEST
end
it "converts a scalar to an array" do
expect_the_message_to_be([["original message", "extra message"]], <<-MANIFEST)
@notify { "testing": message => "original message" }
Notify <| |> {
message +> "extra message"
}
MANIFEST
end
it "collects with override when inside a class (#10963)" do
expect_the_message_to_be(["overridden message"], <<-MANIFEST)
@notify { "testing": message => "original message" }
include collector_test
class collector_test {
Notify <| |> {
message => "overridden message"
}
}
MANIFEST
end
it "collects with override when inside a define (#10963)" do
expect_the_message_to_be(["overridden message"], <<-MANIFEST)
@notify { "testing": message => "original message" }
collector_test { testing: }
define collector_test() {
Notify <| |> {
message => "overridden message"
}
}
MANIFEST
end
# Catches regression in implemented behavior, this is not to be taken as this is the wanted behavior
# but it has been this way for a long time.
it "collects and overrides user defined resources immediately (before queue is evaluated)" do
expect_the_message_to_be(["overridden"], <<-MANIFEST)
define foo($message) {
notify { "testing": message => $message }
}
foo { test: message => 'given' }
Foo <| |> { message => 'overridden' }
MANIFEST
end
# Catches regression in implemented behavior, this is not to be taken as this is the wanted behavior
# but it has been this way for a long time.
it "collects and overrides user defined resources immediately (virtual resources not queued)" do
expect_the_message_to_be(["overridden"], <<-MANIFEST)
define foo($message) {
@notify { "testing": message => $message }
}
foo { test: message => 'given' }
Notify <| |> # must be collected or the assertion does not find it
Foo <| |> { message => 'overridden' }
MANIFEST
end
# Catches regression in implemented behavior, this is not to be taken as this is the wanted behavior
# but it has been this way for a long time.
# Note difference from none +> case where the override takes effect
it "collects and overrides user defined resources with +>" do
expect_the_message_to_be([["given", "overridden"]], <<-MANIFEST)
define foo($message) {
notify { "$name": message => $message }
}
foo { test: message => ['given'] }
Notify <| |> { message +> ['overridden'] }
MANIFEST
end
it "collects and overrides virtual resources multiple times using multiple collects" do
expect_the_message_to_be(["overridden2"], <<-MANIFEST)
@notify { "testing": message => "original" }
Notify <| |> { message => 'overridden1' }
Notify <| |> { message => 'overridden2' }
MANIFEST
end
it "collects and overrides non virtual resources multiple times using multiple collects" do
expect_the_message_to_be(["overridden2"], <<-MANIFEST)
notify { "testing": message => "original" }
Notify <| |> { message => 'overridden1' }
Notify <| |> { message => 'overridden2' }
MANIFEST
end
end
end
describe "in the current parser" do
before :each do
Puppet[:parser] = 'current'
end
it_behaves_like "virtual resource collection"
end
describe "in the future parser" do
before :each do
Puppet[:parser] = 'future'
end
it_behaves_like "virtual resource collection"
end
end
|