summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/graph.py
blob: 4edd6824f9fc731703a22cf5a5fdbf82b1df6713 (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# -*- coding: utf-8 -*-
# Test graph dot file generation
#
# Copyright (C) Andrew Bartlett 2018.
#
# Written by Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Tests for samba.graph"""

import samba
import samba.tests
from samba import graph

import re
import itertools


class DotFileTests(samba.tests.TestCaseInTempDir):

    def assertMatch(self, exp, s):
        m = re.match(exp, s)
        if m is None:
            self.fail("%r did not match /%s/" % (s, exp))
        return m

    def assertHeader(self, lines, title, directed):
        self.assertEqual(lines[0], '/* generated by samba */')
        if directed:
            exp = r'^digraph \w+ {$'
        else:
            exp = r'^graph \w+ {$'
        self.assertMatch(exp, lines[1])
        m = self.assertMatch(r'^label="([\w ]+)";$', lines[2])
        self.assertEqual(m.group(1), title)
        self.assertMatch(r'^fontsize=10;$', lines[3])
        self.assertMatch(r'$', lines[4])
        self.assertEqual(lines[5], 'node[fontname=Helvetica; fontsize=10];')
        self.assertEqual(lines[6], '')

    def assertVertices(self, lines, names):
        for n, line in zip(names, lines):
            m = self.assertMatch(r'^"(\w+)";$', line)
            self.assertEqual(n, m.group(1))

    def assertEdges(self, lines, edges, directed):
        connector = '->' if directed else '--'

        for edge, line in zip(edges, lines):
            a, b = edge
            m = self.assertMatch((r'^"(\w+)" ([>-]{2}) '
                                  r'"(\w+)" ?(?:\[([^\]])\])?;$'),
                                 line)
            self.assertEqual(m.group(1), a)
            self.assertEqual(m.group(2), connector)
            self.assertEqual(m.group(3), b)
            if m.group(4):
                self.assertMatch(r'^[\w ]*$', m.group(4))

    def test_basic_dot_files(self):
        vertices = tuple('abcdefgh')
        all_edges = tuple(itertools.combinations(vertices, 2))
        line_edges = list(zip(vertices[1:], vertices[:-1]))
        ring_edges = line_edges + [(vertices[0], vertices[-1])]
        no_edges = []
        # even join to even numbers, odd to odd
        disjoint_edges = [(a, b) for a, b in all_edges if
                          ord(a) ^ ord(b) == 0]

        for name, edges in (('all', all_edges),
                            ('line', line_edges),
                            ('ring', ring_edges),
                            ('no', no_edges),
                            ('disjoint', disjoint_edges)):

            for directed, tag in ((True, "directed"),
                                  (False, "undirected")):
                title = "%s %s" % (name, tag)

                g = graph.dot_graph(vertices, edges,
                                    directed=directed,
                                    title=title)
                lines = g.split('\n')
                self.assertHeader(lines, title, directed)
                self.assertVertices(lines[7:], vertices)
                self.assertEdges(lines[len(vertices) + 7:], edges, directed)


class DistanceTests(samba.tests.TestCase):

    def setUp(self):
        super().setUp()
        # a sorted list of colour set names.
        self.sorted_colour_sets = sorted(
            graph.COLOUR_SETS,
            # return '' for None, so it's sortable.
            key=lambda name: name or '')

    def test_simple_distance(self):
        edges = [('ant', 'bat'),
                 ('cat', 'dog'),
                 ('ant', 'elephant'),
                 ('elephant', 'dog'),
                 ('bat', 'dog'),
                 ('frog', 'elephant'),
                 ('frog', 'cat'),
                 ('bat', 'elephant'),
                 ('elephant', 'cat'),
                 ('cat', 'ant'),
                 ('cat', 'dog')]

        expected = {
                "utf8 True, colour None": '''
                 destination
         ╭────── ant
         │╭───── bat
         ││╭──── cat
         │││╭─── dog
         ││││╭── elephant
  source │││││╭─ frog
     ant ·1221-
     bat 3·211-
     cat 12·12-
     dog ---·--
elephant 2311·-
    frog 23121·''',
                'utf8 True, colour ansi': '''
                 destination
         ╭────── ant
         │╭───── bat
         ││╭──── cat
         │││╭─── dog
         ││││╭── elephant
  source │││││╭─ frog
     ant ·1221-
     bat 3·211-
     cat 12·12-
     dog ---·--
elephant 2311·-
    frog 23121·
                ''',
                'utf8 True, colour ansi-heatmap': '''
                 destination
         ╭────── ant
         │╭───── bat
         ││╭──── cat
         │││╭─── dog
         ││││╭── elephant
  source │││││╭─ frog
     ant ·1221-
     bat 3·211-
     cat 12·12-
     dog ---·--
elephant 2311·-
    frog 23121·
                ''',
                'utf8 True, colour xterm-256color': '''
                 destination
         ╭────── ant
         │╭───── bat
         ││╭──── cat
         │││╭─── dog
         ││││╭── elephant
  source │││││╭─ frog
     ant ·1221-
     bat 3·211-
     cat 12·12-
     dog ---·--
elephant 2311·-
    frog 23121·
                ''',
            'utf8 True, colour xterm-256color-heatmap': '''
                 destination
         ╭────── ant
         │╭───── bat
         ││╭──── cat
         │││╭─── dog
         ││││╭── elephant
  source │││││╭─ frog
     ant ·1221-
     bat 3·211-
     cat 12·12-
     dog ---·--
elephant 2311·-
    frog 23121·
''',
            'utf8 False, colour None': '''
                 destination
         ,------ ant
         |,----- bat
         ||,---- cat
         |||,--- dog
         ||||,-- elephant
  source |||||,- frog
     ant 01221-
     bat 30211-
     cat 12012-
     dog ---0--
elephant 23110-
    frog 231210
''',
            'utf8 False, colour ansi': '''
                 destination
         ,------ ant
         |,----- bat
         ||,---- cat
         |||,--- dog
         ||||,-- elephant
  source |||||,- frog
     ant 01221-
     bat 30211-
     cat 12012-
     dog ---0--
elephant 23110-
    frog 231210
''',
            'utf8 False, colour ansi-heatmap': '''
                 destination
         ,------ ant
         |,----- bat
         ||,---- cat
         |||,--- dog
         ||||,-- elephant
  source |||||,- frog
     ant 01221-
     bat 30211-
     cat 12012-
     dog ---0--
elephant 23110-
    frog 231210
''',
            'utf8 False, colour xterm-256color': '''
                 destination
         ,------ ant
         |,----- bat
         ||,---- cat
         |||,--- dog
         ||||,-- elephant
  source |||||,- frog
     ant 01221-
     bat 30211-
     cat 12012-
     dog ---0--
elephant 23110-
    frog 231210
''',
            'utf8 False, colour xterm-256color-heatmap': '''
                 destination
         ,------ ant
         |,----- bat
         ||,---- cat
         |||,--- dog
         ||||,-- elephant
  source |||||,- frog
     ant 01221-
     bat 30211-
     cat 12012-
     dog ---0--
elephant 23110-
    frog 231210
'''
        }
        for utf8 in (True, False):
            for colour in self.sorted_colour_sets:
                k = 'utf8 %s, colour %s' % (utf8, colour)
                s = graph.distance_matrix(None, edges, utf8=utf8,
                                          colour=colour)
                self.assertStringsEqual(s, expected[k], strip=True,
                                        msg='Wrong output: %s\n\n%s' % (k, s))

    def test_simple_distance2(self):
        edges = [('ant', 'bat'),
                 ('cat', 'bat'),
                 ('bat', 'ant'),
                 ('ant', 'cat')]
        expected = {
            'utf8 True, colour None': '''
            destination
       ╭─── ant
       │╭── bat
source ││╭─ cat
   ant ·11
   bat 1·2
   cat 21·
            ''',
            'utf8 True, colour ansi': '''
            destination
       ╭─── ant
       │╭── bat
source ││╭─ cat
   ant ·11
   bat 1·2
   cat 21·
            ''',
            'utf8 True, colour ansi-heatmap': '''
            destination
       ╭─── ant
       │╭── bat
source ││╭─ cat
   ant ·11
   bat 1·2
   cat 21·
        ''',
            'utf8 True, colour xterm-256color': '''
            destination
       ╭─── ant
       │╭── bat
source ││╭─ cat
   ant ·11
   bat 1·2
   cat 21·
''',
            'utf8 True, colour xterm-256color-heatmap': '''
            destination
       ╭─── ant
       │╭── bat
source ││╭─ cat
   ant ·11
   bat 1·2
   cat 21·
''',
            'utf8 False, colour None': '''
            destination
       ,--- ant
       |,-- bat
source ||,- cat
   ant 011
   bat 102
   cat 210
''',
            'utf8 False, colour ansi': '''
            destination
       ,--- ant
       |,-- bat
source ||,- cat
   ant 011
   bat 102
   cat 210
''',
            'utf8 False, colour ansi-heatmap': '''
            destination
       ,--- ant
       |,-- bat
source ||,- cat
   ant 011
   bat 102
   cat 210
''',
            'utf8 False, colour xterm-256color': '''
            destination
       ,--- ant
       |,-- bat
source ||,- cat
   ant 011
   bat 102
   cat 210
''',
            'utf8 False, colour xterm-256color-heatmap': '''
            destination
       ,--- ant
       |,-- bat
source ||,- cat
   ant 011
   bat 102
   cat 210
'''
        }
        for utf8 in (True, False):
            for colour in self.sorted_colour_sets:
                k = 'utf8 %s, colour %s' % (utf8, colour)
                s = graph.distance_matrix(None, edges, utf8=utf8,
                                          colour=colour)
                self.assertStringsEqual(s, expected[k], strip=True,
                                        msg='Wrong output: %s\n\n%s' % (k, s))

    def test_simple_distance3(self):
        edges = [('ant', 'bat'),
                 ('bat', 'cat'),
                 ('cat', 'dog'),
                 ('dog', 'ant'),
                 ('dog', 'eel')]
        expected = {
            'utf8 True, colour None': '''
              destination
       ╭───── ant
       │╭──── bat
       ││╭─── cat
       │││╭── dog
source ││││╭─ eel
   ant ·1234
   bat 3·123
   cat 23·12
   dog 123·1
   eel ----·
''',
            'utf8 True, colour ansi': '''
              destination
       ╭───── ant
       │╭──── bat
       ││╭─── cat
       │││╭── dog
source ││││╭─ eel
   ant ·1234
   bat 3·123
   cat 23·12
   dog 123·1
   eel ----·
''',
            'utf8 True, colour ansi-heatmap': '''
              destination
       ╭───── ant
       │╭──── bat
       ││╭─── cat
       │││╭── dog
source ││││╭─ eel
   ant ·1234
   bat 3·123
   cat 23·12
   dog 123·1
   eel ----·
''',
            'utf8 True, colour xterm-256color': '''
              destination
       ╭───── ant
       │╭──── bat
       ││╭─── cat
       │││╭── dog
source ││││╭─ eel
   ant ·1234
   bat 3·123
   cat 23·12
   dog 123·1
   eel ----·
''',
            'utf8 True, colour xterm-256color-heatmap': '''
              destination
       ╭───── ant
       │╭──── bat
       ││╭─── cat
       │││╭── dog
source ││││╭─ eel
   ant ·1234
   bat 3·123
   cat 23·12
   dog 123·1
   eel ----·
''',
            'utf8 False, colour None': '''
              destination
       ,----- ant
       |,---- bat
       ||,--- cat
       |||,-- dog
source ||||,- eel
   ant 01234
   bat 30123
   cat 23012
   dog 12301
   eel ----0
''',
            'utf8 False, colour ansi': '''
              destination
       ,----- ant
       |,---- bat
       ||,--- cat
       |||,-- dog
source ||||,- eel
   ant 01234
   bat 30123
   cat 23012
   dog 12301
   eel ----0
''',
            'utf8 False, colour ansi-heatmap': '''
              destination
       ,----- ant
       |,---- bat
       ||,--- cat
       |||,-- dog
source ||||,- eel
   ant 01234
   bat 30123
   cat 23012
   dog 12301
   eel ----0
''',
            'utf8 False, colour xterm-256color':
            '''              destination
       ,----- ant
       |,---- bat
       ||,--- cat
       |||,-- dog
source ||||,- eel
   ant 01234
   bat 30123
   cat 23012
   dog 12301
   eel ----0
''',
            'utf8 False, colour xterm-256color-heatmap': '''
              destination
       ,----- ant
       |,---- bat
       ||,--- cat
       |||,-- dog
source ||||,- eel
   ant 01234
   bat 30123
   cat 23012
   dog 12301
   eel ----0
'''
        }
        for utf8 in (True, False):
            for colour in self.sorted_colour_sets:
                k = 'utf8 %s, colour %s' % (utf8, colour)
                s = graph.distance_matrix(None, edges, utf8=utf8,
                                          colour=colour)
                self.assertStringsEqual(s, expected[k], strip=True,
                                        msg='Wrong output: %s\n\n%s' % (k, s))