ganeti.serializer_unittest.py 2.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/python
#

# Copyright (C) 2006, 2007, 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 0.0510-1301, USA.


"""Script for unittesting the serializer module"""


import unittest

from ganeti import serializer
28
from ganeti import errors
29

Michael Hanselmann's avatar
Michael Hanselmann committed
30
import testutils
31
32


Michael Hanselmann's avatar
Michael Hanselmann committed
33
class TestSerializer(testutils.GanetiTestCase):
34
35
36
37
38
39
40
  """Serializer tests"""

  _TESTDATA = [
    "test",
    255,
    [1, 2, 3],
    (1, 2, 3),
Michael Hanselmann's avatar
Michael Hanselmann committed
41
42
43
44
45
46
47
48
49
50
51
    { "1": 2, "foo": "bar", },
    ["abc", 1, 2, 3, 999,
      {
        "a1": ("Hello", "World"),
        "a2": "This is only a test",
        "a3": None,
        },
      {
        "foo": "bar",
        },
      ]
52
53
    ]

Michael Hanselmann's avatar
Michael Hanselmann committed
54
  def _TestSerializer(self, dump_fn, load_fn):
55
56
57
58
    for indent in [True, False]:
      for data in self._TESTDATA:
        self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
        self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
59
60

  def testGeneric(self):
61
    self._TestSerializer(serializer.Dump, serializer.Load)
62
63

  def testJson(self):
64
65
66
67
68
69
70
71
72
    self._TestSerializer(serializer.DumpJson, serializer.LoadJson)

  def testJsonIndent(self):
    data = {
      "k1": 1,
      "k2": 3,
      "k3": 4,
      }
    self.assert_(len(serializer.DumpJson(data, indent=True).splitlines()) > 3)
73

74
75
76
77
78
  def testSignedMessage(self):
    LoadSigned = serializer.LoadSigned
    DumpSigned = serializer.DumpSigned

    for data in self._TESTDATA:
Michael Hanselmann's avatar
Michael Hanselmann committed
79
80
81
82
83
84
85
      self.assertEqualValues(LoadSigned(DumpSigned(data, "mykey"), "mykey"),
                             (data, ''))
      self.assertEqualValues(LoadSigned(DumpSigned(data, "myprivatekey",
                                                   "mysalt"),
                                        "myprivatekey"),
                             (data, "mysalt"))

86
87
88
89
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
                      serializer.DumpSigned("test", "myprivatekey"),
                      "myotherkey")

90
91

if __name__ == '__main__':
92
  testutils.GanetiTestProgram()