def test_from_to_json_with_gate(self, mock_get_now, mock_uuid): mock_get_now.return_value = get_now() mock_uuid.return_value = '22cd859d-ee91-4079-a112-000000000002' s = Server('server2', id='22cd859d-ee91-4079-a112-000000000001') gate = mock.MagicMock() gate.to_json.return_value = {'server_id': 1, 'server': 'n1'} type(s).gates = mock.PropertyMock(return_value=[gate]) self.assertDictEqual( { 'id': '22cd859d-ee91-4079-a112-000000000001', 'name': 'server2', 'granules': [], 'gates': [{}], 'created_on': mock_get_now.return_value.strftime(defaults.DATETIME_FORMAT), '_old_name': None, 'deleted': False }, s.to_json(add_gates=True)) db.session.add(s) db.session.commit() s_json = s.to_json(add_gates=True) smashed = Server.from_json(s_json) self.assertIs(s, smashed) self.assertEqual(s.id, smashed.id) self.assertEqual(s.name, smashed.name) self.assertEqual(s.granules, smashed.granules) self.assertEqual(s.last_modified_at, smashed.last_modified_at) self.assertListEqual(s.gates, smashed.gates) # from new Server db.session.remove() db.drop_all() db.create_all() with patch('dimensigon.domain.entities.server.Gate.from_json' ) as mock_gate_from_json: smashed = Server.from_json(s_json) self.assertEqual(s.id, smashed.id) self.assertEqual(s.name, smashed.name) self.assertEqual(s.granules, smashed.granules) self.assertEqual(s.last_modified_at, smashed.last_modified_at) self.assertEqual(1, len(smashed.gates)) mock_gate_from_json.assert_called_once()
def join_acknowledge(server_id): server_data = servers_to_be_created.get(server_id, None) if not server_data: raise errors.EntityNotFound('Server', server_id) s = Server.from_json(server_data) with _lock_add_node: db.session.add(s) db.session.commit() current_app.logger.debug(f"Server join acknowledge {s.name}") return {}, 204
def test_to_from_json(self, mock_uuid, mock_get_now): now = get_now() mock_get_now.return_value = now mock_uuid.side_effect = [ '22cd859d-ee91-4079-a112-000000000001', '22cd859d-ee91-4079-a112-000000000002', '22cd859d-ee91-4079-a112-000000000003' ] s = Server('server', dns_or_ip='dns', gates=[('gdns', 6000)], created_on=now) self.assertDictEqual( { 'id': '22cd859d-ee91-4079-a112-000000000001', 'name': 'server', 'granules': [], 'created_on': now.strftime(defaults.DATETIME_FORMAT), 'deleted': False, '_old_name': None }, s.to_json()) self.assertDictEqual( { 'id': '22cd859d-ee91-4079-a112-000000000001', 'name': 'server', 'granules': [], 'created_on': now.strftime(defaults.DATETIME_FORMAT), 'ignore_on_lock': False, }, s.to_json(no_delete=True, add_ignore=True)) db.session.add(s) db.session.commit() db.session.remove() del s s = Server.query.get('22cd859d-ee91-4079-a112-000000000001') self.assertDictEqual( { 'id': '22cd859d-ee91-4079-a112-000000000001', 'name': 'server', 'granules': [], 'last_modified_at': now.strftime(defaults.DATEMARK_FORMAT), 'created_on': now.strftime(defaults.DATETIME_FORMAT), }, s.to_json(no_delete=True)) smashed = Server.from_json(s.to_json()) self.assertIs(s, smashed) self.assertEqual(s.id, smashed.id) self.assertEqual(s.name, smashed.name) self.assertEqual(s.granules, smashed.granules) self.assertEqual(s.last_modified_at, smashed.last_modified_at)
def join(): global fetched_catalog if get_jwt_identity() == '00000000-0000-0000-0000-000000000004': js = request.get_json() current_app.logger.debug( f"New server wanting to join: {json.dumps(js, indent=2)}") if db.session.query(Server).filter_by( id=js.get('id', None)).count() > 0: raise errors.DuplicatedId(js.get('id', None)) if db.session.query(Server).filter_by( name=js.get('name', None)).count() > 0: raise errors.AlreadyExists('name', js.get('name', None)) s = Server.from_json(js) s.created_on = get_now() external_ip = ipaddress.ip_address(request.remote_addr) if not external_ip.is_loopback and external_ip not in [ gate.ip for gate in s.gates ]: for port in set([gate.port for gate in s.gates]): s.add_new_gate(external_ip, port, hidden=True) certfile = current_app.dm.config.http_conf.get('certfile', None) keyfile = current_app.dm.config.http_conf.get('keyfile', None) if keyfile and os.path.exists(keyfile): with open(keyfile, 'rb') as fh: keyfile_content = fh.read() else: raise errors.FileNotFound(keyfile) if certfile and os.path.exists(certfile): with open(certfile, 'rb') as fh: certfile_content = fh.read() else: raise errors.FileNotFound(certfile) data = { 'keyfile': base64.b64encode(keyfile_content).decode(), 'certfile': base64.b64encode(certfile_content).decode() } data.update(Dimension=g.dimension.to_json()) data.update(me=str(Server.get_current().id)) with _lock: if fetched_catalog[1] is None or fetched_catalog[0] < get_now( ) - dt.timedelta(minutes=1): c = fetch_catalog() fetched_catalog = (get_now(), c) else: c = fetched_catalog[1] data.update(catalog=c) if _lock_delete.acquire(False): try: delete_old_temp_servers() finally: _lock_delete.release() server_data = s.to_json(add_gates=True) servers_to_be_created.update({s.id: server_data}) del s return data, 200 else: raise errors.GenericError('Invalid token', status_code=400)