blob: 74fd916ec7478eb4a0ff37c7c9deb52fbf46f719 [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):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090029 return '/tmp/ThriftTest.thrift.%d' % port
Roger Meier41ad4342015-03-24 22:30:40 +010030
31
32class TestProgram(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090033 def __init__(self, kind, name, protocol, transport, socket, workdir, command, env=None,
34 extra_args=[], extra_args2=[], join_args=False, **kwargs):
35 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
49 self._extra_args2 = extra_args2
50 self._join_args = join_args
Roger Meier41ad4342015-03-24 22:30:40 +010051
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090052 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):
55 p = path_join(self.workdir, arg)
56 return p if os.path.exists(p) else arg
Roger Meier41ad4342015-03-24 22:30:40 +010057
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090058 if cmd[0] == 'python':
59 cmd[0] = sys.executable
60 else:
61 cmd[0] = abs_if_exists(cmd[0])
62 return cmd
Roger Meier41ad4342015-03-24 22:30:40 +010063
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090064 def _socket_args(self, socket, port):
65 return {
66 'ip-ssl': ['--ssl'],
67 'domain': ['--domain-socket=%s' % domain_socket_path(port)],
68 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)],
69 }.get(socket, None)
Roger Meier41ad4342015-03-24 22:30:40 +010070
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090071 def build_command(self, port):
72 cmd = copy.copy(self._base_command)
73 args = copy.copy(self._extra_args2)
74 args.append('--protocol=' + self.protocol)
75 args.append('--transport=' + self.transport)
76 socket_args = self._socket_args(self.socket, port)
77 if socket_args:
78 args += socket_args
79 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
Roger Meier41ad4342015-03-24 22:30:40 +010088
89
90class TestEntry(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090091 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']
98 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)
104 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 self.retry_count = 0
Roger Meier41ad4342015-03-24 22:30:40 +0100113
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900114 def _fix_workdir(self, config):
115 key = 'workdir'
116 path = config.get(key, None)
117 if not path:
118 path = self.testdir
119 if os.path.isabs(path):
120 path = os.path.realpath(path)
121 else:
122 path = os.path.realpath(path_join(self.testdir, path))
123 config.update({key: path})
124 return config
Roger Meier41ad4342015-03-24 22:30:40 +0100125
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 @classmethod
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900127 def get_name(cls, server, client, protocol, transport, socket, *args, **kwargs):
128 return '%s-%s_%s_%s-%s' % (server, client, protocol, transport, socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100129
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900130 @property
131 def name(self):
132 if not self._name:
133 self._name = self.get_name(
134 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
135 return self._name
Roger Meier41ad4342015-03-24 22:30:40 +0100136
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900137 @property
138 def transport_name(self):
139 return '%s-%s' % (self.transport, self.socket)
Roger Meier41ad4342015-03-24 22:30:40 +0100140
141
142def test_name(server, client, protocol, transport, socket, **kwargs):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900143 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)