4 # Copyright (C) 2007, 2008 Google Inc.
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.
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.
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
22 """Script for unittesting the rapi.resources module"""
30 from ganeti import errors
31 from ganeti.rapi import resources
32 from ganeti.rapi import RESTHTTPServer
35 class MapperTests(unittest.TestCase):
36 """Tests for remote API URI mapper."""
39 self.map = resources.Mapper()
41 def _TestUri(self, uri, result):
42 self.assertEquals(self.map.getController(uri), result)
45 """Testing resources.Mapper"""
47 self._TestUri("/tags", (resources.R_tags, [], {}))
48 self._TestUri("/tag", None)
50 self._TestUri('/instances/www.test.com',
51 (resources.R_instances_name,
55 self._TestUri('/instances/www.test.com/tags?f=5&f=6&alt=html',
56 (resources.R_instances_name_tags,
63 class R_RootTests(unittest.TestCase):
64 """Testing for R_root class."""
67 self.root = resources.R_root(None, None, None)
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'},
79 self.assertEquals(self.root.result, expected)
82 class HttpLogfileTests(unittest.TestCase):
83 """Rests for HttpLogfile class."""
86 FAKE_ADDRESS = "1.2.3.4"
88 def address_string(self):
89 return self.FAKE_ADDRESS
92 self.tmpfile = tempfile.NamedTemporaryFile()
93 self.logfile = RESTHTTPServer.HttpLogfile(self.tmpfile.name)
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")
103 def _TestInTimezone(self, seconds, timezone, expected):
104 """Tests HttpLogfile._FormatLogTime with a specific timezone
107 # Preserve environment
108 old_TZ = os.environ.get("TZ", None)
110 os.environ["TZ"] = timezone
112 result = self.logfile._FormatLogTime(seconds)
114 # Restore environment
115 if old_TZ is not None:
116 os.environ["TZ"] = old_TZ
117 elif "TZ" in os.environ:
121 self.assertEqual(result, expected)
126 def testCloseAndWrite(self):
127 request = self.FakeRequest()
129 self.assertRaises(errors.ProgrammerError, self.logfile.LogRequest,
132 def testLogRequest(self):
133 request = self.FakeRequest()
134 self.logfile.LogRequest(request, "This is only a %s", "test")
138 if __name__ == '__main__':