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