blob: bb81c4fe5ea01d650a575af1134c0e21d0850f30 [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
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +090025from .util import merge_dict
Roger Meier41ad4342015-03-24 22:30:40 +010026
27
28def domain_socket_path(port):
29 return '/tmp/ThriftTest.thrift.%d' % port
30
31
32class TestProgram(object):
33 def __init__(self, kind, name, protocol, transport, socket, workdir, command, env=None,
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090034 extra_args=[], extra_args2=[], join_args=False, **kwargs):
Roger Meier41ad4342015-03-24 22:30:40 +010035 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
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090049 self._extra_args2 = extra_args2
Roger Meier41ad4342015-03-24 22:30:40 +010050 self._join_args = join_args
51
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):
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +090055 p = path_join(self.workdir, arg)
Roger Meier41ad4342015-03-24 22:30:40 +010056 return p if os.path.exists(p) else arg
57
58 if cmd[0] == 'python':
59 cmd[0] = sys.executable
60 else:
61 cmd[0] = abs_if_exists(cmd[0])
62 return cmd
63
pavlodd08f6e2015-10-08 16:43:56 -040064 def _socket_args(self, socket, port):
Roger Meier41ad4342015-03-24 22:30:40 +010065 return {
pavlodd08f6e2015-10-08 16:43:56 -040066 'ip-ssl': ['--ssl'],
67 'domain': ['--domain-socket=%s' % domain_socket_path(port)],
68 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)],
Roger Meier41ad4342015-03-24 22:30:40 +010069 }.get(socket, None)
70
71 def build_command(self, port):
72 cmd = copy.copy(self._base_command)
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090073 args = self._extra_args2
Roger Meier41ad4342015-03-24 22:30:40 +010074 args.append('--protocol=' + self.protocol)
75 args.append('--transport=' + self.transport)
pavlodd08f6e2015-10-08 16:43:56 -040076 socket_args = self._socket_args(self.socket, port)
77 if socket_args:
78 args += socket_args
Roger Meier41ad4342015-03-24 22:30:40 +010079 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
88
89
90class TestEntry(object):
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']
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090098 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)
Roger Meier41ad4342015-03-24 22:30:40 +0100104 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
113 def _fix_workdir(self, config):
114 key = 'workdir'
115 path = config.get(key, None)
116 if not path:
117 path = self.testdir
118 if os.path.isabs(path):
119 path = os.path.realpath(path)
120 else:
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +0900121 path = os.path.realpath(path_join(self.testdir, path))
Roger Meier41ad4342015-03-24 22:30:40 +0100122 config.update({key: path})
123 return config
124
125 @classmethod
126 def get_name(cls, server, client, proto, trans, sock, *args):
127 return '%s-%s_%s_%s-%s' % (server, client, proto, trans, sock)
128
129 @property
130 def name(self):
131 if not self._name:
132 self._name = self.get_name(
133 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
134 return self._name
135
136 @property
137 def transport_name(self):
138 return '%s-%s' % (self.transport, self.socket)
139
140
141def test_name(server, client, protocol, transport, socket, **kwargs):
142 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)