Remove authorization code from remote API
[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 import tempfile
28 import time
29
30 from ganeti import errors
31 from ganeti.rapi import resources
32 from ganeti.rapi import RESTHTTPServer
33
34
35 class MapperTests(unittest.TestCase):
36   """Tests for remote API URI mapper."""
37
38   def setUp(self):
39     self.map = resources.Mapper()
40
41   def _TestUri(self, uri, result):
42     self.assertEquals(self.map.getController(uri), result)
43
44   def testMapper(self):
45     """Testing resources.Mapper"""
46
47     self._TestUri("/tags", (resources.R_tags, [], {}))
48     self._TestUri("/tag", None)
49
50     self._TestUri('/instances/www.test.com',
51                   (resources.R_instances_name,
52                    ['www.test.com'],
53                    {}))
54
55     self._TestUri('/instances/www.test.com/tags?f=5&f=6&alt=html',
56                   (resources.R_instances_name_tags,
57                    ['www.test.com'],
58                    {'alt': ['html'],
59                     'f': ['5', '6'],
60                    }))
61
62
63 class R_RootTests(unittest.TestCase):
64   """Testing for R_root class."""
65
66   def setUp(self):
67     self.root = resources.R_root(None, None, None)
68     self.root.result = []
69
70   def testGet(self):
71     expected = [
72       {'name': 'info', 'uri': '/info'},
73       {'name': 'instances', 'uri': '/instances'},
74       {'name': 'nodes', 'uri': '/nodes'},
75       {'name': 'os', 'uri': '/os'},
76       {'name': 'tags', 'uri': '/tags'},
77       ]
78     self.root._get()
79     self.assertEquals(self.root.result, expected)
80
81
82 class HttpLogfileTests(unittest.TestCase):
83   """Rests for HttpLogfile class."""
84
85   class FakeRequest:
86     FAKE_ADDRESS = "1.2.3.4"
87
88     def address_string(self):
89       return self.FAKE_ADDRESS
90
91   def setUp(self):
92     self.tmpfile = tempfile.NamedTemporaryFile()
93     self.logfile = RESTHTTPServer.HttpLogfile(self.tmpfile.name)
94
95   def testFormatLogTime(self):
96     self._TestInTimezone(1208646123.0, "Europe/London",
97                          "19/Apr/2008:23:02:03 +0000")
98     self._TestInTimezone(1208646123, "Europe/Zurich",
99                          "19/Apr/2008:23:02:03 +0000")
100     self._TestInTimezone(1208646123, "Australia/Sydney",
101                          "19/Apr/2008:23:02:03 +0000")
102
103   def _TestInTimezone(self, seconds, timezone, expected):
104     """Tests HttpLogfile._FormatLogTime with a specific timezone
105
106     """
107     # Preserve environment
108     old_TZ = os.environ.get("TZ", None)
109     try:
110       os.environ["TZ"] = timezone
111       time.tzset()
112       result = self.logfile._FormatLogTime(seconds)
113     finally:
114       # Restore environment
115       if old_TZ is not None:
116         os.environ["TZ"] = old_TZ
117       elif "TZ" in os.environ:
118         del os.environ["TZ"]
119       time.tzset()
120
121     self.assertEqual(result, expected)
122
123   def testClose(self):
124     self.logfile.Close()
125
126   def testCloseAndWrite(self):
127     request = self.FakeRequest()
128     self.logfile.Close()
129     self.assertRaises(errors.ProgrammerError, self.logfile.LogRequest,
130                       request, "Message")
131
132   def testLogRequest(self):
133     request = self.FakeRequest()
134     self.logfile.LogRequest(request, "This is only a %s", "test")
135     self.logfile.Close()
136
137
138 if __name__ == '__main__':
139   unittest.main()