Revision 601dfcbb
b/lib/rpc.py | ||
---|---|---|
516 | 516 |
for name, disks in value.items()) |
517 | 517 |
|
518 | 518 |
|
519 |
def _PrepareFileUpload(filename): |
|
519 |
def _PrepareFileUpload(getents_fn, filename):
|
|
520 | 520 |
"""Loads a file and prepares it for an upload to nodes. |
521 | 521 |
|
522 | 522 |
""" |
523 |
# TODO: Use ReadFile(preread=...) and os.fstat |
|
523 | 524 |
data = _Compress(utils.ReadFile(filename)) |
524 | 525 |
st = os.stat(filename) |
525 |
getents = runtime.GetEnts() |
|
526 |
|
|
527 |
if getents_fn is None: |
|
528 |
getents_fn = runtime.GetEnts |
|
529 |
|
|
530 |
getents = getents_fn() |
|
531 |
|
|
526 | 532 |
return [filename, data, st.st_mode, getents.LookupUid(st.st_uid), |
527 | 533 |
getents.LookupGid(st.st_gid), st.st_atime, st.st_mtime] |
528 | 534 |
|
... | ... | |
569 | 575 |
rpc_defs.ED_OBJECT_DICT: _ObjectToDict, |
570 | 576 |
rpc_defs.ED_OBJECT_DICT_LIST: _ObjectListToDict, |
571 | 577 |
rpc_defs.ED_NODE_TO_DISK_DICT: _EncodeNodeToDiskDict, |
572 |
rpc_defs.ED_FILE_DETAILS: _PrepareFileUpload, |
|
573 | 578 |
rpc_defs.ED_COMPRESS: _Compress, |
574 | 579 |
rpc_defs.ED_FINALIZE_EXPORT_DISKS: _PrepareFinalizeExportDisks, |
575 | 580 |
rpc_defs.ED_IMPEXP_IO: _EncodeImportExportIO, |
... | ... | |
597 | 602 |
|
598 | 603 |
encoders = _ENCODERS.copy() |
599 | 604 |
|
600 |
# Add encoders requiring configuration object |
|
601 | 605 |
encoders.update({ |
606 |
# Encoders requiring configuration object |
|
602 | 607 |
rpc_defs.ED_INST_DICT: self._InstDict, |
603 | 608 |
rpc_defs.ED_INST_DICT_HVP_BEP: self._InstDictHvpBep, |
604 | 609 |
rpc_defs.ED_INST_DICT_OSP: self._InstDictOsp, |
610 |
|
|
611 |
# Encoders with special requirements |
|
612 |
rpc_defs.ED_FILE_DETAILS: compat.partial(_PrepareFileUpload, _getents), |
|
605 | 613 |
}) |
606 | 614 |
|
607 | 615 |
# Resolver using configuration |
b/test/ganeti.rpc_unittest.py | ||
---|---|---|
25 | 25 |
import sys |
26 | 26 |
import unittest |
27 | 27 |
import random |
28 |
import tempfile |
|
28 | 29 |
|
29 | 30 |
from ganeti import constants |
30 | 31 |
from ganeti import compat |
... | ... | |
37 | 38 |
from ganeti import backend |
38 | 39 |
|
39 | 40 |
import testutils |
41 |
import mocks |
|
40 | 42 |
|
41 | 43 |
|
42 | 44 |
class _FakeRequestProcessor: |
... | ... | |
682 | 684 |
self.assertFalse(res.fail_msg) |
683 | 685 |
|
684 | 686 |
|
687 |
class _FakeConfigForRpcRunner: |
|
688 |
GetAllNodesInfo = NotImplemented |
|
689 |
|
|
690 |
def GetNodeInfo(self, name): |
|
691 |
return objects.Node(name=name) |
|
692 |
|
|
693 |
|
|
685 | 694 |
class TestRpcRunner(unittest.TestCase): |
686 | 695 |
def testUploadFile(self): |
687 |
runner = rpc.RpcRunner(_req_process_fn=http_proc) |
|
696 |
data = 1779 * "Hello World\n" |
|
697 |
|
|
698 |
tmpfile = tempfile.NamedTemporaryFile() |
|
699 |
tmpfile.write(data) |
|
700 |
tmpfile.flush() |
|
701 |
st = os.stat(tmpfile.name) |
|
702 |
|
|
703 |
def _VerifyRequest(req): |
|
704 |
(uldata, ) = serializer.LoadJson(req.post_data) |
|
705 |
self.assertEqual(len(uldata), 7) |
|
706 |
self.assertEqual(uldata[0], tmpfile.name) |
|
707 |
self.assertEqual(list(uldata[1]), list(rpc._Compress(data))) |
|
708 |
self.assertEqual(uldata[2], st.st_mode) |
|
709 |
self.assertEqual(uldata[3], "user%s" % os.getuid()) |
|
710 |
self.assertEqual(uldata[4], "group%s" % os.getgid()) |
|
711 |
self.assertEqual(uldata[5], st.st_atime) |
|
712 |
self.assertEqual(uldata[6], st.st_mtime) |
|
713 |
|
|
714 |
req.success = True |
|
715 |
req.resp_status_code = http.HTTP_OK |
|
716 |
req.resp_body = serializer.DumpJson((True, None)) |
|
717 |
|
|
718 |
http_proc = _FakeRequestProcessor(_VerifyRequest) |
|
719 |
cfg = _FakeConfigForRpcRunner() |
|
720 |
runner = rpc.RpcRunner(cfg, None, _req_process_fn=http_proc, |
|
721 |
_getents=mocks.FakeGetentResolver) |
|
722 |
|
|
723 |
nodes = [ |
|
724 |
"node1.example.com", |
|
725 |
] |
|
726 |
|
|
727 |
result = runner.call_upload_file(nodes, tmpfile.name) |
|
728 |
self.assertEqual(len(result), len(nodes)) |
|
729 |
for (idx, (node, res)) in enumerate(result.items()): |
|
730 |
self.assertFalse(res.fail_msg) |
|
688 | 731 |
|
689 | 732 |
|
690 | 733 |
if __name__ == "__main__": |
b/test/mocks.py | ||
---|---|---|
109 | 109 |
|
110 | 110 |
self.daemons_gid = gid |
111 | 111 |
self.admin_gid = gid |
112 |
|
|
113 |
def LookupUid(self, uid): |
|
114 |
return "user%s" % uid |
|
115 |
|
|
116 |
def LookupGid(self, gid): |
|
117 |
return "group%s" % gid |
Also available in: Unified diff