Imported Upstream version 1.5.0
[platform/upstream/augeas.git] / tests / xpath.tests
1 # Tests of the XPath matching
2
3 # Blank lines and lines starting with '#' are ignored
4 #
5 # Each test consists of one line declaring the test followed by a complete
6 # list of the expected result of the match.
7 #
8 # The test is declared with a line 'test NAME MATCH'. A result is either
9 # just a path (meaning that the value associated with that node must be
10 # NULL) or of the form PATH = VALUE, meaning that the value for the node at
11 # PATH must be VALUE. If VALUE is '...', the test does not check the value
12 # associated with PATH in the tree.
13 #
14 # The MATCH XPath expression is matched against a fixed tree (the one from the
15 # root/ subdirectory) and the result of the aug_match is compared with the
16 # results listed in the test
17 #
18 # The test framework sets up variables:
19 #   hosts      /files/etc/hosts/*
20 #   localhost  '127.0.0.1'
21 #   php        /files/etc/php.ini
22
23 # Very simple to warm up
24 test wildcard /files/etc/hosts/*/ipaddr
25      /files/etc/hosts/1/ipaddr = 127.0.0.1
26      /files/etc/hosts/2/ipaddr = 172.31.122.14
27
28 test wildcard-var $hosts/ipaddr
29      /files/etc/hosts/1/ipaddr = 127.0.0.1
30      /files/etc/hosts/2/ipaddr = 172.31.122.14
31
32 # Compare the value of the current node with a constant
33 test self-value /files/etc/hosts/*/ipaddr[ . = '127.0.0.1' ]
34      /files/etc/hosts/1/ipaddr = 127.0.0.1
35
36 test self-value-var $hosts/ipaddr[ . = $localhost ]
37      /files/etc/hosts/1/ipaddr = 127.0.0.1
38
39 # Find nodes that have a child named 'ipaddr' with a fixed value
40 test child-value /files/etc/hosts/*[ipaddr = '127.0.0.1']
41      /files/etc/hosts/1
42
43 test child-value-var $hosts[ipaddr = $localhost]
44      /files/etc/hosts/1
45
46 # Find nodes that have a child 'ipaddr' that has no value
47 test child-nil-value /files/etc/hosts/*[ipaddr = '']
48
49 test child-nil-value-var $hosts[ipaddr = '']
50
51 # Find nodes that have no value
52 test self-nil-value /files/etc/hosts/*[. = '']
53      /files/etc/hosts/1
54      /files/etc/hosts/2
55
56 test self-nil-value-var $hosts[. = '']
57      /files/etc/hosts/1
58      /files/etc/hosts/2
59
60 # Match over two levels of the tree
61 test two-wildcards /files/etc/*/*[ipaddr='127.0.0.1']
62      /files/etc/hosts/1
63
64 test pam-system-auth /files/etc/pam.d/*/*[module = 'system-auth']
65      /files/etc/pam.d/login/2
66      /files/etc/pam.d/login/4
67      /files/etc/pam.d/login/5
68      /files/etc/pam.d/login/8
69      /files/etc/pam.d/postgresql/1
70      /files/etc/pam.d/postgresql/2
71      /files/etc/pam.d/newrole/1
72      /files/etc/pam.d/newrole/2
73      /files/etc/pam.d/newrole/3
74
75 # Multiple predicates are treated with 'and'
76 test pam-two-preds /files/etc/pam.d/*/*[module = 'system-auth'][type = 'account']
77      /files/etc/pam.d/login/4
78      /files/etc/pam.d/postgresql/2
79      /files/etc/pam.d/newrole/2
80
81 # Find nodes that have siblings with a given value
82 test pam-two-preds-control /files/etc/pam.d/*/*[module = 'system-auth'][type = 'account']/control
83      /files/etc/pam.d/login/4/control = include
84      /files/etc/pam.d/postgresql/2/control = include
85      /files/etc/pam.d/newrole/2/control = include
86
87 # last() gives the last node with a certain name
88 test last /files/etc/hosts/*[ipaddr = "127.0.0.1"]/alias[last()]
89      /files/etc/hosts/1/alias[3] = galia
90
91 test last-var $hosts[ipaddr = $localhost]/alias[last()]
92      /files/etc/hosts/1/alias[3] = galia
93
94 # We can get nodes counting from the right with 'last()-N'
95 test last-minus-one /files/etc/hosts/*[ipaddr = "127.0.0.1"]/alias[ last() - 1 ]
96      /files/etc/hosts/1/alias[2] = galia.watzmann.net
97
98 # Make sure we look at all nodes with a given label (ticket #23)
99 test transparent-multi-node /files/etc/ssh/sshd_config/AcceptEnv/10
100      /files/etc/ssh/sshd_config/AcceptEnv[2]/10 = LC_ADDRESS
101
102 test abbrev-descendants /files/etc/pam.d//1
103      /files/etc/pam.d/login/1
104      /files/etc/pam.d/postgresql/1
105      /files/etc/pam.d/newrole/1
106
107 test descendant-or-self /files/descendant-or-self :: 4
108      /files/etc/ssh/sshd_config/AcceptEnv[1]/4 = LC_TIME
109      /files/etc/aliases/4
110      /files/etc/fstab/4
111      /files/etc/pam.d/login/4
112      /files/etc/pam.d/newrole/4
113      /files/etc/inittab/4
114
115 test descendant /files/etc/aliases/4/descendant::4
116
117 test descendant-or-self-2 /files/etc/aliases/4/descendant-or-self::4
118      /files/etc/aliases/4
119
120 # No matches because the predicate asks if there is a toplevel node
121 # 'ipaddr' with the given value
122 test abs-locpath /files/etc/hosts/*[/ipaddr = '127.0.0.1']/canonical
123
124 test rel-pred /files/etc/hosts/*/canonical[../ipaddr = '127.0.0.1']
125      /files/etc/hosts/1/canonical = localhost.localdomain
126
127 # Not the best way to write this, but entirely acceptable
128 test path-with-parent /files/etc/hosts/*/canonical[../ipaddr = '127.0.0.1']/../alias
129      /files/etc/hosts/1/alias[1] = localhost
130      /files/etc/hosts/1/alias[2] = galia.watzmann.net
131      /files/etc/hosts/1/alias[3] = galia
132
133 test node-exists-pred /files/etc/hosts/*/canonical[../alias]
134      /files/etc/hosts/1/canonical = localhost.localdomain
135      /files/etc/hosts/2/canonical = orange.watzmann.net
136
137 test ipaddr-child //*[ipaddr]
138      /files/etc/hosts/1
139      /files/etc/hosts/2
140
141 test ipaddr-sibling //*[../ipaddr]
142      /files/etc/hosts/1/ipaddr = 127.0.0.1
143      /files/etc/hosts/1/canonical = localhost.localdomain
144      /files/etc/hosts/1/alias[1] = localhost
145      /files/etc/hosts/1/alias[2] = galia.watzmann.net
146      /files/etc/hosts/1/alias[3] = galia
147      /files/etc/hosts/2/ipaddr = 172.31.122.14
148      /files/etc/hosts/2/canonical = orange.watzmann.net
149      /files/etc/hosts/2/alias = orange
150
151 test lircd-ancestor //*[ancestor::kudzu][label() != '#comment']
152      /augeas/files/etc/sysconfig/kudzu/path = /files/etc/sysconfig/kudzu
153      /augeas/files/etc/sysconfig/kudzu/mtime = ...
154      /augeas/files/etc/sysconfig/kudzu/lens = @Shellvars
155      /augeas/files/etc/sysconfig/kudzu/lens/info = ...
156      /files/etc/sysconfig/kudzu/SAFE = no
157
158 test wildcard-last /files/etc/hosts/*[position() = last()]
159      /files/etc/hosts/2
160
161 test wildcard-not-last /files/etc/hosts/*[position() != last()][ipaddr]
162      /files/etc/hosts/1
163
164 test nodeset-nodeset-eq /files/etc/sysconfig/network-scripts/*[BRIDGE = /files/etc/sysconfig/network-scripts/ifcfg-br0/DEVICE]
165      /files/etc/sysconfig/network-scripts/ifcfg-eth0
166
167 test last-ssh-service /files/etc/services/service-name[port = '22'][last()]
168      /files/etc/services/service-name[24] = ssh
169
170 test count-one-alias /files/etc/hosts/*[count(alias) = 1]
171      /files/etc/hosts/2
172
173 test number-gt /files/etc/hosts/*[count(alias) > 1]
174      /files/etc/hosts/1
175
176 test pred-or /files/etc/hosts/*[canonical = 'localhost' or alias = 'localhost']
177      /files/etc/hosts/1
178
179 test pred-and-or /files/etc/hosts/*[(canonical = 'localhost' or alias = 'localhost') and ipaddr = '127.0.0.1']
180      /files/etc/hosts/1
181
182 # We used to parse this as '/files/etc/.' followed by garbage, that
183 # was silently ignored. This path must not match anything, instead of
184 # every child of /files/etc
185 test path-with-dot /files/etc/.notthere
186
187 test str-neq /files/etc/*['foo' != 'foo']
188
189 # label() returns the label of the context node as a string
190 test label-neq /files/etc/hosts/*[label() != '#comment']
191      /files/etc/hosts/1
192      /files/etc/hosts/2
193
194 # 'and' and 'or' need to coerce types to boolean
195 test coerce-or /files[/files/etc/hosts/*[ipaddr = '127.0.0.1'] or /files/etc/aliases/*[name = 'root'] ]
196      /files
197
198 test coerce-and-true /files['foo' and count(/files/etc/hosts/*) ]
199      /files
200
201 test coerce-and-false /files['' and count(/none) and /none]
202
203 test preceding-sibling /files/etc/hosts/*/preceding-sibling::*[alias = 'localhost']
204      /files/etc/hosts/1
205
206 test preceding-sibling-pred /files/etc/grub.conf/*[preceding-sibling::*[1] = ../default]
207      /files/etc/grub.conf/timeout = 5
208
209 test preceding-sibling-pred2 /files/etc/grub.conf/*[preceding-sibling::*[1][self::default]]
210      /files/etc/grub.conf/timeout = 5
211
212 test following-sibling /files/etc/hosts/1/*/following-sibling::alias
213      /files/etc/hosts/1/alias[1] = localhost
214      /files/etc/hosts/1/alias[2] = galia.watzmann.net
215      /files/etc/hosts/1/alias[3] = galia
216
217 test following-sibling-pred /files/etc/hosts/1/*[following-sibling::alias]
218      /files/etc/hosts/1/ipaddr = 127.0.0.1
219      /files/etc/hosts/1/canonical = localhost.localdomain
220      /files/etc/hosts/1/alias[1] = localhost
221      /files/etc/hosts/1/alias[2] = galia.watzmann.net
222
223 test regexp1 /files/etc/sysconfig/network-scripts/*[label() =~ regexp('.*-eth0')]
224      /files/etc/sysconfig/network-scripts/ifcfg-eth0
225
226 test regexp2 /files/etc/hosts/*[* =~ regexp('127\..*')]
227      /files/etc/hosts/1
228
229 test regexp3 /files/etc/hosts/*[ipaddr =~ regexp(/files/etc/hosts/*/ipaddr)]
230      /files/etc/hosts/1
231      /files/etc/hosts/2
232
233 # Check that we don't crash when the nodeset contains all NULL's
234 test regexp4 /files/etc/hosts/*[ipaddr =~ regexp(/files/etc/hosts/*[ipaddr])]
235
236 # Check case-insensitive matches
237 test regexp5 /files/etc/sysconfig/network-scripts/*[label() =~ regexp('.*-ETH0', 'i')]
238      /files/etc/sysconfig/network-scripts/ifcfg-eth0
239
240 test regexp6 /files/etc/hosts/*[ipaddr =~ regexp(/files/etc/hosts/*/ipaddr, 'i')]
241      /files/etc/hosts/1
242      /files/etc/hosts/2
243
244 test glob1 /files[ 'axxa' =~ glob('a*a') ]
245      /files
246
247 test glob2 /files[ 'axxa' =~ glob('a?[a-z]a') ]
248      /files
249
250 test glob3 /files[ '^a' =~ glob('^a') ]
251      /files
252
253 test glob4 /augeas/load/*[ '/etc/hosts' =~ glob(incl) ]
254      /augeas/load/Hosts
255
256 test glob5 /files[ '/files/etc/hosts/1' =~ glob('/files/*/1') ]
257
258 test glob6 /files[ '/files/etc/hosts/1' =~ glob('/files/*/*/1') ]
259      /files
260
261 test glob_nomatch /files/etc/hosts/*[ipaddr][ ipaddr !~ glob(/files/etc/hosts/*/ipaddr[1]) ]
262     /files/etc/hosts/2
263
264 test glob_for_lens /augeas/load/*[ '/etc/hosts/1/ipaddr' =~ glob(incl) + regexp('/.*') ]/lens
265      /augeas/load/Hosts/lens = @Hosts
266
267 # Union of nodesets
268 test union (/files/etc/yum.conf | /files/etc/yum.repos.d/*)/*/gpgcheck
269      /files/etc/yum.conf/main/gpgcheck = 1
270      /files/etc/yum.repos.d/fedora-updates.repo/updates/gpgcheck = 1
271      /files/etc/yum.repos.d/fedora-updates.repo/updates-debuginfo/gpgcheck = 1
272      /files/etc/yum.repos.d/fedora-updates.repo/updates-source/gpgcheck = 1
273      /files/etc/yum.repos.d/fedora.repo/fedora/gpgcheck = 1
274      /files/etc/yum.repos.d/fedora.repo/fedora-debuginfo/gpgcheck = 1
275      /files/etc/yum.repos.d/fedora.repo/fedora-source/gpgcheck = 1
276      /files/etc/yum.repos.d/remi.repo/remi/gpgcheck = 1
277      /files/etc/yum.repos.d/remi.repo/remi-test/gpgcheck = 1
278
279 # Paths with whitespace in them
280 test php1 $php/mail function
281      /files/etc/php.ini/mail\ function
282
283 test php2 $php[mail function]
284      /files/etc/php.ini
285
286 test php3 $php[count(mail function) = 1]
287      /files/etc/php.ini
288
289 test php4 $php/mail function/SMTP
290      /files/etc/php.ini/mail\ function/SMTP = localhost
291
292 test php5 $php/mail\ function
293      /files/etc/php.ini/mail\ function
294
295 test expr-or /files/etc/group/root/*[self::gid or self::user]
296      /files/etc/group/root/gid = 0
297      /files/etc/group/root/user = root
298
299 test int-ns-eq /files/etc/group/*[int(gid) = 30]
300      /files/etc/group/gopher
301
302 test int-ns-lt /files/etc/group/*[int(gid) < 3]
303     /files/etc/group/root
304     /files/etc/group/bin
305     /files/etc/group/daemon
306
307 test int-bool-f /files[int(1 = 0) > 0]
308
309 test int-bool-t /files[int(1 = 1) > 0]
310      /files
311
312 # Relative paths, relying on default /files context
313 test ctx_file etc/network/interfaces
314      /files/etc/network/interfaces
315
316 test ctx_file_pred etc/network/interfaces/iface[. = "lo"]
317      /files/etc/network/interfaces/iface[1] = lo
318
319 # Test matching with characters that need escaping in the filename
320 test escape1 /files/etc/sysconfig/network-scripts/*
321     /files/etc/sysconfig/network-scripts/ifcfg-eth0
322     /files/etc/sysconfig/network-scripts/ifcfg-wlan0
323     /files/etc/sysconfig/network-scripts/ifcfg-weird\ \[\!\]\ \(used\ to\ fail\)
324     /files/etc/sysconfig/network-scripts/ifcfg-lo
325     /files/etc/sysconfig/network-scripts/ifcfg-br0
326
327 test escape2 /files/etc/sysconfig/network-scripts/ifcfg-weird\ \[\!\]\ \(used\ to\ fail\)/DEVICE
328      /files/etc/sysconfig/network-scripts/ifcfg-weird\ \[\!\]\ \(used\ to\ fail\)/DEVICE = weird
329
330 test escape3 /files/etc/sysconfig/network-scripts/*[DEVICE = 'weird']
331      /files/etc/sysconfig/network-scripts/ifcfg-weird\ \[\!\]\ \(used\ to\ fail\)
332
333 # Test matching in the presence of hidden nodes
334 test hidden1 /files/etc/security/limits.conf/*[label() != '#comment'][1]
335      /files/etc/security/limits.conf/domain[1] = @jackuser