blob: 512e664b0398dd045718f4d804bfbb59bcb9705f [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
24
25from crossrunner.util import merge_dict
26
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):
54 p = os.path.join(self.workdir, arg)
55 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
63 def _socket_arg(self, socket, port):
64 return {
65 'ip-ssl': '--ssl',
66 'domain': '--domain-socket=%s' % domain_socket_path(port),
67 }.get(socket, None)
68
69 def build_command(self, port):
70 cmd = copy.copy(self._base_command)
71 args = []
72 args.append('--protocol=' + self.protocol)
73 args.append('--transport=' + self.transport)
74 socket_arg = self._socket_arg(self.socket, port)
75 if socket_arg:
76 args.append(socket_arg)
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
86
87
88class TestEntry(object):
89 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 self.server = TestProgram('server', **self._fix_workdir(merge_dict(self._config, server)))
97 self.client = TestProgram('client', **self._fix_workdir(merge_dict(self._config, client)))
98 self.delay = delay
99 self.timeout = timeout
100 self._name = None
101 # results
102 self.success = None
103 self.as_expected = None
104 self.returncode = None
105 self.expired = False
106
107 def _fix_workdir(self, config):
108 key = 'workdir'
109 path = config.get(key, None)
110 if not path:
111 path = self.testdir
112 if os.path.isabs(path):
113 path = os.path.realpath(path)
114 else:
115 path = os.path.realpath(os.path.join(self.testdir, path))
116 config.update({key: path})
117 return config
118
119 @classmethod
120 def get_name(cls, server, client, proto, trans, sock, *args):
121 return '%s-%s_%s_%s-%s' % (server, client, proto, trans, sock)
122
123 @property
124 def name(self):
125 if not self._name:
126 self._name = self.get_name(
127 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
128 return self._name
129
130 @property
131 def transport_name(self):
132 return '%s-%s' % (self.transport, self.socket)
133
134
135def test_name(server, client, protocol, transport, socket, **kwargs):
136 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)