ganeti.rapi.resources_unittest.py 2.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python
#

# Copyright (C) 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
# 02110-1301, USA.


22
"""Script for unittesting the RAPI resources module"""
23
24
25
26
27
28


import unittest
import tempfile

from ganeti import errors
29
30
from ganeti import http

31
32
from ganeti.rapi import connector
from ganeti.rapi import rlib1
33
34
35
36
37
38


class MapperTests(unittest.TestCase):
  """Tests for remote API URI mapper."""

  def setUp(self):
39
    self.map = connector.Mapper()
40
41
42
43
44

  def _TestUri(self, uri, result):
    self.assertEquals(self.map.getController(uri), result)

  def _TestFailingUri(self, uri):
45
    self.failUnlessRaises(http.HTTPNotFound, self.map.getController, uri)
46
47

  def testMapper(self):
48
    """Testing Mapper"""
49

50
51
    self._TestUri("/tags", (rlib1.R_tags, [], {}))
    self._TestUri("/instances", (rlib1.R_instances, [], {}))
52
53

    self._TestUri('/instances/www.test.com',
54
                  (rlib1.R_instances_name,
55
56
57
58
                   ['www.test.com'],
                   {}))

    self._TestUri('/instances/www.test.com/tags?f=5&f=6&alt=html',
59
                  (rlib1.R_instances_name_tags,
60
61
62
63
64
65
66
67
68
69
70
71
72
                   ['www.test.com'],
                   {'alt': ['html'],
                    'f': ['5', '6'],
                   }))

    self._TestFailingUri("/tag")
    self._TestFailingUri("/instances/does/not/exist")


class R_RootTests(unittest.TestCase):
  """Testing for R_root class."""

  def setUp(self):
73
    self.root = connector.R_root(None, None, None)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

  def testGet(self):
    expected = [
      {'name': 'info', 'uri': '/info'},
      {'name': 'instances', 'uri': '/instances'},
      {'name': 'nodes', 'uri': '/nodes'},
      {'name': 'os', 'uri': '/os'},
      {'name': 'tags', 'uri': '/tags'},
      {'name': 'version', 'uri': '/version'},
      ]
    self.assertEquals(self.root.GET(), expected)


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