root / test / ganeti.bdev_unittest.py @ 6b90c22e
History | View | Annotate | Download (5.2 kB)
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 |
from ganeti import errors |
30 |
|
31 |
|
32 |
class TestDRBD8Runner(unittest.TestCase): |
33 |
"""Testing case for DRBD8"""
|
34 |
|
35 |
@staticmethod
|
36 |
def _has_disk(data, dname, mname): |
37 |
"""Check local disk corectness"""
|
38 |
retval = ( |
39 |
"local_dev" in data and |
40 |
data["local_dev"] == dname and |
41 |
"meta_dev" in data and |
42 |
data["meta_dev"] == mname and |
43 |
"meta_index" in data and |
44 |
data["meta_index"] == 0 |
45 |
) |
46 |
return retval
|
47 |
|
48 |
@staticmethod
|
49 |
def _get_contents(name): |
50 |
"""Returns the contents of a file"""
|
51 |
|
52 |
prefix = os.environ.get("srcdir", None) |
53 |
if prefix:
|
54 |
name = prefix + "/" + name
|
55 |
fh = open(name, "r") |
56 |
try:
|
57 |
data = fh.read() |
58 |
finally:
|
59 |
fh.close() |
60 |
return data
|
61 |
|
62 |
|
63 |
@staticmethod
|
64 |
def _has_net(data, local, remote): |
65 |
"""Check network connection parameters"""
|
66 |
retval = ( |
67 |
"local_addr" in data and |
68 |
data["local_addr"] == local and |
69 |
"remote_addr" in data and |
70 |
data["remote_addr"] == remote
|
71 |
) |
72 |
return retval
|
73 |
|
74 |
def testParserCreation(self): |
75 |
"""Test drbdsetup show parser creation"""
|
76 |
bdev.DRBD8._GetShowParser() |
77 |
|
78 |
def testParserBoth(self): |
79 |
"""Test drbdsetup show parser for disk and network"""
|
80 |
data = self._get_contents("data/bdev-both.txt") |
81 |
result = bdev.DRBD8._GetDevInfo(data) |
82 |
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", |
83 |
"/dev/xenvg/test.meta"),
|
84 |
"Wrong local disk info")
|
85 |
self.failUnless(self._has_net(result, ("192.168.1.1", 11000), |
86 |
("192.168.1.2", 11000)), |
87 |
"Wrong network info")
|
88 |
|
89 |
def testParserNet(self): |
90 |
"""Test drbdsetup show parser for disk and network"""
|
91 |
data = self._get_contents("data/bdev-net.txt") |
92 |
result = bdev.DRBD8._GetDevInfo(data) |
93 |
self.failUnless(("local_dev" not in result and |
94 |
"meta_dev" not in result and |
95 |
"meta_index" not in result), |
96 |
"Should not find local disk info")
|
97 |
self.failUnless(self._has_net(result, ("192.168.1.1", 11002), |
98 |
("192.168.1.2", 11002)), |
99 |
"Wrong network info")
|
100 |
|
101 |
def testParserDisk(self): |
102 |
"""Test drbdsetup show parser for disk and network"""
|
103 |
data = self._get_contents("data/bdev-disk.txt") |
104 |
result = bdev.DRBD8._GetDevInfo(data) |
105 |
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", |
106 |
"/dev/xenvg/test.meta"),
|
107 |
"Wrong local disk info")
|
108 |
self.failUnless(("local_addr" not in result and |
109 |
"remote_addr" not in result), |
110 |
"Should not find network info")
|
111 |
|
112 |
|
113 |
class TestDRBD8Status(unittest.TestCase): |
114 |
"""Testing case for DRBD8 /proc status"""
|
115 |
|
116 |
def setUp(self): |
117 |
"""Read in txt data"""
|
118 |
self.proc_data = bdev.DRBD8._GetProcData(filename="data/proc_drbd8.txt") |
119 |
self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data) |
120 |
|
121 |
def testMinorNotFound(self): |
122 |
"""Test not-found-minor in /proc"""
|
123 |
self.failUnless(9 not in self.mass_data) |
124 |
|
125 |
def testLineNotMatch(self): |
126 |
"""Test wrong line passed to DRBD8Status"""
|
127 |
self.assertRaises(errors.BlockDeviceError, bdev.DRBD8Status, "foo") |
128 |
|
129 |
def testMinor0(self): |
130 |
"""Test connected, primary device"""
|
131 |
stats = bdev.DRBD8Status(self.mass_data[0]) |
132 |
self.failUnless(stats.is_connected and stats.is_primary and |
133 |
stats.peer_secondary and stats.is_disk_uptodate)
|
134 |
|
135 |
def testMinor1(self): |
136 |
"""Test connected, secondary device"""
|
137 |
stats = bdev.DRBD8Status(self.mass_data[1]) |
138 |
self.failUnless(stats.is_connected and stats.is_secondary and |
139 |
stats.peer_primary and stats.is_disk_uptodate)
|
140 |
|
141 |
def testMinor4(self): |
142 |
"""Test WFconn device"""
|
143 |
stats = bdev.DRBD8Status(self.mass_data[4]) |
144 |
self.failUnless(stats.is_wfconn and stats.is_primary and |
145 |
stats.rrole == 'Unknown' and |
146 |
stats.is_disk_uptodate) |
147 |
|
148 |
def testMinor6(self): |
149 |
"""Test diskless device"""
|
150 |
stats = bdev.DRBD8Status(self.mass_data[6]) |
151 |
self.failUnless(stats.is_connected and stats.is_secondary and |
152 |
stats.peer_primary and stats.is_diskless)
|
153 |
|
154 |
def testMinor8(self): |
155 |
"""Test standalone device"""
|
156 |
stats = bdev.DRBD8Status(self.mass_data[8]) |
157 |
self.failUnless(stats.is_standalone and |
158 |
stats.rrole == 'Unknown' and |
159 |
stats.is_disk_uptodate) |
160 |
|
161 |
if __name__ == '__main__': |
162 |
unittest.main() |