Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 1 | # |
| 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 | |
| 20 | import platform |
| 21 | from itertools import product |
| 22 | |
| 23 | from crossrunner.util import merge_dict |
| 24 | |
| 25 | # Those keys are passed to execution as is. |
| 26 | # Note that there are keys other than these, namely: |
| 27 | # delay: After server is started, client start is delayed for the value |
| 28 | # (seconds). |
| 29 | # timeout: Test timeout after client is started (seconds). |
| 30 | # platforms: Supported platforms. Should match platform.system() value. |
| 31 | # protocols: list of supported protocols |
| 32 | # transports: list of supported transports |
| 33 | # sockets: list of supported sockets |
Nobuaki Sukegawa | f5b795d | 2015-03-29 14:48:48 +0900 | [diff] [blame] | 34 | # |
| 35 | # protocols and transports entries can be colon separated "spec:impl" pair |
| 36 | # (e.g. binary:accel) where test is run for any matching "spec" while actual |
| 37 | # argument passed to test executable is "impl". |
| 38 | # Otherwise "spec" is equivalent to "spec:spec" pair. |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame^] | 39 | # (e.g. "binary" is equivalent to "binary:binary" in tests.json) |
Nobuaki Sukegawa | f5b795d | 2015-03-29 14:48:48 +0900 | [diff] [blame] | 40 | # |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 41 | VALID_JSON_KEYS = [ |
| 42 | 'name', # name of the library, typically a language name |
| 43 | 'workdir', # work directory where command is executed |
| 44 | 'command', # test command |
| 45 | 'extra_args', # args appended to command after other args are appended |
| 46 | 'join_args', # whether args should be passed as single concatenated string |
| 47 | 'env', # additional environmental variable |
| 48 | ] |
| 49 | |
| 50 | DEFAULT_DELAY = 1 |
| 51 | DEFAULT_TIMEOUT = 5 |
| 52 | |
| 53 | |
| 54 | def collect_testlibs(config, server_match, client_match): |
| 55 | """Collects server/client configurations from library configurations""" |
| 56 | def expand_libs(config): |
| 57 | for lib in config: |
| 58 | sv = lib.pop('server', None) |
| 59 | cl = lib.pop('client', None) |
| 60 | yield lib, sv, cl |
| 61 | |
| 62 | def yield_testlibs(base_configs, configs, match): |
| 63 | for base, conf in zip(base_configs, configs): |
| 64 | if conf: |
| 65 | if not match or base['name'] in match: |
| 66 | platforms = conf.get('platforms') or base.get('platforms') |
| 67 | if not platforms or platform.system() in platforms: |
| 68 | yield merge_dict(base, conf) |
| 69 | |
| 70 | libs, svs, cls = zip(*expand_libs(config)) |
| 71 | servers = list(yield_testlibs(libs, svs, server_match)) |
| 72 | clients = list(yield_testlibs(libs, cls, client_match)) |
| 73 | return servers, clients |
| 74 | |
| 75 | |
| 76 | def do_collect_tests(servers, clients): |
| 77 | def intersection(key, o1, o2): |
| 78 | """intersection of two collections. |
| 79 | collections are replaced with sets the first time""" |
| 80 | def cached_set(o, key): |
| 81 | v = o[key] |
| 82 | if not isinstance(v, set): |
| 83 | v = set(v) |
| 84 | o[key] = v |
| 85 | return v |
| 86 | return cached_set(o1, key) & cached_set(o2, key) |
| 87 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 88 | def intersect_with_spec(key, o1, o2): |
| 89 | # store as set of (spec, impl) tuple |
| 90 | def cached_set(o): |
| 91 | def to_spec_impl_tuples(values): |
| 92 | for v in values: |
| 93 | spec, _, impl = v.partition(':') |
| 94 | yield spec, impl or spec |
| 95 | v = o[key] |
| 96 | if not isinstance(v, set): |
| 97 | v = set(to_spec_impl_tuples(set(v))) |
| 98 | o[key] = v |
| 99 | return v |
| 100 | for spec1, impl1 in cached_set(o1): |
| 101 | for spec2, impl2 in cached_set(o2): |
| 102 | if spec1 == spec2: |
| 103 | name = impl1 if impl1 == impl2 else '%s-%s' % (impl1, impl2) |
| 104 | yield name, impl1, impl2 |
| 105 | |
| 106 | def maybe_max(key, o1, o2, default): |
| 107 | """maximum of two if present, otherwise defult value""" |
| 108 | v1 = o1.get(key) |
| 109 | v2 = o2.get(key) |
| 110 | return max(v1, v2) if v1 and v2 else v1 or v2 or default |
| 111 | |
| 112 | def filter_with_validkeys(o): |
| 113 | ret = {} |
| 114 | for key in VALID_JSON_KEYS: |
| 115 | if key in o: |
| 116 | ret[key] = o[key] |
| 117 | return ret |
| 118 | |
| 119 | def merge_metadata(o, **ret): |
| 120 | for key in VALID_JSON_KEYS: |
| 121 | if key in o: |
| 122 | ret[key] = o[key] |
| 123 | return ret |
| 124 | |
| 125 | for sv, cl in product(servers, clients): |
| 126 | for proto, proto1, proto2 in intersect_with_spec('protocols', sv, cl): |
| 127 | for trans, trans1, trans2 in intersect_with_spec('transports', sv, cl): |
| 128 | for sock in intersection('sockets', sv, cl): |
| 129 | yield { |
| 130 | 'server': merge_metadata(sv, **{'protocol': proto1, 'transport': trans1}), |
| 131 | 'client': merge_metadata(cl, **{'protocol': proto2, 'transport': trans2}), |
| 132 | 'delay': maybe_max('delay', sv, cl, DEFAULT_DELAY), |
| 133 | 'timeout': maybe_max('timeout', sv, cl, DEFAULT_TIMEOUT), |
| 134 | 'protocol': proto, |
| 135 | 'transport': trans, |
| 136 | 'socket': sock |
| 137 | } |
| 138 | |
| 139 | |
| 140 | def collect_tests(tests_dict, server_match, client_match): |
| 141 | sv, cl = collect_testlibs(tests_dict, server_match, client_match) |
| 142 | return list(do_collect_tests(sv, cl)) |