Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.bdev_unittest.py @ 549071a0

History | View | Annotate | Download (8.1 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 6b90c22e Iustin Pop
from ganeti import errors
30 3840729d Iustin Pop
31 25231ec5 Michael Hanselmann
import testutils
32 25231ec5 Michael Hanselmann
33 3840729d Iustin Pop
34 149a5439 Iustin Pop
class TestDRBD8Runner(testutils.GanetiTestCase):
35 3840729d Iustin Pop
  """Testing case for DRBD8"""
36 3840729d Iustin Pop
37 3840729d Iustin Pop
  @staticmethod
38 3840729d Iustin Pop
  def _has_disk(data, dname, mname):
39 3840729d Iustin Pop
    """Check local disk corectness"""
40 3840729d Iustin Pop
    retval = (
41 3840729d Iustin Pop
      "local_dev" in data and
42 3840729d Iustin Pop
      data["local_dev"] == dname and
43 3840729d Iustin Pop
      "meta_dev" in data and
44 3840729d Iustin Pop
      data["meta_dev"] == mname and
45 3840729d Iustin Pop
      "meta_index" in data and
46 3840729d Iustin Pop
      data["meta_index"] == 0
47 3840729d Iustin Pop
      )
48 3840729d Iustin Pop
    return retval
49 3840729d Iustin Pop
50 3840729d Iustin Pop
  @staticmethod
51 3840729d Iustin Pop
  def _has_net(data, local, remote):
52 3840729d Iustin Pop
    """Check network connection parameters"""
53 3840729d Iustin Pop
    retval = (
54 3840729d Iustin Pop
      "local_addr" in data and
55 3840729d Iustin Pop
      data["local_addr"] == local and
56 3840729d Iustin Pop
      "remote_addr" in data and
57 3840729d Iustin Pop
      data["remote_addr"] == remote
58 3840729d Iustin Pop
      )
59 3840729d Iustin Pop
    return retval
60 3840729d Iustin Pop
61 3840729d Iustin Pop
  def testParserCreation(self):
62 3840729d Iustin Pop
    """Test drbdsetup show parser creation"""
63 3840729d Iustin Pop
    bdev.DRBD8._GetShowParser()
64 3840729d Iustin Pop
65 5a672c30 Manuel Franceschini
  def testParser80(self):
66 5a672c30 Manuel Franceschini
    """Test drbdsetup show parser for disk and network version 8.0"""
67 5a672c30 Manuel Franceschini
    data = self._ReadTestData("bdev-drbd-8.0.txt")
68 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
69 3840729d Iustin Pop
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
70 3840729d Iustin Pop
                                   "/dev/xenvg/test.meta"),
71 3840729d Iustin Pop
                    "Wrong local disk info")
72 3840729d Iustin Pop
    self.failUnless(self._has_net(result, ("192.168.1.1", 11000),
73 3840729d Iustin Pop
                                  ("192.168.1.2", 11000)),
74 01e2ce3a Iustin Pop
                    "Wrong network info (8.0.x)")
75 01e2ce3a Iustin Pop
76 5a672c30 Manuel Franceschini
  def testParser83(self):
77 5a672c30 Manuel Franceschini
    """Test drbdsetup show parser for disk and network version 8.3"""
78 5a672c30 Manuel Franceschini
    data = self._ReadTestData("bdev-drbd-8.3.txt")
79 01e2ce3a Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
80 01e2ce3a Iustin Pop
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
81 01e2ce3a Iustin Pop
                                   "/dev/xenvg/test.meta"),
82 01e2ce3a Iustin Pop
                    "Wrong local disk info")
83 01e2ce3a Iustin Pop
    self.failUnless(self._has_net(result, ("192.168.1.1", 11000),
84 01e2ce3a Iustin Pop
                                  ("192.168.1.2", 11000)),
85 5a672c30 Manuel Franceschini
                    "Wrong network info (8.0.x)")
86 3840729d Iustin Pop
87 5a672c30 Manuel Franceschini
  def testParserNetIP4(self):
88 5a672c30 Manuel Franceschini
    """Test drbdsetup show parser for IPv4 network"""
89 5a672c30 Manuel Franceschini
    data = self._ReadTestData("bdev-drbd-net-ip4.txt")
90 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
91 3840729d Iustin Pop
    self.failUnless(("local_dev" not in result and
92 3840729d Iustin Pop
                     "meta_dev" not in result and
93 3840729d Iustin Pop
                     "meta_index" not in result),
94 3840729d Iustin Pop
                    "Should not find local disk info")
95 3840729d Iustin Pop
    self.failUnless(self._has_net(result, ("192.168.1.1", 11002),
96 3840729d Iustin Pop
                                  ("192.168.1.2", 11002)),
97 5a672c30 Manuel Franceschini
                    "Wrong network info (IPv4)")
98 5a672c30 Manuel Franceschini
99 5a672c30 Manuel Franceschini
  def testParserNetIP6(self):
100 5a672c30 Manuel Franceschini
    """Test drbdsetup show parser for IPv6 network"""
101 5a672c30 Manuel Franceschini
    data = self._ReadTestData("bdev-drbd-net-ip6.txt")
102 5a672c30 Manuel Franceschini
    result = bdev.DRBD8._GetDevInfo(data)
103 5a672c30 Manuel Franceschini
    self.failUnless(("local_dev" not in result and
104 5a672c30 Manuel Franceschini
                     "meta_dev" not in result and
105 5a672c30 Manuel Franceschini
                     "meta_index" not in result),
106 5a672c30 Manuel Franceschini
                    "Should not find local disk info")
107 5a672c30 Manuel Franceschini
    self.failUnless(self._has_net(result, ("2001:db8:65::1", 11048),
108 5a672c30 Manuel Franceschini
                                  ("2001:db8:66::1", 11048)),
109 5a672c30 Manuel Franceschini
                    "Wrong network info (IPv6)")
110 3840729d Iustin Pop
111 3840729d Iustin Pop
  def testParserDisk(self):
112 5a672c30 Manuel Franceschini
    """Test drbdsetup show parser for disk"""
113 5a672c30 Manuel Franceschini
    data = self._ReadTestData("bdev-drbd-disk.txt")
114 3840729d Iustin Pop
    result = bdev.DRBD8._GetDevInfo(data)
115 3840729d Iustin Pop
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
116 3840729d Iustin Pop
                                   "/dev/xenvg/test.meta"),
117 3840729d Iustin Pop
                    "Wrong local disk info")
118 3840729d Iustin Pop
    self.failUnless(("local_addr" not in result and
119 3840729d Iustin Pop
                     "remote_addr" not in result),
120 3840729d Iustin Pop
                    "Should not find network info")
121 3840729d Iustin Pop
122 6b90c22e Iustin Pop
123 149a5439 Iustin Pop
class TestDRBD8Status(testutils.GanetiTestCase):
124 6b90c22e Iustin Pop
  """Testing case for DRBD8 /proc status"""
125 6b90c22e Iustin Pop
126 6b90c22e Iustin Pop
  def setUp(self):
127 6b90c22e Iustin Pop
    """Read in txt data"""
128 51596eb2 Iustin Pop
    testutils.GanetiTestCase.setUp(self)
129 149a5439 Iustin Pop
    proc_data = self._TestDataFilename("proc_drbd8.txt")
130 67d101d4 Iustin Pop
    proc80e_data = self._TestDataFilename("proc_drbd80-emptyline.txt")
131 01e2ce3a Iustin Pop
    proc83_data = self._TestDataFilename("proc_drbd83.txt")
132 ae9da390 Iustin Pop
    self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
133 67d101d4 Iustin Pop
    self.proc80e_data = bdev.DRBD8._GetProcData(filename=proc80e_data)
134 01e2ce3a Iustin Pop
    self.proc83_data = bdev.DRBD8._GetProcData(filename=proc83_data)
135 6b90c22e Iustin Pop
    self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)
136 67d101d4 Iustin Pop
    self.mass80e_data = bdev.DRBD8._MassageProcData(self.proc80e_data)
137 01e2ce3a Iustin Pop
    self.mass83_data = bdev.DRBD8._MassageProcData(self.proc83_data)
138 6b90c22e Iustin Pop
139 f6eaed12 Iustin Pop
  def testIOErrors(self):
140 f6eaed12 Iustin Pop
    """Test handling of errors while reading the proc file."""
141 f6eaed12 Iustin Pop
    temp_file = self._CreateTempFile()
142 f6eaed12 Iustin Pop
    os.unlink(temp_file)
143 f6eaed12 Iustin Pop
    self.failUnlessRaises(errors.BlockDeviceError,
144 f6eaed12 Iustin Pop
                          bdev.DRBD8._GetProcData, filename=temp_file)
145 f6eaed12 Iustin Pop
146 549071a0 Luca Bigliardi
  def testHelper(self):
147 549071a0 Luca Bigliardi
    """Test reading usermode_helper in /sys."""
148 549071a0 Luca Bigliardi
    sys_drbd_helper = self._TestDataFilename("sys_drbd_usermode_helper.txt")
149 549071a0 Luca Bigliardi
    drbd_helper = bdev.DRBD8.GetUsermodeHelper(filename=sys_drbd_helper)
150 549071a0 Luca Bigliardi
    self.failUnlessEqual(drbd_helper, "/bin/true")
151 549071a0 Luca Bigliardi
152 549071a0 Luca Bigliardi
  def testHelperIOErrors(self):
153 549071a0 Luca Bigliardi
    """Test handling of errors while reading usermode_helper in /sys."""
154 549071a0 Luca Bigliardi
    temp_file = self._CreateTempFile()
155 549071a0 Luca Bigliardi
    os.unlink(temp_file)
156 549071a0 Luca Bigliardi
    self.failUnlessRaises(errors.BlockDeviceError,
157 549071a0 Luca Bigliardi
                          bdev.DRBD8.GetUsermodeHelper, filename=temp_file)
158 549071a0 Luca Bigliardi
159 6b90c22e Iustin Pop
  def testMinorNotFound(self):
160 6b90c22e Iustin Pop
    """Test not-found-minor in /proc"""
161 6b90c22e Iustin Pop
    self.failUnless(9 not in self.mass_data)
162 01e2ce3a Iustin Pop
    self.failUnless(9 not in self.mass83_data)
163 67d101d4 Iustin Pop
    self.failUnless(3 not in self.mass80e_data)
164 6b90c22e Iustin Pop
165 6b90c22e Iustin Pop
  def testLineNotMatch(self):
166 6b90c22e Iustin Pop
    """Test wrong line passed to DRBD8Status"""
167 6b90c22e Iustin Pop
    self.assertRaises(errors.BlockDeviceError, bdev.DRBD8Status, "foo")
168 6b90c22e Iustin Pop
169 6b90c22e Iustin Pop
  def testMinor0(self):
170 6b90c22e Iustin Pop
    """Test connected, primary device"""
171 01e2ce3a Iustin Pop
    for data in [self.mass_data, self.mass83_data]:
172 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[0])
173 01e2ce3a Iustin Pop
      self.failUnless(stats.is_in_use)
174 01e2ce3a Iustin Pop
      self.failUnless(stats.is_connected and stats.is_primary and
175 01e2ce3a Iustin Pop
                      stats.peer_secondary and stats.is_disk_uptodate)
176 6b90c22e Iustin Pop
177 6b90c22e Iustin Pop
  def testMinor1(self):
178 6b90c22e Iustin Pop
    """Test connected, secondary device"""
179 01e2ce3a Iustin Pop
    for data in [self.mass_data, self.mass83_data]:
180 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[1])
181 01e2ce3a Iustin Pop
      self.failUnless(stats.is_in_use)
182 01e2ce3a Iustin Pop
      self.failUnless(stats.is_connected and stats.is_secondary and
183 01e2ce3a Iustin Pop
                      stats.peer_primary and stats.is_disk_uptodate)
184 6b90c22e Iustin Pop
185 767d52d3 Iustin Pop
  def testMinor2(self):
186 767d52d3 Iustin Pop
    """Test unconfigured device"""
187 67d101d4 Iustin Pop
    for data in [self.mass_data, self.mass83_data, self.mass80e_data]:
188 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[2])
189 01e2ce3a Iustin Pop
      self.failIf(stats.is_in_use)
190 767d52d3 Iustin Pop
191 6b90c22e Iustin Pop
  def testMinor4(self):
192 6b90c22e Iustin Pop
    """Test WFconn device"""
193 01e2ce3a Iustin Pop
    for data in [self.mass_data, self.mass83_data]:
194 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[4])
195 01e2ce3a Iustin Pop
      self.failUnless(stats.is_in_use)
196 01e2ce3a Iustin Pop
      self.failUnless(stats.is_wfconn and stats.is_primary and
197 01e2ce3a Iustin Pop
                      stats.rrole == 'Unknown' and
198 01e2ce3a Iustin Pop
                      stats.is_disk_uptodate)
199 6b90c22e Iustin Pop
200 6b90c22e Iustin Pop
  def testMinor6(self):
201 6b90c22e Iustin Pop
    """Test diskless device"""
202 01e2ce3a Iustin Pop
    for data in [self.mass_data, self.mass83_data]:
203 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[6])
204 01e2ce3a Iustin Pop
      self.failUnless(stats.is_in_use)
205 01e2ce3a Iustin Pop
      self.failUnless(stats.is_connected and stats.is_secondary and
206 01e2ce3a Iustin Pop
                      stats.peer_primary and stats.is_diskless)
207 6b90c22e Iustin Pop
208 6b90c22e Iustin Pop
  def testMinor8(self):
209 6b90c22e Iustin Pop
    """Test standalone device"""
210 01e2ce3a Iustin Pop
    for data in [self.mass_data, self.mass83_data]:
211 01e2ce3a Iustin Pop
      stats = bdev.DRBD8Status(data[8])
212 01e2ce3a Iustin Pop
      self.failUnless(stats.is_in_use)
213 01e2ce3a Iustin Pop
      self.failUnless(stats.is_standalone and
214 01e2ce3a Iustin Pop
                      stats.rrole == 'Unknown' and
215 01e2ce3a Iustin Pop
                      stats.is_disk_uptodate)
216 6b90c22e Iustin Pop
217 3840729d Iustin Pop
if __name__ == '__main__':
218 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()