@@ -47,7 +47,7 @@ def extract_stdout(mock_stdout, last_line_only=False):
47
47
pass
48
48
return out
49
49
50
- class TestFstab (unittest .TestCase ):
50
+ class TestMounting (unittest .TestCase ):
51
51
52
52
def setUp (self ):
53
53
self .testdir = os .path .dirname (os .path .abspath (__file__ ))
@@ -61,44 +61,32 @@ def setUp(self):
61
61
def tearDown (self ):
62
62
shutil .rmtree (self .sandbox )
63
63
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
92
69
apt_btrfs = AptBtrfsSnapshot (
93
70
fstab = os .path .join (self .testdir , "data" , "fstab" ))
94
71
mp = apt_btrfs .mp
95
- self .assertTrue (apt_btrfs . commands . mount .called )
72
+ self .assertTrue (mock_mount .called )
96
73
self .assertTrue ("apt-btrfs-snapshot-mp-" in mp )
97
74
self .assertTrue (os .path .exists (mp ))
98
- commands = apt_btrfs .commands
99
75
del apt_btrfs
100
- self .assertTrue (commands . umount .called )
76
+ self .assertTrue (mock_umount .called )
101
77
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 )
102
90
103
91
def test_parser_older_than_to_datetime (self ):
104
92
apt_btrfs = AptBtrfsSnapshot (
@@ -110,39 +98,44 @@ def test_parser_older_than_to_datetime(self):
110
98
self .assertTrue (e - t < datetime .timedelta (0 , 1 ))
111
99
112
100
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 )
113
117
class TestSnapshotting (unittest .TestCase ):
114
118
""" A lengthy setUp function copies a model subvolume tree with parent
115
119
links and some package change info. A couple of LowLevelCommand
116
120
functions are overwritten. All that allows test functions to do some
117
121
real work on the model subvolume tree without any risk of messing
118
122
anything up.
119
123
"""
120
- def setUp (self ):
124
+ def setUp (self ):#, mock_btrfs_subvolume_snapshot, mock_btrfs_delete_snapshot):
125
+
121
126
self .testdir = os .path .dirname (os .path .abspath (__file__ ))
127
+
122
128
# make a copy of a model btrfs subvol tree
123
129
model_root = os .path .join (self .testdir , "data" , "model_root" )
124
130
self .sandbox = os .path .join (self .testdir , "data" , "root3" )
125
131
if os .path .exists (self .sandbox ):
126
132
shutil .rmtree (self .sandbox )
127
133
shutil .copytree (model_root , self .sandbox , symlinks = True )
134
+
128
135
# setup snapshot class
129
136
self .apt_btrfs = AptBtrfsSnapshot (
130
137
fstab = os .path .join (self .testdir , "data" , "fstab" ),
131
138
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
146
139
147
140
def tearDown (self ):
148
141
del self .apt_btrfs
@@ -194,9 +187,10 @@ def test_btrfs_create_snapshot(self, mock_stdout):
194
187
self .assert_child_parent_linked (newdir ,
195
188
SNAP_PREFIX + "2013-08-06_13:26:30" )
196
189
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 ])
200
194
201
195
history = self .load_changes (newdir )
202
196
self .assertEqual (len (history ['install' ]), 10 )
@@ -278,9 +272,10 @@ def test_btrfs_set_default(self, mock_stdout, mock_stdin):
278
272
self .assert_child_parent_linked ("@" ,
279
273
SNAP_PREFIX + "2013-08-01_19:53:16" )
280
274
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 ])
284
279
285
280
@mock .patch ('sys.stdin' )
286
281
@mock .patch ('sys.stdout' )
0 commit comments