|
| 1 | +""" |
| 2 | +This module tests work with connection over socket fd. |
| 3 | +""" |
| 4 | +import os.path |
| 5 | +# pylint: disable=missing-class-docstring,missing-function-docstring |
| 6 | + |
| 7 | +import socket |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | +import tarantool |
| 12 | +from .lib.skip import skip_or_run_box_session_new_tests |
| 13 | +from .lib.tarantool_server import TarantoolServer, find_port |
| 14 | +from .utils import assert_admin_success |
| 15 | + |
| 16 | + |
| 17 | +def find_python(): |
| 18 | + for _dir in os.environ["PATH"].split(os.pathsep): |
| 19 | + exe = os.path.join(_dir, "python") |
| 20 | + if os.access(exe, os.X_OK): |
| 21 | + return os.path.abspath(exe) |
| 22 | + raise RuntimeError("Can't find python executable in " + os.environ["PATH"]) |
| 23 | + |
| 24 | + |
| 25 | +class TestSuiteSocketFD(unittest.TestCase): |
| 26 | + EVAL_USER = "return box.session.user()" |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def setUpClass(cls): |
| 30 | + print(' SOCKET FD '.center(70, '='), file=sys.stderr) |
| 31 | + print('-' * 70, file=sys.stderr) |
| 32 | + |
| 33 | + cls.srv = TarantoolServer() |
| 34 | + cls.srv.script = 'test/suites/box.lua' |
| 35 | + cls.srv.start() |
| 36 | + cls.tcp_port = find_port() |
| 37 | + |
| 38 | + # Start tcp server to test work with blocking sockets. |
| 39 | + # pylint:disable=C0209 |
| 40 | + resp = cls.srv.admin(""" |
| 41 | + local socket = require('socket') |
| 42 | +
|
| 43 | + box.cfg{} |
| 44 | + box.schema.user.create('test', {password = 'test', if_not_exists = true}) |
| 45 | + box.schema.user.grant('test', 'read,write,execute,create', 'universe', |
| 46 | + nil, {if_not_exists = true}) |
| 47 | + box.schema.user.grant('guest', 'execute', 'universe', |
| 48 | + nil, {if_not_exists = true}) |
| 49 | +
|
| 50 | + socket.tcp_server('0.0.0.0', %d, function(s) |
| 51 | + if not s:nonblock(true) then |
| 52 | + s:close() |
| 53 | + return |
| 54 | + end |
| 55 | + box.session.new({ |
| 56 | + type = 'binary', |
| 57 | + fd = s:fd(), |
| 58 | + user = 'test', |
| 59 | + }) |
| 60 | + s:detach() |
| 61 | + end) |
| 62 | +
|
| 63 | + box.schema.create_space('test', { |
| 64 | + format = {{type='unsigned', name='id'}}, |
| 65 | + if_not_exists = true, |
| 66 | + }) |
| 67 | + box.space.test:create_index('primary') |
| 68 | +
|
| 69 | + return true |
| 70 | + """ % cls.tcp_port) |
| 71 | + assert_admin_success(resp) |
| 72 | + |
| 73 | + @skip_or_run_box_session_new_tests |
| 74 | + def setUp(self): |
| 75 | + # Prevent a remote tarantool from clean our session. |
| 76 | + if self.srv.is_started(): |
| 77 | + self.srv.touch_lock() |
| 78 | + |
| 79 | + def _get_tt_sock(self): |
| 80 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 81 | + sock.connect((self.srv.host, self.tcp_port)) |
| 82 | + return sock |
| 83 | + |
| 84 | + def test_01_socket_fd_connect(self): |
| 85 | + sock = self._get_tt_sock() |
| 86 | + conn = tarantool.connect(host=None, port=sock.fileno()) |
| 87 | + sock.detach() |
| 88 | + try: |
| 89 | + self.assertSequenceEqual(conn.eval(self.EVAL_USER), ["test"]) |
| 90 | + finally: |
| 91 | + conn.close() |
| 92 | + |
| 93 | + def test_02_socket_fd_re_auth(self): |
| 94 | + sock = self._get_tt_sock() |
| 95 | + conn = tarantool.connect(host=None, port=sock.fileno(), user="guest") |
| 96 | + sock.detach() |
| 97 | + try: |
| 98 | + self.assertSequenceEqual(conn.eval(self.EVAL_USER), ["guest"]) |
| 99 | + finally: |
| 100 | + conn.close() |
| 101 | + |
| 102 | + def test_03_socket_fd_mesh(self): |
| 103 | + sock = self._get_tt_sock() |
| 104 | + pool = tarantool.ConnectionPool( |
| 105 | + addrs=[{'host': None, 'port': sock.fileno()}] |
| 106 | + ) |
| 107 | + sock.detach() |
| 108 | + try: |
| 109 | + self.assertSequenceEqual(pool.eval(self.EVAL_USER, mode=tarantool.Mode.ANY), ["test"]) |
| 110 | + finally: |
| 111 | + pool.close() |
| 112 | + |
| 113 | + def test_04_socket_fd_pool(self): |
| 114 | + sock = self._get_tt_sock() |
| 115 | + mesh = tarantool.MeshConnection( |
| 116 | + host=None, port=sock.fileno() |
| 117 | + ) |
| 118 | + sock.detach() |
| 119 | + try: |
| 120 | + self.assertSequenceEqual(mesh.eval(self.EVAL_USER), ["test"]) |
| 121 | + finally: |
| 122 | + mesh.close() |
| 123 | + |
| 124 | + @unittest.skipIf(sys.platform.startswith("win"), |
| 125 | + "Skip on Windows since it uses remote server") |
| 126 | + def test_05_tarantool_made_socket(self): |
| 127 | + python_exe = find_python() |
| 128 | + cwd = os.getcwd() |
| 129 | + side_script_path = os.path.join(cwd, "test", "suites", "sidecar.py") |
| 130 | + |
| 131 | + # pylint:disable=C0209 |
| 132 | + ret_code, err = self.srv.admin(""" |
| 133 | + local socket = require('socket') |
| 134 | + local popen = require('popen') |
| 135 | + local os = require('os') |
| 136 | + local s1, s2 = socket.socketpair('AF_UNIX', 'SOCK_STREAM', 0) |
| 137 | +
|
| 138 | + --[[ Tell sidecar which fd use to connect. --]] |
| 139 | + os.setenv('SOCKET_FD', tostring(s2:fd())) |
| 140 | +
|
| 141 | + --[[ Tell sidecar where find `tarantool` module. --]] |
| 142 | + os.setenv('PYTHONPATH', (os.getenv('PYTHONPATH') or '') .. ':' .. '%s') |
| 143 | +
|
| 144 | + box.session.new({ |
| 145 | + type = 'binary', |
| 146 | + fd = s1:fd(), |
| 147 | + user = 'test', |
| 148 | + }) |
| 149 | + s1:detach() |
| 150 | +
|
| 151 | + local ph, err = popen.new({'%s', '%s'}, { |
| 152 | + stdout = popen.opts.PIPE, |
| 153 | + stderr = popen.opts.PIPE, |
| 154 | + inherit_fds = {s2:fd()}, |
| 155 | + }) |
| 156 | +
|
| 157 | + if err ~= nil then |
| 158 | + return 1, err |
| 159 | + end |
| 160 | +
|
| 161 | + ph:wait() |
| 162 | +
|
| 163 | + local status_code = ph:info().status.exit_code |
| 164 | + local stderr = ph:read({stderr=true}):rstrip() |
| 165 | + return status_code, stderr |
| 166 | + """ % (cwd, python_exe, side_script_path)) |
| 167 | + if err is not None: |
| 168 | + print(err) |
| 169 | + self.assertEqual(ret_code, 0) |
| 170 | + self.assertIsNone(err) |
| 171 | + |
| 172 | + @classmethod |
| 173 | + def tearDownClass(cls): |
| 174 | + cls.srv.stop() |
| 175 | + cls.srv.clean() |
0 commit comments