Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.bdev_unittest.py @ b2dabfd6

History | View | Annotate | Download (3.5 kB)

1 3840729d Iustin Pop
#!/usr/bin/python
2 3840729d Iustin Pop
#
3 3840729d Iustin Pop
4 3840729d Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 3840729d Iustin Pop
#
6 3840729d Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 3840729d Iustin Pop
# it under the terms of the GNU General Public License as published by
8 3840729d Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 3840729d Iustin Pop
# (at your option) any later version.
10 3840729d Iustin Pop
#
11 3840729d Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 3840729d Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 3840729d Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 3840729d Iustin Pop
# General Public License for more details.
15 3840729d Iustin Pop
#
16 3840729d Iustin Pop
# You should have received a copy of the GNU General Public License
17 3840729d Iustin Pop
# along with this program; if not, write to the Free Software
18 3840729d Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 3840729d Iustin Pop
# 02110-1301, USA.
20 3840729d Iustin Pop
21 3840729d Iustin Pop
22 3840729d Iustin Pop
"""Script for unittesting the bdev module"""
23 3840729d Iustin Pop
24 3840729d Iustin Pop
25 6634816b Iustin Pop
import os
26 3840729d Iustin Pop
import unittest
27 3840729d Iustin Pop
28 3840729d Iustin Pop
from ganeti import bdev
29 3840729d Iustin Pop
30 3840729d Iustin Pop
31 3840729d Iustin Pop
class TestDRBD8Runner(unittest.TestCase):
32 3840729d Iustin Pop
  """Testing case for DRBD8"""
33 3840729d Iustin Pop
34 3840729d Iustin Pop
  @staticmethod
35 3840729d Iustin Pop
  def _has_disk(data, dname, mname):
36 3840729d Iustin Pop
    """Check local disk corectness"""
37 3840729d Iustin Pop
    retval = (
38 3840729d Iustin Pop
      "local_dev" in data and
39 3840729d Iustin Pop
      data["local_dev"] == dname and
40 3840729d Iustin Pop
      "meta_dev" in data and
41 3840729d Iustin Pop
      data["meta_dev"] == mname and
42 3840729d Iustin Pop
      "meta_index" in data and
43 3840729d Iustin Pop
      data["meta_index"] == 0
44 3840729d Iustin Pop
      )
45 3840729d Iustin Pop
    return retval
46 3840729d Iustin Pop
47 3840729d Iustin Pop
  @staticmethod
48 6634816b Iustin Pop
  def _get_contents(name):
49 6634816b Iustin Pop
    """Returns the contents of a file"""
50 6634816b Iustin Pop
51 6634816b Iustin Pop
    prefix = os.environ.get("srcdir", None)
52 6634816b Iustin Pop
    if prefix:
53 6634816b Iustin Pop
      name = prefix + "/" + name
54 6634816b Iustin Pop
    fh = open(name, "r")
55 6634816b Iustin Pop
    try:
56 6634816b Iustin Pop
      data = fh.read()
57 6634816b Iustin Pop
    finally:
58 6634816b Iustin Pop
      fh.close()
59 6634816b Iustin Pop
    return data
60 6634816b Iustin Pop
61 6634816b Iustin Pop
62 6634816b Iustin Pop
  @staticmethod
63 3840729d Iustin Pop
  def _has_net(data, local, remote):
64 3840729d Iustin Pop
    """Check network connection parameters"""
65 3840729d Iustin Pop
    retval = (
66 3840729d Iustin Pop
      "local_addr" in data and
67 3840729d Iustin Pop
      data["local_addr"] == local and
68 3840729d Iustin Pop
      "remote_addr" in data and
69 3840729d Iustin Pop
      data["remote_addr"] == remote
70 3840729d Iustin Pop
      )
71 3840729d Iustin Pop
    return retval
72 3840729d Iustin Pop
73 3840729d Iustin Pop
  def testParserCreation(self):
74 3840729d Iustin Pop
    """Test drbdsetup show parser creation"""
75 3840729d Iustin Pop
    bdev.DRBD8._GetShowParser()
76 3840729d Iustin Pop
77 3840729d Iustin Pop
  def testParserBoth(self):
78 3840729d Iustin Pop
    """Test drbdsetup show parser for disk and network"""
79 6634816b Iustin Pop
    data = self._get_contents("data/bdev-both.txt")
80 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
81 3840729d Iustin Pop
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
82 3840729d Iustin Pop
                                   "/dev/xenvg/test.meta"),
83 3840729d Iustin Pop
                    "Wrong local disk info")
84 3840729d Iustin Pop
    self.failUnless(self._has_net(result, ("192.168.1.1", 11000),
85 3840729d Iustin Pop
                                  ("192.168.1.2", 11000)),
86 3840729d Iustin Pop
                    "Wrong network info")
87 3840729d Iustin Pop
88 3840729d Iustin Pop
  def testParserNet(self):
89 3840729d Iustin Pop
    """Test drbdsetup show parser for disk and network"""
90 6634816b Iustin Pop
    data = self._get_contents("data/bdev-net.txt")
91 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
92 3840729d Iustin Pop
    self.failUnless(("local_dev" not in result and
93 3840729d Iustin Pop
                     "meta_dev" not in result and
94 3840729d Iustin Pop
                     "meta_index" not in result),
95 3840729d Iustin Pop
                    "Should not find local disk info")
96 3840729d Iustin Pop
    self.failUnless(self._has_net(result, ("192.168.1.1", 11002),
97 3840729d Iustin Pop
                                  ("192.168.1.2", 11002)),
98 3840729d Iustin Pop
                    "Wrong network info")
99 3840729d Iustin Pop
100 3840729d Iustin Pop
  def testParserDisk(self):
101 3840729d Iustin Pop
    """Test drbdsetup show parser for disk and network"""
102 6634816b Iustin Pop
    data = self._get_contents("data/bdev-disk.txt")
103 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
104 3840729d Iustin Pop
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
105 3840729d Iustin Pop
                                   "/dev/xenvg/test.meta"),
106 3840729d Iustin Pop
                    "Wrong local disk info")
107 3840729d Iustin Pop
    self.failUnless(("local_addr" not in result and
108 3840729d Iustin Pop
                     "remote_addr" not in result),
109 3840729d Iustin Pop
                    "Should not find network info")
110 3840729d Iustin Pop
111 3840729d Iustin Pop
if __name__ == '__main__':
112 3840729d Iustin Pop
  unittest.main()