Debian Codegraph @

tests/test_split.py

raw
1 # vim: set ft=python ts=4 sw=4 expandtab:
2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
3 #
4 # C E D A R
5 # S O L U T I O N S "Software done right."
6 # S O F T W A R E
7 #
8 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
9 #
10 # Copyright (c) 2007-2008,2010,2015 Kenneth J. Pronovici.
11 # All rights reserved.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License,
15 # Version 2, as published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 #
21 # Copies of the GNU General Public License are available from
22 # the Free Software Foundation website, http://www.gnu.org/.
23 #
24 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
25 #
26 # Author : Kenneth J. Pronovici <[email protected]>
27 # Language : Python 3
28 # Project : Cedar Backup, release 3
29 # Purpose : Tests split extension functionality.
30 #
31 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
32
33 ########################################################################
34 # Module documentation
35 ########################################################################
36
37 """
38 Unit tests for CedarBackup3/extend/split.py.
39
40 Code Coverage
41 =============
42
43 This module contains individual tests for the the public classes implemented
44 in extend/split.py. There are also tests for some of the private
45 functions.
46
47 Naming Conventions
48 ==================
49
50 I prefer to avoid large unit tests which validate more than one piece of
51 functionality, and I prefer to avoid using overly descriptive (read: long)
52 test names, as well. Instead, I use lots of very small tests that each
53 validate one specific thing. These small tests are then named with an index
54 number, yielding something like ``testAddDir_001`` or ``testValidate_010``.
55 Each method has a docstring describing what it's supposed to accomplish. I
56 feel that this makes it easier to judge how important a given failure is,
57 and also makes it somewhat easier to diagnose and fix individual problems.
58
59 Testing XML Extraction
60 ======================
61
62 It's difficult to validated that generated XML is exactly "right",
63 especially when dealing with pretty-printed XML. We can't just provide a
64 constant string and say "the result must match this". Instead, what we do
65 is extract a node, build some XML from it, and then feed that XML back into
66 another object's constructor. If that parse process succeeds and the old
67 object is equal to the new object, we assume that the extract was
68 successful.
69
70 It would arguably be better if we could do a completely independent check -
71 but implementing that check would be equivalent to re-implementing all of
72 the existing functionality that we're validating here! After all, the most
73 important thing is that data can move seamlessly from object to XML document
74 and back to object.
75
76 Full vs. Reduced Tests
77 ======================
78
79 Some Cedar Backup regression tests require a specialized environment in
80 order to run successfully. This environment won't necessarily be available
81 on every build system out there (for instance, on a Debian autobuilder).
82 Because of this, the default behavior is to run a "reduced feature set" test
83 suite that has no surprising system, kernel or network requirements. If you
84 want to run all of the tests, set SPLITTESTS_FULL to "Y" in the environment.
85
86 In this module, the primary dependency is that the split utility must be
87 available. There is also one test that wants at least one non-English
88 locale (fr_FR, ru_RU or pt_PT) available to check localization issues (but
89 that test will just automatically be skipped if such a locale is not
90 available).
91
92 @author Kenneth J. Pronovici <[email protected]>
93 """
94
95
96 ########################################################################
97 # Import modules and do runtime validations
98 ########################################################################
99
100 import os
101 import tempfile
102 import unittest
103
104 from CedarBackup3.extend.split import ByteQuantity, LocalConfig, SplitConfig, _splitDailyDir, _splitFile
105 from CedarBackup3.testutil import (
106 availableLocales,
107 buildPath,
108 configureLogging,
109 extractTar,
110 failUnlessAssignRaises,
111 findResources,
112 removedir,
113 )
114 from CedarBackup3.util import UNIT_BYTES, UNIT_GBYTES, UNIT_KBYTES, UNIT_MBYTES, pathJoin
115 from CedarBackup3.xmlutil import createOutputDom, serializeDom
116
117 #######################################################################
118 # Module-wide configuration and constants
119 #######################################################################
120
121 DATA_DIRS = [
122 "./data",
123 "./tests/data",
124 ]
125 RESOURCES = [
126 "split.conf.1",
127 "split.conf.2",
128 "split.conf.3",
129 "split.conf.4",
130 "split.conf.5",
131 "tree21.tar.gz",
132 ]
133
134 INVALID_PATH = "bogus" # This path name should never exist
135
136
137 #######################################################################
138 # Utility functions
139 #######################################################################
140
141
142 def runAllTests():
143 """Returns true/false depending on whether the full test suite should be run."""
144 if "SPLITTESTS_FULL" in os.environ:
145 return os.environ["SPLITTESTS_FULL"] == "Y"
146 else:
147 return False
148
149
150 #######################################################################
151 # Test Case Classes
152 #######################################################################
153
154 ##########################
155 # TestSplitConfig class
156 ##########################
157
158
159 class TestSplitConfig(unittest.TestCase):
160 """Tests for the SplitConfig class."""
161
162 ################
163 # Setup methods
164 ################
165
166 @classmethod
167 def setUpClass(cls):
168 configureLogging()
169
170 ##################
171 # Utility methods
172 ##################
173
174 def failUnlessAssignRaises(self, exception, obj, prop, value):
175 """Equivalent of :any:`failUnlessRaises`, but used for property assignments instead."""
176 failUnlessAssignRaises(self, exception, obj, prop, value)
177
178 ############################
179 # Test __repr__ and __str__
180 ############################
181
182 def testStringFuncs_001(self):
183 """
184 Just make sure that the string functions don't have errors (i.e. bad variable names).
185 """
186 obj = SplitConfig()
187 obj.__repr__()
188 obj.__str__()
189
190 ##################################
191 # Test constructor and attributes
192 ##################################
193
194 def testConstructor_001(self):
195 """
196 Test constructor with no values filled in.
197 """
198 split = SplitConfig()
199 self.assertEqual(None, split.sizeLimit)
200 self.assertEqual(None, split.splitSize)
201
202 def testConstructor_002(self):
203 """
204 Test constructor with all values filled in, with valid values.
205 """
206 split = SplitConfig(ByteQuantity("1.0", UNIT_BYTES), ByteQuantity("2.0", UNIT_KBYTES))
207 self.assertEqual(ByteQuantity("1.0", UNIT_BYTES), split.sizeLimit)
208 self.assertEqual(ByteQuantity("2.0", UNIT_KBYTES), split.splitSize)
209
210 def testConstructor_003(self):
211 """
212 Test assignment of sizeLimit attribute, None value.
213 """
214 split = SplitConfig(sizeLimit=ByteQuantity("1.0", UNIT_BYTES))
215 self.assertEqual(ByteQuantity("1.0", UNIT_BYTES), split.sizeLimit)
216 split.sizeLimit = None
217 self.assertEqual(None, split.sizeLimit)
218
219 def testConstructor_004(self):
220 """
221 Test assignment of sizeLimit attribute, valid value.
222 """
223 split = SplitConfig()
224 self.assertEqual(None, split.sizeLimit)
225 split.sizeLimit = ByteQuantity("1.0", UNIT_BYTES)
226 self.assertEqual(ByteQuantity("1.0", UNIT_BYTES), split.sizeLimit)
227
228 def testConstructor_005(self):
229 """
230 Test assignment of sizeLimit attribute, invalid value (empty).
231 """
232 split = SplitConfig()
233 self.assertEqual(None, split.sizeLimit)
234 self.failUnlessAssignRaises(ValueError, split, "sizeLimit", "")
235 self.assertEqual(None, split.sizeLimit)
236
237 def testConstructor_006(self):
238 """
239 Test assignment of sizeLimit attribute, invalid value (not a ByteQuantity).
240 """
241 split = SplitConfig()
242 self.assertEqual(None, split.sizeLimit)
243 self.failUnlessAssignRaises(ValueError, split, "sizeLimit", "1.0 GB")
244 self.assertEqual(None, split.sizeLimit)
245
246 def testConstructor_007(self):
247 """
248 Test assignment of splitSize attribute, None value.
249 """
250 split = SplitConfig(splitSize=ByteQuantity("1.00", UNIT_KBYTES))
251 self.assertEqual(ByteQuantity("1.00", UNIT_KBYTES), split.splitSize)
252 split.splitSize = None
253 self.assertEqual(None, split.splitSize)
254
255 def testConstructor_008(self):
256 """
257 Test assignment of splitSize attribute, valid value.
258 """
259 split = SplitConfig()
260 self.assertEqual(None, split.splitSize)
261 split.splitSize = ByteQuantity("1.00", UNIT_KBYTES)
262 self.assertEqual(ByteQuantity("1.00", UNIT_KBYTES), split.splitSize)
263
264 def testConstructor_009(self):
265 """
266 Test assignment of splitSize attribute, invalid value (empty).
267 """
268 split = SplitConfig()
269 self.assertEqual(None, split.splitSize)
270 self.failUnlessAssignRaises(ValueError, split, "splitSize", "")
271 self.assertEqual(None, split.splitSize)
272
273 def testConstructor_010(self):
274 """
275 Test assignment of splitSize attribute, invalid value (not a ByteQuantity).
276 """
277 split = SplitConfig()
278 self.assertEqual(None, split.splitSize)
279 self.failUnlessAssignRaises(ValueError, split, "splitSize", 12)
280 self.assertEqual(None, split.splitSize)
281
282 ############################
283 # Test comparison operators
284 ############################
285
286 def testComparison_001(self):
287 """
288 Test comparison of two identical objects, all attributes None.
289 """
290 split1 = SplitConfig()
291 split2 = SplitConfig()
292 self.assertEqual(split1, split2)
293 self.assertTrue(split1 == split2)
294 self.assertTrue(not split1 < split2)
295 self.assertTrue(split1 <= split2)
296 self.assertTrue(not split1 > split2)
297 self.assertTrue(split1 >= split2)
298 self.assertTrue(not split1 != split2)
299
300 def testComparison_002(self):
301 """
302 Test comparison of two identical objects, all attributes non-None.
303 """
304 split1 = SplitConfig(ByteQuantity("99", UNIT_KBYTES), ByteQuantity("1.00", UNIT_MBYTES))
305 split2 = SplitConfig(ByteQuantity("99", UNIT_KBYTES), ByteQuantity("1.00", UNIT_MBYTES))
306 self.assertEqual(split1, split2)
307 self.assertTrue(split1 == split2)
308 self.assertTrue(not split1 < split2)
309 self.assertTrue(split1 <= split2)
310 self.assertTrue(not split1 > split2)
311 self.assertTrue(split1 >= split2)
312 self.assertTrue(not split1 != split2)
313
314 def testComparison_003(self):
315 """
316 Test comparison of two differing objects, sizeLimit differs (one None).
317 """
318 split1 = SplitConfig()
319 split2 = SplitConfig(sizeLimit=ByteQuantity("99", UNIT_KBYTES))
320 self.assertNotEqual(split1, split2)
321 self.assertTrue(not split1 == split2)
322 self.assertTrue(split1 < split2)
323 self.assertTrue(split1 <= split2)
324 self.assertTrue(not split1 > split2)
325 self.assertTrue(not split1 >= split2)
326 self.assertTrue(split1 != split2)
327
328 def testComparison_004(self):
329 """
330 Test comparison of two differing objects, sizeLimit differs.
331 """
332 split1 = SplitConfig(ByteQuantity("99", UNIT_BYTES), ByteQuantity("1.00", UNIT_MBYTES))
333 split2 = SplitConfig(ByteQuantity("99", UNIT_KBYTES), ByteQuantity("1.00", UNIT_MBYTES))
334 self.assertNotEqual(split1, split2)
335 self.assertTrue(not split1 == split2)
336 self.assertTrue(split1 < split2)
337 self.assertTrue(split1 <= split2)
338 self.assertTrue(not split1 > split2)
339 self.assertTrue(not split1 >= split2)
340 self.assertTrue(split1 != split2)
341
342 def testComparison_005(self):
343 """
344 Test comparison of two differing objects, splitSize differs (one None).
345 """
346 split1 = SplitConfig()
347 split2 = SplitConfig(splitSize=ByteQuantity("1.00", UNIT_MBYTES))
348 self.assertNotEqual(split1, split2)
349 self.assertTrue(not split1 == split2)
350 self.assertTrue(split1 < split2)
351 self.assertTrue(split1 <= split2)
352 self.assertTrue(not split1 > split2)
353 self.assertTrue(not split1 >= split2)
354 self.assertTrue(split1 != split2)
355
356 def testComparison_006(self):
357 """
358 Test comparison of two differing objects, splitSize differs.
359 """
360 split1 = SplitConfig(ByteQuantity("99", UNIT_KBYTES), ByteQuantity("0.5", UNIT_MBYTES))
361 split2 = SplitConfig(ByteQuantity("99", UNIT_KBYTES), ByteQuantity("1.00", UNIT_MBYTES))
362 self.assertNotEqual(split1, split2)
363 self.assertTrue(not split1 == split2)
364 self.assertTrue(split1 < split2)
365 self.assertTrue(split1 <= split2)
366 self.assertTrue(not split1 > split2)
367 self.assertTrue(not split1 >= split2)
368 self.assertTrue(split1 != split2)
369
370
371 ########################
372 # TestLocalConfig class
373 ########################
374
375
376 class TestLocalConfig(unittest.TestCase):
377 """Tests for the LocalConfig class."""
378
379 ################
380 # Setup methods
381 ################
382
383 @classmethod
384 def setUpClass(cls):
385 configureLogging()
386
387 def setUp(self):
388 try:
389 self.resources = findResources(RESOURCES, DATA_DIRS)
390 except Exception as e:
391 self.fail(e)
392
393 def tearDown(self):
394 pass
395
396 ##################
397 # Utility methods
398 ##################
399
400 def failUnlessAssignRaises(self, exception, obj, prop, value):
401 """Equivalent of :any:`failUnlessRaises`, but used for property assignments instead."""
402 failUnlessAssignRaises(self, exception, obj, prop, value)
403
404 def validateAddConfig(self, origConfig):
405 """
406 Validates that document dumped from ``LocalConfig.addConfig`` results in
407 identical object.
408
409 We dump a document containing just the split configuration, and then
410 make sure that if we push that document back into the ``LocalConfig``
411 object, that the resulting object matches the original.
412
413 The ``self.failUnlessEqual`` method is used for the validation, so if the
414 method call returns normally, everything is OK.
415
416 Args:
417 origConfig: Original configuration
418 """
419 (xmlDom, parentNode) = createOutputDom()
420 origConfig.addConfig(xmlDom, parentNode)
421 xmlData = serializeDom(xmlDom)
422 newConfig = LocalConfig(xmlData=xmlData, validate=False)
423 self.assertEqual(origConfig, newConfig)
424
425 ############################
426 # Test __repr__ and __str__
427 ############################
428
429 def testStringFuncs_001(self):
430 """
431 Just make sure that the string functions don't have errors (i.e. bad variable names).
432 """
433 obj = LocalConfig()
434 obj.__repr__()
435 obj.__str__()
436
437 #####################################################
438 # Test basic constructor and attribute functionality
439 #####################################################
440
441 def testConstructor_001(self):
442 """
443 Test empty constructor, validate=False.
444 """
445 config = LocalConfig(validate=False)
446 self.assertEqual(None, config.split)
447
448 def testConstructor_002(self):
449 """
450 Test empty constructor, validate=True.
451 """
452 config = LocalConfig(validate=True)
453 self.assertEqual(None, config.split)
454
455 def testConstructor_003(self):
456 """
457 Test with empty config document as both data and file, validate=False.
458 """
459 path = self.resources["split.conf.1"]
460 with open(path) as f:
461 contents = f.read()
462 self.assertRaises(ValueError, LocalConfig, xmlData=contents, xmlPath=path, validate=False)
463
464 def testConstructor_004(self):
465 """
466 Test assignment of split attribute, None value.
467 """
468 config = LocalConfig()
469 config.split = None
470 self.assertEqual(None, config.split)
471
472 def testConstructor_005(self):
473 """
474 Test assignment of split attribute, valid value.
475 """
476 config = LocalConfig()
477 config.split = SplitConfig()
478 self.assertEqual(SplitConfig(), config.split)
479
480 def testConstructor_006(self):
481 """
482 Test assignment of split attribute, invalid value (not SplitConfig).
483 """
484 config = LocalConfig()
485 self.failUnlessAssignRaises(ValueError, config, "split", "STRING!")
486
487 ############################
488 # Test comparison operators
489 ############################
490
491 def testComparison_001(self):
492 """
493 Test comparison of two identical objects, all attributes None.
494 """
495 config1 = LocalConfig()
496 config2 = LocalConfig()
497 self.assertEqual(config1, config2)
498 self.assertTrue(config1 == config2)
499 self.assertTrue(not config1 < config2)
500 self.assertTrue(config1 <= config2)
501 self.assertTrue(not config1 > config2)
502 self.assertTrue(config1 >= config2)
503 self.assertTrue(not config1 != config2)
504
505 def testComparison_002(self):
506 """
507 Test comparison of two identical objects, all attributes non-None.
508 """
509 config1 = LocalConfig()
510 config1.split = SplitConfig()
511
512 config2 = LocalConfig()
513 config2.split = SplitConfig()
514
515 self.assertEqual(config1, config2)
516 self.assertTrue(config1 == config2)
517 self.assertTrue(not config1 < config2)
518 self.assertTrue(config1 <= config2)
519 self.assertTrue(not config1 > config2)
520 self.assertTrue(config1 >= config2)
521 self.assertTrue(not config1 != config2)
522
523 def testComparison_003(self):
524 """
525 Test comparison of two differing objects, split differs (one None).
526 """
527 config1 = LocalConfig()
528 config2 = LocalConfig()
529 config2.split = SplitConfig()
530 self.assertNotEqual(config1, config2)
531 self.assertTrue(not config1 == config2)
532 self.assertTrue(config1 < config2)
533 self.assertTrue(config1 <= config2)
534 self.assertTrue(not config1 > config2)
535 self.assertTrue(not config1 >= config2)
536 self.assertTrue(config1 != config2)
537
538 def testComparison_004(self):
539 """
540 Test comparison of two differing objects, split differs.
541 """
542 config1 = LocalConfig()
543 config1.split = SplitConfig(sizeLimit=ByteQuantity("0.1", UNIT_MBYTES))
544
545 config2 = LocalConfig()
546 config2.split = SplitConfig(sizeLimit=ByteQuantity("1.00", UNIT_MBYTES))
547
548 self.assertNotEqual(config1, config2)
549 self.assertTrue(not config1 == config2)
550 self.assertTrue(config1 < config2)
551 self.assertTrue(config1 <= config2)
552 self.assertTrue(not config1 > config2)
553 self.assertTrue(not config1 >= config2)
554 self.assertTrue(config1 != config2)
555
556 ######################
557 # Test validate logic
558 ######################
559
560 def testValidate_001(self):
561 """
562 Test validate on a None split section.
563 """
564 config = LocalConfig()
565 config.split = None
566 self.assertRaises(ValueError, config.validate)
567
568 def testValidate_002(self):
569 """
570 Test validate on an empty split section.
571 """
572 config = LocalConfig()
573 config.split = SplitConfig()
574 self.assertRaises(ValueError, config.validate)
575
576 def testValidate_003(self):
577 """
578 Test validate on a non-empty split section with no values filled in.
579 """
580 config = LocalConfig()
581 config.split = SplitConfig(None, None)
582 self.assertRaises(ValueError, config.validate)
583
584 def testValidate_004(self):
585 """
586 Test validate on a non-empty split section with only one value filled in.
587 """
588 config = LocalConfig()
589 config.split = SplitConfig(ByteQuantity("1.00", UNIT_MBYTES), None)
590 self.assertRaises(ValueError, config.validate)
591 config.split = SplitConfig(None, ByteQuantity("1.00", UNIT_MBYTES))
592 self.assertRaises(ValueError, config.validate)
593
594 def testValidate_005(self):
595 """
596 Test validate on a non-empty split section with valid values filled in.
597 """
598 config = LocalConfig()
599 config.split = SplitConfig(ByteQuantity("1.00", UNIT_MBYTES), ByteQuantity("1.00", UNIT_MBYTES))
600 config.validate()
601
602 ############################
603 # Test parsing of documents
604 ############################
605
606 def testParse_001(self):
607 """
608 Parse empty config document.
609 """
610 path = self.resources["split.conf.1"]
611 with open(path) as f:
612 contents = f.read()
613 self.assertRaises(ValueError, LocalConfig, xmlPath=path, validate=True)
614 self.assertRaises(ValueError, LocalConfig, xmlData=contents, validate=True)
615 config = LocalConfig(xmlPath=path, validate=False)
616 self.assertEqual(None, config.split)
617 config = LocalConfig(xmlData=contents, validate=False)
618 self.assertEqual(None, config.split)
619
620 def testParse_002(self):
621 """
622 Parse config document with filled-in values, size in bytes.
623 """
624 path = self.resources["split.conf.2"]
625 with open(path) as f:
626 contents = f.read()
627 config = LocalConfig(xmlPath=path, validate=False)
628 self.assertNotEqual(None, config.split)
629 self.assertEqual(ByteQuantity("12345", UNIT_BYTES), config.split.sizeLimit)
630 self.assertEqual(ByteQuantity("67890.0", UNIT_BYTES), config.split.splitSize)
631 config = LocalConfig(xmlData=contents, validate=False)
632 self.assertNotEqual(None, config.split)
633 self.assertEqual(ByteQuantity("12345", UNIT_BYTES), config.split.sizeLimit)
634 self.assertEqual(ByteQuantity("67890.0", UNIT_BYTES), config.split.splitSize)
635
636 def testParse_003(self):
637 """
638 Parse config document with filled-in values, size in KB.
639 """
640 path = self.resources["split.conf.3"]
641 with open(path) as f:
642 contents = f.read()
643 config = LocalConfig(xmlPath=path, validate=False)
644 self.assertNotEqual(None, config.split)
645 self.assertEqual(ByteQuantity("1.25", UNIT_KBYTES), config.split.sizeLimit)
646 self.assertEqual(ByteQuantity("0.6", UNIT_KBYTES), config.split.splitSize)
647 config = LocalConfig(xmlData=contents, validate=False)
648 self.assertNotEqual(None, config.split)
649 self.assertEqual(ByteQuantity("1.25", UNIT_KBYTES), config.split.sizeLimit)
650 self.assertEqual(ByteQuantity("0.6", UNIT_KBYTES), config.split.splitSize)
651
652 def testParse_004(self):
653 """
654 Parse config document with filled-in values, size in MB.
655 """
656 path = self.resources["split.conf.4"]
657 with open(path) as f:
658 contents = f.read()
659 config = LocalConfig(xmlPath=path, validate=False)
660 self.assertNotEqual(None, config.split)
661 self.assertEqual(ByteQuantity("1.25", UNIT_MBYTES), config.split.sizeLimit)
662 self.assertEqual(ByteQuantity("0.6", UNIT_MBYTES), config.split.splitSize)
663 config = LocalConfig(xmlData=contents, validate=False)
664 self.assertNotEqual(None, config.split)
665 self.assertEqual(ByteQuantity("1.25", UNIT_MBYTES), config.split.sizeLimit)
666 self.assertEqual(ByteQuantity("0.6", UNIT_MBYTES), config.split.splitSize)
667
668 def testParse_005(self):
669 """
670 Parse config document with filled-in values, size in GB.
671 """
672 path = self.resources["split.conf.5"]
673 with open(path) as f:
674 contents = f.read()
675 config = LocalConfig(xmlPath=path, validate=False)
676 self.assertNotEqual(None, config.split)
677 self.assertEqual(ByteQuantity("1.25", UNIT_GBYTES), config.split.sizeLimit)
678 self.assertEqual(ByteQuantity("0.6", UNIT_GBYTES), config.split.splitSize)
679 config = LocalConfig(xmlData=contents, validate=False)
680 self.assertNotEqual(None, config.split)
681 self.assertEqual(ByteQuantity("1.25", UNIT_GBYTES), config.split.sizeLimit)
682 self.assertEqual(ByteQuantity("0.6", UNIT_GBYTES), config.split.splitSize)
683
684 ###################
685 # Test addConfig()
686 ###################
687
688 def testAddConfig_001(self):
689 """
690 Test with empty config document.
691 """
692 split = SplitConfig()
693 config = LocalConfig()
694 config.split = split
695 self.validateAddConfig(config)
696
697 def testAddConfig_002(self):
698 """
699 Test with values set, byte values.
700 """
701 split = SplitConfig(ByteQuantity("57521.0", UNIT_BYTES), ByteQuantity("121231", UNIT_BYTES))
702 config = LocalConfig()
703 config.split = split
704 self.validateAddConfig(config)
705
706 def testAddConfig_003(self):
707 """
708 Test with values set, KB values.
709 """
710 split = SplitConfig(ByteQuantity("12", UNIT_KBYTES), ByteQuantity("63352", UNIT_KBYTES))
711 config = LocalConfig()
712 config.split = split
713 self.validateAddConfig(config)
714
715 def testAddConfig_004(self):
716 """
717 Test with values set, MB values.
718 """
719 split = SplitConfig(ByteQuantity("12", UNIT_MBYTES), ByteQuantity("63352", UNIT_MBYTES))
720 config = LocalConfig()
721 config.split = split
722 self.validateAddConfig(config)
723
724 def testAddConfig_005(self):
725 """
726 Test with values set, GB values.
727 """
728 split = SplitConfig(ByteQuantity("12", UNIT_GBYTES), ByteQuantity("63352", UNIT_GBYTES))
729 config = LocalConfig()
730 config.split = split
731 self.validateAddConfig(config)
732
733
734 ######################
735 # TestFunctions class
736 ######################
737
738
739 @unittest.skipUnless(runAllTests(), "Limited test suite")
740 class TestFunctions(unittest.TestCase):
741 """Tests for the functions in split.py."""
742
743 ################
744 # Setup methods
745 ################
746
747 @classmethod
748 def setUpClass(cls):
749 configureLogging()
750
751 @classmethod
752 def checkDisabled(cls):
753 if not runAllTests():
754 raise unittest.SkipTest("Disabled")
755
756 def setUp(self):
757 try:
758 self.tmpdir = tempfile.mkdtemp()
759 self.resources = findResources(RESOURCES, DATA_DIRS)
760 except Exception as e:
761 self.fail(e)
762
763 def tearDown(self):
764 try:
765 removedir(self.tmpdir)
766 except:
767 pass
768
769 ##################
770 # Utility methods
771 ##################
772
773 def extractTar(self, tarname):
774 """Extracts a tarfile with a particular name."""
775 extractTar(self.tmpdir, self.resources["%s.tar.gz" % tarname])
776
777 def buildPath(self, components):
778 """Builds a complete search path from a list of components."""
779 components.insert(0, self.tmpdir)
780 return buildPath(components)
781
782 def checkSplit(self, sourcePath, origSize, splitSize):
783 """Checks that a file was split properly."""
784 wholeFiles = int(float(origSize) / float(splitSize))
785 leftoverBytes = int(float(origSize) % float(splitSize))
786 for i in range(wholeFiles):
787 splitPath = "%s_%05d" % (sourcePath, i)
788 self.assertTrue(os.path.exists(splitPath))
789 self.assertEqual(splitSize, os.stat(splitPath).st_size)
790 if leftoverBytes > 0:
791 splitPath = "%s_%05d" % (sourcePath, wholeFiles)
792 self.assertTrue(os.path.exists(splitPath))
793 self.assertEqual(leftoverBytes, os.stat(splitPath).st_size)
794
795 def findBadLocale(self):
796 """
797 The split command localizes its output for certain locales. This breaks
798 the parsing code in split.py. This method returns a list of the locales
799 (if any) that are currently configured which could be expected to cause a
800 failure if the localization-fixing code doesn't work.
801 """
802 locales = availableLocales()
803 if "fr_FR" in locales:
804 return "fr_FR"
805 if "pl_PL" in locales:
806 return "pl_PL"
807 if "ru_RU" in locales:
808 return "ru_RU"
809 return None
810
811 ####################
812 # Test _splitFile()
813 ####################
814
815 def testSplitFile_001(self):
816 """
817 Test with a nonexistent file.
818 """
819 self.extractTar("tree21")
820 sourcePath = self.buildPath(["tree21", "2007", "01", "01", INVALID_PATH])
821 self.assertFalse(os.path.exists(sourcePath))
822 splitSize = ByteQuantity("320", UNIT_BYTES)
823 self.assertRaises(ValueError, _splitFile, sourcePath, splitSize, None, None, removeSource=False)
824
825 def testSplitFile_002(self):
826 """
827 Test with integer split size, removeSource=False.
828 """
829 self.extractTar("tree21")
830 sourcePath = self.buildPath(["tree21", "2007", "01", "01", "system1", "file001.a.b"])
831 self.assertTrue(os.path.exists(sourcePath))
832 splitSize = ByteQuantity("320", UNIT_BYTES)
833 _splitFile(sourcePath, splitSize, None, None, removeSource=False)
834 self.assertTrue(os.path.exists(sourcePath))
835 self.checkSplit(sourcePath, 3200, 320)
836
837 def testSplitFile_003(self):
838 """
839 Test with floating point split size, removeSource=False.
840 """
841 self.extractTar("tree21")
842 sourcePath = self.buildPath(["tree21", "2007", "01", "01", "system1", "file001.a.b"])
843 self.assertTrue(os.path.exists(sourcePath))
844 splitSize = ByteQuantity("320.1", UNIT_BYTES)
845 _splitFile(sourcePath, splitSize, None, None, removeSource=False)
846 self.assertTrue(os.path.exists(sourcePath))
847 self.checkSplit(sourcePath, 3200, 320)
848
849 def testSplitFile_004(self):
850 """
851 Test with integer split size, removeSource=True.
852 """
853 self.extractTar("tree21")
854 sourcePath = self.buildPath(["tree21", "2007", "01", "01", "system1", "file001.a.b"])
855 self.assertTrue(os.path.exists(sourcePath))
856 splitSize = ByteQuantity("320", UNIT_BYTES)
857 _splitFile(sourcePath, splitSize, None, None, removeSource=True)
858 self.assertFalse(os.path.exists(sourcePath))
859 self.checkSplit(sourcePath, 3200, 320)
860
861 def testSplitFile_005(self):
862 """
863 Test with a local other than "C" or "en_US" set.
864 """
865 locale = self.findBadLocale()
866 if locale is not None:
867 os.environ["LANG"] = locale
868 os.environ["LC_ADDRESS"] = locale
869 os.environ["LC_ALL"] = locale
870 os.environ["LC_COLLATE"] = locale
871 os.environ["LC_CTYPE"] = locale
872 os.environ["LC_IDENTIFICATION"] = locale
873 os.environ["LC_MEASUREMENT"] = locale
874 os.environ["LC_MESSAGES"] = locale
875 os.environ["LC_MONETARY"] = locale
876 os.environ["LC_NAME"] = locale
877 os.environ["LC_NUMERIC"] = locale
878 os.environ["LC_PAPER"] = locale
879 os.environ["LC_TELEPHONE"] = locale
880 os.environ["LC_TIME"] = locale
881 self.extractTar("tree21")
882 sourcePath = self.buildPath(["tree21", "2007", "01", "01", "system1", "file001.a.b"])
883 self.assertTrue(os.path.exists(sourcePath))
884 splitSize = ByteQuantity("320", UNIT_BYTES)
885 _splitFile(sourcePath, splitSize, None, None, removeSource=True)
886 self.assertFalse(os.path.exists(sourcePath))
887 self.checkSplit(sourcePath, 3200, 320)
888
889 ##########################
890 # Test _splitDailyDir()
891 ##########################
892
893 def testSplitDailyDir_001(self):
894 """
895 Test with a nonexistent daily staging directory.
896 """
897 self.extractTar("tree21")
898 dailyDir = self.buildPath(["tree21", "2007", "01", INVALID_PATH])
899 self.assertFalse(os.path.exists(dailyDir))
900 sizeLimit = ByteQuantity("1.0", UNIT_MBYTES)
901 splitSize = ByteQuantity("100000", UNIT_BYTES)
902 self.assertRaises(ValueError, _splitDailyDir, dailyDir, sizeLimit, splitSize, None, None)
903
904 def testSplitDailyDir_002(self):
905 """
906 Test with 1.0 MB limit.
907 """
908 self.extractTar("tree21")
909 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
910 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
911 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
912 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
913 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
914 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
915 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
916 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
917 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
918 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
919 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
920 sizeLimit = ByteQuantity("1.0", UNIT_MBYTES)
921 splitSize = ByteQuantity("100000", UNIT_BYTES)
922 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
923 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
924 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
925 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
926 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
927 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
928 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
929 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
930 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
931 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
932
933 def testSplitDailyDir_003(self):
934 """
935 Test with 100,000 byte limit, chopped down to 10 KB
936 """
937 self.extractTar("tree21")
938 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
939 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
940 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
941 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
942 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
943 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
944 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
945 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
946 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
947 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
948 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
949 sizeLimit = ByteQuantity("100000", UNIT_BYTES)
950 splitSize = ByteQuantity("10", UNIT_KBYTES)
951 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
952 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
953 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
954 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
955 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
956 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
957 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
958 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
959 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
960 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
961 self.checkSplit(pathJoin(dailyDir, "system1", "file003"), 320000, 10 * 1024)
962 self.checkSplit(pathJoin(dailyDir, "system3", "file003"), 100001, 10 * 1024)
963
964 def testSplitDailyDir_004(self):
965 """
966 Test with 99,999 byte limit, chopped down to 5,000 bytes
967 """
968 self.extractTar("tree21")
969 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
970 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
971 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
972 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
973 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
974 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
975 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
976 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
977 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
978 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
979 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
980 sizeLimit = ByteQuantity("99999", UNIT_BYTES)
981 splitSize = ByteQuantity("5000", UNIT_BYTES)
982 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
983 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
984 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
985 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
986 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
987 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
988 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
989 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
990 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
991 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
992 self.checkSplit(pathJoin(dailyDir, "system1", "file003"), 320000, 5000)
993 self.checkSplit(pathJoin(dailyDir, "system2", "file003"), 100000, 5000)
994 self.checkSplit(pathJoin(dailyDir, "system3", "file002"), 100000, 5000)
995 self.checkSplit(pathJoin(dailyDir, "system3", "file003"), 100001, 5000)
996
997 def testSplitDailyDir_005(self):
998 """
999 Test with 99,998 byte limit, chopped down to 2500 bytes
1000 """
1001 self.extractTar("tree21")
1002 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
1003 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
1004 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1005 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1006 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1007 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1008 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1009 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1010 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1011 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1012 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1013 sizeLimit = ByteQuantity("10000.0", UNIT_BYTES)
1014 splitSize = ByteQuantity("2500", UNIT_BYTES)
1015 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
1016 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1017 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1018 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1019 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1020 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1021 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1022 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1023 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1024 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1025 self.checkSplit(pathJoin(dailyDir, "system1", "file002"), 32000, 2500)
1026 self.checkSplit(pathJoin(dailyDir, "system1", "file003"), 320000, 2500)
1027 self.checkSplit(pathJoin(dailyDir, "system2", "file003"), 100000, 2500)
1028 self.checkSplit(pathJoin(dailyDir, "system3", "file001"), 99999, 2500)
1029 self.checkSplit(pathJoin(dailyDir, "system3", "file002"), 100000, 2500)
1030 self.checkSplit(pathJoin(dailyDir, "system3", "file003"), 100001, 2500)
1031
1032 def testSplitDailyDir_006(self):
1033 """
1034 Test with 10,000 byte limit, chopped down to 1024 bytes
1035 """
1036 self.extractTar("tree21")
1037 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
1038 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
1039 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1040 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1041 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1042 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1043 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1044 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1045 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1046 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1047 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1048 sizeLimit = ByteQuantity("10000", UNIT_BYTES)
1049 splitSize = ByteQuantity("1.0", UNIT_KBYTES)
1050 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
1051 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1052 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1053 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1054 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1055 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1056 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1057 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1058 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1059 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1060 self.checkSplit(pathJoin(dailyDir, "system1", "file002"), 32000, 1 * 1024)
1061 self.checkSplit(pathJoin(dailyDir, "system1", "file003"), 320000, 1 * 1024)
1062 self.checkSplit(pathJoin(dailyDir, "system2", "file003"), 100000, 1 * 1024)
1063 self.checkSplit(pathJoin(dailyDir, "system3", "file001"), 99999, 1 * 1024)
1064 self.checkSplit(pathJoin(dailyDir, "system3", "file002"), 100000, 1 * 1024)
1065 self.checkSplit(pathJoin(dailyDir, "system3", "file003"), 100001, 1 * 1024)
1066
1067 def testSplitDailyDir_007(self):
1068 """
1069 Test with 9,999 byte limit, chopped down to 1000 bytes
1070 """
1071 self.extractTar("tree21")
1072 dailyDir = self.buildPath(["tree21", "2007", "01", "01"])
1073 self.assertTrue(os.path.exists(dailyDir) and os.path.isdir(dailyDir))
1074 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1075 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1076 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1077 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1078 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1079 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1080 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1081 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1082 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1083 sizeLimit = ByteQuantity("9999", UNIT_BYTES)
1084 splitSize = ByteQuantity("1000", UNIT_BYTES)
1085 _splitDailyDir(dailyDir, sizeLimit, splitSize, None, None)
1086 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system1", "file001.a.b")))
1087 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file002")))
1088 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system1", "file003")))
1089 self.assertTrue(os.path.exists(pathJoin(dailyDir, "system2", "file001")))
1090 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system2", "file002")))
1091 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system2", "file003")))
1092 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file001")))
1093 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file002")))
1094 self.assertFalse(os.path.exists(pathJoin(dailyDir, "system3", "file003")))
1095 self.checkSplit(pathJoin(dailyDir, "system1", "file002"), 32000, 1000)
1096 self.checkSplit(pathJoin(dailyDir, "system1", "file003"), 320000, 1000)
1097 self.checkSplit(pathJoin(dailyDir, "system2", "file002"), 10000, 1000)
1098 self.checkSplit(pathJoin(dailyDir, "system2", "file003"), 100000, 1000)
1099 self.checkSplit(pathJoin(dailyDir, "system3", "file001"), 99999, 1000)
1100 self.checkSplit(pathJoin(dailyDir, "system3", "file002"), 100000, 1000)
1101 self.checkSplit(pathJoin(dailyDir, "system3", "file003"), 100001, 1000)