Esempio n. 1
0
    def test_ping_request(self):
        from pydispatch import dispatcher
        monitor = SystemMonitor(NodeMetadataModel("CLIID", "SESSID", "hackix", "3.1337", "Descr", ClientConfigDescriptor()), MONITOR_CONFIG)
        port = random.randint(20, 50000)
        with mock.patch('requests.post') as post_mock:
            post_mock.return_value = response_mock = mock.MagicMock()
            response_mock.json = mock.MagicMock(return_value={'success': True})
            dispatcher.send(signal='golem.p2p', event='no event at all', port=port)
            self.assertEquals(post_mock.call_count, 0)
            dispatcher.send(signal='golem.p2p', event='listening', port=port)
            post_mock.assert_called_once_with(
                '%sping-me' % (MONITOR_CONFIG['HOST'],),
                data={'port': port},
                timeout=mock.ANY,
            )

            signals = []
            def l(sender, signal, event, **kwargs):
                signals.append((signal, event, kwargs))
            dispatcher.connect(l, signal="golem.p2p")
            response_mock.json = mock.MagicMock(return_value={'success': False, 'description': 'failure'})
            dispatcher.send(signal='golem.p2p', event='listening', port=port)
            signals = [s for s in signals if s[1] != 'listening']
            self.assertEquals(signals, [('golem.p2p', 'unreachable',
                                         {'description': 'failure', 'port': port})])
Esempio n. 2
0
    def test_task_computed(self):
        """golem.monitor signal"""
        from golem.monitor.model.nodemetadatamodel import NodeMetadataModel
        from golem.monitor.monitor import SystemMonitor
        from golem.monitorconfig import MONITOR_CONFIG
        monitor = SystemMonitor(
            NodeMetadataModel("CLIID", "SESSID", "hackix", "3.1337", "Descr",
                              config_desc()), MONITOR_CONFIG)
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()
        task = TaskComputer("ABC",
                            task_server,
                            use_docker_machine_manager=False)

        task_thread = mock.MagicMock()
        task_thread.start_time = time.time()
        duration = random.randint(1, 100)
        task_thread.end_time = task_thread.start_time + duration

        def prepare():
            subtask = mock.MagicMock()
            subtask_id = random.randint(3000, 4000)
            task.assigned_subtasks[subtask_id] = subtask
            task_thread.subtask_id = subtask_id

        def check(expected):
            with mock.patch(
                    'golem.monitor.monitor.SenderThread.send') as mock_send:
                task.task_computed(task_thread)
                self.assertEquals(mock_send.call_count, 1)
                result = mock_send.call_args[0][0].dict_repr()
                for key in ('cliid', 'sessid', 'timestamp'):
                    del result[key]
                expected_d = {
                    'type': 'ComputationTime',
                    'success': expected,
                    'value': duration,
                }
                self.assertEquals(expected_d, result)

        # error case
        prepare()
        task_thread.error = True
        check(False)

        # success case
        prepare()
        task_thread.error = False
        task_thread.error_msg = None
        task_thread.result = {
            'data': 'oh senora!!!',
            'result_type': 'Cadencia da Vila'
        }
        check(True)

        # default case (error)
        prepare()
        task_thread.result = None
        check(False)
Esempio n. 3
0
def meta_data():
    client_mock = mock.MagicMock()
    client_mock.cliid = str(uuid4())
    client_mock.sessid = str(uuid4())
    client_mock.config_desc = ClientConfigDescriptor()
    client_mock.mainnet = False
    os_info = OSInfo('linux', 'Linux', '1', '1.2.3')
    return NodeMetadataModel(client_mock, os_info, 'app_version')
Esempio n. 4
0
    def test_monitor_messages(self):
        nmm = NodeMetadataModel("CLIID", "SESSID", "win32", "1.3", "Random description\n\t with additional data",
                                ClientConfigDescriptor())
        m = MONITOR_CONFIG.copy()
        m['HOST'] = "http://localhost/88881"
        monitor = SystemMonitor(nmm, m)
        monitor.start()
        monitor.on_login()

        monitor.on_payment(addr="some address", value=30139019301)
        monitor.on_income("different address", 319031904194810)
        monitor.on_peer_snapshot([{"node_id": "firt node", "port": 19301},
                                  {"node_id": "second node", "port": 3193}])
        ccd = ClientConfigDescriptor()
        ccd.node_name = "new node name"
        nmm = NodeMetadataModel("CLIID", "SESSID", "win32", "1.3", "Random description\n\t with additional data",
                                ccd)
        monitor.on_config_update(nmm)
        monitor.on_logout()
        monitor.shut_down()
Esempio n. 5
0
    def test_protocol_versions(self):
        """Test wether correct protocol versions were sent."""
        from golem.network.p2p.peersession import P2P_PROTOCOL_ID
        from golem.task.tasksession import TASK_PROTOCOL_ID
        monitor = SystemMonitor(NodeMetadataModel("CLIID", "SESSID", "hackix", "3.1337", "Descr", ClientConfigDescriptor()), MONITOR_CONFIG)

        def check(f, msg_type):
            with mock.patch('golem.monitor.monitor.SenderThread.send') as mock_send:
                f()
                self.assertEquals(mock_send.call_count, 1)
                result = mock_send.call_args[0][0].dict_repr()
                for key in ('cliid', 'sessid', 'timestamp', 'metadata'):
                    del result[key]
                expected_d = {
                    'type': msg_type,
                    'protocol_versions': {
                        'monitor': MONITOR_CONFIG['PROTO_VERSION'],
                        'p2p': P2P_PROTOCOL_ID,
                        'task': TASK_PROTOCOL_ID,
                    },
                }

        check(monitor.on_login, "Login")
        check(monitor.on_logout, "Logout")
Esempio n. 6
0
 def __get_nodemetadatamodel(self):
     return NodeMetadataModel(self.get_client_id(), self.session_id,
                              sys.platform, APP_VERSION,
                              self.get_description(), self.config_desc)
Esempio n. 7
0
    def test_task_computed(self):
        """golem.monitor signal"""
        from golem.monitor.model.nodemetadatamodel import NodeMetadataModel
        from golem.monitor.monitor import SystemMonitor
        from golem.monitorconfig import MONITOR_CONFIG
        #  hold reference to avoid GC of monitor
        client_mock = mock.MagicMock()
        client_mock.cliid = 'CLIID'
        client_mock.sessid = 'SESSID'
        client_mock.config_desc = ClientConfigDescriptor()
        os_info = OSInfo('linux', 'Linux', '1', '1.2.3')
        monitor = SystemMonitor(  # noqa pylint: disable=unused-variable
            NodeMetadataModel(client_mock, os_info, "3.1337"), MONITOR_CONFIG)
        task_server = mock.MagicMock()
        task_server.config_desc = ClientConfigDescriptor()
        task_server.benchmark_manager.benchmarks_needed.return_value = False
        task_server.get_task_computer_root.return_value = self.new_path

        task = TaskComputer(task_server, use_docker_manager=False)

        task_thread = mock.MagicMock()
        task_thread.start_time = time.time()
        duration = random.randint(1, 100)
        task_thread.end_time = task_thread.start_time + duration

        def prepare():
            subtask = mock.MagicMock()
            subtask_id = random.randint(3000, 4000)
            subtask['subtask_id'] = subtask_id
            task_server\
                .task_keeper.task_headers[subtask_id].subtask_timeout = duration

            task.assigned_subtask = subtask
            task_thread.subtask_id = subtask_id

        def check(expected):
            with mock.patch('golem.monitor.monitor.SenderThread.send') \
                    as mock_send:
                task.task_computed(task_thread)
                self.assertEqual(mock_send.call_count, 1)
                result = mock_send.call_args[0][0].dict_repr()
                for key in ('cliid', 'sessid', 'timestamp'):
                    del result[key]
                expected_d = {
                    'type': 'ComputationTime',
                    'success': expected,
                    'value': duration,
                }
                self.assertEqual(expected_d, result)

        # error case
        prepare()
        task_thread.error = True
        check(False)

        # success case
        prepare()
        task_thread.error = False
        task_thread.error_msg = None
        task_thread.result = {'data': 'oh senora!!!'}
        check(True)

        # default case (error)
        prepare()
        task_thread.result = None
        check(False)
Esempio n. 8
0
def meta_data():
    cliid = uuid4().get_hex()
    sessid = uuid4().get_hex()
    return NodeMetadataModel(cliid, sessid, sys.platform, 'app_version',
                             'description', ClientConfigDescriptor())