def __init__(self): """Constructs a new GanetiContext object. There should be only a GanetiContext object at any time, so this function raises an error if this is not the case. """ assert self.__class__._instance is None, "double GanetiContext instance" # Create global configuration object self.cfg = config.ConfigWriter() # Locking manager self.glm = locking.GanetiLockManager( self.cfg.GetNodeList(), self.cfg.GetNodeGroupList(), [inst.name for inst in self.cfg.GetAllInstancesInfo().values()], self.cfg.GetNetworkList()) self.cfg.SetContext(self) # RPC runner self.rpc = rpc.RpcRunner(self.cfg, self.glm.AddToLockMonitor) # Job queue self.jobqueue = jqueue.JobQueue(self) # setting this also locks the class against attribute modifications self.__class__._instance = self
def testUploadFile(self): data = 1779 * b"Hello World\n" tmpfile = tempfile.NamedTemporaryFile() tmpfile.write(data) tmpfile.flush() st = os.stat(tmpfile.name) nodes = [ "node1.example.com", ] def _VerifyRequest(req): (uldata, ) = serializer.LoadJson(req.post_data) self.assertEqual(len(uldata), 7) self.assertEqual(uldata[0], tmpfile.name) compressed = [ x.decode("ascii") if isinstance(x, bytes) else x for x in rpc._Compress(nodes[0], data) ] self.assertEqual(list(uldata[1]), compressed) self.assertEqual(uldata[2], st.st_mode) self.assertEqual(uldata[3], "user%s" % os.getuid()) self.assertEqual(uldata[4], "group%s" % os.getgid()) self.assertTrue(uldata[5] is not None) self.assertEqual(uldata[6], st.st_mtime) req.success = True req.resp_status_code = http.HTTP_OK req.resp_body = serializer.DumpJson((True, None)) http_proc = _FakeRequestProcessor(_VerifyRequest) std_runner = rpc.RpcRunner(_FakeConfigForRpcRunner(), None, _req_process_fn=http_proc, _getents=mocks.FakeGetentResolver) cfg_runner = rpc.ConfigRunner(None, ["192.0.2.13"], _req_process_fn=http_proc, _getents=mocks.FakeGetentResolver) for runner in [std_runner, cfg_runner]: result = runner.call_upload_file(nodes, tmpfile.name) self.assertEqual(len(result), len(nodes)) for (idx, (node, res)) in enumerate(result.items()): self.assertFalse(res.fail_msg)
def GetRpc(self, cfg): return rpc.RpcRunner(cfg, lambda _: None)
def testEncodeInstance(self): cluster = objects.Cluster(hvparams={ constants.HT_KVM: { constants.HV_CDROM_IMAGE_PATH: "foo", }, }, beparams={ constants.PP_DEFAULT: { constants.BE_MAXMEM: 8192, }, }, os_hvp={}, osparams={ "linux": { "role": "unknown", }, }) cluster.UpgradeConfig() inst = objects.Instance(name="inst1.example.com", hypervisor=constants.HT_KVM, os="linux", hvparams={ constants.HV_CDROM_IMAGE_PATH: "bar", constants.HV_ROOT_PATH: "/tmp", }, beparams={ constants.BE_MINMEM: 128, constants.BE_MAXMEM: 256, }, nics=[ objects.NIC(nicparams={ constants.NIC_MODE: "mymode", }), ], disk_template=constants.DT_PLAIN, disks=["disk_uuid_1", "disk_uuid_2"]) inst.UpgradeConfig() cfg = _FakeConfigForRpcRunner(cluster=cluster) runner = rpc.RpcRunner(cfg, None, _req_process_fn=NotImplemented, _getents=mocks.FakeGetentResolver) def _CheckBasics(result): self.assertEqual(result["name"], "inst1.example.com") self.assertEqual(result["os"], "linux") self.assertEqual(result["beparams"][constants.BE_MINMEM], 128) self.assertEqual(len(result["nics"]), 1) self.assertEqual( result["nics"][0]["nicparams"][constants.NIC_MODE], "mymode") # Generic object serialization result = runner._encoder(NotImplemented, (rpc_defs.ED_OBJECT_DICT, inst)) _CheckBasics(result) self.assertEqual(len(result["hvparams"]), 2) result = runner._encoder(NotImplemented, (rpc_defs.ED_OBJECT_DICT_LIST, 5 * [inst])) map(_CheckBasics, result) map(lambda r: self.assertEqual(len(r["hvparams"]), 2), result) # Just an instance result = runner._encoder(NotImplemented, (rpc_defs.ED_INST_DICT, inst)) _CheckBasics(result) self.assertEqual(result["beparams"][constants.BE_MAXMEM], 256) self.assertEqual(result["hvparams"][constants.HV_CDROM_IMAGE_PATH], "bar") self.assertEqual(result["hvparams"][constants.HV_ROOT_PATH], "/tmp") self.assertEqual(result["osparams"], { "role": "unknown", }) self.assertEqual(len(result["hvparams"]), len(constants.HVC_DEFAULTS[constants.HT_KVM])) # Instance with OS parameters result = runner._encoder(NotImplemented, (rpc_defs.ED_INST_DICT_OSP_DP, (inst, { "role": "webserver", "other": "field", }))) _CheckBasics(result) self.assertEqual(result["beparams"][constants.BE_MAXMEM], 256) self.assertEqual(result["hvparams"][constants.HV_CDROM_IMAGE_PATH], "bar") self.assertEqual(result["hvparams"][constants.HV_ROOT_PATH], "/tmp") self.assertEqual(result["osparams"], { "role": "webserver", "other": "field", }) # Instance with hypervisor and backend parameters result = runner._encoder(NotImplemented, (rpc_defs.ED_INST_DICT_HVP_BEP_DP, (inst, { constants.HT_KVM: { constants.HV_BOOT_ORDER: "xyz", }, }, { constants.BE_VCPUS: 100, constants.BE_MAXMEM: 4096, }))) _CheckBasics(result) self.assertEqual(result["beparams"][constants.BE_MAXMEM], 4096) self.assertEqual(result["beparams"][constants.BE_VCPUS], 100) self.assertEqual(result["hvparams"][constants.HT_KVM], { constants.HV_BOOT_ORDER: "xyz", }) del result["disks_info"][0]["ctime"] del result["disks_info"][0]["mtime"] del result["disks_info"][1]["ctime"] del result["disks_info"][1]["mtime"] self.assertEqual( result["disks_info"], [{ "dev_type": constants.DT_PLAIN, "dynamic_params": {}, "size": 4096, "logical_id": ("vg", "disk6120"), "params": constants.DISK_DT_DEFAULTS[inst.disk_template], "serial_no": 1, "uuid": "disk_uuid_1", }, { "dev_type": constants.DT_PLAIN, "dynamic_params": {}, "size": 1024, "logical_id": ("vg", "disk8508"), "params": constants.DISK_DT_DEFAULTS[inst.disk_template], "serial_no": 1, "uuid": "disk_uuid_2", }]) inst_disks = cfg.GetInstanceDisks(inst.uuid) self.assertTrue(compat.all(disk.params == {} for disk in inst_disks), msg="Configuration objects were modified")
def GetRpc(self, cfg): return rpc.RpcRunner(cfg, self.glm.AddToLockMonitor)