~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/provider/augeas/augeas.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
provider_class = Puppet::Type.type(:augeas).provider(:augeas)
6
6
 
7
7
describe provider_class do
 
8
 
8
9
    describe "command parsing" do
9
 
        it "should break apart a single line into three tokens" do
10
 
            provider = provider_class.new()
11
 
            tokens = provider.parse_commands("set /Jar/Jar Binks")
 
10
        before do
 
11
            @resource = stub("resource")
 
12
            @provider = provider_class.new(@resource)
 
13
        end
 
14
 
 
15
        it "should break apart a single line into three tokens and clean up the context" do
 
16
            @resource.stubs(:[]).returns("/context")
 
17
            tokens = @provider.parse_commands("set Jar/Jar Binks")
12
18
            tokens.size.should == 1
13
19
            tokens[0].size.should == 3
14
20
            tokens[0][0].should == "set"
15
 
            tokens[0][1].should == "/Jar/Jar"
 
21
            tokens[0][1].should == "/context/Jar/Jar"
16
22
            tokens[0][2].should == "Binks"
17
23
        end
18
24
 
19
25
        it "should break apart a multiple line into six tokens" do
20
 
            provider = provider_class.new()
21
 
            tokens = provider.parse_commands("set /Jar/Jar Binks\nrm anakin skywalker")
22
 
            tokens.size.should == 2
23
 
            tokens[0].size.should == 3
24
 
            tokens[1].size.should == 3
25
 
            tokens[0][0].should == "set"
26
 
            tokens[0][1].should == "/Jar/Jar"
27
 
            tokens[0][2].should == "Binks"
28
 
            tokens[1][0].should == "rm"
29
 
            tokens[1][1].should == "anakin"
30
 
            tokens[1][2].should == "skywalker"
 
26
            @resource.stubs(:[]).returns("")
 
27
            tokens = @provider.parse_commands("set /Jar/Jar Binks\nrm anakin")
 
28
            tokens.size.should == 2
 
29
            tokens[0].size.should == 3
 
30
            tokens[1].size.should == 2
 
31
            tokens[0][0].should == "set"
 
32
            tokens[0][1].should == "/Jar/Jar"
 
33
            tokens[0][2].should == "Binks"
 
34
            tokens[1][0].should == "rm"
 
35
            tokens[1][1].should == "anakin"
 
36
        end
 
37
 
 
38
        it "should strip whitespace and ignore blank lines" do
 
39
            @resource.stubs(:[]).returns("")
 
40
            tokens = @provider.parse_commands("  set /Jar/Jar Binks \t\n  \n\n  rm anakin ")
 
41
            tokens.size.should == 2
 
42
            tokens[0].size.should == 3
 
43
            tokens[1].size.should == 2
 
44
            tokens[0][0].should == "set"
 
45
            tokens[0][1].should == "/Jar/Jar"
 
46
            tokens[0][2].should == "Binks"
 
47
            tokens[1][0].should == "rm"
 
48
            tokens[1][1].should == "anakin"
31
49
        end
32
50
 
33
51
        it "should handle arrays" do
34
 
            provider = provider_class.new()
35
 
            commands = ["set /Jar/Jar Binks", "rm anakin skywalker"]
36
 
            tokens = provider.parse_commands(commands)
 
52
            @resource.stubs(:[]).returns("/foo/")
 
53
            commands = ["set /Jar/Jar Binks", "rm anakin"]
 
54
            tokens = @provider.parse_commands(commands)
37
55
            tokens.size.should == 2
38
56
            tokens[0].size.should == 3
39
 
            tokens[1].size.should == 3
 
57
            tokens[1].size.should == 2
40
58
            tokens[0][0].should == "set"
41
59
            tokens[0][1].should == "/Jar/Jar"
42
60
            tokens[0][2].should == "Binks"
43
61
            tokens[1][0].should == "rm"
44
 
            tokens[1][1].should == "anakin"
45
 
            tokens[1][2].should == "skywalker"
46
 
        end
47
 
 
48
 
        it "should concat the last values" do
49
 
            provider = provider_class.new()
50
 
            tokens = provider.parse_commands("set /Jar/Jar Binks is my copilot")
51
 
            tokens.size.should == 1
52
 
            tokens[0].size.should == 3
53
 
            tokens[0][0].should == "set"
54
 
            tokens[0][1].should == "/Jar/Jar"
55
 
            tokens[0][2].should == "Binks is my copilot"
56
 
        end
57
 
 
58
 
        it "should accept spaces and and single ticks" do
59
 
            provider = provider_class.new()
60
 
            tokens = provider.parse_commands("set 'Jar Jar' Binks")
61
 
            tokens.size.should == 1
62
 
            tokens[0].size.should == 3
63
 
            tokens[0][0].should == "set"
64
 
            tokens[0][1].should == "Jar Jar"
65
 
            tokens[0][2].should == "Binks"
66
 
        end
67
 
 
68
 
        it "should accept spaces in the value and and single ticks" do
69
 
            provider = provider_class.new()
70
 
            tokens = provider.parse_commands("set 'Jar Jar' 'Binks is my copilot'")
71
 
            tokens.size.should == 1
72
 
            tokens[0].size.should == 3
73
 
            tokens[0][0].should == "set"
74
 
            tokens[0][1].should == "Jar Jar"
75
 
            tokens[0][2].should == "Binks is my copilot"
76
 
        end
77
 
 
78
 
        it "should accept spaces and and double ticks" do
79
 
            provider = provider_class.new()
80
 
            tokens = provider.parse_commands('set "Jar Jar" Binks')
81
 
            tokens.size.should == 1
82
 
            tokens[0].size.should == 3
83
 
            tokens[0][0].should == "set"
84
 
            tokens[0][1].should == 'Jar Jar'
85
 
            tokens[0][2].should == 'Binks'
86
 
        end
87
 
 
88
 
        it "should accept spaces in the value and and double ticks" do
89
 
            provider = provider_class.new()
90
 
            tokens = provider.parse_commands('set "Jar Jar" "Binks is my copilot"')
91
 
            tokens.size.should == 1
92
 
            tokens[0].size.should == 3
93
 
            tokens[0][0].should == "set"
94
 
            tokens[0][1].should == 'Jar Jar'
 
62
            tokens[1][1].should == "/foo/anakin"
 
63
        end
 
64
 
 
65
        # This is not supported in the new parsing class
 
66
        #it "should concat the last values" do
 
67
        #    provider = provider_class.new()
 
68
        #    tokens = provider.parse_commands("set /Jar/Jar Binks is my copilot")
 
69
        #    tokens.size.should == 1
 
70
        #    tokens[0].size.should == 3
 
71
        #    tokens[0][0].should == "set"
 
72
        #    tokens[0][1].should == "/Jar/Jar"
 
73
        #    tokens[0][2].should == "Binks is my copilot"
 
74
        #end
 
75
 
 
76
        it "should accept spaces in the value and single ticks" do
 
77
            @resource.stubs(:[]).returns("/foo/")
 
78
            tokens = @provider.parse_commands("set JarJar 'Binks is my copilot'")
 
79
            tokens.size.should == 1
 
80
            tokens[0].size.should == 3
 
81
            tokens[0][0].should == "set"
 
82
            tokens[0][1].should == "/foo/JarJar"
 
83
            tokens[0][2].should == "Binks is my copilot"
 
84
        end
 
85
 
 
86
        it "should accept spaces in the value and double ticks" do
 
87
            @resource.stubs(:[]).returns("/foo/")
 
88
            tokens = @provider.parse_commands('set /JarJar "Binks is my copilot"')
 
89
            tokens.size.should == 1
 
90
            tokens[0].size.should == 3
 
91
            tokens[0][0].should == "set"
 
92
            tokens[0][1].should == '/JarJar'
95
93
            tokens[0][2].should == 'Binks is my copilot'
96
94
        end
97
95
 
98
96
        it "should accept mixed ticks" do
99
 
            provider = provider_class.new()
100
 
            tokens = provider.parse_commands('set "Jar Jar" "Some \'Test\'"')
 
97
            @resource.stubs(:[]).returns("/foo/")
 
98
            tokens = @provider.parse_commands('set JarJar "Some \'Test\'"')
101
99
            tokens.size.should == 1
102
100
            tokens[0].size.should == 3
103
101
            tokens[0][0].should == "set"
104
 
            tokens[0][1].should == 'Jar Jar'
 
102
            tokens[0][1].should == '/foo/JarJar'
105
103
            tokens[0][2].should == "Some \'Test\'"
106
104
        end
107
105
 
108
 
        it "should accept only the last value using ticks" do
109
 
            provider = provider_class.new()
110
 
            tokens = provider.parse_commands('set /Jar/Jar "Binks is my copilot"')
111
 
            tokens.size.should == 1
112
 
            tokens[0].size.should == 3
113
 
            tokens[0][0].should == "set"
114
 
            tokens[0][1].should == '/Jar/Jar'
115
 
            tokens[0][2].should == "Binks is my copilot"
116
 
        end
117
 
 
118
 
        it "should accept only the first value using ticks" do
119
 
            provider = provider_class.new()
120
 
            tokens = provider.parse_commands('set "Jar Jar" copilot')
121
 
            tokens.size.should == 1
122
 
            tokens[0].size.should == 3
123
 
            tokens[0][0].should == "set"
124
 
            tokens[0][1].should == 'Jar Jar'
125
 
            tokens[0][2].should == "copilot"
126
 
        end
127
 
 
128
 
        it "should accept only the first value using ticks and the last values being concatenated" do
129
 
            provider = provider_class.new()
130
 
            tokens = provider.parse_commands('set "Jar Jar" Binks is my copilot')
131
 
            tokens.size.should == 1
132
 
            tokens[0].size.should == 3
133
 
            tokens[0][0].should == "set"
134
 
            tokens[0][1].should == 'Jar Jar'
135
 
            tokens[0][2].should == "Binks is my copilot"
 
106
        it "should handle predicates with literals" do
 
107
            @resource.stubs(:[]).returns("/foo/")
 
108
            tokens = @provider.parse_commands("rm */*[module='pam_console.so']")
 
109
            tokens.should == [["rm", "/foo/*/*[module='pam_console.so']"]]
 
110
        end
 
111
 
 
112
        it "should handle whitespace in predicates" do
 
113
            @resource.stubs(:[]).returns("/foo/")
 
114
            tokens = @provider.parse_commands("ins 42 before /files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]")
 
115
            tokens.should == [["ins", "42", "before","/files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]"]]
 
116
        end
 
117
 
 
118
        it "should handle multiple predicates" do
 
119
            @resource.stubs(:[]).returns("/foo/")
 
120
            tokens = @provider.parse_commands("clear pam.d/*/*[module = 'system-auth'][type = 'account']")
 
121
            tokens.should == [["clear", "/foo/pam.d/*/*[module = 'system-auth'][type = 'account']"]]
 
122
        end
 
123
 
 
124
        it "should handle nested predicates" do
 
125
            @resource.stubs(:[]).returns("/foo/")
 
126
            args = ["clear", "/foo/pam.d/*/*[module[ ../type = 'type] = 'system-auth'][type[last()] = 'account']"]
 
127
            tokens = @provider.parse_commands(args.join(" "))
 
128
            tokens.should == [ args ]
 
129
        end
 
130
 
 
131
        it "should handle escaped doublequotes in doublequoted string" do
 
132
            @resource.stubs(:[]).returns("/foo/")
 
133
            tokens = @provider.parse_commands("set /foo \"''\\\"''\"")
 
134
            tokens.should == [[ "set", "/foo", "''\\\"''" ]]
 
135
        end
 
136
 
 
137
        it "should allow escaped spaces and brackets in paths" do
 
138
            @resource.stubs(:[]).returns("/foo/")
 
139
            args = [ "set", "/white\\ space/\\[section", "value" ]
 
140
            tokens = @provider.parse_commands(args.join(" \t "))
 
141
            tokens.should == [ args ]
 
142
        end
 
143
 
 
144
        it "should remove trailing slashes" do
 
145
            @resource.stubs(:[]).returns("/foo/")
 
146
            tokens = @provider.parse_commands("set foo/ bar")
 
147
            tokens.should == [[ "set", "/foo/foo", "bar" ]]
136
148
        end
137
149
    end
138
150
 
166
178
 
167
179
    describe "match filters" do
168
180
        before do
 
181
            resource = stub("resource", :[] => "")
169
182
            augeas_stub = stub("augeas", :match => ["set", "of", "values"])
170
 
            @provider = provider_class.new()
 
183
            @provider = provider_class.new(resource)
171
184
            @provider.aug= augeas_stub
172
185
        end
173
186
 
174
187
        it "should return true for size match" do
175
 
            command = ["match", "fake value", "size", "==", "3"]
 
188
            command = ["match", "fake value", "size == 3"]
176
189
            @provider.process_match(command).should == true
177
190
        end
178
191
 
179
192
        it "should return false for a size non match" do
180
 
            command = ["match", "fake value", "size", "<", "3"]
 
193
            command = ["match", "fake value", "size < 3"]
181
194
            @provider.process_match(command).should == false
182
195
        end
183
196
 
184
197
        it "should return true for includes match" do
185
 
            command = ["get", "fake value", "include", "values"]
 
198
            command = ["match", "fake value", "include values"]
186
199
            @provider.process_match(command).should == true
187
200
        end
188
201
 
189
202
        it "should return false for includes non match" do
190
 
            command = ["get", "fake value", "include", "JarJar"]
 
203
            command = ["match", "fake value", "include JarJar"]
191
204
            @provider.process_match(command).should == false
192
205
        end
193
206
 
194
207
        it "should return true for an array match" do
195
 
            command = ["get", "fake value", "==", "['set', 'of', 'values']"]
 
208
            command = ["match", "fake value", "== ['set', 'of', 'values']"]
196
209
            @provider.process_match(command).should == true
197
210
        end
198
211
 
199
212
        it "should return false for an array non match" do
200
 
            command = ["get", "fake value", "==", "['this', 'should', 'not', 'match']"]
201
 
            @provider.process_match(command).should == false
 
213
            command = ["match", "fake value", "== ['this', 'should', 'not', 'match']"]
 
214
            @provider.process_match(command).should == false
 
215
        end
 
216
 
 
217
        it "should return false for an array match with noteq" do
 
218
            command = ["match", "fake value", "!= ['set', 'of', 'values']"]
 
219
            @provider.process_match(command).should == false
 
220
        end
 
221
 
 
222
        it "should return true for an array non match with noteq" do
 
223
            command = ["match", "fake value", "!= ['this', 'should', 'not', 'match']"]
 
224
            @provider.process_match(command).should == true
202
225
        end
203
226
    end
204
227
 
205
228
    describe "need to run" do
206
229
        it "should handle no filters" do
207
230
            resource = stub("resource")
208
 
            resource.stubs(:[]).returns(false).then.returns("")
 
231
            resource.stubs(:[]).returns(false).then.returns("").then.returns("")
209
232
            augeas_stub = stub("augeas", :match => ["set", "of", "values"])
210
233
            augeas_stub.stubs("close")
211
234
            provider = provider_class.new(resource)
 
235
            provider.aug= augeas_stub
212
236
            provider.stubs(:get_augeas_version).returns("0.3.5")
213
237
            provider.need_to_run?.should == true
214
238
        end
215
239
 
216
240
        it "should return true when a get filter matches" do
217
241
            resource = stub("resource")
218
 
            resource.stubs(:[]).returns(false).then.returns("get path == value")
 
242
            resource.stubs(:[]).returns(false).then.returns("get path == value").then.returns("")
219
243
            provider = provider_class.new(resource)
220
244
            augeas_stub = stub("augeas", :get => "value")
221
245
            augeas_stub.stubs("close")
226
250
 
227
251
        it "should return false when a get filter does not match" do
228
252
            resource = stub("resource")
229
 
            resource.stubs(:[]).returns(false).then.returns("get path == another value")
 
253
            resource.stubs(:[]).returns(false).then.returns("get path == another value").then.returns("")
230
254
            provider = provider_class.new(resource)
231
255
            augeas_stub = stub("augeas", :get => "value")
232
256
            augeas_stub.stubs("close")
237
261
 
238
262
        it "should return true when a match filter matches" do
239
263
            resource = stub("resource")
240
 
            resource.stubs(:[]).returns(false).then.returns("match path size == 3")
 
264
            resource.stubs(:[]).returns(false).then.returns("match path size == 3").then.returns("")
241
265
            provider = provider_class.new(resource)
242
266
            augeas_stub = stub("augeas", :match => ["set", "of", "values"])
243
267
            augeas_stub.stubs("close")
248
272
 
249
273
        it "should return false when a match filter does not match" do
250
274
            resource = stub("resource")
251
 
            resource.stubs(:[]).returns(false).then.returns("match path size == 2")
 
275
            resource.stubs(:[]).returns(false).then.returns("match path size == 2").then.returns("")
252
276
            provider = provider_class.new(resource)
253
277
            augeas_stub = stub("augeas", :match => ["set", "of", "values"])
254
278
            augeas_stub.stubs("close")
260
284
        #This is a copy of the last one, with setting the force to true
261
285
        it "setting force should not change the above logic" do
262
286
            resource = stub("resource")
263
 
            resource.stubs(:[]).returns(true).then.returns("match path size == 2")
 
287
            resource.stubs(:[]).returns(true).then.returns("match path size == 2").then.returns("")
264
288
            provider = provider_class.new(resource)
265
289
            augeas_stub = stub("augeas", :match => ["set", "of", "values"])
266
290
            augeas_stub.stubs("close")
281
305
        end
282
306
 
283
307
        it "should handle set commands" do
284
 
            command = [["set", "/Jar/Jar", "Binks"]]
285
 
            context = "/some/path"
 
308
            command = "set JarJar Binks"
 
309
            context = "/some/path/"
286
310
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
287
 
            @augeas.expects(:set).with("/some/path/Jar/Jar", "Binks")
 
311
            @augeas.expects(:set).with("/some/path/JarJar", "Binks")
288
312
            @augeas.expects(:save).returns(true)
289
313
            @augeas.expects(:close)
290
314
            @provider.execute_changes.should == :executed
291
315
        end
292
316
 
293
317
        it "should handle rm commands" do
294
 
            command = [["rm", "/Jar/Jar"]]
 
318
            command = "rm /Jar/Jar"
295
319
            context = ""
296
320
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
297
321
            @augeas.expects(:rm).with("/Jar/Jar")
301
325
        end
302
326
 
303
327
        it "should handle remove commands" do
304
 
            command = [["remove", "Jar/Jar"]]
 
328
            command = "remove /Jar/Jar"
305
329
            context = ""
306
330
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
307
331
            @augeas.expects(:rm).with("/Jar/Jar")
311
335
        end
312
336
 
313
337
        it "should handle clear commands" do
314
 
            command = [["clear", "/Jar/Jar"]]
315
 
            context = "/foo"
 
338
            command = "clear Jar/Jar"
 
339
            context = "/foo/"
316
340
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
317
341
            @augeas.expects(:clear).with("/foo/Jar/Jar")
318
342
            @augeas.expects(:save).returns(true)
322
346
 
323
347
 
324
348
        it "should handle ins commands with before" do
325
 
            command = [["ins", "Binks", "before /Jar/Jar"]]
 
349
            command = "ins Binks before Jar/Jar"
326
350
            context = "/foo"
327
351
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
328
352
            @augeas.expects(:insert).with("/foo/Jar/Jar", "Binks", true)
331
355
            @provider.execute_changes.should == :executed
332
356
        end
333
357
 
334
 
        it "should handle ins commands with before" do
335
 
            command = [["ins", "Binks", "after /Jar/Jar"]]
 
358
        it "should handle ins commands with after" do
 
359
            command = "ins Binks after /Jar/Jar"
336
360
            context = "/foo"
337
361
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
338
 
            @augeas.expects(:insert).with("/foo/Jar/Jar", "Binks", false)
 
362
            @augeas.expects(:insert).with("/Jar/Jar", "Binks", false)
339
363
            @augeas.expects(:save).returns(true)
340
364
            @augeas.expects(:close)
341
365
            @provider.execute_changes.should == :executed
342
366
        end
343
367
 
344
368
        it "should handle ins with no context" do
345
 
            command = [["ins", "Binks", "after /Jar/Jar"]]
 
369
            command = "ins Binks after /Jar/Jar"
346
370
            context = "" # this is the default
347
371
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
348
372
            @augeas.expects(:insert).with("/Jar/Jar", "Binks", false)
352
376
        end
353
377
 
354
378
        it "should handle multiple commands" do
355
 
            command = [["ins", "Binks", "after /Jar/Jar"], ["clear", "/Jar/Jar"]]
356
 
            context = "/foo"
 
379
            command = ["ins Binks after /Jar/Jar", "clear Jar/Jar"]
 
380
            context = "/foo/"
357
381
            @resource.expects(:[]).times(2).returns(command).then.returns(context)
358
 
            @augeas.expects(:insert).with("/foo/Jar/Jar", "Binks", false)
 
382
            @augeas.expects(:insert).with("/Jar/Jar", "Binks", false)
359
383
            @augeas.expects(:clear).with("/foo/Jar/Jar")
360
384
            @augeas.expects(:save).returns(true)
361
385
            @augeas.expects(:close)