blob: 3da38f4e25731307331c1c3a2951783823939927 [file] [log] [blame]
Roger Meier41ad4342015-03-24 22:30:40 +01001#
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
20import copy
21import multiprocessing
22import os
23import sys
James E. King III9bea32f2018-03-16 16:07:42 -040024from .util import merge_dict, domain_socket_path
Roger Meier41ad4342015-03-24 22:30:40 +010025
26
27class TestProgram(object):
James E. King III9bea32f2018-03-16 16:07:42 -040028 def __init__(self, kind, name, protocol, transport, socket, workdir, stop_signal, command, env=None,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090029 extra_args=[], extra_args2=[], join_args=False, **kwargs):
James E. King III9bea32f2018-03-16 16:07:42 -040030
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090031 self.kind = kind
32 self.name = name
33 self.protocol = protocol
34 self.transport = transport
35 self.socket = socket
36 self.workdir = workdir
James E. King III9bea32f2018-03-16 16:07:42 -040037 self.stop_signal = stop_signal
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090038 self.command = None
39 self._base_command = self._fix_cmd_path(command)
40 if env:
41 self.env = copy.copy(os.environ)
42 self.env.update(env)
43 else:
44 self.env = os.environ
45 self._extra_args = extra_args
46 self._extra_args2 = extra_args2
47 self._join_args = join_args
Roger Meier41ad4342015-03-24 22:30:40 +010048
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090049 def _fix_cmd_path(self, cmd):
50 # if the arg is a file in the current directory, make it path
51 def abs_if_exists(arg):
Alexandre Detiste24df0a52025-01-16 00:39:20 +010052 p = os.path.join(self.workdir, arg)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090053 return p if os.path.exists(p) else arg
Roger Meier41ad4342015-03-24 22:30:40 +010054
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 if cmd[0] == 'python':
56 cmd[0] = sys.executable
57 else:
58 cmd[0] = abs_if_exists(cmd[0])
59 return cmd
Roger Meier41ad4342015-03-24 22:30:40 +010060
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090061 def _socket_args(self, socket, port):
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020062 support_socket_activation = self.kind == 'server' and sys.platform != "win32"
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 return {
64 'ip-ssl': ['--ssl'],
65 'domain': ['--domain-socket=%s' % domain_socket_path(port)],
Federico Giovanardi3b21bc92025-08-22 15:29:24 +020066 'domain-socketactivated': (['--emulate-socketactivation'] if support_socket_activation else []) + ['--domain-socket=%s' % domain_socket_path(port)],
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090067 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)],
68 }.get(socket, None)
Roger Meier41ad4342015-03-24 22:30:40 +010069
James E. King IIIb2b767e2018-09-15 20:32:04 +000070 def _transport_args(self, transport):
71 return {
72 'zlib': ['--zlib'],
73 }.get(transport, None)
74
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 def build_command(self, port):
76 cmd = copy.copy(self._base_command)
77 args = copy.copy(self._extra_args2)
78 args.append('--protocol=' + self.protocol)
79 args.append('--transport=' + self.transport)
James E. King IIIb2b767e2018-09-15 20:32:04 +000080 transport_args = self._transport_args(self.transport)
81 if transport_args:
82 args += transport_args
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090083 socket_args = self._socket_args(self.socket, port)
84 if socket_args:
85 args += socket_args
86 args.append('--port=%d' % port)
87 if self._join_args:
88 cmd.append('%s' % " ".join(args))
89 else:
90 cmd.extend(args)
91 if self._extra_args:
92 cmd.extend(self._extra_args)
93 self.command = cmd
94 return self.command
Roger Meier41ad4342015-03-24 22:30:40 +010095
96
97class TestEntry(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090098 def __init__(self, testdir, server, client, delay, timeout, **kwargs):
99 self.testdir = testdir
100 self._log = multiprocessing.get_logger()
101 self._config = kwargs
102 self.protocol = kwargs['protocol']
103 self.transport = kwargs['transport']
104 self.socket = kwargs['socket']
105 srv_dict = self._fix_workdir(merge_dict(self._config, server))
106 cli_dict = self._fix_workdir(merge_dict(self._config, client))
107 cli_dict['extra_args2'] = srv_dict.pop('remote_args', [])
108 srv_dict['extra_args2'] = cli_dict.pop('remote_args', [])
109 self.server = TestProgram('server', **srv_dict)
110 self.client = TestProgram('client', **cli_dict)
111 self.delay = delay
112 self.timeout = timeout
113 self._name = None
114 # results
115 self.success = None
116 self.as_expected = None
117 self.returncode = None
118 self.expired = False
119 self.retry_count = 0
Roger Meier41ad4342015-03-24 22:30:40 +0100120
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900121 def _fix_workdir(self, config):
122 key = 'workdir'
123 path = config.get(key, None)
124 if not path:
125 path = self.testdir
126 if os.path.isabs(path):
127 path = os.path.realpath(path)
128 else:
Alexandre Detiste24df0a52025-01-16 00:39:20 +0100129 path = os.path.realpath(os.path.join(self.testdir, path))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900130 config.update({key: path})
131 return config
Roger Meier41ad4342015-03-24 22:30:40 +0100132
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900133 @classmethod
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900134 def get_name(cls, server, client, protocol, transport, socket, *args, **kwargs):
135 return '%s-%s_%s_%s-%s' % (server, client, protocol, transport, socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100136
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900137 @property
138 def name(self):
139 if not self._name:
140 self._name = self.get_name(
141 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
142 return self._name
Roger Meier41ad4342015-03-24 22:30:40 +0100143
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900144 @property
145 def transport_name(self):
146 return '%s-%s' % (self.transport, self.socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100147
148
149def test_name(server, client, protocol, transport, socket, **kwargs):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900150 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)