blob: 455189cbc56d40300c2a075eb3da78ad418aef36 [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
47 'join_args', # whether args should be passed as single concatenated string
48 'env', # additional environmental variable
49]
50
51DEFAULT_DELAY = 1
52DEFAULT_TIMEOUT = 5
53
54
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090055def _collect_testlibs(config, server_match, client_match=[None]):
Roger Meier41ad4342015-03-24 22:30:40 +010056 """Collects server/client configurations from library configurations"""
57 def expand_libs(config):
58 for lib in config:
59 sv = lib.pop('server', None)
60 cl = lib.pop('client', None)
61 yield lib, sv, cl
62
63 def yield_testlibs(base_configs, configs, match):
64 for base, conf in zip(base_configs, configs):
65 if conf:
66 if not match or base['name'] in match:
67 platforms = conf.get('platforms') or base.get('platforms')
68 if not platforms or platform.system() in platforms:
69 yield merge_dict(base, conf)
70
71 libs, svs, cls = zip(*expand_libs(config))
72 servers = list(yield_testlibs(libs, svs, server_match))
73 clients = list(yield_testlibs(libs, cls, client_match))
74 return servers, clients
75
76
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090077def collect_features(config, match):
78 res = list(map(re.compile, match))
79 return list(filter(lambda c: any(map(lambda r: r.search(c['name']), res)), config))
80
81
82def _do_collect_tests(servers, clients):
Roger Meier41ad4342015-03-24 22:30:40 +010083 def intersection(key, o1, o2):
84 """intersection of two collections.
85 collections are replaced with sets the first time"""
86 def cached_set(o, key):
87 v = o[key]
88 if not isinstance(v, set):
89 v = set(v)
90 o[key] = v
91 return v
92 return cached_set(o1, key) & cached_set(o2, key)
93
Roger Meier41ad4342015-03-24 22:30:40 +010094 def intersect_with_spec(key, o1, o2):
95 # store as set of (spec, impl) tuple
96 def cached_set(o):
97 def to_spec_impl_tuples(values):
98 for v in values:
99 spec, _, impl = v.partition(':')
100 yield spec, impl or spec
101 v = o[key]
102 if not isinstance(v, set):
103 v = set(to_spec_impl_tuples(set(v)))
104 o[key] = v
105 return v
106 for spec1, impl1 in cached_set(o1):
107 for spec2, impl2 in cached_set(o2):
108 if spec1 == spec2:
109 name = impl1 if impl1 == impl2 else '%s-%s' % (impl1, impl2)
110 yield name, impl1, impl2
111
112 def maybe_max(key, o1, o2, default):
113 """maximum of two if present, otherwise defult value"""
114 v1 = o1.get(key)
115 v2 = o2.get(key)
116 return max(v1, v2) if v1 and v2 else v1 or v2 or default
117
118 def filter_with_validkeys(o):
119 ret = {}
120 for key in VALID_JSON_KEYS:
121 if key in o:
122 ret[key] = o[key]
123 return ret
124
125 def merge_metadata(o, **ret):
126 for key in VALID_JSON_KEYS:
127 if key in o:
128 ret[key] = o[key]
129 return ret
130
131 for sv, cl in product(servers, clients):
132 for proto, proto1, proto2 in intersect_with_spec('protocols', sv, cl):
133 for trans, trans1, trans2 in intersect_with_spec('transports', sv, cl):
134 for sock in intersection('sockets', sv, cl):
135 yield {
136 'server': merge_metadata(sv, **{'protocol': proto1, 'transport': trans1}),
137 'client': merge_metadata(cl, **{'protocol': proto2, 'transport': trans2}),
138 'delay': maybe_max('delay', sv, cl, DEFAULT_DELAY),
139 'timeout': maybe_max('timeout', sv, cl, DEFAULT_TIMEOUT),
140 'protocol': proto,
141 'transport': trans,
142 'socket': sock
143 }
144
145
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900146def collect_cross_tests(tests_dict, server_match, client_match):
147 sv, cl = _collect_testlibs(tests_dict, server_match, client_match)
148 return list(_do_collect_tests(sv, cl))
149
150
151def collect_feature_tests(tests_dict, features_dict, server_match, feature_match):
152 sv, _ = _collect_testlibs(tests_dict, server_match)
153 ft = collect_features(features_dict, feature_match)
154 return list(_do_collect_tests(sv, ft))