forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test_graphs.py
388 lines (314 loc) · 15.3 KB
/
generate_test_graphs.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
#
# Copyright 2022 MongoDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
"""Test graphs for the graph visualizer and analyzer."""
import os
import json
import argparse
import networkx
from libdeps.graph import LibdepsGraph, EdgeProps, NodeProps
def get_args():
"""Create the argparse and return passed args."""
parser = argparse.ArgumentParser()
parser.add_argument('--graph-output-dir', type=str, action='store',
help="Directory test graphml files will be saved.",
default="build/opt/libdeps")
parser.add_argument('--generate-big-graphs', action='store_true',
help="Makes graphs which are large for testing scale.", default=False)
return parser.parse_args()
def add_node(graph, node, builder):
"""Add a node to the graph."""
graph.add_nodes_from([(node, {NodeProps.bin_type.name: builder})])
def add_edge(graph, from_node, to_node, **kwargs):
"""Add an edge to the graph."""
edge_props = {
EdgeProps.direct.name: kwargs[EdgeProps.direct.name],
EdgeProps.visibility.name: int(kwargs[EdgeProps.visibility.name]),
}
if kwargs.get('symbols'):
edge_props[EdgeProps.symbols.name] = kwargs.get('symbols')
graph.add_edges_from([(from_node, to_node, edge_props)])
def get_big_graph(int_id):
"""Generate a big graph."""
graph = LibdepsGraph()
graph.graph['build_dir'] = '.'
graph.graph['graph_schema_version'] = 2
graph.graph['deptypes'] = json.dumps({
"Global": 0,
"Public": 1,
"Private": 2,
"Interface": 3,
})
graph.graph['git_hash'] = f'BIG{int_id.zfill(4)}'
num_nodes = 200
for i in range(num_nodes):
add_node(graph, f'lib{i}.so', 'SharedLibrary')
for j in range(num_nodes - i):
add_edge(graph, f'lib{i}.so', f'lib{j}.so', direct=True,
visibility=graph.get_deptype('Public'), symbols='\n'.join(
[f"RandomString{i+j}" * 100 for i in range(10)]))
return graph
def get_double_diamond_mock_graph():
"""Construct a mock graph which covers a double diamond structure."""
graph = LibdepsGraph()
graph.graph['build_dir'] = '.'
graph.graph['graph_schema_version'] = 2
graph.graph['deptypes'] = json.dumps({
"Global": 0,
"Public": 1,
"Private": 2,
"Interface": 3,
})
graph.graph['git_hash'] = 'TEST001'
# builds a graph of mostly public edges that looks like this:
#
#
# /lib3.so /lib7.so
# | \ | \
# <-lib1.so--lib2.so lib5.so--lib6.so lib9.so
# | / | /
# \lib4.so \lib8.so
#
add_node(graph, 'lib1.so', 'SharedLibrary')
add_node(graph, 'lib2.so', 'SharedLibrary')
add_node(graph, 'lib3.so', 'SharedLibrary')
add_node(graph, 'lib4.so', 'SharedLibrary')
add_node(graph, 'lib5.so', 'SharedLibrary')
add_node(graph, 'lib6.so', 'SharedLibrary')
add_node(graph, 'lib7.so', 'SharedLibrary')
add_node(graph, 'lib8.so', 'SharedLibrary')
add_node(graph, 'lib9.so', 'SharedLibrary')
add_edge(graph, 'lib1.so', 'lib2.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib3.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib4.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib5.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib5.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib5.so', 'lib6.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib6.so', 'lib7.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib6.so', 'lib8.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib7.so', 'lib9.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib8.so', 'lib9.so', direct=True, visibility=graph.get_deptype('Public'))
# trans for 3 and 4
add_edge(graph, 'lib1.so', 'lib3.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib1.so', 'lib4.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 5
add_edge(graph, 'lib1.so', 'lib5.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib5.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 6
add_edge(graph, 'lib1.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 7
add_edge(graph, 'lib1.so', 'lib7.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib7.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib7.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib7.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib5.so', 'lib7.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 8
add_edge(graph, 'lib1.so', 'lib8.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib8.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib8.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib8.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib5.so', 'lib8.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 9
add_edge(graph, 'lib1.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib5.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib6.so', 'lib9.so', direct=False, visibility=graph.get_deptype('Public'))
return graph
def get_basic_mock_graph():
"""Construct a mock graph which covers most cases and is easy to understand."""
graph = LibdepsGraph()
graph.graph['build_dir'] = '.'
graph.graph['graph_schema_version'] = 2
graph.graph['deptypes'] = json.dumps({
"Global": 0,
"Public": 1,
"Private": 2,
"Interface": 3,
})
graph.graph['git_hash'] = 'TEST002'
# builds a graph of mostly public edges:
#
# /-lib5.so
# /lib3.so
# | \-lib6.so
# <-lib1.so--lib2.so
# | /-lib5.so (private)
# \lib4.so
# \-lib6.so
# nodes
add_node(graph, 'lib1.so', 'SharedLibrary')
add_node(graph, 'lib2.so', 'SharedLibrary')
add_node(graph, 'lib3.so', 'SharedLibrary')
add_node(graph, 'lib4.so', 'SharedLibrary')
add_node(graph, 'lib5.so', 'SharedLibrary')
add_node(graph, 'lib6.so', 'SharedLibrary')
# direct edges
add_edge(graph, 'lib1.so', 'lib2.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib3.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib2.so', 'lib4.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib6.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib5.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib3.so', 'lib6.so', direct=True, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib4.so', 'lib5.so', direct=True, visibility=graph.get_deptype('Private'))
# trans for 3
add_edge(graph, 'lib1.so', 'lib3.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 4
add_edge(graph, 'lib1.so', 'lib4.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 5
add_edge(graph, 'lib2.so', 'lib5.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib1.so', 'lib5.so', direct=False, visibility=graph.get_deptype('Public'))
# trans for 6
add_edge(graph, 'lib2.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
add_edge(graph, 'lib1.so', 'lib6.so', direct=False, visibility=graph.get_deptype('Public'))
return graph
def get_basic_mock_directory_graph():
"""Construct a mock graph which covers most cases and is easy to understand."""
graph = LibdepsGraph()
graph.graph['build_dir'] = '.'
graph.graph['graph_schema_version'] = 2
graph.graph['deptypes'] = json.dumps({
"Global": 0,
"Public": 1,
"Private": 2,
"Interface": 3,
})
graph.graph['git_hash'] = 'TEST003'
# builds a graph of mostly public edges:
#
# /-lib5.so
# /lib3
# | \-lib6.so
# <-lib1.so--lib2
# | /-lib5.so (private)
# \lib4.so
# \-lib6.so
# nodes
add_node(graph, 'dir1/lib1.so', 'SharedLibrary')
add_node(graph, 'dir1/sub1/lib2', 'Program')
add_node(graph, 'dir1/sub1/lib3', 'Program')
add_node(graph, 'dir1/sub2/lib4.so', 'SharedLibrary')
add_node(graph, 'dir2/lib5.so', 'SharedLibrary')
add_node(graph, 'dir2/lib6.so', 'SharedLibrary')
# direct edges
add_edge(graph, 'dir1/lib1.so', 'dir1/sub1/lib2', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub1/lib2', 'dir1/sub1/lib3', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub1/lib2', 'dir1/sub2/lib4.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub2/lib4.so', 'dir2/lib6.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub1/lib3', 'dir2/lib5.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub1/lib3', 'dir2/lib6.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/sub2/lib4.so', 'dir2/lib5.so', direct=True,
visibility=graph.get_deptype('Private'))
# trans for 3
add_edge(graph, 'dir1/lib1.so', 'dir1/sub1/lib3', direct=False,
visibility=graph.get_deptype('Public'))
# trans for 4
add_edge(graph, 'dir1/lib1.so', 'dir1/sub2/lib4.so', direct=False,
visibility=graph.get_deptype('Public'))
# trans for 5
add_edge(graph, 'dir1/sub1/lib2', 'dir2/lib5.so', direct=False,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/lib1.so', 'dir2/lib5.so', direct=False,
visibility=graph.get_deptype('Public'))
# trans for 6
add_edge(graph, 'dir1/sub1/lib2', 'dir2/lib6.so', direct=False,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'dir1/lib1.so', 'dir2/lib6.so', direct=False,
visibility=graph.get_deptype('Public'))
return graph
def get_simple_directory_graph():
"""Construct a mock graph which covers most cases and is easy to understand."""
graph = LibdepsGraph()
graph.graph['build_dir'] = '.'
graph.graph['graph_schema_version'] = 2
graph.graph['deptypes'] = json.dumps({
"Global": 0,
"Public": 1,
"Private": 2,
"Interface": 3,
})
graph.graph['git_hash'] = 'TEST004'
# lib2.so <- lib4.so
# /∧ \∨
# lib1.so prog1 <- lib5.so
# \∨ /∧
# lib3.so -> prog2
# nodes
add_node(graph, 'mongo/base/lib1.so', 'SharedLibrary')
add_node(graph, 'mongo/base/lib2.so', 'SharedLibrary')
add_node(graph, 'mongo/db/lib3.so', 'SharedLibrary')
add_node(graph, 'third_party/lib4.so', 'SharedLibrary')
add_node(graph, 'third_party/lib5.so', 'SharedLibrary')
add_node(graph, 'mongo/base/prog1', 'Program')
add_node(graph, 'mongo/db/prog2', 'Program')
# direct edges
add_edge(graph, 'mongo/base/lib1.so', 'mongo/base/lib2.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'mongo/base/lib1.so', 'mongo/db/lib3.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'mongo/base/lib2.so', 'mongo/base/prog1', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'mongo/db/lib3.so', 'mongo/base/prog1', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'mongo/db/lib3.so', 'mongo/db/prog2', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'third_party/lib4.so', 'mongo/base/lib2.so', direct=True,
visibility=graph.get_deptype('Public'))
add_edge(graph, 'third_party/lib5.so', 'mongo/base/prog1', direct=True,
visibility=graph.get_deptype('Public'))
return graph
def save_graph_file(graph, output_dir):
"""Save a graph locally as a .graphml."""
filename = output_dir + "/libdeps_" + graph.graph['git_hash'] + ".graphml"
networkx.write_graphml(graph, filename, named_key_ids=True)
def main():
"""Generate and save the test graphs as .graphml files."""
args = get_args()
output_dir = args.graph_output_dir
os.makedirs(output_dir, exist_ok=True)
graph = get_double_diamond_mock_graph()
save_graph_file(graph, output_dir)
graph = get_basic_mock_graph()
save_graph_file(graph, output_dir)
graph = get_basic_mock_directory_graph()
save_graph_file(graph, output_dir)
graph = get_simple_directory_graph()
save_graph_file(graph, output_dir)
if args.generate_big_graphs:
graph = get_big_graph("0")
for i in range(1, 30):
print(f"generating big graph {i}...")
graph.graph['git_hash'] = f'BIG{str(i).zfill(4)}'
save_graph_file(graph, output_dir)
if __name__ == "__main__":
main()