blob: 3142fb5eeb9384ab26c8688425babb6dcf1ce84e [file] [log] [blame]
Aleš Komárek4fdb8e92016-11-26 12:11:16 +01001-- Copyright 2015 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7-- http://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14
15EXPORT_ASSERT_TO_GLOBALS=true
16require('luaunit')
17package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua"
18local M = require('value_matching')
19
20TestValueMatching = {}
21
22function TestValueMatching:test_simple_matching()
23 local tests = {
24 {'/var/log', '/var/log'},
25 {'== /var/log', '/var/log'},
26 {'==/var/log', '/var/log'},
27 {'==/var/log ', '/var/log'},
28 {'==\t/var/log', '/var/log'},
29 {'=="/var/log"', '/var/log'},
30 {'== "/var/log"', '/var/log'},
31 {'== " /var/log"', ' /var/log'},
32 {'== "/var/log "', '/var/log '},
33 {'== " /var/log "', ' /var/log '},
34 {8, 8},
35 {8, '8'},
36 {"9", "9"},
37 {"==10", " 10"},
38 {"10", 10},
39 {"== 10", " 10"},
40 {"== 10.0", " 10.0"},
41 {"== -10.01", " -10.01"},
42 {"== 10 ", " 10 "},
43 {' <=11', '-11'},
44 {"!= -12", 42},
45 {"!= 12", 42},
46 {" > 13", 42},
47 {">= 13", 13},
48 {">= -13", 42},
49 {"< 14", -0},
50 {"<= 14 ", 0},
51 {"<= 14", "14"},
52 }
53 local r
54 for _, v in ipairs(tests) do
55 local exp, value = v[1], v[2]
56 local m = M.new(exp)
57 r = m:matches(value)
58 assertTrue(r)
59 end
60end
61
62function TestValueMatching:test_simple_not_matching()
63 local tests = {
64 {'/var/log', '/var/log/mysql'},
65 {'== "/var/log"' , '/var/log '},
66 {'"/var/log"', '/var/log '},
67 {'"/var/log "', '/var/log'},
68 {'nova-api', 'nova-compute'},
69 {'== /var/log', '/var/log/mysql'},
70 {'==/var/log', '/var/log/mysql'},
71 {'!=/var/log', '/var/log'},
72 {'!= /var/log', '/var/log'},
73 {'>10', '5'},
74 {'> 10', '5 '},
75 {' <11', '11'},
76 {' >=11', '-11'},
77 {' >=11 && <= 42', '-11'},
78 {' >=11 || == 42', '-11'},
79 }
80
81 for _, v in ipairs(tests) do
82 local exp, value = v[1], v[2]
83 local m = M.new(exp)
84 r = m:matches(value)
85 assertFalse(r)
86 end
87end
88
89function TestValueMatching:test_string_matching()
90 local tests = {
91 {'== "foo.bar"', "foo.bar", true},
92 {'== foo.bar', "foo.bar", true},
93 {'== foo.bar ', "foo.bar", true},
94 {'== foo || bar', "bar", true},
95 {'== foo || bar', "foo", true},
96 {'== foo || bar', "??", false},
97 {'!= foo || != bar', "42", true},
98 }
99
100 for _, v in ipairs(tests) do
101 local exp, value, expected = v[1], v[2], v[3]
102 local m = M.new(exp)
103 r = m:matches(value)
104 assertEquals(r, expected)
105 end
106
107end
108
109function TestValueMatching:test_invalid_expression()
110 local tests = {
111 '&& 1 && 1',
112 ' && 1',
113 '|| == 1',
114 '&& != 12',
115 ' ',
116 ' ',
117 '\t',
118 '',
119 nil,
120 }
121 for _, exp in ipairs(tests) do
122 assertError(M.new, exp)
123 end
124end
125
126function TestValueMatching:test_range_matching()
127 local tests = {
128 {'>= 200 && < 300', 200, true},
129 {'>=200&&<300' , 200, true},
130 {' >=200&&<300' , 200, true},
131 {'>= 200 && < 300', 204, true},
132 {'>= 200 && < 300', 300, false},
133 {'>= 200 && < 300', 42, false},
134 {'>= 200 && < 300', 0, false},
135 }
136
137 for _, v in ipairs(tests) do
138 local exp, value, expected = v[1], v[2], v[3]
139 local m = M.new(exp)
140 r = m:matches(value)
141 assertEquals(r, expected)
142 end
143end
144
145function TestValueMatching:test_wrong_data()
146 local tests = {
147 {'>= 200 && < 300', "foo", false},
148 {'>= 200 && < 300', "" , false},
149 {'== 200' , "bar", false},
150 {'== foo' , "10" , false},
151 {'!= foo' , " 10", true},
152 }
153 for _, v in ipairs(tests) do
154 local exp, value, expected = v[1], v[2], v[3]
155 local m = M.new(exp)
156 r = m:matches(value)
157 assertEquals(r, expected)
158 end
159end
160
161function TestValueMatching:test_precedence()
162 local tests = {
163 {'>= 200 && < 300 || >500', "200", true},
164 {'>= 200 && < 300 || >500', "501", true},
165 {'>= 200 && < 300 || >=500', "500", true},
166 {'>400 || >= 200 && < 300', "500", true},
167 {'>=300 && <500 || >= 200 && < 300', "300", true},
168 {'>=300 && <500 || >= 200 && < 300', "500", false},
169 }
170
171 for _, v in ipairs(tests) do
172 local exp, value, expected = v[1], v[2], v[3]
173 local m = M.new(exp)
174 r = m:matches(value)
175 assertEquals(r, expected)
176 end
177end
178
179function TestValueMatching:test_pattern_matching()
180 local tests = {
181 {'=~ /var/lib/ceph/osd/ceph%-%d+', "/var/lib/ceph/osd/ceph-1", true},
182 {'=~ /var/lib/ceph/osd/ceph%-%d+', "/var/lib/ceph/osd/ceph-42", true},
183 {'=~ ^/var/lib/ceph/osd/ceph%-%d+$', "/var/lib/ceph/osd/ceph-42", true},
184 {'=~ "/var/lib/ceph/osd/ceph%-%d+"', "/var/lib/ceph/osd/ceph-42", true},
185 {'=~ "ceph%-%d+"', "/var/lib/ceph/osd/ceph-42", true},
186 {'=~ "/var/lib/ceph/osd/ceph%-%d+$"', "/var/lib/ceph/osd/ceph-42 ", false}, -- trailing space
187 {'=~ /var/lib/ceph/osd/ceph%-%d+', "/var/log", false},
188 {'=~ /var/lib/ceph/osd/ceph%-%d+ || foo', "/var/lib/ceph/osd/ceph-1", true},
189 {'=~ "foo||bar" || foo', "foo||bar", true},
190 {'=~ "foo||bar" || foo', "foo", true},
191 {'=~ "foo&&bar" || foo', "foo&&bar", true},
192 {'=~ "foo&&bar" || foo', "foo", true},
193 {'=~ bar && /var/lib/ceph/osd/ceph%-%d+', "/var/lib/ceph/osd/ceph-1", false},
194 {'=~ -', "-", true},
195 {'=~ %-', "-", true},
196 {'!~ /var/lib/ceph/osd/ceph', "/var/log", true},
197 {'!~ /var/lib/ceph/osd/ceph%-%d+', "/var/log", true},
198 {'!~ .+osd%-%d+', "/var/log", true},
199 {'!~ osd%-%d+', "/var/log", true},
200 --{'=~ [', "[", true},
201 }
202
203 for _, v in ipairs(tests) do
204 local exp, value, expected = v[1], v[2], v[3]
205 local m = M.new(exp)
206 r = m:matches(value)
207 assertEquals(r, expected)
208 end
209end
210
211function TestValueMatching:test_wrong_patterns_never_match()
212 -- These patterns raise errors like:
213 -- malformed pattern (missing ']')
214 local tests = {
215 {'=~ [', "[", false},
216 {'!~ [', "[", false},
217 }
218
219 for _, v in ipairs(tests) do
220 local exp, value, expected = v[1], v[2], v[3]
221 local m = M.new(exp)
222 r = m:matches(value)
223 assertEquals(r, expected)
224 end
225end
226
227lu = LuaUnit
228lu:setVerbosity( 1 )
229os.exit( lu:run() )