From 33b4fa9f87c0294590d40da1fae06eb49cf70cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com> Date: Wed, 19 Sep 2012 11:14:14 +0200 Subject: [PATCH] Adding some fundamental unittests for iallocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This test covers the bug fixes found in the previous two patches Signed-off-by: RenΓ© Nussbaumer <rn@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- Makefile.am | 1 + test/ganeti.masterd.iallocator_unittest.py | 92 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100755 test/ganeti.masterd.iallocator_unittest.py diff --git a/Makefile.am b/Makefile.am index 0cd3cfab7..5ec9cf772 100644 --- a/Makefile.am +++ b/Makefile.am @@ -880,6 +880,7 @@ python_tests = \ test/ganeti.jstore_unittest.py \ test/ganeti.locking_unittest.py \ test/ganeti.luxi_unittest.py \ + test/ganeti.masterd.iallocator_unittest.py \ test/ganeti.masterd.instance_unittest.py \ test/ganeti.mcpu_unittest.py \ test/ganeti.netutils_unittest.py \ diff --git a/test/ganeti.masterd.iallocator_unittest.py b/test/ganeti.masterd.iallocator_unittest.py new file mode 100755 index 000000000..8aa2bd05b --- /dev/null +++ b/test/ganeti.masterd.iallocator_unittest.py @@ -0,0 +1,92 @@ +#!/usr/bin/python +# + +# Copyright (C) 2012 Google Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + + +"""Script for testing ganeti.masterd.iallocator""" + +import unittest + +from ganeti import compat +from ganeti import constants +from ganeti import errors +from ganeti import ht +from ganeti.masterd import iallocator + +import testutils + + +class _StubIAllocator(object): + def __init__(self, success): + self.success = success + + +class TestIAReqMultiInstanceAlloc(unittest.TestCase): + def testResult(self): + good_results = [ + # First result (all instances "allocate") + [ + [["foo", ["a", "b"]], + ["bar", ["c"]], + ["baz", []]], + [] + ], + # Second result (partial "allocate", partial "fail") + [ + [["bar", ["c", "b"]], + ["baz", ["a"]]], + ["foo"] + ], + # Third result (all instances "fail") + [ + [], + ["foo", "bar", "baz"] + ], + ] + bad_results = [ + "foobar", + 1234, + [], + [[]], + [[], [], []], + ] + + result_fn = iallocator.IAReqMultiInstanceAlloc.REQ_RESULT + + self.assertTrue(compat.all(map(result_fn, good_results))) + self.assertFalse(compat.any(map(result_fn, bad_results))) + + +class TestIARequestBase(unittest.TestCase): + def testValidateResult(self): + class _StubReqBase(iallocator.IARequestBase): + MODE = constants.IALLOCATOR_MODE_ALLOC + REQ_RESULT = ht.TBool + + stub = _StubReqBase() + stub.ValidateResult(_StubIAllocator(True), True) + self.assertRaises(errors.ResultValidationError, stub.ValidateResult, + _StubIAllocator(True), "foo") + stub.ValidateResult(_StubIAllocator(False), True) + # We don't validate the result if the iallocation request was not successful + stub.ValidateResult(_StubIAllocator(False), "foo") + + +if __name__ == "__main__": + testutils.GanetiTestProgram() -- GitLab