Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.http_unittest.py @ fa10bdc5

History | View | Annotate | Download (2.3 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 http module"""
23

    
24

    
25
import os
26
import unittest
27
import tempfile
28
import time
29

    
30
from ganeti import http
31

    
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")
81

    
82

    
83
if __name__ == '__main__':
84
  unittest.main()