ganeti.bdev_unittest.py 5.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Script for unittesting the bdev module"""


25
import os
26
27
28
import unittest

from ganeti import bdev
29
from ganeti import errors
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


class TestDRBD8Runner(unittest.TestCase):
  """Testing case for DRBD8"""

  @staticmethod
  def _has_disk(data, dname, mname):
    """Check local disk corectness"""
    retval = (
      "local_dev" in data and
      data["local_dev"] == dname and
      "meta_dev" in data and
      data["meta_dev"] == mname and
      "meta_index" in data and
      data["meta_index"] == 0
      )
    return retval

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  @staticmethod
  def _get_contents(name):
    """Returns the contents of a file"""

    prefix = os.environ.get("srcdir", None)
    if prefix:
      name = prefix + "/" + name
    fh = open(name, "r")
    try:
      data = fh.read()
    finally:
      fh.close()
    return data


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  @staticmethod
  def _has_net(data, local, remote):
    """Check network connection parameters"""
    retval = (
      "local_addr" in data and
      data["local_addr"] == local and
      "remote_addr" in data and
      data["remote_addr"] == remote
      )
    return retval

  def testParserCreation(self):
    """Test drbdsetup show parser creation"""
    bdev.DRBD8._GetShowParser()

  def testParserBoth(self):
    """Test drbdsetup show parser for disk and network"""
80
    data = self._get_contents("data/bdev-both.txt")
81
82
83
84
85
86
87
88
89
90
    result = bdev.DRBD8._GetDevInfo(data)
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
                                   "/dev/xenvg/test.meta"),
                    "Wrong local disk info")
    self.failUnless(self._has_net(result, ("192.168.1.1", 11000),
                                  ("192.168.1.2", 11000)),
                    "Wrong network info")

  def testParserNet(self):
    """Test drbdsetup show parser for disk and network"""
91
    data = self._get_contents("data/bdev-net.txt")
92
93
94
95
96
97
98
99
100
101
102
    result = bdev.DRBD8._GetDevInfo(data)
    self.failUnless(("local_dev" not in result and
                     "meta_dev" not in result and
                     "meta_index" not in result),
                    "Should not find local disk info")
    self.failUnless(self._has_net(result, ("192.168.1.1", 11002),
                                  ("192.168.1.2", 11002)),
                    "Wrong network info")

  def testParserDisk(self):
    """Test drbdsetup show parser for disk and network"""
103
    data = self._get_contents("data/bdev-disk.txt")
104
105
106
107
108
109
110
111
    result = bdev.DRBD8._GetDevInfo(data)
    self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
                                   "/dev/xenvg/test.meta"),
                    "Wrong local disk info")
    self.failUnless(("local_addr" not in result and
                     "remote_addr" not in result),
                    "Should not find network info")

112
113
114
115
116
117

class TestDRBD8Status(unittest.TestCase):
  """Testing case for DRBD8 /proc status"""

  def setUp(self):
    """Read in txt data"""
118
119
120
121
122
    proc_data = "data/proc_drbd8.txt"
    prefix = os.environ.get("srcdir", None)
    if prefix:
      proc_data = prefix + "/" + proc_data
    self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)

  def testMinorNotFound(self):
    """Test not-found-minor in /proc"""
    self.failUnless(9 not in self.mass_data)

  def testLineNotMatch(self):
    """Test wrong line passed to DRBD8Status"""
    self.assertRaises(errors.BlockDeviceError, bdev.DRBD8Status, "foo")

  def testMinor0(self):
    """Test connected, primary device"""
    stats = bdev.DRBD8Status(self.mass_data[0])
    self.failUnless(stats.is_connected and stats.is_primary and
                    stats.peer_secondary and stats.is_disk_uptodate)

  def testMinor1(self):
    """Test connected, secondary device"""
    stats = bdev.DRBD8Status(self.mass_data[1])
    self.failUnless(stats.is_connected and stats.is_secondary and
                    stats.peer_primary and stats.is_disk_uptodate)

  def testMinor4(self):
    """Test WFconn device"""
    stats = bdev.DRBD8Status(self.mass_data[4])
    self.failUnless(stats.is_wfconn and stats.is_primary and
                    stats.rrole == 'Unknown' and
                    stats.is_disk_uptodate)

  def testMinor6(self):
    """Test diskless device"""
    stats = bdev.DRBD8Status(self.mass_data[6])
    self.failUnless(stats.is_connected and stats.is_secondary and
                    stats.peer_primary and stats.is_diskless)

  def testMinor8(self):
    """Test standalone device"""
    stats = bdev.DRBD8Status(self.mass_data[8])
    self.failUnless(stats.is_standalone and
                    stats.rrole == 'Unknown' and
                    stats.is_disk_uptodate)

165
166
if __name__ == '__main__':
  unittest.main()