blob: 63219e17b9eab1f5e6054cf4a71f3e50dc62d243 [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
Roger Meier41ad4342015-03-24 22:30:40 +010025
26from crossrunner.util import merge_dict
27
28
29def domain_socket_path(port):
30 return '/tmp/ThriftTest.thrift.%d' % port
31
32
33class TestProgram(object):
34 def __init__(self, kind, name, protocol, transport, socket, workdir, command, env=None,
35 extra_args=[], join_args=False, **kwargs):
36 self.kind = kind
37 self.name = name
38 self.protocol = protocol
39 self.transport = transport
40 self.socket = socket
41 self.workdir = workdir
42 self.command = None
43 self._base_command = self._fix_cmd_path(command)
44 if env:
45 self.env = copy.copy(os.environ)
46 self.env.update(env)
47 else:
48 self.env = os.environ
49 self._extra_args = extra_args
50 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)
73 args = []
74 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']
98 self.server = TestProgram('server', **self._fix_workdir(merge_dict(self._config, server)))
99 self.client = TestProgram('client', **self._fix_workdir(merge_dict(self._config, client)))
100 self.delay = delay
101 self.timeout = timeout
102 self._name = None
103 # results
104 self.success = None
105 self.as_expected = None
106 self.returncode = None
107 self.expired = False
108
109 def _fix_workdir(self, config):
110 key = 'workdir'
111 path = config.get(key, None)
112 if not path:
113 path = self.testdir
114 if os.path.isabs(path):
115 path = os.path.realpath(path)
116 else:
Nobuaki Sukegawa2de27002015-11-22 01:13:48 +0900117 path = os.path.realpath(path_join(self.testdir, path))
Roger Meier41ad4342015-03-24 22:30:40 +0100118 config.update({key: path})
119 return config
120
121 @classmethod
122 def get_name(cls, server, client, proto, trans, sock, *args):
123 return '%s-%s_%s_%s-%s' % (server, client, proto, trans, sock)
124
125 @property
126 def name(self):
127 if not self._name:
128 self._name = self.get_name(
129 self.server.name, self.client.name, self.protocol, self.transport, self.socket)
130 return self._name
131
132 @property
133 def transport_name(self):
134 return '%s-%s' % (self.transport, self.socket)
135
136
137def test_name(server, client, protocol, transport, socket, **kwargs):
138 return TestEntry.get_name(server['name'], client['name'], protocol, transport, socket)