Optimize rapi
[ganeti-local] / test / ganeti.rapi.resources_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2007, 2008 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 unittesting the rapi.resources module"""
23
24
25 import os
26 import unittest
27
28 from ganeti.rapi import resources
29
30
31 class MapperTests(unittest.TestCase):
32   """"""
33   def setUp(self):
34     self.map = resources.Mapper()
35
36   def _TestUri(self, uri, result):
37     self.assertEquals(self.map.getController(uri), result)
38
39   def testMapper(self):
40     """Testing resources.Mapper"""
41
42     self._TestUri("/tags", (resources.R_tags, [], {}))
43     self._TestUri("/tag", None)
44
45     self._TestUri('/instances/www.test.com',
46                   (resources.R_instances_name,
47                    ['www.test.com'],
48                    {}))
49
50     self._TestUri('/instances/www.test.com/tags?f=5&f=6&alt=html',
51                   (resources.R_instances_name_tags,
52                    ['www.test.com'],
53                    {'alt': ['html'],
54                     'f': ['5', '6'],
55                    }))
56
57
58 class R_RootTests(unittest.TestCase):
59   """Testing for R_root class."""
60
61   def setUp(self):
62     self.root = resources.R_root(None, None, None)
63     self.root.result = []
64
65   def testGet(self):
66     expected = [
67       {'name': 'info', 'uri': '/info'},
68       {'name': 'instances', 'uri': '/instances'},
69       {'name': 'nodes', 'uri': '/nodes'},
70       {'name': 'os', 'uri': '/os'},
71       {'name': 'status', 'uri': '/status'},
72       {'name': 'tags', 'uri': '/tags'},
73       ]
74     self.root._get()
75     self.assertEquals(self.root.result, expected)
76
77
78 if __name__ == '__main__':
79   unittest.main()