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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'
# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/parser_rspec_helper')
describe "egrammar parsing resource declarations" do
include ParserRspecHelper
context "When parsing regular resource" do
["File", "file"].each do |word|
it "#{word} { 'title': }" do
dump(parse("#{word} { 'title': }")).should == [
"(resource file",
" ('title'))"
].join("\n")
end
it "#{word} { 'title': path => '/somewhere', mode => '0777'}" do
dump(parse("#{word} { 'title': path => '/somewhere', mode => '0777'}")).should == [
"(resource file",
" ('title'",
" (path => '/somewhere')",
" (mode => '0777')))"
].join("\n")
end
it "#{word} { 'title': path => '/somewhere', }" do
dump(parse("#{word} { 'title': path => '/somewhere', }")).should == [
"(resource file",
" ('title'",
" (path => '/somewhere')))"
].join("\n")
end
it "#{word} { 'title': , }" do
dump(parse("#{word} { 'title': , }")).should == [
"(resource file",
" ('title'))"
].join("\n")
end
it "#{word} { 'title': ; }" do
dump(parse("#{word} { 'title': ; }")).should == [
"(resource file",
" ('title'))"
].join("\n")
end
it "#{word} { 'title': ; 'other_title': }" do
dump(parse("#{word} { 'title': ; 'other_title': }")).should == [
"(resource file",
" ('title')",
" ('other_title'))"
].join("\n")
end
# PUP-2898, trailing ';'
it "#{word} { 'title': ; 'other_title': ; }" do
dump(parse("#{word} { 'title': ; 'other_title': ; }")).should == [
"(resource file",
" ('title')",
" ('other_title'))"
].join("\n")
end
it "#{word} { 'title1': path => 'x'; 'title2': path => 'y'}" do
dump(parse("#{word} { 'title1': path => 'x'; 'title2': path => 'y'}")).should == [
"(resource file",
" ('title1'",
" (path => 'x'))",
" ('title2'",
" (path => 'y')))",
].join("\n")
end
it "#{word} { title: * => {mode => '0777'} }" do
dump(parse("#{word} { title: * => {mode => '0777'}}")).should == [
"(resource file",
" (title",
" (* => ({} (mode '0777')))))"
].join("\n")
end
end
end
context "When parsing (type based) resource defaults" do
it "File { }" do
dump(parse("File { }")).should == "(resource-defaults file)"
end
it "File { mode => '0777' }" do
dump(parse("File { mode => '0777'}")).should == [
"(resource-defaults file",
" (mode => '0777'))"
].join("\n")
end
it "File { * => {mode => '0777'} } (even if validated to be illegal)" do
dump(parse("File { * => {mode => '0777'}}")).should == [
"(resource-defaults file",
" (* => ({} (mode '0777'))))"
].join("\n")
end
end
context "When parsing resource override" do
it "File['x'] { }" do
dump(parse("File['x'] { }")).should == "(override (slice file 'x'))"
end
it "File['x'] { x => 1 }" do
dump(parse("File['x'] { x => 1}")).should == [
"(override (slice file 'x')",
" (x => 1))"
].join("\n")
end
it "File['x', 'y'] { x => 1 }" do
dump(parse("File['x', 'y'] { x => 1}")).should == [
"(override (slice file ('x' 'y'))",
" (x => 1))"
].join("\n")
end
it "File['x'] { x => 1, y => 2 }" do
dump(parse("File['x'] { x => 1, y=> 2}")).should == [
"(override (slice file 'x')",
" (x => 1)",
" (y => 2))"
].join("\n")
end
it "File['x'] { x +> 1 }" do
dump(parse("File['x'] { x +> 1}")).should == [
"(override (slice file 'x')",
" (x +> 1))"
].join("\n")
end
it "File['x'] { * => {mode => '0777'} } (even if validated to be illegal)" do
dump(parse("File['x'] { * => {mode => '0777'}}")).should == [
"(override (slice file 'x')",
" (* => ({} (mode '0777'))))"
].join("\n")
end
end
context "When parsing virtual and exported resources" do
it "parses exported @@file { 'title': }" do
dump(parse("@@file { 'title': }")).should == "(exported-resource file\n ('title'))"
end
it "parses virtual @file { 'title': }" do
dump(parse("@file { 'title': }")).should == "(virtual-resource file\n ('title'))"
end
it "nothing before the title colon is a syntax error" do
expect do
parse("@file {: mode => '0777' }")
end.to raise_error(/Syntax error/)
end
it "raises error for user error; not a resource" do
# The expression results in VIRTUAL, CALL FUNCTION('file', HASH) since the resource body has
# no title.
expect do
parse("@file { mode => '0777' }")
end.to raise_error(/Virtual \(@\) can only be applied to a Resource Expression/)
end
it "parses global defaults with @ (even if validated to be illegal)" do
dump(parse("@File { mode => '0777' }")).should == [
"(virtual-resource-defaults file",
" (mode => '0777'))"
].join("\n")
end
it "parses global defaults with @@ (even if validated to be illegal)" do
dump(parse("@@File { mode => '0777' }")).should == [
"(exported-resource-defaults file",
" (mode => '0777'))"
].join("\n")
end
it "parses override with @ (even if validated to be illegal)" do
dump(parse("@File[foo] { mode => '0777' }")).should == [
"(virtual-override (slice file foo)",
" (mode => '0777'))"
].join("\n")
end
it "parses override combined with @@ (even if validated to be illegal)" do
dump(parse("@@File[foo] { mode => '0777' }")).should == [
"(exported-override (slice file foo)",
" (mode => '0777'))"
].join("\n")
end
end
context "When parsing class resource" do
it "class { 'cname': }" do
dump(parse("class { 'cname': }")).should == [
"(resource class",
" ('cname'))"
].join("\n")
end
it "@class { 'cname': }" do
dump(parse("@class { 'cname': }")).should == [
"(virtual-resource class",
" ('cname'))"
].join("\n")
end
it "@@class { 'cname': }" do
dump(parse("@@class { 'cname': }")).should == [
"(exported-resource class",
" ('cname'))"
].join("\n")
end
it "class { 'cname': x => 1, y => 2}" do
dump(parse("class { 'cname': x => 1, y => 2}")).should == [
"(resource class",
" ('cname'",
" (x => 1)",
" (y => 2)))"
].join("\n")
end
it "class { 'cname1': x => 1; 'cname2': y => 2}" do
dump(parse("class { 'cname1': x => 1; 'cname2': y => 2}")).should == [
"(resource class",
" ('cname1'",
" (x => 1))",
" ('cname2'",
" (y => 2)))",
].join("\n")
end
end
context "reported issues in 3.x" do
it "should not screw up on brackets in title of resource #19632" do
dump(parse('notify { "thisisa[bug]": }')).should == [
"(resource notify",
" ('thisisa[bug]'))",
].join("\n")
end
end
context "When parsing Relationships" do
it "File[a] -> File[b]" do
dump(parse("File[a] -> File[b]")).should == "(-> (slice file a) (slice file b))"
end
it "File[a] <- File[b]" do
dump(parse("File[a] <- File[b]")).should == "(<- (slice file a) (slice file b))"
end
it "File[a] ~> File[b]" do
dump(parse("File[a] ~> File[b]")).should == "(~> (slice file a) (slice file b))"
end
it "File[a] <~ File[b]" do
dump(parse("File[a] <~ File[b]")).should == "(<~ (slice file a) (slice file b))"
end
it "Should chain relationships" do
dump(parse("a -> b -> c")).should ==
"(-> (-> a b) c)"
end
it "Should chain relationships" do
dump(parse("File[a] -> File[b] ~> File[c] <- File[d] <~ File[e]")).should ==
"(<~ (<- (~> (-> (slice file a) (slice file b)) (slice file c)) (slice file d)) (slice file e))"
end
it "should create relationships between collects" do
dump(parse("File <| mode == 0644 |> -> File <| mode == 0755 |>")).should ==
"(-> (collect file\n (<| |> (== mode 0644))) (collect file\n (<| |> (== mode 0755))))"
end
end
context "When parsing collection" do
context "of virtual resources" do
it "File <| |>" do
dump(parse("File <| |>")).should == "(collect file\n (<| |>))"
end
end
context "of exported resources" do
it "File <<| |>>" do
dump(parse("File <<| |>>")).should == "(collect file\n (<<| |>>))"
end
end
context "queries are parsed with correct precedence" do
it "File <| tag == 'foo' |>" do
dump(parse("File <| tag == 'foo' |>")).should == "(collect file\n (<| |> (== tag 'foo')))"
end
it "File <| tag == 'foo' and mode != '0777' |>" do
dump(parse("File <| tag == 'foo' and mode != '0777' |>")).should == "(collect file\n (<| |> (&& (== tag 'foo') (!= mode '0777'))))"
end
it "File <| tag == 'foo' or mode != '0777' |>" do
dump(parse("File <| tag == 'foo' or mode != '0777' |>")).should == "(collect file\n (<| |> (|| (== tag 'foo') (!= mode '0777'))))"
end
it "File <| tag == 'foo' or tag == 'bar' and mode != '0777' |>" do
dump(parse("File <| tag == 'foo' or tag == 'bar' and mode != '0777' |>")).should ==
"(collect file\n (<| |> (|| (== tag 'foo') (&& (== tag 'bar') (!= mode '0777')))))"
end
it "File <| (tag == 'foo' or tag == 'bar') and mode != '0777' |>" do
dump(parse("File <| (tag == 'foo' or tag == 'bar') and mode != '0777' |>")).should ==
"(collect file\n (<| |> (&& (|| (== tag 'foo') (== tag 'bar')) (!= mode '0777'))))"
end
end
end
end
|