X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/f75ab789e91dcf834422c934d7f335edb18cd0f1..63a3d8f7aa5242819c1474ad0956371c8dbb982b:/test/ganeti.rapi.rlib2_unittest.py diff --git a/test/ganeti.rapi.rlib2_unittest.py b/test/ganeti.rapi.rlib2_unittest.py index 2807a3b..f56dec1 100755 --- a/test/ganeti.rapi.rlib2_unittest.py +++ b/test/ganeti.rapi.rlib2_unittest.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -# Copyright (C) 2010 Google Inc. +# Copyright (C) 2010, 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 @@ -57,7 +57,7 @@ def _CreateHandler(cls, items, queryargs, body_data, client_cls): class _FakeClient: - def __init__(self): + def __init__(self, address=None): self._jobs = [] def GetNextSubmittedJob(self): @@ -77,8 +77,8 @@ class _FakeClientFactory: def GetNextClient(self): return self._clients.pop(0) - def __call__(self): - cl = self._client_cls() + def __call__(self, address=None): + cl = self._client_cls(address=address) self._clients.append(cl) return cl @@ -103,7 +103,7 @@ class TestConstants(unittest.TestCase): class TestClientConnectError(unittest.TestCase): @staticmethod - def _FailingClient(): + def _FailingClient(address=None): raise luxi.NoMasterError("test") def test(self): @@ -119,6 +119,9 @@ class TestClientConnectError(unittest.TestCase): class TestJobSubmitError(unittest.TestCase): class _SubmitErrorClient: + def __init__(self, address=None): + pass + @staticmethod def SubmitJob(ops): raise errors.JobQueueFull("test") @@ -272,6 +275,26 @@ class TestNodeEvacuate(unittest.TestCase): self.assertRaises(IndexError, cl.GetNextSubmittedJob) +class TestNodePowercycle(unittest.TestCase): + def test(self): + clfactory = _FakeClientFactory(_FakeClient) + handler = _CreateHandler(rlib2.R_2_nodes_name_powercycle, ["node20744"], { + "force": ["1"], + }, None, clfactory) + job_id = handler.POST() + + cl = clfactory.GetNextClient() + self.assertRaises(IndexError, clfactory.GetNextClient) + + (exp_job_id, (op, )) = cl.GetNextSubmittedJob() + self.assertEqual(job_id, exp_job_id) + self.assertTrue(isinstance(op, opcodes.OpNodePowercycle)) + self.assertEqual(op.node_name, "node20744") + self.assertTrue(op.force) + + self.assertRaises(IndexError, cl.GetNextSubmittedJob) + + class TestGroupAssignNodes(unittest.TestCase): def test(self): clfactory = _FakeClientFactory(_FakeClient) @@ -446,6 +469,26 @@ class TestInstanceDeactivateDisks(unittest.TestCase): self.assertRaises(IndexError, cl.GetNextSubmittedJob) +class TestInstanceRecreateDisks(unittest.TestCase): + def test(self): + clfactory = _FakeClientFactory(_FakeClient) + handler = _CreateHandler(rlib2.R_2_instances_name_recreate_disks, + ["inst22357"], {}, {}, clfactory) + job_id = handler.POST() + + cl = clfactory.GetNextClient() + self.assertRaises(IndexError, clfactory.GetNextClient) + + (exp_job_id, (op, )) = cl.GetNextSubmittedJob() + self.assertEqual(job_id, exp_job_id) + self.assertTrue(isinstance(op, opcodes.OpInstanceRecreateDisks)) + self.assertEqual(op.instance_name, "inst22357") + self.assertFalse(hasattr(op, "dry_run")) + self.assertFalse(hasattr(op, "force")) + + self.assertRaises(IndexError, cl.GetNextSubmittedJob) + + class TestInstanceFailover(unittest.TestCase): def test(self): clfactory = _FakeClientFactory(_FakeClient) @@ -778,10 +821,13 @@ class TestInstanceCreation(testutils.GanetiTestCase): None, {}, { constants.BE_VCPUS: 2, }, - { constants.BE_MEMORY: 123, }, + { constants.BE_MAXMEM: 200, }, + { constants.BE_MEMORY: 256, }, { constants.BE_VCPUS: 2, - constants.BE_MEMORY: 1024, - constants.BE_AUTO_BALANCE: True, } + constants.BE_MAXMEM: 1024, + constants.BE_MINMEM: 1024, + constants.BE_AUTO_BALANCE: True, + constants.BE_ALWAYS_FAILOVER: True, } ] hvparam_variants = [ @@ -1208,7 +1254,7 @@ class TestParseModifyInstanceRequest(unittest.TestCase): for osparams in [{}, { "some": "value", "other": "Hello World", }]: for hvparams in [{}, { constants.HV_KERNEL_PATH: "/some/kernel", }]: - for beparams in [{}, { constants.BE_MEMORY: 128, }]: + for beparams in [{}, { constants.BE_MAXMEM: 128, }]: for force in [False, True]: for nics in [[], [(0, { constants.INIC_IP: "192.0.2.1", })]]: for disks in test_disks: @@ -1320,6 +1366,10 @@ class TestParseInstanceReinstallRequest(testutils.GanetiTestCase): self.assertEqual(ops[1].os_type, "linux1") self.assertFalse(ops[1].osparams) + def testErrors(self): + self.assertRaises(http.HttpBadRequest, self.Parse, + "foo", "not a dictionary") + class TestGroupRename(unittest.TestCase): def test(self): @@ -1427,6 +1477,20 @@ class TestInstanceReplaceDisks(unittest.TestCase): self.assertFalse(hasattr(op, "disks")) self.assertRaises(IndexError, cl.GetNextSubmittedJob) + def testNoDisks(self): + clfactory = _FakeClientFactory(_FakeClient) + + handler = _CreateHandler(rlib2.R_2_instances_name_replace_disks, + ["inst20661"], {}, {}, clfactory) + self.assertRaises(http.HttpBadRequest, handler.POST) + + for disks in [None, "", {}]: + handler = _CreateHandler(rlib2.R_2_instances_name_replace_disks, + ["inst20661"], {}, { + "disks": disks, + }, clfactory) + self.assertRaises(http.HttpBadRequest, handler.POST) + def testWrong(self): clfactory = _FakeClientFactory(_FakeClient) @@ -1641,9 +1705,10 @@ class TestSimpleResources(unittest.TestCase): handler = _CreateHandler(rlib2.R_2_features, [], {}, None, self.clfactory) self.assertEqual(set(handler.GET()), rlib2.ALL_FEATURES) - def testRoot(self): - handler = _CreateHandler(rlib2.R_root, [], {}, None, self.clfactory) - self.assertTrue(handler.GET() is None) + def testEmpty(self): + for cls in [rlib2.R_root, rlib2.R_2]: + handler = _CreateHandler(cls, [], {}, None, self.clfactory) + self.assertTrue(handler.GET() is None) def testVersion(self): handler = _CreateHandler(rlib2.R_version, [], {}, None, self.clfactory) @@ -1652,7 +1717,7 @@ class TestSimpleResources(unittest.TestCase): class TestClusterInfo(unittest.TestCase): class _ClusterInfoClient: - def __init__(self): + def __init__(self, address=None): self.cluster_info = None def QueryClusterInfo(self): @@ -1669,5 +1734,24 @@ class TestClusterInfo(unittest.TestCase): self.assertEqual(result, cl.cluster_info) +class TestInstancesMultiAlloc(unittest.TestCase): + def testInstanceUpdate(self): + clfactory = _FakeClientFactory(_FakeClient) + data = { + "instances": [{ + "instance_name": "bar", + "mode": "create", + }, { + "instance_name": "foo", + "mode": "create", + }], + } + handler = _CreateHandler(rlib2.R_2_instances_multi_alloc, [], {}, data, + clfactory) + (body, _) = handler.GetPostOpInput() + self.assertTrue(compat.all([inst["OP_ID"] == handler.POST_OPCODE.OP_ID + for inst in body["instances"]])) + + if __name__ == '__main__': testutils.GanetiTestProgram()