qlang: Add function to build simple filter
[ganeti-local] / 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 class TestMakeSimpleFilter(unittest.TestCase):
60   def _Test(self, field, names, expected, parse_exp=None):
61     if parse_exp is None:
62       parse_exp = names
63
64     filter_ = qlang.MakeSimpleFilter(field, names)
65     self.assertEqual(filter_, expected)
66     self.assertEqual(qlang.ReadSimpleFilter(field, filter_), parse_exp)
67
68   def test(self):
69     self._Test("name", None, None, parse_exp=[])
70     self._Test("name", [], None)
71     self._Test("name", ["node1.example.com"],
72                ["|", ["=", "name", "node1.example.com"]])
73     self._Test("xyz", ["a", "b", "c"],
74                ["|", ["=", "xyz", "a"], ["=", "xyz", "b"], ["=", "xyz", "c"]])
75
76
77 if __name__ == "__main__":
78   testutils.GanetiTestProgram()