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 |
Nobuaki Sukegawa | 2de2700 | 2015-11-22 01:13:48 +0900 | [diff] [blame] | 24 | from .compat import path_join |
Nobuaki Sukegawa | a6ab1f5 | 2015-11-28 15:04:39 +0900 | [diff] [blame] | 25 | from .util import merge_dict |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def domain_socket_path(port): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 29 | return '/tmp/ThriftTest.thrift.%d' % port |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class TestProgram(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 33 | def __init__(self, kind, name, protocol, transport, socket, workdir, command, env=None, |
| 34 | extra_args=[], extra_args2=[], 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._extra_args2 = extra_args2 |
| 50 | self._join_args = join_args |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 51 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | def _fix_cmd_path(self, cmd): |
| 53 | # if the arg is a file in the current directory, make it path |
| 54 | def abs_if_exists(arg): |
| 55 | p = path_join(self.workdir, arg) |
| 56 | return p if os.path.exists(p) else arg |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 57 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 58 | if cmd[0] == 'python': |
| 59 | cmd[0] = sys.executable |
| 60 | else: |
| 61 | cmd[0] = abs_if_exists(cmd[0]) |
| 62 | return cmd |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 63 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | def _socket_args(self, socket, port): |
| 65 | return { |
| 66 | 'ip-ssl': ['--ssl'], |
| 67 | 'domain': ['--domain-socket=%s' % domain_socket_path(port)], |
| 68 | 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)], |
| 69 | }.get(socket, None) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 70 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 71 | def build_command(self, port): |
| 72 | cmd = copy.copy(self._base_command) |
| 73 | args = copy.copy(self._extra_args2) |
| 74 | args.append('--protocol=' + self.protocol) |
| 75 | args.append('--transport=' + self.transport) |
| 76 | socket_args = self._socket_args(self.socket, port) |
| 77 | if socket_args: |
| 78 | args += socket_args |
| 79 | args.append('--port=%d' % port) |
| 80 | if self._join_args: |
| 81 | cmd.append('%s' % " ".join(args)) |
| 82 | else: |
| 83 | cmd.extend(args) |
| 84 | if self._extra_args: |
| 85 | cmd.extend(self._extra_args) |
| 86 | self.command = cmd |
| 87 | return self.command |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 88 | |
| 89 | |
| 90 | class TestEntry(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | def __init__(self, testdir, server, client, delay, timeout, **kwargs): |
| 92 | self.testdir = testdir |
| 93 | self._log = multiprocessing.get_logger() |
| 94 | self._config = kwargs |
| 95 | self.protocol = kwargs['protocol'] |
| 96 | self.transport = kwargs['transport'] |
| 97 | self.socket = kwargs['socket'] |
| 98 | srv_dict = self._fix_workdir(merge_dict(self._config, server)) |
| 99 | cli_dict = self._fix_workdir(merge_dict(self._config, client)) |
| 100 | cli_dict['extra_args2'] = srv_dict.pop('remote_args', []) |
| 101 | srv_dict['extra_args2'] = cli_dict.pop('remote_args', []) |
| 102 | self.server = TestProgram('server', **srv_dict) |
| 103 | self.client = TestProgram('client', **cli_dict) |
| 104 | self.delay = delay |
| 105 | self.timeout = timeout |
| 106 | self._name = None |
| 107 | # results |
| 108 | self.success = None |
| 109 | self.as_expected = None |
| 110 | self.returncode = None |
| 111 | self.expired = False |
| 112 | self.retry_count = 0 |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 113 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 114 | def _fix_workdir(self, config): |
| 115 | key = 'workdir' |
| 116 | path = config.get(key, None) |
| 117 | if not path: |
| 118 | path = self.testdir |
| 119 | if os.path.isabs(path): |
| 120 | path = os.path.realpath(path) |
| 121 | else: |
| 122 | path = os.path.realpath(path_join(self.testdir, path)) |
| 123 | config.update({key: path}) |
| 124 | return config |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 125 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 126 | @classmethod |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 127 | def get_name(cls, server, client, protocol, transport, socket, *args, **kwargs): |
| 128 | return '%s-%s_%s_%s-%s' % (server, client, protocol, transport, socket) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 129 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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 |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 136 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 137 | @property |
| 138 | def transport_name(self): |
| 139 | return '%s-%s' % (self.transport, self.socket) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def test_name(server, client, protocol, transport, socket, **kwargs): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 143 | return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket) |