Revision e820685c test/ganeti.http_unittest.py
b/test/ganeti.http_unittest.py | ||
---|---|---|
30 | 30 |
from ganeti import http |
31 | 31 |
|
32 | 32 |
|
33 |
class HttpLogfileTests(unittest.TestCase): |
|
34 |
"""Tests for ApacheLogfile class.""" |
|
35 |
|
|
36 |
class FakeRequest: |
|
37 |
FAKE_ADDRESS = "1.2.3.4" |
|
38 |
|
|
39 |
def address_string(self): |
|
40 |
return self.FAKE_ADDRESS |
|
41 |
|
|
42 |
def setUp(self): |
|
43 |
self.tmpfile = tempfile.NamedTemporaryFile() |
|
44 |
self.logfile = http.ApacheLogfile(self.tmpfile) |
|
45 |
|
|
46 |
def tearDown(self): |
|
47 |
self.tmpfile.close() |
|
48 |
|
|
49 |
def testFormatLogTime(self): |
|
50 |
self._TestInTimezone(1208646123.0, "Europe/London", |
|
51 |
"19/Apr/2008:23:02:03 +0000") |
|
52 |
self._TestInTimezone(1208646123, "Europe/Zurich", |
|
53 |
"19/Apr/2008:23:02:03 +0000") |
|
54 |
self._TestInTimezone(1208646123, "Australia/Sydney", |
|
55 |
"19/Apr/2008:23:02:03 +0000") |
|
56 |
|
|
57 |
def _TestInTimezone(self, seconds, timezone, expected): |
|
58 |
"""Tests HttpLogfile._FormatLogTime with a specific timezone |
|
59 |
|
|
60 |
""" |
|
61 |
# Preserve environment |
|
62 |
old_TZ = os.environ.get("TZ", None) |
|
63 |
try: |
|
64 |
os.environ["TZ"] = timezone |
|
65 |
time.tzset() |
|
66 |
result = self.logfile._FormatLogTime(seconds) |
|
67 |
finally: |
|
68 |
# Restore environment |
|
69 |
if old_TZ is not None: |
|
70 |
os.environ["TZ"] = old_TZ |
|
71 |
elif "TZ" in os.environ: |
|
72 |
del os.environ["TZ"] |
|
73 |
time.tzset() |
|
74 |
|
|
75 |
self.assertEqual(result, expected) |
|
76 |
|
|
77 |
|
|
78 |
def testLogRequest(self): |
|
79 |
request = self.FakeRequest() |
|
80 |
self.logfile.LogRequest(request, "This is only a %s", "test") |
|
33 |
# TODO: Write unittests |
|
81 | 34 |
|
82 | 35 |
|
83 | 36 |
if __name__ == '__main__': |
Also available in: Unified diff