blob: f92b9e2d7521b03f6dc2388e016c627b11a05b28 [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 platform
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090021import re
Roger Meier41ad4342015-03-24 22:30:40 +010022from itertools import product
23
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +090024from .util import merge_dict
Roger Meier41ad4342015-03-24 22:30:40 +010025
26# Those keys are passed to execution as is.
27# Note that there are keys other than these, namely:
28# delay: After server is started, client start is delayed for the value
29# (seconds).
30# timeout: Test timeout after client is started (seconds).
31# platforms: Supported platforms. Should match platform.system() value.
32# protocols: list of supported protocols
33# transports: list of supported transports
34# sockets: list of supported sockets
Nobuaki Sukegawaf5b795d2015-03-29 14:48:48 +090035#
36# protocols and transports entries can be colon separated "spec:impl" pair
37# (e.g. binary:accel) where test is run for any matching "spec" while actual
38# argument passed to test executable is "impl".
39# Otherwise "spec" is equivalent to "spec:spec" pair.
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020040# (e.g. "binary" is equivalent to "binary:binary" in tests.json)
Nobuaki Sukegawaf5b795d2015-03-29 14:48:48 +090041#
Roger Meier41ad4342015-03-24 22:30:40 +010042VALID_JSON_KEYS = [
43 'name', # name of the library, typically a language name
44 'workdir', # work directory where command is executed
45 'command', # test command
46 'extra_args', # args appended to command after other args are appended
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090047 'remote_args', # args added to the other side of the program
Roger Meier41ad4342015-03-24 22:30:40 +010048 'join_args', # whether args should be passed as single concatenated string
49 'env', # additional environmental variable
50]
51
52DEFAULT_DELAY = 1
53DEFAULT_TIMEOUT = 5
54
55
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090056def _collect_testlibs(config, server_match, client_match=[None]):
Roger Meier41ad4342015-03-24 22:30:40 +010057 """Collects server/client configurations from library configurations"""
58 def expand_libs(config):
59 for lib in config:
60 sv = lib.pop('server', None)
61 cl = lib.pop('client', None)
62 yield lib, sv, cl
63
64 def yield_testlibs(base_configs, configs, match):
65 for base, conf in zip(base_configs, configs):
66 if conf:
67 if not match or base['name'] in match:
68 platforms = conf.get('platforms') or base.get('platforms')
69 if not platforms or platform.system() in platforms:
70 yield merge_dict(base, conf)
71
72 libs, svs, cls = zip(*expand_libs(config))
73 servers = list(yield_testlibs(libs, svs, server_match))
74 clients = list(yield_testlibs(libs, cls, client_match))
75 return servers, clients
76
77
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090078def collect_features(config, match):
79 res = list(map(re.compile, match))
80 return list(filter(lambda c: any(map(lambda r: r.search(c['name']), res)), config))
81
82
83def _do_collect_tests(servers, clients):
Roger Meier41ad4342015-03-24 22:30:40 +010084 def intersection(key, o1, o2):
85 """intersection of two collections.
86 collections are replaced with sets the first time"""
87 def cached_set(o, key):
88 v = o[key]
89 if not isinstance(v, set):
90 v = set(v)
91 o[key] = v
92 return v
93 return cached_set(o1, key) & cached_set(o2, key)
94
Roger Meier41ad4342015-03-24 22:30:40 +010095 def intersect_with_spec(key, o1, o2):
96 # store as set of (spec, impl) tuple
97 def cached_set(o):
98 def to_spec_impl_tuples(values):
99 for v in values:
100 spec, _, impl = v.partition(':')
101 yield spec, impl or spec
102 v = o[key]
103 if not isinstance(v, set):
104 v = set(to_spec_impl_tuples(set(v)))
105 o[key] = v
106 return v
107 for spec1, impl1 in cached_set(o1):
108 for spec2, impl2 in cached_set(o2):
109 if spec1 == spec2:
110 name = impl1 if impl1 == impl2 else '%s-%s' % (impl1, impl2)
111 yield name, impl1, impl2
112
113 def maybe_max(key, o1, o2, default):
114 """maximum of two if present, otherwise defult value"""
115 v1 = o1.get(key)
116 v2 = o2.get(key)
117 return max(v1, v2) if v1 and v2 else v1 or v2 or default
118
119 def filter_with_validkeys(o):
120 ret = {}
121 for key in VALID_JSON_KEYS:
122 if key in o:
123 ret[key] = o[key]
124 return ret
125
126 def merge_metadata(o, **ret):
127 for key in VALID_JSON_KEYS:
128 if key in o:
129 ret[key] = o[key]
130 return ret
131
132 for sv, cl in product(servers, clients):
133 for proto, proto1, proto2 in intersect_with_spec('protocols', sv, cl):
134 for trans, trans1, trans2 in intersect_with_spec('transports', sv, cl):
135 for sock in intersection('sockets', sv, cl):
136 yield {
137 'server': merge_metadata(sv, **{'protocol': proto1, 'transport': trans1}),
138 'client': merge_metadata(cl, **{'protocol': proto2, 'transport': trans2}),
139 'delay': maybe_max('delay', sv, cl, DEFAULT_DELAY),
140 'timeout': maybe_max('timeout', sv, cl, DEFAULT_TIMEOUT),
141 'protocol': proto,
142 'transport': trans,
143 'socket': sock
144 }
145
146
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900147def collect_cross_tests(tests_dict, server_match, client_match):
148 sv, cl = _collect_testlibs(tests_dict, server_match, client_match)
149 return list(_do_collect_tests(sv, cl))
150
151
152def collect_feature_tests(tests_dict, features_dict, server_match, feature_match):
153 sv, _ = _collect_testlibs(tests_dict, server_match)
154 ft = collect_features(features_dict, feature_match)
155 return list(_do_collect_tests(sv, ft))