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