Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | import copy |
| 21 | import multiprocessing |
| 22 | import os |
| 23 | import sys |
| 24 | |
| 25 | from crossrunner.util import merge_dict |
| 26 | |
| 27 | |
| 28 | def domain_socket_path(port): |
| 29 | return '/tmp/ThriftTest.thrift.%d' % port |
| 30 | |
| 31 | |
| 32 | class TestProgram(object): |
| 33 | def __init__(self, kind, name, protocol, transport, socket, workdir, command, env=None, |
| 34 | extra_args=[], join_args=False, **kwargs): |
| 35 | self.kind = kind |
| 36 | self.name = name |
| 37 | self.protocol = protocol |
| 38 | self.transport = transport |
| 39 | self.socket = socket |
| 40 | self.workdir = workdir |
| 41 | self.command = None |
| 42 | self._base_command = self._fix_cmd_path(command) |
| 43 | if env: |
| 44 | self.env = copy.copy(os.environ) |
| 45 | self.env.update(env) |
| 46 | else: |
| 47 | self.env = os.environ |
| 48 | self._extra_args = extra_args |
| 49 | self._join_args = join_args |
| 50 | |
| 51 | def _fix_cmd_path(self, cmd): |
| 52 | # if the arg is a file in the current directory, make it path |
| 53 | def abs_if_exists(arg): |
Jens Geyer | aad06de | 2015-11-21 14:43:56 +0100 | [diff] [blame^] | 54 | p = self.workdir.decode(sys.getfilesystemencoding()) |
| 55 | p = os.path.join(p, arg.decode(sys.getfilesystemencoding())) |
| 56 | p = p.encode(sys.getfilesystemencoding()) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 57 | return p if os.path.exists(p) else arg |
| 58 | |
| 59 | if cmd[0] == 'python': |
| 60 | cmd[0] = sys.executable |
| 61 | else: |
| 62 | cmd[0] = abs_if_exists(cmd[0]) |
| 63 | return cmd |
| 64 | |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 65 | def _socket_args(self, socket, port): |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 66 | return { |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 67 | 'ip-ssl': ['--ssl'], |
| 68 | 'domain': ['--domain-socket=%s' % domain_socket_path(port)], |
| 69 | 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)], |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 70 | }.get(socket, None) |
| 71 | |
| 72 | def build_command(self, port): |
| 73 | cmd = copy.copy(self._base_command) |
| 74 | args = [] |
| 75 | args.append('--protocol=' + self.protocol) |
| 76 | args.append('--transport=' + self.transport) |
pavlo | dd08f6e | 2015-10-08 16:43:56 -0400 | [diff] [blame] | 77 | socket_args = self._socket_args(self.socket, port) |
| 78 | if socket_args: |
| 79 | args += socket_args |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 80 | args.append('--port=%d' % port) |
| 81 | if self._join_args: |
| 82 | cmd.append('%s' % " ".join(args)) |
| 83 | else: |
| 84 | cmd.extend(args) |
| 85 | if self._extra_args: |
| 86 | cmd.extend(self._extra_args) |
| 87 | self.command = cmd |
| 88 | return self.command |
| 89 | |
| 90 | |
| 91 | class TestEntry(object): |
| 92 | def __init__(self, testdir, server, client, delay, timeout, **kwargs): |
| 93 | self.testdir = testdir |
| 94 | self._log = multiprocessing.get_logger() |
| 95 | self._config = kwargs |
| 96 | self.protocol = kwargs['protocol'] |
| 97 | self.transport = kwargs['transport'] |
| 98 | self.socket = kwargs['socket'] |
| 99 | self.server = TestProgram('server', **self._fix_workdir(merge_dict(self._config, server))) |
| 100 | self.client = TestProgram('client', **self._fix_workdir(merge_dict(self._config, client))) |
| 101 | self.delay = delay |
| 102 | self.timeout = timeout |
| 103 | self._name = None |
| 104 | # results |
| 105 | self.success = None |
| 106 | self.as_expected = None |
| 107 | self.returncode = None |
| 108 | self.expired = False |
| 109 | |
| 110 | def _fix_workdir(self, config): |
| 111 | key = 'workdir' |
| 112 | path = config.get(key, None) |
| 113 | if not path: |
| 114 | path = self.testdir |
| 115 | if os.path.isabs(path): |
| 116 | path = os.path.realpath(path) |
| 117 | else: |
Jens Geyer | aad06de | 2015-11-21 14:43:56 +0100 | [diff] [blame^] | 118 | tmp = self.testdir.decode(sys.getfilesystemencoding()) |
| 119 | path = path.decode(sys.getfilesystemencoding()) |
| 120 | path = os.path.join(tmp, path) |
| 121 | path = path.encode(sys.getfilesystemencoding()) |
| 122 | path = os.path.realpath(path) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 123 | config.update({key: path}) |
| 124 | return config |
| 125 | |
| 126 | @classmethod |
| 127 | def get_name(cls, server, client, proto, trans, sock, *args): |
| 128 | return '%s-%s_%s_%s-%s' % (server, client, proto, trans, sock) |
| 129 | |
| 130 | @property |
| 131 | def name(self): |
| 132 | if not self._name: |
| 133 | self._name = self.get_name( |
| 134 | self.server.name, self.client.name, self.protocol, self.transport, self.socket) |
| 135 | return self._name |
| 136 | |
| 137 | @property |
| 138 | def transport_name(self): |
| 139 | return '%s-%s' % (self.transport, self.socket) |
| 140 | |
| 141 | |
| 142 | def test_name(server, client, protocol, transport, socket, **kwargs): |
| 143 | return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket) |