Skip to content

Commit b9cab9d

Browse files
committed
Added test for mount failure and corrected function mocking
Added class-wide LowLevelCommand mocking for TestSnapshotting class.
1 parent e61beb1 commit b9cab9d

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

test/test_apt_btrfs_snapshot.py

+48-53
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def extract_stdout(mock_stdout, last_line_only=False):
4747
pass
4848
return out
4949

50-
class TestFstab(unittest.TestCase):
50+
class TestMounting(unittest.TestCase):
5151

5252
def setUp(self):
5353
self.testdir = os.path.dirname(os.path.abspath(__file__))
@@ -61,44 +61,32 @@ def setUp(self):
6161
def tearDown(self):
6262
shutil.rmtree(self.sandbox)
6363

64-
@mock.patch('os.path.exists')
65-
def test_fstab_detect_snapshot(self, mock_commands):
66-
#Using python-mock 0.7 style, for precise compatibility
67-
mock_commands.side_effect = lambda f: f in ('/sbin/btrf')
68-
self.assertFalse(supported(
69-
fstab=os.path.join(self.testdir, "data", "fstab")))
70-
mock_commands.side_effect = lambda f: f in ('/sbin/btrfs')
71-
self.assertTrue(supported(
72-
fstab=os.path.join(self.testdir, "data", "fstab")))
73-
self.assertFalse(supported(
74-
fstab=os.path.join(self.testdir, "data", "fstab.no-btrfs")))
75-
self.assertFalse(supported(
76-
fstab=os.path.join(self.testdir, "data", "fstab.bug806065")))
77-
self.assertTrue(supported(
78-
fstab=os.path.join(self.testdir, "data", "fstab.bug872145")))
79-
80-
def test_fstab_get_uuid(self):
81-
fstab = Fstab(
82-
fstab=os.path.join(self.testdir, "data", "fstab"))
83-
self.assertEqual(fstab.uuid_for_mountpoint("/"),
84-
"UUID=fe63f598-1906-478e-acc7-f74740e78d1f")
85-
86-
@mock.patch('apt_btrfs_snapshot.LowLevelCommands')
87-
def test_mount_btrfs_root_volume(self, mock_commands):
88-
# mocking like this doesn't work. mock objects get returned
89-
# instead of the return_value
90-
mock_commands.mount.return_value = True
91-
mock_commands.umount.return_value = True
64+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.mount')
65+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.umount')
66+
def test_mount_btrfs_root_volume(self, mock_umount, mock_mount):
67+
mock_mount.return_value = True
68+
mock_umount.return_value = True
9269
apt_btrfs = AptBtrfsSnapshot(
9370
fstab=os.path.join(self.testdir, "data", "fstab"))
9471
mp = apt_btrfs.mp
95-
self.assertTrue(apt_btrfs.commands.mount.called)
72+
self.assertTrue(mock_mount.called)
9673
self.assertTrue("apt-btrfs-snapshot-mp-" in mp)
9774
self.assertTrue(os.path.exists(mp))
98-
commands = apt_btrfs.commands
9975
del apt_btrfs
100-
self.assertTrue(commands.umount.called)
76+
self.assertTrue(mock_umount.called)
10177
self.assertFalse(os.path.exists(mp))
78+
79+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.mount')
80+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.umount')
81+
def test_mount_btrfs_root_volume_fails(self, mock_umount, mock_mount):
82+
mock_mount.return_value = False
83+
mock_umount.return_value = True
84+
message = "Unable to mount root volume"
85+
with self.assertRaisesRegexp(Exception, message):
86+
apt_btrfs = AptBtrfsSnapshot(
87+
fstab=os.path.join(self.testdir, "data", "fstab"))
88+
self.assertTrue(mock_mount.called)
89+
self.assertFalse(mock_umount.called)
10290

10391
def test_parser_older_than_to_datetime(self):
10492
apt_btrfs = AptBtrfsSnapshot(
@@ -110,39 +98,44 @@ def test_parser_older_than_to_datetime(self):
11098
self.assertTrue(e - t < datetime.timedelta(0, 1))
11199

112100

101+
# fake low level snapshot
102+
def mock_snapshot_fn(source, dest):
103+
shutil.copytree(source, dest, symlinks=True)
104+
return True
105+
mock_snapshot = mock.Mock(side_effect=mock_snapshot_fn)
106+
107+
# fake low level delete
108+
def mock_delete_fn(which):
109+
shutil.rmtree(which)
110+
return True
111+
mock_delete = mock.Mock(side_effect=mock_delete_fn)
112+
113+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.btrfs_delete_snapshot',
114+
new=mock_delete)
115+
@mock.patch('apt_btrfs_snapshot.LowLevelCommands.btrfs_subvolume_snapshot',
116+
new=mock_snapshot)
113117
class TestSnapshotting(unittest.TestCase):
114118
""" A lengthy setUp function copies a model subvolume tree with parent
115119
links and some package change info. A couple of LowLevelCommand
116120
functions are overwritten. All that allows test functions to do some
117121
real work on the model subvolume tree without any risk of messing
118122
anything up.
119123
"""
120-
def setUp(self):
124+
def setUp(self):#, mock_btrfs_subvolume_snapshot, mock_btrfs_delete_snapshot):
125+
121126
self.testdir = os.path.dirname(os.path.abspath(__file__))
127+
122128
# make a copy of a model btrfs subvol tree
123129
model_root = os.path.join(self.testdir, "data", "model_root")
124130
self.sandbox = os.path.join(self.testdir, "data", "root3")
125131
if os.path.exists(self.sandbox):
126132
shutil.rmtree(self.sandbox)
127133
shutil.copytree(model_root, self.sandbox, symlinks=True)
134+
128135
# setup snapshot class
129136
self.apt_btrfs = AptBtrfsSnapshot(
130137
fstab=os.path.join(self.testdir, "data", "fstab"),
131138
sandbox=self.sandbox)
132-
# hack to replace low level snapshot command with a working copy func
133-
# that reports back on its working.
134-
# I couldn't see how to do this class-wide using mock
135-
self.args = []
136-
def mock_snapshot(source, dest):
137-
shutil.copytree(source, dest, symlinks=True)
138-
self.args = source, dest
139-
return True
140-
self.apt_btrfs.commands.btrfs_subvolume_snapshot = mock_snapshot
141-
# low level delete
142-
def mock_delete(which):
143-
shutil.rmtree(which)
144-
return True
145-
self.apt_btrfs.commands.btrfs_delete_snapshot = mock_delete
146139

147140
def tearDown(self):
148141
del self.apt_btrfs
@@ -194,9 +187,10 @@ def test_btrfs_create_snapshot(self, mock_stdout):
194187
self.assert_child_parent_linked(newdir,
195188
SNAP_PREFIX + "2013-08-06_13:26:30")
196189

197-
self.assertTrue(len(self.args), 2)
198-
self.assertTrue(self.args[0].endswith("@"))
199-
self.assertTrue(SNAP_PREFIX in self.args[1])
190+
args = LowLevelCommands.btrfs_subvolume_snapshot.call_args[0]
191+
self.assertTrue(len(args), 2)
192+
self.assertTrue(args[0].endswith("@"))
193+
self.assertTrue(SNAP_PREFIX in args[1])
200194

201195
history = self.load_changes(newdir)
202196
self.assertEqual(len(history['install']), 10)
@@ -278,9 +272,10 @@ def test_btrfs_set_default(self, mock_stdout, mock_stdin):
278272
self.assert_child_parent_linked("@",
279273
SNAP_PREFIX + "2013-08-01_19:53:16")
280274

281-
self.assertTrue(len(self.args), 2)
282-
self.assertTrue(self.args[1].endswith("@apt-btrfs-staging"))
283-
self.assertTrue(SNAP_PREFIX + "" in self.args[0])
275+
args = LowLevelCommands.btrfs_subvolume_snapshot.call_args[0]
276+
self.assertTrue(len(args), 2)
277+
self.assertTrue(args[1].endswith("@apt-btrfs-staging"))
278+
self.assertTrue(SNAP_PREFIX + "" in args[0])
284279

285280
@mock.patch('sys.stdin')
286281
@mock.patch('sys.stdout')

0 commit comments

Comments
 (0)