1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
|
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vim: set ft=python ts=3 sw=3 expandtab:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# C E D A R
# S O L U T I O N S "Software done right."
# S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2008,2010 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : Python (>= 2.5)
# Project : Cedar Backup, release 2
# Revision : $Id: capacitytests.py 1006 2010-07-07 21:03:57Z pronovic $
# Purpose : Tests capacity extension functionality.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
########################################################################
# Module documentation
########################################################################
"""
Unit tests for CedarBackup2/extend/capacity.py.
Code Coverage
=============
This module contains individual tests for the the public classes implemented
in extend/capacity.py.
Naming Conventions
==================
I prefer to avoid large unit tests which validate more than one piece of
functionality, and I prefer to avoid using overly descriptive (read: long)
test names, as well. Instead, I use lots of very small tests that each
validate one specific thing. These small tests are then named with an index
number, yielding something like C{testAddDir_001} or C{testValidate_010}.
Each method has a docstring describing what it's supposed to accomplish. I
feel that this makes it easier to judge how important a given failure is,
and also makes it somewhat easier to diagnose and fix individual problems.
Testing XML Extraction
======================
It's difficult to validated that generated XML is exactly "right",
especially when dealing with pretty-printed XML. We can't just provide a
constant string and say "the result must match this". Instead, what we do
is extract a node, build some XML from it, and then feed that XML back into
another object's constructor. If that parse process succeeds and the old
object is equal to the new object, we assume that the extract was
successful.
It would arguably be better if we could do a completely independent check -
but implementing that check would be equivalent to re-implementing all of
the existing functionality that we're validating here! After all, the most
important thing is that data can move seamlessly from object to XML document
and back to object.
Full vs. Reduced Tests
======================
All of the tests in this module are considered safe to be run in an average
build environment. There is a no need to use a CAPACITYTESTS_FULL
environment variable to provide a "reduced feature set" test suite as for
some of the other test modules.
@author Kenneth J. Pronovici <pronovic@ieee.org>
"""
########################################################################
# Import modules and do runtime validations
########################################################################
# System modules
import unittest
# Cedar Backup modules
from CedarBackup2.util import UNIT_BYTES, UNIT_KBYTES, UNIT_MBYTES, UNIT_GBYTES
from CedarBackup2.testutil import hexFloatLiteralAllowed, findResources, failUnlessAssignRaises
from CedarBackup2.xmlutil import createOutputDom, serializeDom
from CedarBackup2.extend.capacity import LocalConfig, CapacityConfig, ByteQuantity, PercentageQuantity
#######################################################################
# Module-wide configuration and constants
#######################################################################
DATA_DIRS = [ "./data", "./testcase/data", ]
RESOURCES = [ "capacity.conf.1", "capacity.conf.2", "capacity.conf.3", "capacity.conf.4", ]
#######################################################################
# Test Case Classes
#######################################################################
###############################
# TestPercentageQuantity class
###############################
class TestPercentageQuantity(unittest.TestCase):
"""Tests for the PercentageQuantity class."""
##################
# Utility methods
##################
def failUnlessAssignRaises(self, exception, obj, prop, value):
"""Equivalent of L{failUnlessRaises}, but used for property assignments instead."""
failUnlessAssignRaises(self, exception, obj, prop, value)
############################
# Test __repr__ and __str__
############################
def testStringFuncs_001(self):
"""
Just make sure that the string functions don't have errors (i.e. bad variable names).
"""
obj = PercentageQuantity()
obj.__repr__()
obj.__str__()
##################################
# Test constructor and attributes
##################################
def testConstructor_001(self):
"""
Test constructor with no values filled in.
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessEqual(0.0, quantity.percentage)
def testConstructor_002(self):
"""
Test constructor with all values filled in, with valid values.
"""
quantity = PercentageQuantity("6")
self.failUnlessEqual("6", quantity.quantity)
self.failUnlessEqual(6.0, quantity.percentage)
def testConstructor_003(self):
"""
Test assignment of quantity attribute, None value.
"""
quantity = PercentageQuantity(quantity="1.0")
self.failUnlessEqual("1.0", quantity.quantity)
self.failUnlessEqual(1.0, quantity.percentage)
quantity.quantity = None
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessEqual(0.0, quantity.percentage)
def testConstructor_004(self):
"""
Test assignment of quantity attribute, valid values.
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessEqual(0.0, quantity.percentage)
quantity.quantity = "1.0"
self.failUnlessEqual("1.0", quantity.quantity)
self.failUnlessEqual(1.0, quantity.percentage)
quantity.quantity = ".1"
self.failUnlessEqual(".1", quantity.quantity)
self.failUnlessEqual(0.1, quantity.percentage)
quantity.quantity = "12"
self.failUnlessEqual("12", quantity.quantity)
self.failUnlessEqual(12.0, quantity.percentage)
quantity.quantity = "0.5"
self.failUnlessEqual("0.5", quantity.quantity)
self.failUnlessEqual(0.5, quantity.percentage)
quantity.quantity = "0.25E2"
self.failUnlessEqual("0.25E2", quantity.quantity)
self.failUnlessEqual(0.25e2, quantity.percentage)
if hexFloatLiteralAllowed():
# Some interpreters allow this, some don't
quantity.quantity = "0x0C"
self.failUnlessEqual("0x0C", quantity.quantity)
self.failUnlessEqual(12.0, quantity.percentage)
def testConstructor_005(self):
"""
Test assignment of quantity attribute, invalid value (empty).
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "")
self.failUnlessEqual(None, quantity.quantity)
def testConstructor_006(self):
"""
Test assignment of quantity attribute, invalid value (not a floating point number).
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "blech")
self.failUnlessEqual(None, quantity.quantity)
def testConstructor_007(self):
"""
Test assignment of quantity attribute, invalid value (negative number).
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "-3")
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "-6.8")
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "-0.2")
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "-.1")
self.failUnlessEqual(None, quantity.quantity)
def testConstructor_008(self):
"""
Test assignment of quantity attribute, invalid value (larger than 100%).
"""
quantity = PercentageQuantity()
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "100.0001")
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "101")
self.failUnlessEqual(None, quantity.quantity)
self.failUnlessAssignRaises(ValueError, quantity, "quantity", "1e6")
self.failUnlessEqual(None, quantity.quantity)
############################
# Test comparison operators
############################
def testComparison_001(self):
"""
Test comparison of two identical objects, all attributes None.
"""
quantity1 = PercentageQuantity()
quantity2 = PercentageQuantity()
self.failUnlessEqual(quantity1, quantity2)
self.failUnless(quantity1 == quantity2)
self.failUnless(not quantity1 < quantity2)
self.failUnless(quantity1 <= quantity2)
self.failUnless(not quantity1 > quantity2)
self.failUnless(quantity1 >= quantity2)
self.failUnless(not quantity1 != quantity2)
def testComparison_002(self):
"""
Test comparison of two identical objects, all attributes non-None.
"""
quantity1 = PercentageQuantity("12")
quantity2 = PercentageQuantity("12")
self.failUnlessEqual(quantity1, quantity2)
self.failUnless(quantity1 == quantity2)
self.failUnless(not quantity1 < quantity2)
self.failUnless(quantity1 <= quantity2)
self.failUnless(not quantity1 > quantity2)
self.failUnless(quantity1 >= quantity2)
self.failUnless(not quantity1 != quantity2)
def testComparison_003(self):
"""
Test comparison of two differing objects, quantity differs (one None).
"""
quantity1 = PercentageQuantity()
quantity2 = PercentageQuantity(quantity="12")
self.failIfEqual(quantity1, quantity2)
self.failUnless(not quantity1 == quantity2)
self.failUnless(quantity1 < quantity2)
self.failUnless(quantity1 <= quantity2)
self.failUnless(not quantity1 > quantity2)
self.failUnless(not quantity1 >= quantity2)
self.failUnless(quantity1 != quantity2)
def testComparison_004(self):
"""
Test comparison of two differing objects, quantity differs.
"""
quantity1 = PercentageQuantity("10")
quantity2 = PercentageQuantity("12")
self.failIfEqual(quantity1, quantity2)
self.failUnless(not quantity1 == quantity2)
self.failUnless(quantity1 < quantity2)
self.failUnless(quantity1 <= quantity2)
self.failUnless(not quantity1 > quantity2)
self.failUnless(not quantity1 >= quantity2)
self.failUnless(quantity1 != quantity2)
##########################
# TestCapacityConfig class
##########################
class TestCapacityConfig(unittest.TestCase):
"""Tests for the CapacityConfig class."""
##################
# Utility methods
##################
def failUnlessAssignRaises(self, exception, obj, prop, value):
"""Equivalent of L{failUnlessRaises}, but used for property assignments instead."""
failUnlessAssignRaises(self, exception, obj, prop, value)
############################
# Test __repr__ and __str__
############################
def testStringFuncs_001(self):
"""
Just make sure that the string functions don't have errors (i.e. bad variable names).
"""
obj = CapacityConfig()
obj.__repr__()
obj.__str__()
##################################
# Test constructor and attributes
##################################
def testConstructor_001(self):
"""
Test constructor with no values filled in.
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.maxPercentage)
self.failUnlessEqual(None, capacity.minBytes)
def testConstructor_002(self):
"""
Test constructor with all values filled in, with valid values.
"""
capacity = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("2.0", UNIT_KBYTES))
self.failUnlessEqual(PercentageQuantity("63.2"), capacity.maxPercentage)
self.failUnlessEqual(ByteQuantity("2.0", UNIT_KBYTES), capacity.minBytes)
def testConstructor_003(self):
"""
Test assignment of maxPercentage attribute, None value.
"""
capacity = CapacityConfig(maxPercentage=PercentageQuantity("63.2"))
self.failUnlessEqual(PercentageQuantity("63.2"), capacity.maxPercentage)
capacity.maxPercentage = None
self.failUnlessEqual(None, capacity.maxPercentage)
def testConstructor_004(self):
"""
Test assignment of maxPercentage attribute, valid value.
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.maxPercentage)
capacity.maxPercentage = PercentageQuantity("63.2")
self.failUnlessEqual(PercentageQuantity("63.2"), capacity.maxPercentage)
def testConstructor_005(self):
"""
Test assignment of maxPercentage attribute, invalid value (empty).
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.maxPercentage)
self.failUnlessAssignRaises(ValueError, capacity, "maxPercentage", "")
self.failUnlessEqual(None, capacity.maxPercentage)
def testConstructor_006(self):
"""
Test assignment of maxPercentage attribute, invalid value (not a PercentageQuantity).
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.maxPercentage)
self.failUnlessAssignRaises(ValueError, capacity, "maxPercentage", "1.0 GB")
self.failUnlessEqual(None, capacity.maxPercentage)
def testConstructor_007(self):
"""
Test assignment of minBytes attribute, None value.
"""
capacity = CapacityConfig(minBytes=ByteQuantity("1.00", UNIT_KBYTES))
self.failUnlessEqual(ByteQuantity("1.00", UNIT_KBYTES), capacity.minBytes)
capacity.minBytes = None
self.failUnlessEqual(None, capacity.minBytes)
def testConstructor_008(self):
"""
Test assignment of minBytes attribute, valid value.
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.minBytes)
capacity.minBytes = ByteQuantity("1.00", UNIT_KBYTES)
self.failUnlessEqual(ByteQuantity("1.00", UNIT_KBYTES), capacity.minBytes)
def testConstructor_009(self):
"""
Test assignment of minBytes attribute, invalid value (empty).
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.minBytes)
self.failUnlessAssignRaises(ValueError, capacity, "minBytes", "")
self.failUnlessEqual(None, capacity.minBytes)
def testConstructor_010(self):
"""
Test assignment of minBytes attribute, invalid value (not a ByteQuantity).
"""
capacity = CapacityConfig()
self.failUnlessEqual(None, capacity.minBytes)
self.failUnlessAssignRaises(ValueError, capacity, "minBytes", 12)
self.failUnlessEqual(None, capacity.minBytes)
############################
# Test comparison operators
############################
def testComparison_001(self):
"""
Test comparison of two identical objects, all attributes None.
"""
capacity1 = CapacityConfig()
capacity2 = CapacityConfig()
self.failUnlessEqual(capacity1, capacity2)
self.failUnless(capacity1 == capacity2)
self.failUnless(not capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(capacity1 >= capacity2)
self.failUnless(not capacity1 != capacity2)
def testComparison_002(self):
"""
Test comparison of two identical objects, all attributes non-None.
"""
capacity1 = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("1.00", UNIT_MBYTES))
capacity2 = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("1.00", UNIT_MBYTES))
self.failUnlessEqual(capacity1, capacity2)
self.failUnless(capacity1 == capacity2)
self.failUnless(not capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(capacity1 >= capacity2)
self.failUnless(not capacity1 != capacity2)
def testComparison_003(self):
"""
Test comparison of two differing objects, maxPercentage differs (one None).
"""
capacity1 = CapacityConfig()
capacity2 = CapacityConfig(maxPercentage=PercentageQuantity("63.2"))
self.failIfEqual(capacity1, capacity2)
self.failUnless(not capacity1 == capacity2)
self.failUnless(capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(not capacity1 >= capacity2)
self.failUnless(capacity1 != capacity2)
def testComparison_004(self):
"""
Test comparison of two differing objects, maxPercentage differs.
"""
capacity1 = CapacityConfig(PercentageQuantity("15.0"), ByteQuantity("1.00", UNIT_MBYTES))
capacity2 = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("1.00", UNIT_MBYTES))
self.failIfEqual(capacity1, capacity2)
self.failUnless(not capacity1 == capacity2)
self.failUnless(capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(not capacity1 >= capacity2)
self.failUnless(capacity1 != capacity2)
def testComparison_005(self):
"""
Test comparison of two differing objects, minBytes differs (one None).
"""
capacity1 = CapacityConfig()
capacity2 = CapacityConfig(minBytes=ByteQuantity("1.00", UNIT_MBYTES))
self.failIfEqual(capacity1, capacity2)
self.failUnless(not capacity1 == capacity2)
self.failUnless(capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(not capacity1 >= capacity2)
self.failUnless(capacity1 != capacity2)
def testComparison_006(self):
"""
Test comparison of two differing objects, minBytes differs.
"""
capacity1 = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("0.5", UNIT_MBYTES))
capacity2 = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("1.00", UNIT_MBYTES))
self.failIfEqual(capacity1, capacity2)
self.failUnless(not capacity1 == capacity2)
self.failUnless(capacity1 < capacity2)
self.failUnless(capacity1 <= capacity2)
self.failUnless(not capacity1 > capacity2)
self.failUnless(not capacity1 >= capacity2)
self.failUnless(capacity1 != capacity2)
########################
# TestLocalConfig class
########################
class TestLocalConfig(unittest.TestCase):
"""Tests for the LocalConfig class."""
################
# Setup methods
################
def setUp(self):
try:
self.resources = findResources(RESOURCES, DATA_DIRS)
except Exception, e:
self.fail(e)
def tearDown(self):
pass
##################
# Utility methods
##################
def failUnlessAssignRaises(self, exception, obj, prop, value):
"""Equivalent of L{failUnlessRaises}, but used for property assignments instead."""
failUnlessAssignRaises(self, exception, obj, prop, value)
def validateAddConfig(self, origConfig):
"""
Validates that document dumped from C{LocalConfig.addConfig} results in
identical object.
We dump a document containing just the capacity configuration, and then
make sure that if we push that document back into the C{LocalConfig}
object, that the resulting object matches the original.
The C{self.failUnlessEqual} method is used for the validation, so if the
method call returns normally, everything is OK.
@param origConfig: Original configuration.
"""
(xmlDom, parentNode) = createOutputDom()
origConfig.addConfig(xmlDom, parentNode)
xmlData = serializeDom(xmlDom)
newConfig = LocalConfig(xmlData=xmlData, validate=False)
self.failUnlessEqual(origConfig, newConfig)
############################
# Test __repr__ and __str__
############################
def testStringFuncs_001(self):
"""
Just make sure that the string functions don't have errors (i.e. bad variable names).
"""
obj = LocalConfig()
obj.__repr__()
obj.__str__()
#####################################################
# Test basic constructor and attribute functionality
#####################################################
def testConstructor_001(self):
"""
Test empty constructor, validate=False.
"""
config = LocalConfig(validate=False)
self.failUnlessEqual(None, config.capacity)
def testConstructor_002(self):
"""
Test empty constructor, validate=True.
"""
config = LocalConfig(validate=True)
self.failUnlessEqual(None, config.capacity)
def testConstructor_003(self):
"""
Test with empty config document as both data and file, validate=False.
"""
path = self.resources["capacity.conf.1"]
contents = open(path).read()
self.failUnlessRaises(ValueError, LocalConfig, xmlData=contents, xmlPath=path, validate=False)
def testConstructor_004(self):
"""
Test assignment of capacity attribute, None value.
"""
config = LocalConfig()
config.capacity = None
self.failUnlessEqual(None, config.capacity)
def testConstructor_005(self):
"""
Test assignment of capacity attribute, valid value.
"""
config = LocalConfig()
config.capacity = CapacityConfig()
self.failUnlessEqual(CapacityConfig(), config.capacity)
def testConstructor_006(self):
"""
Test assignment of capacity attribute, invalid value (not CapacityConfig).
"""
config = LocalConfig()
self.failUnlessAssignRaises(ValueError, config, "capacity", "STRING!")
############################
# Test comparison operators
############################
def testComparison_001(self):
"""
Test comparison of two identical objects, all attributes None.
"""
config1 = LocalConfig()
config2 = LocalConfig()
self.failUnlessEqual(config1, config2)
self.failUnless(config1 == config2)
self.failUnless(not config1 < config2)
self.failUnless(config1 <= config2)
self.failUnless(not config1 > config2)
self.failUnless(config1 >= config2)
self.failUnless(not config1 != config2)
def testComparison_002(self):
"""
Test comparison of two identical objects, all attributes non-None.
"""
config1 = LocalConfig()
config1.capacity = CapacityConfig()
config2 = LocalConfig()
config2.capacity = CapacityConfig()
self.failUnlessEqual(config1, config2)
self.failUnless(config1 == config2)
self.failUnless(not config1 < config2)
self.failUnless(config1 <= config2)
self.failUnless(not config1 > config2)
self.failUnless(config1 >= config2)
self.failUnless(not config1 != config2)
def testComparison_003(self):
"""
Test comparison of two differing objects, capacity differs (one None).
"""
config1 = LocalConfig()
config2 = LocalConfig()
config2.capacity = CapacityConfig()
self.failIfEqual(config1, config2)
self.failUnless(not config1 == config2)
self.failUnless(config1 < config2)
self.failUnless(config1 <= config2)
self.failUnless(not config1 > config2)
self.failUnless(not config1 >= config2)
self.failUnless(config1 != config2)
def testComparison_004(self):
"""
Test comparison of two differing objects, capacity differs.
"""
config1 = LocalConfig()
config1.capacity = CapacityConfig(minBytes=ByteQuantity("0.1", UNIT_MBYTES))
config2 = LocalConfig()
config2.capacity = CapacityConfig(minBytes=ByteQuantity("1.00", UNIT_MBYTES))
self.failIfEqual(config1, config2)
self.failUnless(not config1 == config2)
self.failUnless(config1 < config2)
self.failUnless(config1 <= config2)
self.failUnless(not config1 > config2)
self.failUnless(not config1 >= config2)
self.failUnless(config1 != config2)
######################
# Test validate logic
######################
def testValidate_001(self):
"""
Test validate on a None capacity section.
"""
config = LocalConfig()
config.capacity = None
self.failUnlessRaises(ValueError, config.validate)
def testValidate_002(self):
"""
Test validate on an empty capacity section.
"""
config = LocalConfig()
config.capacity = CapacityConfig()
self.failUnlessRaises(ValueError, config.validate)
def testValidate_003(self):
"""
Test validate on a non-empty capacity section with no values filled in.
"""
config = LocalConfig()
config.capacity = CapacityConfig(None, None)
self.failUnlessRaises(ValueError, config.validate)
def testValidate_004(self):
"""
Test validate on a non-empty capacity section with both max percentage and min bytes filled in.
"""
config = LocalConfig()
config.capacity = CapacityConfig(PercentageQuantity("63.2"), ByteQuantity("1.00", UNIT_MBYTES))
self.failUnlessRaises(ValueError, config.validate)
def testValidate_005(self):
"""
Test validate on a non-empty capacity section with only max percentage filled in.
"""
config = LocalConfig()
config.capacity = CapacityConfig(maxPercentage=PercentageQuantity("63.2"))
config.validate()
def testValidate_006(self):
"""
Test validate on a non-empty capacity section with only min bytes filled in.
"""
config = LocalConfig()
config.capacity = CapacityConfig(minBytes=ByteQuantity("1.00", UNIT_MBYTES))
config.validate()
############################
# Test parsing of documents
############################
# Some of the byte-size parsing logic is tested more fully in splittests.py.
# I decided not to duplicate it here, since it's shared from config.py.
def testParse_001(self):
"""
Parse empty config document.
"""
path = self.resources["capacity.conf.1"]
contents = open(path).read()
self.failUnlessRaises(ValueError, LocalConfig, xmlPath=path, validate=True)
self.failUnlessRaises(ValueError, LocalConfig, xmlData=contents, validate=True)
config = LocalConfig(xmlPath=path, validate=False)
self.failUnlessEqual(None, config.capacity)
config = LocalConfig(xmlData=contents, validate=False)
self.failUnlessEqual(None, config.capacity)
def testParse_002(self):
"""
Parse config document that configures max percentage.
"""
path = self.resources["capacity.conf.2"]
contents = open(path).read()
config = LocalConfig(xmlPath=path, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(PercentageQuantity("63.2"), config.capacity.maxPercentage)
self.failUnlessEqual(None, config.capacity.minBytes)
config = LocalConfig(xmlData=contents, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(PercentageQuantity("63.2"), config.capacity.maxPercentage)
self.failUnlessEqual(None, config.capacity.minBytes)
def testParse_003(self):
"""
Parse config document that configures min bytes, size in bytes.
"""
path = self.resources["capacity.conf.3"]
contents = open(path).read()
config = LocalConfig(xmlPath=path, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(None, config.capacity.maxPercentage)
self.failUnlessEqual(ByteQuantity("18", UNIT_BYTES), config.capacity.minBytes)
config = LocalConfig(xmlData=contents, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(None, config.capacity.maxPercentage)
self.failUnlessEqual(ByteQuantity("18", UNIT_BYTES), config.capacity.minBytes)
def testParse_004(self):
"""
Parse config document with filled-in values, size in KB.
"""
path = self.resources["capacity.conf.4"]
contents = open(path).read()
config = LocalConfig(xmlPath=path, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(None, config.capacity.maxPercentage)
self.failUnlessEqual(ByteQuantity("1.25", UNIT_KBYTES), config.capacity.minBytes)
config = LocalConfig(xmlData=contents, validate=False)
self.failIfEqual(None, config.capacity)
self.failUnlessEqual(None, config.capacity.maxPercentage)
self.failUnlessEqual(ByteQuantity("1.25", UNIT_KBYTES), config.capacity.minBytes)
###################
# Test addConfig()
###################
def testAddConfig_001(self):
"""
Test with empty config document.
"""
capacity = CapacityConfig()
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
def testAddConfig_002(self):
"""
Test with max percentage value set.
"""
capacity = CapacityConfig(maxPercentage=PercentageQuantity("63.29128310980123"))
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
def testAddConfig_003(self):
"""
Test with min bytes value set, byte values.
"""
capacity = CapacityConfig(minBytes=ByteQuantity("121231", UNIT_BYTES))
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
def testAddConfig_004(self):
"""
Test with min bytes value set, KB values.
"""
capacity = CapacityConfig(minBytes=ByteQuantity("63352", UNIT_KBYTES))
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
def testAddConfig_005(self):
"""
Test with min bytes value set, MB values.
"""
capacity = CapacityConfig(minBytes=ByteQuantity("63352", UNIT_MBYTES))
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
def testAddConfig_006(self):
"""
Test with min bytes value set, GB values.
"""
capacity = CapacityConfig(minBytes=ByteQuantity("63352", UNIT_GBYTES))
config = LocalConfig()
config.capacity = capacity
self.validateAddConfig(config)
#######################################################################
# Suite definition
#######################################################################
def suite():
"""Returns a suite containing all the test cases in this module."""
return unittest.TestSuite((
unittest.makeSuite(TestPercentageQuantity, 'test'),
unittest.makeSuite(TestCapacityConfig, 'test'),
unittest.makeSuite(TestLocalConfig, 'test'),
))
########################################################################
# Module entry point
########################################################################
# When this module is executed from the command-line, run its tests
if __name__ == '__main__':
unittest.main()
|