forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomocks
executable file
·120 lines (100 loc) · 5.48 KB
/
gomocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python3
# Copyright 2019 ETH Zurich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod: 'gomocks' --- Helper script to regenerate go mock files.
==============================================================
"""
import argparse
import os.path
from typing import Tuple
from plumbum import local
from plumbum.cmd import mkdir
# Prefix of SCION packages
SCION_PACKAGE_PREFIX = "github.com/scionproto/scion"
# Directory for standard library mocks
STD_LIB_MOCKS_DIRECTORY = os.path.join("go", "lib", "mocks")
MOCK_TARGETS = [
("net", "Addr,Conn,PacketConn"),
(SCION_PACKAGE_PREFIX + "/go/beacon_srv/internal/beacon", "DB,Transaction"),
(SCION_PACKAGE_PREFIX + "/go/beacon_srv/internal/beaconing",
"BeaconInserter,BeaconProvider,SegmentProvider"),
(SCION_PACKAGE_PREFIX + "/go/beacon_srv/internal/keepalive", "IfStatePusher,RevDropper"),
(SCION_PACKAGE_PREFIX + "/go/beacon_srv/internal/revocation", "Store"),
(SCION_PACKAGE_PREFIX + "/go/hidden_path_srv/internal/registration", "Validator"),
(SCION_PACKAGE_PREFIX + "/go/hidden_path_srv/internal/hpsegreq", "Fetcher"),
(SCION_PACKAGE_PREFIX + "/go/lib/ctrl/seg", "Signer"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra",
"ASInspector,Messenger,ResponseWriter,TrustStore,Verifier"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra/messenger", "LocalSVCRouter,Resolver"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra/modules/segfetcher",
"DstProvider,ReplyHandler,Requester,RequestAPI,Resolver," +
"Splitter,Validator"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra/modules/seghandler", "Storage,Verifier"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra/modules/trust/trustdb", "TrustDB"),
(SCION_PACKAGE_PREFIX + "/go/lib/infra/modules/trust/v2",
"CryptoProvider,DB,Inserter,Recurser,Resolver,Router,RPC"),
(SCION_PACKAGE_PREFIX + "/go/lib/l4", "L4Header"),
(SCION_PACKAGE_PREFIX + "/go/lib/log", "Handler,Logger"),
(SCION_PACKAGE_PREFIX + "/go/lib/overlay/conn", "Conn"),
(SCION_PACKAGE_PREFIX + "/go/lib/pathdb", "PathDB,Transaction,ReadWrite"),
(SCION_PACKAGE_PREFIX + "/go/lib/pathmgr", "Policy,Querier,Resolver"),
(SCION_PACKAGE_PREFIX + "/go/lib/revcache", "RevCache"),
(SCION_PACKAGE_PREFIX + "/go/lib/sciond", "Service,Connector"),
(SCION_PACKAGE_PREFIX +
"/go/lib/snet", "Conn,PacketDispatcherService,Network,PacketConn,Path,Router"),
(SCION_PACKAGE_PREFIX + "/go/lib/snet/internal/ctxmonitor", "Monitor"),
(SCION_PACKAGE_PREFIX + "/go/lib/snet/internal/pathsource", "PathSource"),
(SCION_PACKAGE_PREFIX + "/go/lib/sock/reliable", "DispatcherService"),
(SCION_PACKAGE_PREFIX + "/go/lib/sock/reliable/reconnect", "IOOperation,Reconnecter"),
(SCION_PACKAGE_PREFIX + "/go/lib/svc", "RequestHandler,RoundTripper"),
(SCION_PACKAGE_PREFIX + "/go/lib/svc/internal/ctxconn", "DeadlineCloser"),
(SCION_PACKAGE_PREFIX + "/go/lib/periodic/internal/metrics", "ExportMetric"),
(SCION_PACKAGE_PREFIX + "/go/lib/xtest", "Callback"),
(SCION_PACKAGE_PREFIX + "/go/path_srv/internal/segreq", "LocalInfo"),
(SCION_PACKAGE_PREFIX + "/go/path_srv/internal/segutil", "Policy"),
(SCION_PACKAGE_PREFIX + "/go/sciond/internal/fetcher", "Policy"),
(SCION_PACKAGE_PREFIX + "/go/sig/egress/iface", "Session"),
(SCION_PACKAGE_PREFIX + "/go/sig/egress/worker", "SCIONWriter"),
]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--package-matcher", default=os.environ.get("PKG_MATCHER", ""),
help="Generate mocks for all matched packages")
args = parser.parse_args()
mockgen = local[local.env["MOCKTOOL"]] if "MOCKTOOL" in local.env else local["mockgen"]
print("Generating mocks using tool", mockgen)
for (package, interfaces) in MOCK_TARGETS:
if args.package_matcher not in package:
continue
(mock_dir, mock_file) = get_mock_file_path(package)
mkdir("-p", mock_dir)
(mockgen[package, interfaces] > mock_file)()
print("Generated mocks for %s (%s)" % (package, interfaces))
def get_mock_file_path(package: str) -> Tuple[str, str]:
mock_parent_dir = get_relative_path(package)
package_name = os.path.basename(mock_parent_dir)
mock_dir = os.path.join(mock_parent_dir, "mock_" + package_name)
mock_file = os.path.join(mock_dir, package_name + ".go")
return (mock_dir, mock_file)
def get_relative_path(target_package: str) -> str:
if is_scion_package(target_package):
return strip_scion_package_prefix(target_package)
return os.path.join(STD_LIB_MOCKS_DIRECTORY, target_package)
def is_scion_package(target_package: str) -> bool:
return os.path.commonpath([SCION_PACKAGE_PREFIX, target_package]) == SCION_PACKAGE_PREFIX
def strip_scion_package_prefix(target_package: str) -> str:
return target_package[len(SCION_PACKAGE_PREFIX)+1:]
if __name__ == "__main__":
main()