Revision a123dc19
b/Makefile.am | ||
---|---|---|
138 | 138 |
lib/netutils.py \ |
139 | 139 |
lib/objects.py \ |
140 | 140 |
lib/opcodes.py \ |
141 |
lib/qlang.py \ |
|
141 | 142 |
lib/query.py \ |
142 | 143 |
lib/rpc.py \ |
143 | 144 |
lib/runtime.py \ |
... | ... | |
443 | 444 |
test/ganeti.netutils_unittest.py \ |
444 | 445 |
test/ganeti.objects_unittest.py \ |
445 | 446 |
test/ganeti.opcodes_unittest.py \ |
447 |
test/ganeti.qlang_unittest.py \ |
|
446 | 448 |
test/ganeti.query_unittest.py \ |
447 | 449 |
test/ganeti.rapi.client_unittest.py \ |
448 | 450 |
test/ganeti.rapi.resources_unittest.py \ |
b/lib/qlang.py | ||
---|---|---|
1 |
# |
|
2 |
# |
|
3 |
|
|
4 |
# Copyright (C) 2010 Google Inc. |
|
5 |
# |
|
6 |
# This program is free software; you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation; either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, but |
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
# General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program; if not, write to the Free Software |
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 |
# 02110-1301, USA. |
|
20 |
|
|
21 |
|
|
22 |
"""Module for a simple query language""" |
|
23 |
|
|
24 |
from ganeti import errors |
|
25 |
|
|
26 |
|
|
27 |
OP_OR = "|" |
|
28 |
OP_EQUAL = "=" |
|
29 |
|
|
30 |
|
|
31 |
def ReadSimpleFilter(namefield, filter_): |
|
32 |
"""Function extracting wanted names from restricted filter. |
|
33 |
|
|
34 |
This should only be used until proper filtering is implemented. The filter |
|
35 |
must either be empty or of the format C{["|", ["=", field, "name1"], ["=", |
|
36 |
field, "name2"], ...]}. |
|
37 |
|
|
38 |
""" |
|
39 |
if filter_ is None: |
|
40 |
return [] |
|
41 |
|
|
42 |
if not isinstance(filter_, list): |
|
43 |
raise errors.ParameterError("Filter should be list") |
|
44 |
|
|
45 |
if not filter_ or filter_[0] != OP_OR: |
|
46 |
raise errors.ParameterError("Filter should start with OR operator") |
|
47 |
|
|
48 |
if len(filter_) < 2: |
|
49 |
raise errors.ParameterError("Invalid filter, OR operator should have" |
|
50 |
" operands") |
|
51 |
|
|
52 |
result = [] |
|
53 |
|
|
54 |
for idx, item in enumerate(filter_[1:]): |
|
55 |
if not isinstance(item, list): |
|
56 |
raise errors.ParameterError("Invalid OR operator, operand %s not a" |
|
57 |
" list" % idx) |
|
58 |
|
|
59 |
if len(item) != 3 or item[0] != OP_EQUAL: |
|
60 |
raise errors.ParameterError("Invalid OR operator, operand %s is not an" |
|
61 |
" equality filter" % idx) |
|
62 |
|
|
63 |
(_, name, value) = item |
|
64 |
|
|
65 |
if not isinstance(value, basestring): |
|
66 |
raise errors.ParameterError("Operand %s for OR should compare against a" |
|
67 |
" string" % idx) |
|
68 |
|
|
69 |
if name != namefield: |
|
70 |
raise errors.ParameterError("Operand %s for OR should filter field '%s'," |
|
71 |
" not '%s'" % (idx, namefield, name)) |
|
72 |
|
|
73 |
result.append(value) |
|
74 |
|
|
75 |
return result |
b/test/ganeti.qlang_unittest.py | ||
---|---|---|
1 |
#!/usr/bin/python |
|
2 |
# |
|
3 |
|
|
4 |
# Copyright (C) 2010 Google Inc. |
|
5 |
# |
|
6 |
# This program is free software; you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation; either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, but |
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
# General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program; if not, write to the Free Software |
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 |
# 02110-1301, USA. |
|
20 |
|
|
21 |
|
|
22 |
"""Script for testing ganeti.qlang""" |
|
23 |
|
|
24 |
import unittest |
|
25 |
|
|
26 |
from ganeti import utils |
|
27 |
from ganeti import errors |
|
28 |
from ganeti import qlang |
|
29 |
|
|
30 |
import testutils |
|
31 |
|
|
32 |
|
|
33 |
class TestReadSimpleFilter(unittest.TestCase): |
|
34 |
def _Test(self, filter_, expected): |
|
35 |
self.assertEqual(qlang.ReadSimpleFilter("name", filter_), expected) |
|
36 |
|
|
37 |
def test(self): |
|
38 |
self._Test(None, []) |
|
39 |
self._Test(["|", ["=", "name", "xyz"]], ["xyz"]) |
|
40 |
|
|
41 |
for i in [1, 3, 10, 25, 140]: |
|
42 |
self._Test(["|"] + [["=", "name", "node%s" % j] for j in range(i)], |
|
43 |
["node%s" % j for j in range(i)]) |
|
44 |
|
|
45 |
def testErrors(self): |
|
46 |
for i in [123, True, False, "", "Hello World", "a==b", |
|
47 |
[], ["x"], ["x", "y", "z"], ["|"], |
|
48 |
["|", ["="]], ["|", "x"], ["|", 123], |
|
49 |
["|", ["=", "otherfield", "xyz"]], |
|
50 |
["|", ["=", "name", "xyz"], "abc"], |
|
51 |
["|", ["=", "name", "xyz", "too", "long"]], |
|
52 |
["|", ["=", "name", []]], |
|
53 |
["|", ["=", "name", 999]], |
|
54 |
["|", ["=", "name", "abc"], ["=", "otherfield", "xyz"]]]: |
|
55 |
self.assertRaises(errors.ParameterError, qlang.ReadSimpleFilter, |
|
56 |
"name", i) |
|
57 |
|
|
58 |
|
|
59 |
if __name__ == "__main__": |
|
60 |
testutils.GanetiTestProgram() |
Also available in: Unified diff