Fix ‘make distcheck’ breakage introduced in r455
[ganeti-local] / test / ganeti.bdev_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007 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 bdev module"""
23
24
25 import os
26 import unittest
27
28 from ganeti import bdev
29
30
31 class TestDRBD8Runner(unittest.TestCase):
32   """Testing case for DRBD8"""
33
34   @staticmethod
35   def _has_disk(data, dname, mname):
36     """Check local disk corectness"""
37     retval = (
38       "local_dev" in data and
39       data["local_dev"] == dname and
40       "meta_dev" in data and
41       data["meta_dev"] == mname and
42       "meta_index" in data and
43       data["meta_index"] == 0
44       )
45     return retval
46
47   @staticmethod
48   def _get_contents(name):
49     """Returns the contents of a file"""
50
51     prefix = os.environ.get("srcdir", None)
52     if prefix:
53       name = prefix + "/" + name
54     fh = open(name, "r")
55     try:
56       data = fh.read()
57     finally:
58       fh.close()
59     return data
60
61
62   @staticmethod
63   def _has_net(data, local, remote):
64     """Check network connection parameters"""
65     retval = (
66       "local_addr" in data and
67       data["local_addr"] == local and
68       "remote_addr" in data and
69       data["remote_addr"] == remote
70       )
71     return retval
72
73   def testParserCreation(self):
74     """Test drbdsetup show parser creation"""
75     bdev.DRBD8._GetShowParser()
76
77   def testParserBoth(self):
78     """Test drbdsetup show parser for disk and network"""
79     data = self._get_contents("data/bdev-both.txt")
80     result = bdev.DRBD8._GetDevInfo(data)
81     self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
82                                    "/dev/xenvg/test.meta"),
83                     "Wrong local disk info")
84     self.failUnless(self._has_net(result, ("192.168.1.1", 11000),
85                                   ("192.168.1.2", 11000)),
86                     "Wrong network info")
87
88   def testParserNet(self):
89     """Test drbdsetup show parser for disk and network"""
90     data = self._get_contents("data/bdev-net.txt")
91     result = bdev.DRBD8._GetDevInfo(data)
92     self.failUnless(("local_dev" not in result and
93                      "meta_dev" not in result and
94                      "meta_index" not in result),
95                     "Should not find local disk info")
96     self.failUnless(self._has_net(result, ("192.168.1.1", 11002),
97                                   ("192.168.1.2", 11002)),
98                     "Wrong network info")
99
100   def testParserDisk(self):
101     """Test drbdsetup show parser for disk and network"""
102     data = self._get_contents("data/bdev-disk.txt")
103     result = bdev.DRBD8._GetDevInfo(data)
104     self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
105                                    "/dev/xenvg/test.meta"),
106                     "Wrong local disk info")
107     self.failUnless(("local_addr" not in result and
108                      "remote_addr" not in result),
109                     "Should not find network info")
110
111 if __name__ == '__main__':
112   unittest.main()