blob: 633e9261691d0e20a9070566c20771a925fd0376 [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
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +090024from .compat import path_join
James E. King III9bea32f2018-03-16 16:07:42 -040025from .util import merge_dict, domain_socket_path
Roger Meier41ad4342015-03-24 22:30:40 +010026
27
28class TestProgram(object):
James E. King III9bea32f2018-03-16 16:07:42 -040029 def __init__(self, kind, name, protocol, transport, socket, workdir, stop_signal, command, env=None,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090030 extra_args=[], extra_args2=[], join_args=False, **kwargs):
James E. King III9bea32f2018-03-16 16:07:42 -040031
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 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 III9bea32f2018-03-16 16:07:42 -040038 self.stop_signal = stop_signal
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090039 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 Meier41ad4342015-03-24 22:30:40 +010049
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 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 Meier41ad4342015-03-24 22:30:40 +010055
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 if cmd[0] == 'python':
57 cmd[0] = sys.executable
58 else:
59 cmd[0] = abs_if_exists(cmd[0])
60 return cmd
Roger Meier41ad4342015-03-24 22:30:40 +010061
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 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 Meier41ad4342015-03-24 22:30:40 +010068
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090069 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 Meier41ad4342015-03-24 22:30:40 +010086
87
88class TestEntry(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090089 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 Meier41ad4342015-03-24 22:30:40 +0100111
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900112 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 Meier41ad4342015-03-24 22:30:40 +0100123
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900124 @classmethod
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900125 def get_name(cls, server, client, protocol, transport, socket, *args, **kwargs):
126 return '%s-%s_%s_%s-%s' % (server, client, protocol, transport, socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100127
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900128 @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 Meier41ad4342015-03-24 22:30:40 +0100134
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900135 @property
136 def transport_name(self):
137 return '%s-%s' % (self.transport, self.socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100138
139
140def test_name(server, client, protocol, transport, socket, **kwargs):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)