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