File: test_log_analyzer.py

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (459 lines) | stat: -rw-r--r-- 17,591 bytes parent folder | download | duplicates (2)
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
import argparse
import json
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
from io import StringIO
import os
from typing import Any, Tuple, TypedDict, Literal, Union
import unittest
from unittest.mock import Mock, patch

# Since log-analyzer has a hyphen and is missing the .py extension,
# we need to do some extra work to import the module to test
dir_path = os.path.dirname(os.path.realpath(__file__))
spec = spec_from_loader(
    "log-analyzer",
    SourceFileLoader("log-analyzer", os.path.join(dir_path, "log-analyzer")),
)
if spec is None:
    raise RuntimeError("cannot get log-analyzer spec")
if spec.loader is None:
    raise RuntimeError("cannot get log-analyzer spec loader")
log_analyzer = module_from_spec(spec)
if log_analyzer is None:
    raise RuntimeError("cannot get log-analyzer spec")
spec.loader.exec_module(log_analyzer)


class SpreadLog_TypePhase(TypedDict):
    type: Literal["phase"]
    task: str
    verb: str


class SpreadLogDetail(TypedDict):
    lines: list[str]


class SpreadLog_TypeResult(TypedDict):
    type: Literal["result"]
    result_type: str
    level: str
    stage: str
    detail: SpreadLogDetail


SpreadLog = Union[SpreadLog_TypePhase, SpreadLog_TypeResult]


def create_data(
    num_executed_no_fail: int,
    num_fail_execution: int,
    num_fail_restore: int,
    num_fail_prepare: int,
    num_not_executed: int,
) -> Tuple[set[str], list[SpreadLog]]:
    # The order will be:
    #   1. tasks that executed and didn't fail
    #   2. tasks that failed during execution
    #   3. tasks that failed during restore
    #   4. tasks that failed during prepare
    #   5. tasks that were not executed at all

    exec_param = [
        "test_" + str(i)
        for i in range(
            num_executed_no_fail
            + num_fail_execution
            + num_fail_prepare
            + num_fail_restore
            + num_not_executed
        )
    ]

    # The tasks that executed are those that didn't fail plus those that failed during execution or restore
    spread_logs: list[SpreadLog] = [
        SpreadLog_TypePhase(
            {"type": "phase", "verb": "Executing", "task": param})
        for param in exec_param[
            : num_executed_no_fail + num_fail_execution + num_fail_restore
        ]
    ]

    begin = num_executed_no_fail
    end = num_executed_no_fail + num_fail_execution
    # The tasks that failed are those that failed during execution, not during restore or prepare
    spread_logs.append(
        SpreadLog_TypeResult(
            {
                "type": "result",
                "result_type": "Failed",
                "level": "tasks",
                "stage": "",
                "detail": {
                    "lines": ["- %s\n" % param for param in exec_param[begin:end]]
                },
            }
        )
    )

    begin = num_executed_no_fail + num_fail_execution
    end = num_executed_no_fail + num_fail_execution + num_fail_restore
    # Tasks that failed during the restore phase
    spread_logs.append(
        SpreadLog_TypeResult(
            {
                "type": "result",
                "result_type": "Failed",
                "level": "task",
                "stage": "restore",
                "detail": {
                    "lines": ["- %s\n" % param for param in exec_param[begin:end]]
                },
            }
        )
    )

    begin = num_executed_no_fail + num_fail_execution + num_fail_restore
    end = (
        num_executed_no_fail + num_fail_execution + num_fail_restore + num_fail_prepare
    )
    # Tasks that failed during the prepare phase
    spread_logs.append(
        SpreadLog_TypeResult(
            {
                "type": "result",
                "result_type": "Failed",
                "level": "task",
                "stage": "prepare",
                "detail": {
                    "lines": ["- %s\n" % param for param in exec_param[begin:end]]
                },
            }
        )
    )

    return set(exec_param), spread_logs


class TestLogAnalyzer(unittest.TestCase):

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super(TestLogAnalyzer, self).__init__(*args, **kwargs)

        self.filtered_exec_param_mixed, self.spread_logs_mixed = create_data(
            num_executed_no_fail=10,
            num_fail_execution=10,
            num_fail_restore=10,
            num_fail_prepare=10,
            num_not_executed=10,
        )
        self.exec_param_mixed = ["tests/...", "other-tests/..."]

        self.filtered_exec_param_no_failed, self.spread_logs_no_failed = create_data(
            num_executed_no_fail=10,
            num_fail_execution=0,
            num_fail_restore=0,
            num_fail_prepare=0,
            num_not_executed=0,
        )
        self.exec_param_no_failed = ["tests/...", "other-tests/..."]

        self.filtered_exec_param_no_exec, self.spread_logs_no_exec = create_data(
            num_executed_no_fail=0,
            num_fail_execution=0,
            num_fail_restore=0,
            num_fail_prepare=10,
            num_not_executed=10,
        )
        self.exec_param_no_exec = ["tests/...", "other-tests/..."]

        (
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        ) = create_data(
            num_executed_no_fail=10,
            num_fail_execution=0,
            num_fail_restore=0,
            num_fail_prepare=0,
            num_not_executed=10,
        )
        self.exec_param_mix_success_abort = ["tests/...", "other-tests/..."]

    # The following test group has mixed results with task results
    # of all kinds: successful, failed in all three phases, and not run

    def test_list_executed__mixed(self) -> None:
        actual = log_analyzer.list_executed_tasks(
            self.filtered_exec_param_mixed, self.spread_logs_mixed
        )
        expected = set(["test_" + str(i) for i in range(30)])
        self.assertSetEqual(expected, actual)

    def test_list_failed__mixed(self) -> None:
        actual = log_analyzer.list_failed_tasks(
            self.filtered_exec_param_mixed, self.spread_logs_mixed
        )
        expected = set(["test_" + str(i) for i in range(10, 20)])
        self.assertSetEqual(expected, actual)

    def test_list_successful__mixed(self) -> None:
        actual = log_analyzer.list_successful_tasks(
            self.filtered_exec_param_mixed, self.spread_logs_mixed
        )
        expected = set(["test_" + str(i) for i in range(10)])
        self.assertSetEqual(expected, actual)

    def test_executed_and_failed__mixed(self) -> None:
        actual = log_analyzer.list_executed_and_failed(
            self.filtered_exec_param_mixed, self.spread_logs_mixed
        )
        expected = set(["test_" + str(i) for i in range(10, 30)])
        self.assertSetEqual(expected, actual)

    def test_aborted_tasks__mixed(self) -> None:
        actual = log_analyzer.list_aborted_tasks(
            self.filtered_exec_param_mixed, self.spread_logs_mixed
        )
        expected = set(["test_" + str(i) for i in range(30, 50)])
        self.assertSetEqual(expected, actual)

    def test_reexecute_tasks__mixed(self) -> None:
        actual = log_analyzer.list_rexecute_tasks(
            self.exec_param_mixed,
            self.filtered_exec_param_mixed,
            self.spread_logs_mixed,
        )
        expected = set(["test_" + str(i) for i in range(10, 50)])
        self.assertSetEqual(expected, actual)

    # The following test group has only tasks that were successfully run

    def test_list_executed__no_fail(self) -> None:
        actual = log_analyzer.list_executed_tasks(
            self.filtered_exec_param_no_failed, self.spread_logs_no_failed
        )
        expected = set(["test_" + str(i) for i in range(10)])
        self.assertSetEqual(expected, actual)

    def test_list_failed__no_fail(self) -> None:
        actual = log_analyzer.list_failed_tasks(
            self.filtered_exec_param_no_failed, self.spread_logs_no_failed
        )
        self.assertEqual(0, len(actual))

    def test_list_successful__no_fail(self) -> None:
        actual = log_analyzer.list_successful_tasks(
            self.filtered_exec_param_no_failed, self.spread_logs_no_failed
        )
        expected = set(["test_" + str(i) for i in range(10)])
        self.assertSetEqual(expected, actual)

    def test_executed_and_failed__no_fail(self) -> None:
        actual = log_analyzer.list_executed_and_failed(
            self.filtered_exec_param_no_failed, self.spread_logs_no_failed
        )
        self.assertEqual(0, len(actual))

    def test_aborted_tasks__no_fail(self) -> None:
        actual = log_analyzer.list_aborted_tasks(
            self.filtered_exec_param_no_failed, self.spread_logs_no_failed
        )
        self.assertEqual(0, len(actual))

    def test_reexecute_tasks__no_fail(self) -> None:
        actual = log_analyzer.list_rexecute_tasks(
            self.exec_param_no_failed,
            self.filtered_exec_param_no_failed,
            self.spread_logs_no_failed,
        )
        self.assertEqual(0, len(actual))

    # The following group only has tasks that either failed
    # during the prepare phase or were not run at all

    def test_list_executed__no_exec(self) -> None:
        actual = log_analyzer.list_executed_tasks(
            self.filtered_exec_param_no_exec, self.spread_logs_no_exec
        )
        self.assertEqual(0, len(actual))

    def test_list_failed__no_exec(self) -> None:
        actual = log_analyzer.list_failed_tasks(
            self.filtered_exec_param_no_exec, self.spread_logs_no_exec
        )
        self.assertEqual(0, len(actual))

    def test_list_successful__no_exec(self) -> None:
        actual = log_analyzer.list_successful_tasks(
            self.filtered_exec_param_no_exec, self.spread_logs_no_exec
        )
        self.assertEqual(0, len(actual))

    def test_executed_and_failed__no_exec(self) -> None:
        actual = log_analyzer.list_executed_and_failed(
            self.filtered_exec_param_no_exec, self.spread_logs_no_exec
        )
        self.assertEqual(0, len(actual))

    def test_aborted_tasks__no_exec(self) -> None:
        actual = log_analyzer.list_aborted_tasks(
            self.filtered_exec_param_no_exec, self.spread_logs_no_exec
        )
        expected = set(["test_" + str(i) for i in range(20)])
        self.assertSetEqual(expected, actual)

    def test_reexecute_tasks__no_exec(self) -> None:
        actual = log_analyzer.list_rexecute_tasks(
            self.exec_param_no_exec,
            self.filtered_exec_param_no_exec,
            self.spread_logs_no_exec,
        )
        self.assertSetEqual(set(self.exec_param_no_exec), actual)

    # The following test group has tasks that either
    # were successful or did not run at all

    def test_list_executed__mix_success_abort(self) -> None:
        actual = log_analyzer.list_executed_tasks(
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        expected = set(["test_" + str(i) for i in range(10)])
        self.assertSetEqual(expected, actual)

    def test_list_failed__mix_success_abort(self) -> None:
        actual = log_analyzer.list_failed_tasks(
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        self.assertEqual(len(actual), 0)

    def test_list_successful__mix_success_abort(self) -> None:
        actual = log_analyzer.list_successful_tasks(
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        expected = set(["test_" + str(i) for i in range(10)])
        self.assertSetEqual(expected, actual)

    def test_executed_and_failed__mix_success_abort(self) -> None:
        actual = log_analyzer.list_executed_and_failed(
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        self.assertEqual(len(actual), 0)

    def test_aborted_tasks__mix_success_abort(self) -> None:
        actual = log_analyzer.list_aborted_tasks(
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        expected = set(["test_" + str(i) for i in range(10, 20)])
        self.assertSetEqual(expected, actual)

    def test_reexecute_tasks__mix_success_abort(self) -> None:
        actual = log_analyzer.list_rexecute_tasks(
            self.exec_param_mix_success_abort,
            self.filtered_exec_param_mix_success_abort,
            self.spread_logs_mix_success_abort,
        )
        expected = set(["test_" + str(i) for i in range(10, 20)])
        self.assertSetEqual(expected, actual)

    # The following test group checks the main function with
    # mixed results (some failures, some aborts, some successes)

    @patch('argparse.ArgumentParser.parse_args')
    def test_list_executed__main(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-executed-tasks',
            exec_params=' '.join(self.exec_param_mixed),
            parsed_log=StringIO(json.dumps(self.spread_logs_mixed))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(["test_" + str(i) for i in range(30)])
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))

    @patch('argparse.ArgumentParser.parse_args')
    def test_list_failed__main(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-failed-tasks',
            exec_params=' '.join(self.exec_param_mixed),
            parsed_log=StringIO(json.dumps(self.spread_logs_mixed))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(["test_" + str(i) for i in range(10, 20)])
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))

    @patch('argparse.ArgumentParser.parse_args')
    def test_list_successful__main(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-successful-tasks',
            exec_params=' '.join(self.exec_param_mixed),
            parsed_log=StringIO(json.dumps(self.spread_logs_mixed))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(["test_" + str(i) for i in range(10)])
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))

    @patch('argparse.ArgumentParser.parse_args')
    def test_aborted_tasks__main(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-aborted-tasks',
            exec_params=' '.join(self.exec_param_mixed),
            parsed_log=StringIO(json.dumps(self.spread_logs_mixed))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(["test_" + str(i) for i in range(30, 50)])
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))

    @patch('argparse.ArgumentParser.parse_args')
    def test_reexecute_tasks__main(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-reexecute-tasks',
            exec_params=' '.join(self.exec_param_mixed),
            parsed_log=StringIO(json.dumps(self.spread_logs_mixed))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(["test_" + str(i) for i in range(10, 50)])
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))

    @patch('argparse.ArgumentParser.parse_args')
    def test_reexecute_tasks__main_no_exec(self, parse_args_mock: Mock) -> None:
        log_analyzer.filter_with_spread = Mock()
        log_analyzer.filter_with_spread.return_value = [
            "test_" + str(i) for i in range(50)]
        parse_args_mock.return_value = argparse.Namespace(
            command='list-reexecute-tasks',
            exec_params=' '.join(self.exec_param_no_exec),
            parsed_log=StringIO(json.dumps(self.spread_logs_no_exec))
        )
        with patch('sys.stdout', new=StringIO()) as stdout_patch:
            log_analyzer.main()
            expected = set(self.exec_param_no_exec)
            self.assertSetEqual(expected, set(stdout_patch.getvalue().split()))


if __name__ == "__main__":
    unittest.main()