Skip to content

Commit

Permalink
fix deprecation warnings in a backward compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed May 27, 2015
1 parent 4145eb5 commit 510b86f
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 155 deletions.
11 changes: 11 additions & 0 deletions tests/__init__.py
@@ -0,0 +1,11 @@
import six
import unittest

class BlivetTestCase(unittest.TestCase):
def __init__(self, methodName='runTest'):
# deprecation compatibility b/w python 2 and 3
if six.PY2:
self.assertRaisesRegex = self.assertRaisesRegexp
self.assertRegex = self.assertRegexpMatches

super(BlivetTestCase, self).__init__(methodName)
59 changes: 22 additions & 37 deletions tests/action_test.py
Expand Up @@ -201,48 +201,39 @@ def testActionCreation(self):
sdd = self.storage.devicetree.getDeviceByName("sdd")
p = self.newDevice(device_class=PartitionDevice,
name="sdd1", size=Size("32 GiB"), parents=[sdd])
self.failUnlessRaises(ValueError,
ActionResizeDevice,
p,
p.size + Size("7232 MiB"))
with self.assertRaises(ValueError):
ActionResizeDevice(p, p.size + Size("7232 MiB"))

# instantiation of device resize action for non-resizable device
# should fail
vg = self.storage.devicetree.getDeviceByName("VolGroup")
self.assertNotEqual(vg, None)
self.failUnlessRaises(ValueError,
ActionResizeDevice,
vg,
vg.size + Size("32 MiB"))
with self.assertRaises(ValueError):
ActionResizeDevice(vg, vg.size + Size("32 MiB"))

# instantiation of format resize action for non-resizable format type
# should fail
lv_swap = self.storage.devicetree.getDeviceByName("VolGroup-lv_swap")
self.assertNotEqual(lv_swap, None)
self.failUnlessRaises(ValueError,
ActionResizeFormat,
lv_swap,
lv_swap.size + Size("32 MiB"))
with self.assertRaises(ValueError):
ActionResizeFormat(lv_swap, lv_swap.size + Size("32 MiB"))

# instantiation of format resize action for non-existent format
# should fail
lv_root = self.storage.devicetree.getDeviceByName("VolGroup-lv_root")
self.assertNotEqual(lv_root, None)
lv_root.format.exists = False
self.failUnlessRaises(ValueError,
ActionResizeFormat,
lv_root,
lv_root.size - Size("1000 MiB"))
with self.assertRaises(ValueError):
ActionResizeFormat(lv_root, lv_root.size - Size("1000 MiB"))
lv_root.format.exists = True

# instantiation of device create action for existing device should
# fail
lv_swap = self.storage.devicetree.getDeviceByName("VolGroup-lv_swap")
self.assertNotEqual(lv_swap, None)
self.assertEqual(lv_swap.exists, True)
self.failUnlessRaises(ValueError,
ActionCreateDevice,
lv_swap)
with self.assertRaises(ValueError):
ActionCreateDevice(lv_swap)

# instantiation of format destroy action for device causes device's
# format attribute to be a DeviceFormat instance
Expand Down Expand Up @@ -274,9 +265,8 @@ def testActionRegistration(self):
self.assertNotEqual(vg, None)
self.assertEqual(vg.isleaf, False)
a = ActionDestroyDevice(vg)
self.failUnlessRaises(ValueError,
self.storage.devicetree.registerAction,
a)
with self.assertRaises(ValueError):
self.storage.devicetree.registerAction(a)

# registering any action other than create for a device that's not in
# the devicetree should fail
Expand All @@ -289,38 +279,33 @@ def testActionRegistration(self):
sdc1_format = self.newFormat("ext2", device=sdc1.path, mountpoint="/")
create_sdc1_format = ActionCreateFormat(sdc1, sdc1_format)
create_sdc1_format.apply()
self.failUnlessRaises(blivet.errors.DeviceTreeError,
self.storage.devicetree.registerAction,
create_sdc1_format)
with self.assertRaises(blivet.errors.DeviceTreeError):
self.storage.devicetree.registerAction(create_sdc1_format)

sdc1_format.exists = True
sdc1_format._resizable = True
resize_sdc1_format = ActionResizeFormat(sdc1,
sdc1.size - Size("10 GiB"))
resize_sdc1_format.apply()
self.failUnlessRaises(blivet.errors.DeviceTreeError,
self.storage.devicetree.registerAction,
resize_sdc1_format)
with self.assertRaises(blivet.errors.DeviceTreeError):
self.storage.devicetree.registerAction(resize_sdc1_format)

resize_sdc1 = ActionResizeDevice(sdc1, sdc1.size - Size("10 GiB"))
resize_sdc1.apply()
self.failUnlessRaises(blivet.errors.DeviceTreeError,
self.storage.devicetree.registerAction,
resize_sdc1)
with self.assertRaises(blivet.errors.DeviceTreeError):
self.storage.devicetree.registerAction(resize_sdc1)

resize_sdc1.cancel()
resize_sdc1_format.cancel()

destroy_sdc1_format = ActionDestroyFormat(sdc1)
self.failUnlessRaises(blivet.errors.DeviceTreeError,
self.storage.devicetree.registerAction,
destroy_sdc1_format)
with self.assertRaises(blivet.errors.DeviceTreeError):
self.storage.devicetree.registerAction(destroy_sdc1_format)


destroy_sdc1 = ActionDestroyDevice(sdc1)
self.failUnlessRaises(blivet.errors.DeviceTreeError,
self.storage.devicetree.registerAction,
destroy_sdc1)
with self.assertRaises(blivet.errors.DeviceTreeError):
self.storage.devicetree.registerAction(destroy_sdc1)

# registering a device destroy action should cause the device to be
# removed from the devicetree
Expand Down
11 changes: 6 additions & 5 deletions tests/devicefactory_test.py
Expand Up @@ -10,8 +10,9 @@
from blivet.errors import RaidError
from blivet.formats import getFormat
from blivet.size import Size
from tests import BlivetTestCase

class MDFactoryTestCase(unittest.TestCase):
class MDFactoryTestCase(BlivetTestCase):
"""Note that these tests postdate the code that they test.
Therefore, they capture the behavior of the code as it is now,
not necessarily its intended or its correct behavior. See the
Expand All @@ -30,16 +31,16 @@ def setUp(self):
raid_level=0)

def testMDFactory(self):
with self.assertRaisesRegexp(devicefactory.DeviceFactoryError, "must have some RAID level"):
with self.assertRaisesRegex(devicefactory.DeviceFactoryError, "must have some RAID level"):
devicefactory.get_device_factory(
self.b,
devicefactory.DEVICE_TYPE_MD,
Size("1 GiB"))

with self.assertRaisesRegexp(RaidError, "requires at least"):
with self.assertRaisesRegex(RaidError, "requires at least"):
self.factory1._get_device_space()

with self.assertRaisesRegexp(RaidError, "requires at least"):
with self.assertRaisesRegex(RaidError, "requires at least"):
self.factory1._configure()

self.assertEqual(self.factory1.container_list, [])
Expand All @@ -52,7 +53,7 @@ def testMDFactory(self):
]
self.assertIsNotNone(self.factory1._get_new_device(parents=parents))

with self.assertRaisesRegexp(RaidError, "requires at least"):
with self.assertRaisesRegex(RaidError, "requires at least"):
self.factory2._get_device_space()

self.assertEqual(self.factory2.container_list, [])
Expand Down
11 changes: 6 additions & 5 deletions tests/devicelibs_test/raid_test.py
Expand Up @@ -4,8 +4,9 @@
import blivet.devicelibs.raid as raid
import blivet.errors as errors
from blivet.size import Size
from tests import BlivetTestCase

class RaidTestCase(unittest.TestCase):
class RaidTestCase(BlivetTestCase):

def setUp(self):
self.levels = raid.RAIDLevels(["raid0", "raid1", "raid4", "raid5", "raid6", "raid10"])
Expand All @@ -14,7 +15,7 @@ def setUp(self):

def testRaid(self):

with self.assertRaisesRegexp(TypeError, "Can't instantiate abstract class"):
with self.assertRaisesRegex(TypeError, "Can't instantiate abstract class"):
raid.ErsatzRAID()

##
Expand Down Expand Up @@ -146,11 +147,11 @@ def testRaid(self):
##
## __init__
##
with self.assertRaisesRegexp(errors.RaidError, "invalid RAID level"):
with self.assertRaisesRegex(errors.RaidError, "invalid RAID level"):
self.levels_none.raidLevel(10)

with self.assertRaisesRegexp(errors.RaidError, "invalid RAID level"):
with self.assertRaisesRegex(errors.RaidError, "invalid RAID level"):
self.levels_some.raidLevel(10)

with self.assertRaisesRegexp(errors.RaidError, "invalid standard RAID level descriptor"):
with self.assertRaisesRegex(errors.RaidError, "invalid standard RAID level descriptor"):
raid.RAIDLevels(["raid3.1415"])
42 changes: 22 additions & 20 deletions tests/devices_test/device_properties_test.py
Expand Up @@ -26,6 +26,8 @@

from blivet.formats import getFormat

from tests import BlivetTestCase

def xform(func):
""" Simple wrapper function that transforms a function that takes
a precalculated value and a message to a function that takes
Expand All @@ -40,7 +42,7 @@ def xform(func):
"""
return lambda d, a: func(getattr(d, a), a)

class DeviceStateTestCase(unittest.TestCase):
class DeviceStateTestCase(BlivetTestCase):
"""A class which implements a simple method of checking the state
of a device object.
"""
Expand All @@ -61,7 +63,7 @@ def __init__(self, methodName='runTest'):
"parents" : xform(lambda x, m: self.assertEqual(len(x), 0, m) and
self.assertIsInstance(x, ParentList, m)),
"partitionable" : xform(self.assertFalse),
"path" : xform(lambda x, m: self.assertRegexpMatches(x, "^/dev", m)),
"path" : xform(lambda x, m: self.assertRegex(x, "^/dev", m)),
"raw_device" : xform(self.assertIsNotNone),
"resizable" : xform(self.assertFalse),
"size" : xform(lambda x, m: self.assertEqual(x, Size(0), m)),
Expand Down Expand Up @@ -517,35 +519,35 @@ def testMDRaidArrayDeviceInit(self):
parents=xform(lambda x, m: self.assertEqual(len(x), 2, m)),
uuid=xform(lambda x, m: self.assertEqual(x, self.dev20.uuid, m)))

with self.assertRaisesRegexp(DeviceError, "invalid"):
with self.assertRaisesRegex(DeviceError, "invalid"):
MDRaidArrayDevice("dev")

with self.assertRaisesRegexp(DeviceError, "invalid"):
with self.assertRaisesRegex(DeviceError, "invalid"):
MDRaidArrayDevice("dev", level="raid2")

with self.assertRaisesRegexp(DeviceError, "invalid"):
with self.assertRaisesRegex(DeviceError, "invalid"):
MDRaidArrayDevice(
"dev",
parents=[StorageDevice("parent", fmt=getFormat("mdmember"))])

with self.assertRaisesRegexp(DeviceError, "at least 2 members"):
with self.assertRaisesRegex(DeviceError, "at least 2 members"):
MDRaidArrayDevice(
"dev",
level="raid0",
parents=[StorageDevice("parent", fmt=getFormat("mdmember"))])

with self.assertRaisesRegexp(DeviceError, "invalid"):
with self.assertRaisesRegex(DeviceError, "invalid"):
MDRaidArrayDevice("dev", level="junk")

with self.assertRaisesRegexp(DeviceError, "at least 2 members"):
with self.assertRaisesRegex(DeviceError, "at least 2 members"):
MDRaidArrayDevice("dev", level=0, memberDevices=2)

def testMDRaidArrayDeviceMethods(self):
"""Test for method calls on initialized MDRaidDevices."""
with self.assertRaisesRegexp(DeviceError, "invalid" ):
with self.assertRaisesRegex(DeviceError, "invalid" ):
self.dev7.level = "junk"

with self.assertRaisesRegexp(DeviceError, "invalid" ):
with self.assertRaisesRegex(DeviceError, "invalid" ):
self.dev7.level = None

class BTRFSDeviceTestCase(DeviceStateTestCase):
Expand Down Expand Up @@ -615,26 +617,26 @@ def testBTRFSDeviceInit(self):
size=xform(lambda x, m: self.assertEqual(x, Size("32 MiB"), m)),
type=xform(lambda x, m: self.assertEqual(x, "btrfs volume", m)))

with self.assertRaisesRegexp(ValueError, "BTRFSDevice.*must have at least one parent"):
with self.assertRaisesRegex(ValueError, "BTRFSDevice.*must have at least one parent"):
BTRFSVolumeDevice("dev")

with self.assertRaisesRegexp(ValueError, "format"):
with self.assertRaisesRegex(ValueError, "format"):
BTRFSVolumeDevice("dev", parents=[StorageDevice("deva", size=btrfs.MIN_MEMBER_SIZE)])

with self.assertRaisesRegexp(DeviceError, "btrfs subvolume.*must be a btrfs volume"):
with self.assertRaisesRegex(DeviceError, "btrfs subvolume.*must be a btrfs volume"):
fmt = blivet.formats.getFormat("btrfs")
device = StorageDevice("deva", fmt=fmt, size=btrfs.MIN_MEMBER_SIZE)
BTRFSSubVolumeDevice("dev1", parents=[device])

deva = OpticalDevice("deva", fmt=blivet.formats.getFormat("btrfs"))
with self.assertRaisesRegexp(BTRFSValueError, "at least"):
with self.assertRaisesRegex(BTRFSValueError, "at least"):
BTRFSVolumeDevice("dev1", dataLevel="raid1", parents=[deva])

deva = StorageDevice("deva", fmt=blivet.formats.getFormat("btrfs"), size=btrfs.MIN_MEMBER_SIZE)
self.assertIsNotNone(BTRFSVolumeDevice("dev1", metaDataLevel="dup", parents=[deva]))

deva = StorageDevice("deva", fmt=blivet.formats.getFormat("btrfs"), size=btrfs.MIN_MEMBER_SIZE)
with self.assertRaisesRegexp(BTRFSValueError, "invalid"):
with self.assertRaisesRegex(BTRFSValueError, "invalid"):
BTRFSVolumeDevice("dev1", dataLevel="dup", parents=[deva])

self.assertEqual(self.dev1.isleaf, False)
Expand All @@ -659,24 +661,24 @@ def testBTRFSDeviceMethods(self):
self.assertIsNotNone(self.dev2.volume)

# size
with self.assertRaisesRegexp(RuntimeError, "cannot directly set size of btrfs volume"):
with self.assertRaisesRegex(RuntimeError, "cannot directly set size of btrfs volume"):
self.dev1.size = 32

def testBTRFSSnapShotDeviceInit(self):
parents = [StorageDevice("p1", fmt=blivet.formats.getFormat("btrfs"), size=btrfs.MIN_MEMBER_SIZE)]
vol = BTRFSVolumeDevice("test", parents=parents)
with self.assertRaisesRegexp(ValueError, "non-existent btrfs snapshots must have a source"):
with self.assertRaisesRegex(ValueError, "non-existent btrfs snapshots must have a source"):
BTRFSSnapShotDevice("snap1", parents=[vol])

with self.assertRaisesRegexp(ValueError, "btrfs snapshot source must already exist"):
with self.assertRaisesRegex(ValueError, "btrfs snapshot source must already exist"):
BTRFSSnapShotDevice("snap1", parents=[vol], source=vol)

with self.assertRaisesRegexp(ValueError, "btrfs snapshot source must be a btrfs subvolume"):
with self.assertRaisesRegex(ValueError, "btrfs snapshot source must be a btrfs subvolume"):
BTRFSSnapShotDevice("snap1", parents=[vol], source=parents[0])

parents2 = [StorageDevice("p1", fmt=blivet.formats.getFormat("btrfs"), size=btrfs.MIN_MEMBER_SIZE)]
vol2 = BTRFSVolumeDevice("test2", parents=parents2, exists=True)
with self.assertRaisesRegexp(ValueError, ".*snapshot and source must be in the same volume"):
with self.assertRaisesRegex(ValueError, ".*snapshot and source must be in the same volume"):
BTRFSSnapShotDevice("snap1", parents=[vol], source=vol2)

vol.exists = True
Expand Down

0 comments on commit 510b86f

Please sign in to comment.