Exemple #1
0
    def test_header_serialization(self):
        node = Node(node_name="test node",
                    pub_addr="10.10.10.10",
                    pub_port=1023)

        task_header = TaskHeader(
            task_id="xyz", environment="DEFAULT", task_owner=node)
        # ignore dynamic properties
        task_header.fixed_header.last_checking = 0

        task_header_dict = task_header.to_dict()
        serialized = CBORSerializer.dumps(task_header_dict)
        deserialized = CBORSerializer.loads(serialized)
        task_header_from_dict = TaskHeader.from_dict(deserialized)

        # ignore dynamic properties
        task_header_from_dict.fixed_header.last_checking = 0

        assert task_header_from_dict.to_dict() == task_header_dict
        assert isinstance(task_header_from_dict.task_owner, Node)

        task_header_bin = task_header.to_binary()
        bin_serialized = CBORSerializer.dumps(task_header_bin)
        bin_deserialized = CBORSerializer.loads(bin_serialized)

        assert bin_deserialized == task_header_bin
Exemple #2
0
 def get_short_hash(self):
     """Return short message representation for signature
     :return str: short hash of serialized and sorted message dictionary
                  representation
     """
     sorted_dict = self._sort_obj(self.dict_repr())
     return SimpleHash.hash(CBORSerializer.dumps(sorted_dict))
Exemple #3
0
 def serialize(self):
     """ Return serialized message
     :return str: serialized message """
     try:
         return CBORSerializer.dumps(
             [self.TYPE, self.sig, self.timestamp,
              self.dict_repr()])
     except Exception:
         logger.exception("Error serializing message:")
         raise
Exemple #4
0
    def dict_to_binary(cls, dictionary):
        self_dict = dict(dictionary)
        self_dict.pop('last_checking', None)
        self_dict.pop('signature', None)

        self_dict['task_owner'] = cls._ordered(self_dict['task_owner'])
        if self_dict.get('docker_images'):
            self_dict['docker_images'] = [
                cls._ordered(di) for di in self_dict['docker_images']
            ]

        return CBORSerializer.dumps(cls._ordered(self_dict))
Exemple #5
0
    def test_header_serialization(self):
        node = dict(node_name="test node")
        docker_images = [
            dict(repository="repo_{}".format(i),
                 id="id_{}".format(i),
                 tag="tag_{}".format(i)) for i in xrange(4)
        ]

        task_header = TaskHeader("ABC",
                                 "xyz",
                                 "10.10.10.10",
                                 1023,
                                 "key",
                                 "DEFAULT",
                                 task_owner=node,
                                 docker_images=docker_images)
        # ignore dynamic properties
        task_header.last_checking = 0

        task_header_dict = task_header.to_dict()
        serialized = CBORSerializer.dumps(task_header_dict)
        deserialized = CBORSerializer.loads(serialized)
        task_header_from_dict = TaskHeader.from_dict(deserialized)

        # ignore dynamic properties
        task_header_from_dict.last_checking = 0

        assert task_header_from_dict.to_dict() == task_header_dict
        assert isinstance(task_header_from_dict.task_owner, Node)
        assert all([
            isinstance(di, DockerImage)
            for di in task_header_from_dict.docker_images
        ])

        task_header_bin = task_header.to_binary()
        bin_serialized = CBORSerializer.dumps(task_header_bin)
        bin_deserialized = CBORSerializer.loads(bin_serialized)

        assert bin_deserialized == task_header_bin
Exemple #6
0
    def create(self, output_path, disk_files=None, cbor_files=None, **kwargs):

        if not disk_files and not cbor_files:
            raise ValueError('No files to pack')

        with self.generator(output_path) as of:

            if disk_files:
                for file_path in disk_files:
                    file_name = os.path.basename(file_path)
                    self.write_disk_file(of, file_path, file_name)

            if cbor_files:
                for file_name, file_data in cbor_files:
                    cbor_data = CBORSerializer.dumps(file_data)
                    self.write_cbor_file(of, file_name, cbor_data)

        return output_path
Exemple #7
0
 def dict_to_binary(cls, dictionary: dict) -> bytes:
     return CBORSerializer.dumps(cls.dict_to_binarizable(dictionary))
Exemple #8
0
 def __send_data_results(self, res):
     result = CBORSerializer.dumps(res.result)
     extra_data = {"subtask_id": res.subtask_id, "data_type": "result"}
     self.conn.producer = tcpnetwork.EncryptDataProducer(
         self.encrypt(result), self, extra_data=extra_data)
Exemple #9
0
 def __compress_and_dump_file(file_name, data):
     file_data = zlib.compress(data, 9)
     return CBORSerializer.dumps((os.path.basename(file_name), file_data))
Exemple #10
0
 def test(self):
     obj = MockSerializationSubject()
     serialized = CBORSerializer.dumps(obj)
     deserialized = CBORSerializer.loads(serialized)
     assert_properties(deserialized, obj)