blob: 49ba7d3f0dc4d565a2aa58684ed9dbe7095bca67 [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,
34 extra_args=[], 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._join_args = join_args
50
51 def _fix_cmd_path(self, cmd):
52 # if the arg is a file in the current directory, make it path
53 def abs_if_exists(arg):
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +090054 p = path_join(self.workdir, arg)
Roger Meier41ad4342015-03-24 22:30:40 +010055 return p if os.path.exists(p) else arg
56
57 if cmd[0] == 'python':
58 cmd[0] = sys.executable
59 else:
60 cmd[0] = abs_if_exists(cmd[0])
61 return cmd
62
pavlodd08f6e2015-10-08 16:43:56 -040063 def _socket_args(self, socket, port):
Roger Meier41ad4342015-03-24 22:30:40 +010064 return {
pavlodd08f6e2015-10-08 16:43:56 -040065 'ip-ssl': ['--ssl'],
66 'domain': ['--domain-socket=%s' % domain_socket_path(port)],
67 'abstract': ['--abstract-namespace', '--domain-socket=%s' % domain_socket_path(port)],
Roger Meier41ad4342015-03-24 22:30:40 +010068 }.get(socket, None)
69
70 def build_command(self, port):
71 cmd = copy.copy(self._base_command)
72 args = []
73 args.append('--protocol=' + self.protocol)
74 args.append('--transport=' + self.transport)
pavlodd08f6e2015-10-08 16:43:56 -040075 socket_args = self._socket_args(self.socket, port)
76 if socket_args:
77 args += socket_args
Roger Meier41ad4342015-03-24 22:30:40 +010078 args.append('--port=%d' % port)
79 if self._join_args:
80 cmd.append('%s' % " ".join(args))
81 else:
82 cmd.extend(args)
83 if self._extra_args:
84 cmd.extend(self._extra_args)
85 self.command = cmd
86 return self.command
87
88
89class TestEntry(object):
90 def __init__(self, testdir, server, client, delay, timeout, **kwargs):
91 self.testdir = testdir
92 self._log = multiprocessing.get_logger()
93 self._config = kwargs
94 self.protocol = kwargs['protocol']
95 self.transport = kwargs['transport']
96 self.socket = kwargs['socket']
97 self.server = TestProgram('server', **self._fix_workdir(merge_dict(self._config, server)))
98 self.client = TestProgram('client', **self._fix_workdir(merge_dict(self._config, client)))
99 self.delay = delay
100 self.timeout = timeout
101 self._name = None
102 # results
103 self.success = None
104 self.as_expected = None
105 self.returncode = None
106 self.expired = False
107
108 def _fix_workdir(self, config):
109 key = 'workdir'
110 path = config.get(key, None)
111 if not path:
112 path = self.testdir
113 if os.path.isabs(path):
114 path = os.path.realpath(path)
115 else:
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +0900116 path = os.path.realpath(path_join(self.testdir, path))
Roger Meier41ad4342015-03-24 22:30:40 +0100117 config.update({key: path})
118 return config
119
120 @classmethod
121 def get_name(cls, server, client, proto, trans, sock, *args):
122 return '%s-%s_%s_%s-%s' % (server, client, proto, trans, sock)
123
124 @property
125 def name(self):
126 if not self._name:
127 self._name = self.get_name(
128 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
129 return self._name
130
131 @property
132 def transport_name(self):
133 return '%s-%s' % (self.transport, self.socket)
134
135
136def test_name(server, client, protocol, transport, socket, **kwargs):
137 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)