blob: 2a1a4da7c4834fcd71aaead1c1d8a302e1df237d [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):
62 return {
63 'ip-ssl': ['--ssl'],
64 'domain': ['--domain-socket=%s' % domain_socket_path(port)],
65 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)],
66 }.get(socket, None)
Roger Meier41ad4342015-03-24 22:30:40 +010067
James E. King IIIb2b767e2018-09-15 20:32:04 +000068 def _transport_args(self, transport):
69 return {
70 'zlib': ['--zlib'],
71 }.get(transport, None)
72
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 def build_command(self, port):
74 cmd = copy.copy(self._base_command)
75 args = copy.copy(self._extra_args2)
76 args.append('--protocol=' + self.protocol)
77 args.append('--transport=' + self.transport)
James E. King IIIb2b767e2018-09-15 20:32:04 +000078 transport_args = self._transport_args(self.transport)
79 if transport_args:
80 args += transport_args
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090081 socket_args = self._socket_args(self.socket, port)
82 if socket_args:
83 args += socket_args
84 args.append('--port=%d' % port)
85 if self._join_args:
86 cmd.append('%s' % " ".join(args))
87 else:
88 cmd.extend(args)
89 if self._extra_args:
90 cmd.extend(self._extra_args)
91 self.command = cmd
92 return self.command
Roger Meier41ad4342015-03-24 22:30:40 +010093
94
95class TestEntry(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090096 def __init__(self, testdir, server, client, delay, timeout, **kwargs):
97 self.testdir = testdir
98 self._log = multiprocessing.get_logger()
99 self._config = kwargs
100 self.protocol = kwargs['protocol']
101 self.transport = kwargs['transport']
102 self.socket = kwargs['socket']
103 srv_dict = self._fix_workdir(merge_dict(self._config, server))
104 cli_dict = self._fix_workdir(merge_dict(self._config, client))
105 cli_dict['extra_args2'] = srv_dict.pop('remote_args', [])
106 srv_dict['extra_args2'] = cli_dict.pop('remote_args', [])
107 self.server = TestProgram('server', **srv_dict)
108 self.client = TestProgram('client', **cli_dict)
109 self.delay = delay
110 self.timeout = timeout
111 self._name = None
112 # results
113 self.success = None
114 self.as_expected = None
115 self.returncode = None
116 self.expired = False
117 self.retry_count = 0
Roger Meier41ad4342015-03-24 22:30:40 +0100118
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900119 def _fix_workdir(self, config):
120 key = 'workdir'
121 path = config.get(key, None)
122 if not path:
123 path = self.testdir
124 if os.path.isabs(path):
125 path = os.path.realpath(path)
126 else:
Alexandre Detiste24df0a52025-01-16 00:39:20 +0100127 path = os.path.realpath(os.path.join(self.testdir, path))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900128 config.update({key: path})
129 return config
Roger Meier41ad4342015-03-24 22:30:40 +0100130
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 @classmethod
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900132 def get_name(cls, server, client, protocol, transport, socket, *args, **kwargs):
133 return '%s-%s_%s_%s-%s' % (server, client, protocol, transport, socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100134
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900135 @property
136 def name(self):
137 if not self._name:
138 self._name = self.get_name(
139 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
140 return self._name
Roger Meier41ad4342015-03-24 22:30:40 +0100141
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900142 @property
143 def transport_name(self):
144 return '%s-%s' % (self.transport, self.socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100145
146
147def test_name(server, client, protocol, transport, socket, **kwargs):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900148 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)