11import unittest
2- from datetime import datetime
2+ from datetime import datetime , timedelta , date as dt_date
33import zfslib as zfs
44from zfslib_test_tools import *
55
@@ -83,9 +83,8 @@ def test_poolset_get_pool(self):
8383 self .assertIsInstance (pool , zfs .Pool )
8484 self .assertEqual (pool .name , pname )
8585
86- with self .assertRaises (KeyError ):
87- poolset .get_pool ('pool' )
88- poolset .get_pool ('*pool' )
86+ with self .assertRaises (KeyError ): poolset .get_pool ('pool' )
87+ with self .assertRaises (KeyError ): poolset .get_pool ('*pool' )
8988
9089
9190 # Test PoolSet.lookup and properties: name
@@ -95,9 +94,8 @@ def test_poolset_lookup_pool(self):
9594 self .assertEqual (pool .name , 'dpool' )
9695
9796 # Lookup should fail unless pool name is specified
98- with self .assertRaises (KeyError ):
99- poolset .lookup ('pool' )
100- poolset .lookup ('*pool' )
97+ with self .assertRaises (KeyError ): poolset .lookup ('pool' )
98+ with self .assertRaises (KeyError ): poolset .lookup ('*pool' )
10199
102100
103101 # Test PoolSet.lookup, ZFSItem.lookup and properties: name, pool, dataset, parent, path, dspath
@@ -127,9 +125,8 @@ def test_poolset_lookup_dataset(self):
127125 self .assertIsInstance (ds .parent , zfs .Pool )
128126
129127 # Lookup should fail unless full path to dataset is specified
130- with self .assertRaises (KeyError ):
131- poolset .lookup ('bpool/*OOT' )
132- poolset .lookup ('bpool/OOT' )
128+ with self .assertRaises (KeyError ): poolset .lookup ('bpool/*OOT' )
129+ with self .assertRaises (KeyError ): poolset .lookup ('bpool/OOT' )
133130
134131
135132 # Test PoolSet.lookup, ZfsItem.lookup, ZfsItem.get_child, and properties: name, pool, dataset, parent, path
@@ -157,10 +154,9 @@ def test_poolset_lookup_snapshot(self):
157154 self .assertEqual (snap .get_property ('creation' ), cdate_c )
158155
159156 # Lookup should fail unless full path to snapshot is specified
160- with self .assertRaises (KeyError ):
161- poolset .lookup ('BOOT/ubuntu_n2qr5q@autozsys_68frge' )
162- poolset .lookup ('autozsys_68frge' )
163- poolset .lookup ('@autozsys_68frge' )
157+ with self .assertRaises (KeyError ): poolset .lookup ('BOOT/ubuntu_n2qr5q@autozsys_68frge' )
158+ with self .assertRaises (KeyError ): poolset .lookup ('autozsys_68frge' )
159+ with self .assertRaises (KeyError ): poolset .lookup ('@autozsys_68frge' )
164160
165161
166162
@@ -180,16 +176,15 @@ def test_zfsitem_get_child_n_properties(self):
180176 self .assertIsInstance (snap , zfs .Snapshot )
181177
182178 # Negative tests
183- with self .assertRaises (KeyError ):
184- ds2 .get_child ('foo' )
185- ds3 .get_child ('bar' )
179+ with self .assertRaises (KeyError ): ds2 .get_child ('foo' )
180+ with self .assertRaises (KeyError ): ds3 .get_child ('bar' )
186181
187182
188183 ds = poolset .lookup ('dpool/other' )
189184 self .assertIsInstance (ds , zfs .Dataset )
190185 s_ts = '1608446351'
191186 self .assertEqual (ds .get_property ('creation' ), s_ts )
192- self .assertEqual (ds .creation , datetime . fromtimestamp ( int ( s_ts ) ))
187+ self .assertEqual (ds .creation , dt_from_creation ( s_ts ))
193188 self .assertEqual (ds .get_property ('used' ), '280644734976' )
194189 self .assertEqual (ds .get_property ('available' ), '3564051083264' )
195190 self .assertEqual (ds .get_property ('referenced' ), '266918285312' )
@@ -242,11 +237,10 @@ def test_pool_get_datasets_etc(self):
242237
243238 # Negative Tests
244239 pool = poolset .lookup ('rpool' )
245- with self .assertRaises (KeyError ):
246- pool .get_dataset ('foo' )
247- pool .get_dataset ('ubuntu_n2qr5q/srv' )
248- pool .get_dataset ('OOT/ubuntu_n2qr5q/srv' )
249- pool .get_dataset ('*OOT/ubuntu_n2qr5q/srv' )
240+ with self .assertRaises (KeyError ): pool .get_dataset ('foo' )
241+ with self .assertRaises (KeyError ): pool .get_dataset ('ubuntu_n2qr5q/srv' )
242+ with self .assertRaises (KeyError ): pool .get_dataset ('OOT/ubuntu_n2qr5q/srv' )
243+ with self .assertRaises (KeyError ): pool .get_dataset ('*OOT/ubuntu_n2qr5q/srv' )
250244
251245
252246
@@ -278,15 +272,117 @@ def test_snapable_get_snapshots(self):
278272
279273
280274 # Negative tests
281- with self .assertRaises (AssertionError ):
282- ds .get_snapshots (flt = None )
283- ds .get_snapshots (index = 'test' )
275+ with self .assertRaises (AssertionError ): ds .get_snapshots (flt = None )
276+ with self .assertRaises (AssertionError ): ds .get_snapshots (index = 'test' )
277+
278+
279+ def test_snapable_get_snapshot (self ):
280+ for pool in poolset :
281+ self .assertIsInstance (pool , zfs .Pool )
282+ all_ds = pool .get_all_datasets (with_depth = True )
283+
284+ for (depth , ds ) in all_ds :
285+ snaps = ds .get_all_snapshots ()
286+ for snap in snaps :
287+ self .assertIsInstance (snap , zfs .Snapshot )
288+ snap2 = snap .dataset .get_snapshot (snap .name )
289+ self .assertIs (snap , snap2 )
290+
291+ # Negative tests
292+ ds = poolset .lookup ('rpool/ROOT/ubuntu_n2qr5q/var/spool' )
293+ with self .assertRaises (KeyError ): ds .get_snapshot ('*utozsys_pfofay' )
294+ with self .assertRaises (KeyError ): ds .get_snapshot ('foobar' )
295+
296+ # Notes:
297+ # contains flag cannot be tested with static data. Must move to dynamic testing
298+ def test_snapable_find_snapshots (self ):
299+ ds = poolset .lookup ('rpool/USERDATA/jbloggs_jb327m' )
300+ self .assertIsInstance (ds , zfs .Dataset )
301+
302+ all_snaps = ds .get_all_snapshots ()
303+
304+ # Empty options
305+ snaps = ds .find_snapshots ({})
306+ self .assertEqual (len (snaps ), len (all_snaps ))
307+
308+ # Name only
309+ snaps = ds .find_snapshots ({'name' : '*zsys_w*' })
310+ self .assertEqual (len (snaps ), 3 )
311+
312+ snaps = ds .find_snapshots ({'name' : '*' })
313+ self .assertEqual (len (snaps ), len (all_snaps ))
314+
315+ # Name and dt_from
316+ snaps = ds .find_snapshots ({'name' : '*zsys_*e*'
317+ , 'dt_from' : dt_from_creation ('1609272411' )})
318+ self .assertEqual (len (snaps ), 2 )
319+
320+ # Name and tdelta and dt_to
321+ # . Note: tdelta alone cannot be used for static unit testing
322+ snaps = ds .find_snapshots ({'name' : '*zsys_*e*' , 'tdelta' : timedelta (hours = 36 )
323+ ,'dt_to' : dt_from_creation ('1609362247' )})
324+ self .assertEqual (len (snaps ), 5 )
325+ snaps2 = ds .find_snapshots ({'name' : '*zsys_*e*' , 'tdelta' : '36H'
326+ ,'dt_to' : dt_from_creation ('1609362247' )})
327+ self .assertEqual (len (snaps2 ), len (snaps ))
328+ for i , snap in enumerate (snaps ):
329+ self .assertIs (snap , snaps2 [i ])
330+
331+ # Name, dt_from and tdelta
332+ snaps = ds .find_snapshots ({'name' : '*zsys_*w*'
333+ ,'dt_from' : dt_from_creation ('1608233673' ), 'tdelta' : timedelta (hours = 48 )})
334+ self .assertEqual (len (snaps ), 5 )
335+ snaps2 = ds .find_snapshots ({'name' : '*zsys_*w*'
336+ ,'dt_from' : dt_from_creation ('1608233673' ), 'tdelta' : '48H' })
337+ self .assertEqual (len (snaps2 ), len (snaps ))
338+ for i , snap in enumerate (snaps ):
339+ self .assertIs (snap , snaps2 [i ])
340+
341+ # Name, dt_from and dt_to
342+ snaps = ds .find_snapshots ({'name' : '*zsys_*w*'
343+ ,'dt_from' : dt_from_creation ('1608233673' )
344+ ,'dt_to' : dt_from_creation ('1608772856' )})
345+ self .assertEqual (len (snaps ), 6 )
346+
347+ # Name, dt_from and dt_to using date instead of datetime
348+ snaps = ds .find_snapshots ({'name' : '*zsys_*w*'
349+ ,'dt_from' : dt_date (2020 , 12 , 17 )
350+ ,'dt_to' : dt_date (2020 , 12 , 23 )})
351+ self .assertEqual (len (snaps ), 5 )
352+
353+ for i , snap in enumerate (snaps ):
354+ self .assertIsInstance (snap , zfs .Snapshot )
355+
356+ # Name, dt_from and dt_to using date instead of datetime with index=True
357+ snaps = ds .find_snapshots ({'name' : '*zsys_*w*' , "index" : True
358+ ,'dt_from' : dt_date (2020 , 12 , 17 )
359+ ,'dt_to' : dt_date (2020 , 12 , 23 )})
360+ self .assertEqual (len (snaps ), 5 )
361+ self .assertIsInstance (snaps [0 ], tuple )
362+ self .assertEqual (len (snaps [0 ]), 2 )
363+ self .assertIsInstance (snaps [0 ][0 ], int )
364+ self .assertIsInstance (snaps [0 ][1 ], zfs .Snapshot )
365+
366+ with self .assertRaises (TypeError ): snaps = ds .find_snapshots ()
367+ with self .assertRaises (AssertionError ): snaps = ds .find_snapshots ({'name' : True })
368+ with self .assertRaises (AssertionError ): snaps = ds .find_snapshots ({'dt_from' : 'asdf' })
369+ with self .assertRaises (AssertionError ): snaps = ds .find_snapshots ({'dt_to' : 'asdf' })
370+ with self .assertRaises (AssertionError ): snaps = ds .find_snapshots ({'tdelta' : 10 })
371+ with self .assertRaises (KeyError ): snaps = ds .find_snapshots ({'tdelta' : '-10H' })
372+ with self .assertRaises (AssertionError ): snaps = ds .find_snapshots ({'index' : 1 })
373+ with self .assertRaises (KeyError ):
374+ snaps = ds .find_snapshots ({'dt_to' : dt_date (2020 , 12 , 20 )
375+ , 'dt_from' : dt_date (2020 , 12 , 21 )})
376+ with self .assertRaises (KeyError ):
377+ snaps = ds .find_snapshots ({'dt_to' : dt_date (2020 , 12 , 21 )
378+ ,'dt_from' : dt_date (2020 , 12 , 20 )
379+ ,'tdelta' : "1H" })
380+
381+
284382
285383
286384 # TODO:
287- # [.] - Write tests for Snapable.get_snapshot
288- # [.] - Write tests for Snapable.find_snapshots
289- # [.] - Write tests for Dataset.find_snapshots
385+ # [.] - Write tests for Dataset.get_diffs
290386 # Not sure how to write this it needs dynamic data calls
291387 # [.] - Write tests for Dataset.get_rel_path
292388 # [.] - Write tests for Dataset.assertHaveMounts
0 commit comments