5
5
provider_class = Puppet::Type.type(:augeas).provider(:augeas)
7
7
describe provider_class do
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")
11
@resource = stub("resource")
12
@provider = provider_class.new(@resource)
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"
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"
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"
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"
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"
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"
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"
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'
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"
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"
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"
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'
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\'"
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"
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"
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']"]]
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' ]"]]
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']"]]
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 ]
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", "''\\\"''" ]]
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 ]
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" ]]
167
179
describe "match filters" 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
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
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
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
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
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
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
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
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
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
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")