Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.block.drbd_unittest.py @ 74db37c0

History | View | Annotate | Download (15.9 kB)

1 873878b9 Thomas Thrainer
#!/usr/bin/python
2 873878b9 Thomas Thrainer
#
3 873878b9 Thomas Thrainer
4 873878b9 Thomas Thrainer
# Copyright (C) 2006, 2007, 2010, 2012, 2013 Google Inc.
5 873878b9 Thomas Thrainer
#
6 873878b9 Thomas Thrainer
# This program is free software; you can redistribute it and/or modify
7 873878b9 Thomas Thrainer
# it under the terms of the GNU General Public License as published by
8 873878b9 Thomas Thrainer
# the Free Software Foundation; either version 2 of the License, or
9 873878b9 Thomas Thrainer
# (at your option) any later version.
10 873878b9 Thomas Thrainer
#
11 873878b9 Thomas Thrainer
# This program is distributed in the hope that it will be useful, but
12 873878b9 Thomas Thrainer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 873878b9 Thomas Thrainer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 873878b9 Thomas Thrainer
# General Public License for more details.
15 873878b9 Thomas Thrainer
#
16 873878b9 Thomas Thrainer
# You should have received a copy of the GNU General Public License
17 873878b9 Thomas Thrainer
# along with this program; if not, write to the Free Software
18 873878b9 Thomas Thrainer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 873878b9 Thomas Thrainer
# 02110-1301, USA.
20 873878b9 Thomas Thrainer
21 873878b9 Thomas Thrainer
22 873878b9 Thomas Thrainer
"""Script for unittesting the drbd module"""
23 873878b9 Thomas Thrainer
24 873878b9 Thomas Thrainer
25 873878b9 Thomas Thrainer
import os
26 873878b9 Thomas Thrainer
27 873878b9 Thomas Thrainer
from ganeti import constants
28 873878b9 Thomas Thrainer
from ganeti import errors
29 873878b9 Thomas Thrainer
from ganeti.block import drbd
30 873878b9 Thomas Thrainer
from ganeti.block import drbd_info
31 873878b9 Thomas Thrainer
from ganeti.block import drbd_cmdgen
32 873878b9 Thomas Thrainer
33 873878b9 Thomas Thrainer
import testutils
34 873878b9 Thomas Thrainer
35 873878b9 Thomas Thrainer
36 873878b9 Thomas Thrainer
class TestDRBD8(testutils.GanetiTestCase):
37 873878b9 Thomas Thrainer
  def testGetVersion(self):
38 873878b9 Thomas Thrainer
    data = [
39 873878b9 Thomas Thrainer
      ["version: 8.0.12 (api:76/proto:86-91)"],
40 873878b9 Thomas Thrainer
      ["version: 8.2.7 (api:88/proto:0-100)"],
41 873878b9 Thomas Thrainer
      ["version: 8.3.7.49 (api:188/proto:13-191)"],
42 873878b9 Thomas Thrainer
    ]
43 873878b9 Thomas Thrainer
    result = [
44 873878b9 Thomas Thrainer
      {
45 873878b9 Thomas Thrainer
        "k_major": 8,
46 873878b9 Thomas Thrainer
        "k_minor": 0,
47 873878b9 Thomas Thrainer
        "k_point": 12,
48 873878b9 Thomas Thrainer
        "api": 76,
49 873878b9 Thomas Thrainer
        "proto": 86,
50 873878b9 Thomas Thrainer
        "proto2": "91",
51 873878b9 Thomas Thrainer
      },
52 873878b9 Thomas Thrainer
      {
53 873878b9 Thomas Thrainer
        "k_major": 8,
54 873878b9 Thomas Thrainer
        "k_minor": 2,
55 873878b9 Thomas Thrainer
        "k_point": 7,
56 873878b9 Thomas Thrainer
        "api": 88,
57 873878b9 Thomas Thrainer
        "proto": 0,
58 873878b9 Thomas Thrainer
        "proto2": "100",
59 873878b9 Thomas Thrainer
      },
60 873878b9 Thomas Thrainer
      {
61 873878b9 Thomas Thrainer
        "k_major": 8,
62 873878b9 Thomas Thrainer
        "k_minor": 3,
63 873878b9 Thomas Thrainer
        "k_point": 7,
64 873878b9 Thomas Thrainer
        "api": 188,
65 873878b9 Thomas Thrainer
        "proto": 13,
66 873878b9 Thomas Thrainer
        "proto2": "191",
67 873878b9 Thomas Thrainer
      }
68 873878b9 Thomas Thrainer
    ]
69 873878b9 Thomas Thrainer
    for d, r in zip(data, result):
70 873878b9 Thomas Thrainer
      info = drbd.DRBD8Info.CreateFromLines(d)
71 873878b9 Thomas Thrainer
      self.assertEqual(info.GetVersion(), r)
72 873878b9 Thomas Thrainer
73 873878b9 Thomas Thrainer
74 873878b9 Thomas Thrainer
class TestDRBD8Runner(testutils.GanetiTestCase):
75 873878b9 Thomas Thrainer
  """Testing case for drbd.DRBD8"""
76 873878b9 Thomas Thrainer
77 873878b9 Thomas Thrainer
  @staticmethod
78 873878b9 Thomas Thrainer
  def _has_disk(data, dname, mname):
79 873878b9 Thomas Thrainer
    """Check local disk corectness"""
80 873878b9 Thomas Thrainer
    retval = (
81 873878b9 Thomas Thrainer
      "local_dev" in data and
82 873878b9 Thomas Thrainer
      data["local_dev"] == dname and
83 873878b9 Thomas Thrainer
      "meta_dev" in data and
84 873878b9 Thomas Thrainer
      data["meta_dev"] == mname and
85 873878b9 Thomas Thrainer
      "meta_index" in data and
86 873878b9 Thomas Thrainer
      data["meta_index"] == 0
87 873878b9 Thomas Thrainer
      )
88 873878b9 Thomas Thrainer
    return retval
89 873878b9 Thomas Thrainer
90 873878b9 Thomas Thrainer
  @staticmethod
91 873878b9 Thomas Thrainer
  def _has_net(data, local, remote):
92 873878b9 Thomas Thrainer
    """Check network connection parameters"""
93 873878b9 Thomas Thrainer
    retval = (
94 873878b9 Thomas Thrainer
      "local_addr" in data and
95 873878b9 Thomas Thrainer
      data["local_addr"] == local and
96 873878b9 Thomas Thrainer
      "remote_addr" in data and
97 873878b9 Thomas Thrainer
      data["remote_addr"] == remote
98 873878b9 Thomas Thrainer
      )
99 873878b9 Thomas Thrainer
    return retval
100 873878b9 Thomas Thrainer
101 873878b9 Thomas Thrainer
  def testParser83Creation(self):
102 873878b9 Thomas Thrainer
    """Test drbdsetup show parser creation"""
103 873878b9 Thomas Thrainer
    drbd_info.DRBD83ShowInfo._GetShowParser()
104 873878b9 Thomas Thrainer
105 873878b9 Thomas Thrainer
  def testParser84Creation(self):
106 873878b9 Thomas Thrainer
    """Test drbdsetup show parser creation"""
107 873878b9 Thomas Thrainer
    drbd_info.DRBD84ShowInfo._GetShowParser()
108 873878b9 Thomas Thrainer
109 873878b9 Thomas Thrainer
  def testParser80(self):
110 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for disk and network version 8.0"""
111 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-8.0.txt")
112 873878b9 Thomas Thrainer
    result = drbd_info.DRBD83ShowInfo.GetDevInfo(data)
113 873878b9 Thomas Thrainer
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
114 873878b9 Thomas Thrainer
                                   "/dev/xenvg/test.meta"),
115 873878b9 Thomas Thrainer
                    "Wrong local disk info")
116 873878b9 Thomas Thrainer
    self.failUnless(self._has_net(result, ("192.0.2.1", 11000),
117 873878b9 Thomas Thrainer
                                  ("192.0.2.2", 11000)),
118 873878b9 Thomas Thrainer
                    "Wrong network info (8.0.x)")
119 873878b9 Thomas Thrainer
120 873878b9 Thomas Thrainer
  def testParser83(self):
121 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for disk and network version 8.3"""
122 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-8.3.txt")
123 873878b9 Thomas Thrainer
    result = drbd_info.DRBD83ShowInfo.GetDevInfo(data)
124 873878b9 Thomas Thrainer
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
125 873878b9 Thomas Thrainer
                                   "/dev/xenvg/test.meta"),
126 873878b9 Thomas Thrainer
                    "Wrong local disk info")
127 873878b9 Thomas Thrainer
    self.failUnless(self._has_net(result, ("192.0.2.1", 11000),
128 873878b9 Thomas Thrainer
                                  ("192.0.2.2", 11000)),
129 873878b9 Thomas Thrainer
                    "Wrong network info (8.3.x)")
130 873878b9 Thomas Thrainer
131 873878b9 Thomas Thrainer
  def testParser84(self):
132 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for disk and network version 8.4"""
133 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-8.4.txt")
134 873878b9 Thomas Thrainer
    result = drbd_info.DRBD84ShowInfo.GetDevInfo(data)
135 873878b9 Thomas Thrainer
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
136 873878b9 Thomas Thrainer
                                   "/dev/xenvg/test.meta"),
137 873878b9 Thomas Thrainer
                    "Wrong local disk info")
138 873878b9 Thomas Thrainer
    self.failUnless(self._has_net(result, ("192.0.2.1", 11000),
139 873878b9 Thomas Thrainer
                                  ("192.0.2.2", 11000)),
140 873878b9 Thomas Thrainer
                    "Wrong network info (8.4.x)")
141 873878b9 Thomas Thrainer
142 873878b9 Thomas Thrainer
  def testParserNetIP4(self):
143 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for IPv4 network"""
144 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-net-ip4.txt")
145 873878b9 Thomas Thrainer
    result = drbd_info.DRBD83ShowInfo.GetDevInfo(data)
146 873878b9 Thomas Thrainer
    self.failUnless(("local_dev" not in result and
147 873878b9 Thomas Thrainer
                     "meta_dev" not in result and
148 873878b9 Thomas Thrainer
                     "meta_index" not in result),
149 873878b9 Thomas Thrainer
                    "Should not find local disk info")
150 873878b9 Thomas Thrainer
    self.failUnless(self._has_net(result, ("192.0.2.1", 11002),
151 873878b9 Thomas Thrainer
                                  ("192.0.2.2", 11002)),
152 873878b9 Thomas Thrainer
                    "Wrong network info (IPv4)")
153 873878b9 Thomas Thrainer
154 873878b9 Thomas Thrainer
  def testParserNetIP6(self):
155 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for IPv6 network"""
156 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-net-ip6.txt")
157 873878b9 Thomas Thrainer
    result = drbd_info.DRBD83ShowInfo.GetDevInfo(data)
158 873878b9 Thomas Thrainer
    self.failUnless(("local_dev" not in result and
159 873878b9 Thomas Thrainer
                     "meta_dev" not in result and
160 873878b9 Thomas Thrainer
                     "meta_index" not in result),
161 873878b9 Thomas Thrainer
                    "Should not find local disk info")
162 873878b9 Thomas Thrainer
    self.failUnless(self._has_net(result, ("2001:db8:65::1", 11048),
163 873878b9 Thomas Thrainer
                                  ("2001:db8:66::1", 11048)),
164 873878b9 Thomas Thrainer
                    "Wrong network info (IPv6)")
165 873878b9 Thomas Thrainer
166 873878b9 Thomas Thrainer
  def testParserDisk(self):
167 873878b9 Thomas Thrainer
    """Test drbdsetup show parser for disk"""
168 873878b9 Thomas Thrainer
    data = testutils.ReadTestData("bdev-drbd-disk.txt")
169 873878b9 Thomas Thrainer
    result = drbd_info.DRBD83ShowInfo.GetDevInfo(data)
170 873878b9 Thomas Thrainer
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
171 873878b9 Thomas Thrainer
                                   "/dev/xenvg/test.meta"),
172 873878b9 Thomas Thrainer
                    "Wrong local disk info")
173 873878b9 Thomas Thrainer
    self.failUnless(("local_addr" not in result and
174 873878b9 Thomas Thrainer
                     "remote_addr" not in result),
175 873878b9 Thomas Thrainer
                    "Should not find network info")
176 873878b9 Thomas Thrainer
177 873878b9 Thomas Thrainer
  def testBarriersOptions(self):
178 873878b9 Thomas Thrainer
    """Test class method that generates drbdsetup options for disk barriers"""
179 873878b9 Thomas Thrainer
    # Tests that should fail because of wrong version/options combinations
180 873878b9 Thomas Thrainer
    should_fail = [
181 873878b9 Thomas Thrainer
      (8, 0, 12, "bfd", True),
182 873878b9 Thomas Thrainer
      (8, 0, 12, "fd", False),
183 873878b9 Thomas Thrainer
      (8, 0, 12, "b", True),
184 873878b9 Thomas Thrainer
      (8, 2, 7, "bfd", True),
185 873878b9 Thomas Thrainer
      (8, 2, 7, "b", True)
186 873878b9 Thomas Thrainer
    ]
187 873878b9 Thomas Thrainer
188 873878b9 Thomas Thrainer
    for vmaj, vmin, vrel, opts, meta in should_fail:
189 873878b9 Thomas Thrainer
      self.assertRaises(errors.BlockDeviceError,
190 873878b9 Thomas Thrainer
                        drbd_cmdgen.DRBD83CmdGenerator._ComputeDiskBarrierArgs,
191 873878b9 Thomas Thrainer
                        vmaj, vmin, vrel, opts, meta)
192 873878b9 Thomas Thrainer
193 873878b9 Thomas Thrainer
    # get the valid options from the frozenset(frozenset()) in constants.
194 873878b9 Thomas Thrainer
    valid_options = [list(x)[0] for x in constants.DRBD_VALID_BARRIER_OPT]
195 873878b9 Thomas Thrainer
196 873878b9 Thomas Thrainer
    # Versions that do not support anything
197 873878b9 Thomas Thrainer
    for vmaj, vmin, vrel in ((8, 0, 0), (8, 0, 11), (8, 2, 6)):
198 873878b9 Thomas Thrainer
      for opts in valid_options:
199 873878b9 Thomas Thrainer
        self.assertRaises(
200 873878b9 Thomas Thrainer
          errors.BlockDeviceError,
201 873878b9 Thomas Thrainer
          drbd_cmdgen.DRBD83CmdGenerator._ComputeDiskBarrierArgs,
202 873878b9 Thomas Thrainer
          vmaj, vmin, vrel, opts, True)
203 873878b9 Thomas Thrainer
204 873878b9 Thomas Thrainer
    # Versions with partial support (testing only options that are supported)
205 873878b9 Thomas Thrainer
    tests = [
206 873878b9 Thomas Thrainer
      (8, 0, 12, "n", False, []),
207 873878b9 Thomas Thrainer
      (8, 0, 12, "n", True, ["--no-md-flushes"]),
208 873878b9 Thomas Thrainer
      (8, 2, 7, "n", False, []),
209 873878b9 Thomas Thrainer
      (8, 2, 7, "fd", False, ["--no-disk-flushes", "--no-disk-drain"]),
210 873878b9 Thomas Thrainer
      (8, 0, 12, "n", True, ["--no-md-flushes"]),
211 873878b9 Thomas Thrainer
      ]
212 873878b9 Thomas Thrainer
213 873878b9 Thomas Thrainer
    # Versions that support everything
214 873878b9 Thomas Thrainer
    for vmaj, vmin, vrel in ((8, 3, 0), (8, 3, 12)):
215 873878b9 Thomas Thrainer
      tests.append((vmaj, vmin, vrel, "bfd", True,
216 873878b9 Thomas Thrainer
                    ["--no-disk-barrier", "--no-disk-drain",
217 873878b9 Thomas Thrainer
                     "--no-disk-flushes", "--no-md-flushes"]))
218 873878b9 Thomas Thrainer
      tests.append((vmaj, vmin, vrel, "n", False, []))
219 873878b9 Thomas Thrainer
      tests.append((vmaj, vmin, vrel, "b", True,
220 873878b9 Thomas Thrainer
                    ["--no-disk-barrier", "--no-md-flushes"]))
221 873878b9 Thomas Thrainer
      tests.append((vmaj, vmin, vrel, "fd", False,
222 873878b9 Thomas Thrainer
                    ["--no-disk-flushes", "--no-disk-drain"]))
223 873878b9 Thomas Thrainer
      tests.append((vmaj, vmin, vrel, "n", True, ["--no-md-flushes"]))
224 873878b9 Thomas Thrainer
225 873878b9 Thomas Thrainer
    # Test execution
226 873878b9 Thomas Thrainer
    for test in tests:
227 873878b9 Thomas Thrainer
      vmaj, vmin, vrel, disabled_barriers, disable_meta_flush, expected = test
228 873878b9 Thomas Thrainer
      args = \
229 873878b9 Thomas Thrainer
        drbd_cmdgen.DRBD83CmdGenerator._ComputeDiskBarrierArgs(
230 873878b9 Thomas Thrainer
          vmaj, vmin, vrel,
231 873878b9 Thomas Thrainer
          disabled_barriers,
232 873878b9 Thomas Thrainer
          disable_meta_flush)
233 873878b9 Thomas Thrainer
      self.failUnless(set(args) == set(expected),
234 873878b9 Thomas Thrainer
                      "For test %s, got wrong results %s" % (test, args))
235 873878b9 Thomas Thrainer
236 873878b9 Thomas Thrainer
    # Unsupported or invalid versions
237 873878b9 Thomas Thrainer
    for vmaj, vmin, vrel in ((0, 7, 25), (9, 0, 0), (7, 0, 0), (8, 4, 0)):
238 873878b9 Thomas Thrainer
      self.assertRaises(errors.BlockDeviceError,
239 873878b9 Thomas Thrainer
                        drbd_cmdgen.DRBD83CmdGenerator._ComputeDiskBarrierArgs,
240 873878b9 Thomas Thrainer
                        vmaj, vmin, vrel, "n", True)
241 873878b9 Thomas Thrainer
242 873878b9 Thomas Thrainer
    # Invalid options
243 873878b9 Thomas Thrainer
    for option in ("", "c", "whatever", "nbdfc", "nf"):
244 873878b9 Thomas Thrainer
      self.assertRaises(errors.BlockDeviceError,
245 873878b9 Thomas Thrainer
                        drbd_cmdgen.DRBD83CmdGenerator._ComputeDiskBarrierArgs,
246 873878b9 Thomas Thrainer
                        8, 3, 11, option, True)
247 873878b9 Thomas Thrainer
248 873878b9 Thomas Thrainer
249 873878b9 Thomas Thrainer
class TestDRBD8Status(testutils.GanetiTestCase):
250 873878b9 Thomas Thrainer
  """Testing case for DRBD8 /proc status"""
251 873878b9 Thomas Thrainer
252 873878b9 Thomas Thrainer
  def setUp(self):
253 873878b9 Thomas Thrainer
    """Read in txt data"""
254 873878b9 Thomas Thrainer
    testutils.GanetiTestCase.setUp(self)
255 873878b9 Thomas Thrainer
    proc_data = testutils.TestDataFilename("proc_drbd8.txt")
256 873878b9 Thomas Thrainer
    proc80e_data = testutils.TestDataFilename("proc_drbd80-emptyline.txt")
257 873878b9 Thomas Thrainer
    proc83_data = testutils.TestDataFilename("proc_drbd83.txt")
258 873878b9 Thomas Thrainer
    proc83_sync_data = testutils.TestDataFilename("proc_drbd83_sync.txt")
259 873878b9 Thomas Thrainer
    proc83_sync_krnl_data = \
260 873878b9 Thomas Thrainer
      testutils.TestDataFilename("proc_drbd83_sync_krnl2.6.39.txt")
261 873878b9 Thomas Thrainer
    proc84_data = testutils.TestDataFilename("proc_drbd84.txt")
262 873878b9 Thomas Thrainer
    proc84_sync_data = testutils.TestDataFilename("proc_drbd84_sync.txt")
263 873878b9 Thomas Thrainer
264 873878b9 Thomas Thrainer
    self.proc80ev_data = \
265 873878b9 Thomas Thrainer
      testutils.TestDataFilename("proc_drbd80-emptyversion.txt")
266 873878b9 Thomas Thrainer
267 873878b9 Thomas Thrainer
    self.drbd_info = drbd.DRBD8Info.CreateFromFile(filename=proc_data)
268 873878b9 Thomas Thrainer
    self.drbd_info80e = drbd.DRBD8Info.CreateFromFile(filename=proc80e_data)
269 873878b9 Thomas Thrainer
    self.drbd_info83 = drbd.DRBD8Info.CreateFromFile(filename=proc83_data)
270 873878b9 Thomas Thrainer
    self.drbd_info83_sync = \
271 873878b9 Thomas Thrainer
      drbd.DRBD8Info.CreateFromFile(filename=proc83_sync_data)
272 873878b9 Thomas Thrainer
    self.drbd_info83_sync_krnl = \
273 873878b9 Thomas Thrainer
      drbd.DRBD8Info.CreateFromFile(filename=proc83_sync_krnl_data)
274 873878b9 Thomas Thrainer
    self.drbd_info84 = drbd.DRBD8Info.CreateFromFile(filename=proc84_data)
275 873878b9 Thomas Thrainer
    self.drbd_info84_sync = \
276 873878b9 Thomas Thrainer
      drbd.DRBD8Info.CreateFromFile(filename=proc84_sync_data)
277 873878b9 Thomas Thrainer
278 873878b9 Thomas Thrainer
  def testIOErrors(self):
279 873878b9 Thomas Thrainer
    """Test handling of errors while reading the proc file."""
280 873878b9 Thomas Thrainer
    temp_file = self._CreateTempFile()
281 873878b9 Thomas Thrainer
    os.unlink(temp_file)
282 873878b9 Thomas Thrainer
    self.failUnlessRaises(errors.BlockDeviceError,
283 873878b9 Thomas Thrainer
                          drbd.DRBD8Info.CreateFromFile, filename=temp_file)
284 873878b9 Thomas Thrainer
285 873878b9 Thomas Thrainer
  def testHelper(self):
286 873878b9 Thomas Thrainer
    """Test reading usermode_helper in /sys."""
287 873878b9 Thomas Thrainer
    sys_drbd_helper = testutils.TestDataFilename("sys_drbd_usermode_helper.txt")
288 873878b9 Thomas Thrainer
    drbd_helper = drbd.DRBD8.GetUsermodeHelper(filename=sys_drbd_helper)
289 873878b9 Thomas Thrainer
    self.failUnlessEqual(drbd_helper, "/bin/true")
290 873878b9 Thomas Thrainer
291 873878b9 Thomas Thrainer
  def testHelperIOErrors(self):
292 873878b9 Thomas Thrainer
    """Test handling of errors while reading usermode_helper in /sys."""
293 873878b9 Thomas Thrainer
    temp_file = self._CreateTempFile()
294 873878b9 Thomas Thrainer
    os.unlink(temp_file)
295 873878b9 Thomas Thrainer
    self.failUnlessRaises(errors.BlockDeviceError,
296 873878b9 Thomas Thrainer
                          drbd.DRBD8.GetUsermodeHelper, filename=temp_file)
297 873878b9 Thomas Thrainer
298 873878b9 Thomas Thrainer
  def testMinorNotFound(self):
299 873878b9 Thomas Thrainer
    """Test not-found-minor in /proc"""
300 873878b9 Thomas Thrainer
    self.failUnless(not self.drbd_info.HasMinorStatus(9))
301 873878b9 Thomas Thrainer
    self.failUnless(not self.drbd_info83.HasMinorStatus(9))
302 873878b9 Thomas Thrainer
    self.failUnless(not self.drbd_info80e.HasMinorStatus(3))
303 873878b9 Thomas Thrainer
304 873878b9 Thomas Thrainer
  def testLineNotMatch(self):
305 873878b9 Thomas Thrainer
    """Test wrong line passed to drbd_info.DRBD8Status"""
306 873878b9 Thomas Thrainer
    self.assertRaises(errors.BlockDeviceError, drbd_info.DRBD8Status, "foo")
307 873878b9 Thomas Thrainer
308 873878b9 Thomas Thrainer
  def testMinor0(self):
309 873878b9 Thomas Thrainer
    """Test connected, primary device"""
310 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83, self.drbd_info84]:
311 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(0)
312 873878b9 Thomas Thrainer
      self.failUnless(stats.is_in_use)
313 873878b9 Thomas Thrainer
      self.failUnless(stats.is_connected and stats.is_primary and
314 873878b9 Thomas Thrainer
                      stats.peer_secondary and stats.is_disk_uptodate)
315 873878b9 Thomas Thrainer
316 873878b9 Thomas Thrainer
  def testMinor1(self):
317 873878b9 Thomas Thrainer
    """Test connected, secondary device"""
318 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83, self.drbd_info84]:
319 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(1)
320 873878b9 Thomas Thrainer
      self.failUnless(stats.is_in_use)
321 873878b9 Thomas Thrainer
      self.failUnless(stats.is_connected and stats.is_secondary and
322 873878b9 Thomas Thrainer
                      stats.peer_primary and stats.is_disk_uptodate)
323 873878b9 Thomas Thrainer
324 873878b9 Thomas Thrainer
  def testMinor2(self):
325 873878b9 Thomas Thrainer
    """Test unconfigured device"""
326 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83,
327 873878b9 Thomas Thrainer
                 self.drbd_info80e, self.drbd_info84]:
328 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(2)
329 873878b9 Thomas Thrainer
      self.failIf(stats.is_in_use)
330 873878b9 Thomas Thrainer
331 873878b9 Thomas Thrainer
  def testMinor4(self):
332 873878b9 Thomas Thrainer
    """Test WFconn device"""
333 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83, self.drbd_info84]:
334 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(4)
335 873878b9 Thomas Thrainer
      self.failUnless(stats.is_in_use)
336 873878b9 Thomas Thrainer
      self.failUnless(stats.is_wfconn and stats.is_primary and
337 873878b9 Thomas Thrainer
                      stats.rrole == "Unknown" and
338 873878b9 Thomas Thrainer
                      stats.is_disk_uptodate)
339 873878b9 Thomas Thrainer
340 873878b9 Thomas Thrainer
  def testMinor6(self):
341 873878b9 Thomas Thrainer
    """Test diskless device"""
342 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83, self.drbd_info84]:
343 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(6)
344 873878b9 Thomas Thrainer
      self.failUnless(stats.is_in_use)
345 873878b9 Thomas Thrainer
      self.failUnless(stats.is_connected and stats.is_secondary and
346 873878b9 Thomas Thrainer
                      stats.peer_primary and stats.is_diskless)
347 873878b9 Thomas Thrainer
348 873878b9 Thomas Thrainer
  def testMinor8(self):
349 873878b9 Thomas Thrainer
    """Test standalone device"""
350 873878b9 Thomas Thrainer
    for info in [self.drbd_info, self.drbd_info83, self.drbd_info84]:
351 873878b9 Thomas Thrainer
      stats = info.GetMinorStatus(8)
352 873878b9 Thomas Thrainer
      self.failUnless(stats.is_in_use)
353 873878b9 Thomas Thrainer
      self.failUnless(stats.is_standalone and
354 873878b9 Thomas Thrainer
                      stats.rrole == "Unknown" and
355 873878b9 Thomas Thrainer
                      stats.is_disk_uptodate)
356 873878b9 Thomas Thrainer
357 873878b9 Thomas Thrainer
  def testDRBD83SyncFine(self):
358 873878b9 Thomas Thrainer
    stats = self.drbd_info83_sync.GetMinorStatus(3)
359 873878b9 Thomas Thrainer
    self.failUnless(stats.is_in_resync)
360 873878b9 Thomas Thrainer
    self.assertAlmostEqual(stats.sync_percent, 34.9)
361 873878b9 Thomas Thrainer
362 873878b9 Thomas Thrainer
  def testDRBD83SyncBroken(self):
363 873878b9 Thomas Thrainer
    stats = self.drbd_info83_sync_krnl.GetMinorStatus(3)
364 873878b9 Thomas Thrainer
    self.failUnless(stats.is_in_resync)
365 873878b9 Thomas Thrainer
    self.assertAlmostEqual(stats.sync_percent, 2.4)
366 873878b9 Thomas Thrainer
367 873878b9 Thomas Thrainer
  def testDRBD84Sync(self):
368 873878b9 Thomas Thrainer
    stats = self.drbd_info84_sync.GetMinorStatus(5)
369 873878b9 Thomas Thrainer
    self.failUnless(stats.is_in_resync)
370 873878b9 Thomas Thrainer
    self.assertAlmostEqual(stats.sync_percent, 68.5)
371 873878b9 Thomas Thrainer
372 873878b9 Thomas Thrainer
  def testDRBDEmptyVersion(self):
373 873878b9 Thomas Thrainer
    self.assertRaises(errors.BlockDeviceError,
374 873878b9 Thomas Thrainer
                      drbd.DRBD8Info.CreateFromFile,
375 873878b9 Thomas Thrainer
                      filename=self.proc80ev_data)
376 873878b9 Thomas Thrainer
377 873878b9 Thomas Thrainer
378 74db37c0 Thomas Thrainer
class TestDRBD8Construction(testutils.GanetiTestCase):
379 74db37c0 Thomas Thrainer
  def setUp(self):
380 74db37c0 Thomas Thrainer
    """Read in txt data"""
381 74db37c0 Thomas Thrainer
    testutils.GanetiTestCase.setUp(self)
382 74db37c0 Thomas Thrainer
    self.proc80_info = \
383 74db37c0 Thomas Thrainer
      drbd_info.DRBD8Info.CreateFromFile(
384 74db37c0 Thomas Thrainer
        filename=testutils.TestDataFilename("proc_drbd8.txt"))
385 74db37c0 Thomas Thrainer
    self.proc83_info = \
386 74db37c0 Thomas Thrainer
      drbd_info.DRBD8Info.CreateFromFile(
387 74db37c0 Thomas Thrainer
        filename=testutils.TestDataFilename("proc_drbd83.txt"))
388 74db37c0 Thomas Thrainer
    self.proc84_info = \
389 74db37c0 Thomas Thrainer
      drbd_info.DRBD8Info.CreateFromFile(
390 74db37c0 Thomas Thrainer
        filename=testutils.TestDataFilename("proc_drbd84.txt"))
391 74db37c0 Thomas Thrainer
392 74db37c0 Thomas Thrainer
    self.test_unique_id = ("hosta.com", 123, "host2.com", 123, 0, "secret")
393 74db37c0 Thomas Thrainer
394 74db37c0 Thomas Thrainer
  @testutils.patch_object(drbd_info.DRBD8Info, "CreateFromFile")
395 74db37c0 Thomas Thrainer
  def testConstructionWith80Data(self, mock_create_from_file):
396 74db37c0 Thomas Thrainer
    mock_create_from_file.return_value = self.proc80_info
397 74db37c0 Thomas Thrainer
398 74db37c0 Thomas Thrainer
    inst = drbd.DRBD8(self.test_unique_id, [], 123, {})
399 74db37c0 Thomas Thrainer
    self.assertEqual(inst._show_info_cls, drbd_info.DRBD83ShowInfo)
400 74db37c0 Thomas Thrainer
    self.assertTrue(isinstance(inst._cmd_gen, drbd_cmdgen.DRBD83CmdGenerator))
401 74db37c0 Thomas Thrainer
402 74db37c0 Thomas Thrainer
  @testutils.patch_object(drbd_info.DRBD8Info, "CreateFromFile")
403 74db37c0 Thomas Thrainer
  def testConstructionWith83Data(self, mock_create_from_file):
404 74db37c0 Thomas Thrainer
    mock_create_from_file.return_value = self.proc83_info
405 74db37c0 Thomas Thrainer
406 74db37c0 Thomas Thrainer
    inst = drbd.DRBD8(self.test_unique_id, [], 123, {})
407 74db37c0 Thomas Thrainer
    self.assertEqual(inst._show_info_cls, drbd_info.DRBD83ShowInfo)
408 74db37c0 Thomas Thrainer
    self.assertTrue(isinstance(inst._cmd_gen, drbd_cmdgen.DRBD83CmdGenerator))
409 74db37c0 Thomas Thrainer
410 74db37c0 Thomas Thrainer
  @testutils.patch_object(drbd_info.DRBD8Info, "CreateFromFile")
411 74db37c0 Thomas Thrainer
  def testConstructionWith84Data(self, mock_create_from_file):
412 74db37c0 Thomas Thrainer
    mock_create_from_file.return_value = self.proc84_info
413 74db37c0 Thomas Thrainer
414 74db37c0 Thomas Thrainer
    inst = drbd.DRBD8(self.test_unique_id, [], 123, {})
415 74db37c0 Thomas Thrainer
    self.assertEqual(inst._show_info_cls, drbd_info.DRBD84ShowInfo)
416 74db37c0 Thomas Thrainer
    # FIXME: add assertion for right class here!
417 74db37c0 Thomas Thrainer
    self.assertEqual(inst._cmd_gen, None)
418 74db37c0 Thomas Thrainer
419 74db37c0 Thomas Thrainer
420 873878b9 Thomas Thrainer
if __name__ == "__main__":
421 873878b9 Thomas Thrainer
  testutils.GanetiTestProgram()