summaryrefslogtreecommitdiff
path: root/src/ToolBox/SOS/tests/test_libsosplugin.py
blob: e5a590626470be5faea681144ddae6e8cce808d2 (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
from __future__ import print_function
import unittest
import argparse
import re
import tempfile
import subprocess
import threading
import os
import sys
import inspect

lldb = ''
clrdir = ''
workdir = ''
corerun = ''
sosplugin = ''
assembly = ''
fail_flag = ''
fail_flag_lldb = ''
summary_file = ''
timeout = 0
regex = ''
repeat = 0


def runWithTimeout(cmd):
    p = None

    def run():
        global p
        p = subprocess.Popen(cmd, shell=True)
        p.communicate()

    thread = threading.Thread(target=run)
    thread.start()

    thread.join(timeout)
    if thread.is_alive():
        with open(summary_file, 'a+') as summary:
            print('Timeout!', file=summary)
        p.kill()
        thread.join()


class TestSosCommands(unittest.TestCase):

    def do_test(self, command):
        open(fail_flag, 'a').close()
        try:
            os.unlink(fail_flag_lldb)
        except:
            pass

        cmd = (('%s -b ' % lldb) +
               ("-k \"script open('%s', 'a').close()\" " % fail_flag_lldb) +
               ("-k 'quit' ") +
               ("--no-lldbinit ") +
               ("-O \"plugin load %s \" " % sosplugin) +
               ("-o \"script import testutils as test\" ") +
               ("-o \"script test.fail_flag = '%s'\" " % fail_flag) +
               ("-o \"script test.summary_file = '%s'\" " % summary_file) +
               ("-o \"script test.run('%s', '%s')\" " % (assembly, command)) +
               ("-o \"quit\" ") +
               (" -- %s %s > %s.log 2> %s.log.2" % (corerun, assembly,
                                                    command, command)))

        runWithTimeout(cmd)
        self.assertFalse(os.path.isfile(fail_flag))
        self.assertFalse(os.path.isfile(fail_flag_lldb))

        try:
            os.unlink(fail_flag)
        except:
            pass
        try:
            os.unlink(fail_flag_lldb)
        except:
            pass

    def t_cmd_bpmd_nofuturemodule_module_function(self):
        self.do_test('t_cmd_bpmd_nofuturemodule_module_function')

    def t_cmd_bpmd_module_function(self):
        self.do_test('t_cmd_bpmd_module_function')

    def t_cmd_bpmd_module_function_iloffset(self):
        self.do_test('t_cmd_bpmd_module_function_iloffset')

    def t_cmd_bpmd_methoddesc(self):
        self.do_test('t_cmd_bpmd_methoddesc')

    def t_cmd_bpmd_clearall(self):
        self.do_test('t_cmd_bpmd_clearall')

    def t_cmd_clrstack(self):
        self.do_test('t_cmd_clrstack')

    def t_cmd_clrthreads(self):
        self.do_test('t_cmd_clrthreads')

    def t_cmd_clru(self):
        self.do_test('t_cmd_clru')

    def t_cmd_dumpclass(self):
        self.do_test('t_cmd_dumpclass')

    def t_cmd_dumpheap(self):
        self.do_test('t_cmd_dumpheap')

    def t_cmd_dumpil(self):
        self.do_test('t_cmd_dumpil')

    def t_cmd_dumplog(self):
        self.do_test('t_cmd_dumplog')

    def t_cmd_dumpmd(self):
        self.do_test('t_cmd_dumpmd')

    def t_cmd_dumpmodule(self):
        self.do_test('t_cmd_dumpmodule')

    def t_cmd_dumpmt(self):
        self.do_test('t_cmd_dumpmt')

    def t_cmd_dumpobj(self):
        self.do_test('t_cmd_dumpobj')

    def t_cmd_dumpstack(self):
        self.do_test('t_cmd_dumpstack')

    def t_cmd_dso(self):
        self.do_test('t_cmd_dso')

    def t_cmd_eeheap(self):
        self.do_test('t_cmd_eeheap')

    def t_cmd_eestack(self):
        self.do_test('t_cmd_eestack')

    def t_cmd_gcroot(self):
        self.do_test('t_cmd_gcroot')

    def t_cmd_ip2md(self):
        self.do_test('t_cmd_ip2md')

    def t_cmd_name2ee(self):
        self.do_test('t_cmd_name2ee')

    def t_cmd_pe(self):
        self.do_test('t_cmd_pe')

    def t_cmd_histclear(self):
        self.do_test('t_cmd_histclear')

    def t_cmd_histinit(self):
        self.do_test('t_cmd_histinit')

    def t_cmd_histobj(self):
        self.do_test('t_cmd_histobj')

    def t_cmd_histobjfind(self):
        self.do_test('t_cmd_histobjfind')

    def t_cmd_histroot(self):
        self.do_test('t_cmd_histroot')

    def t_cmd_sos(self):
        self.do_test('t_cmd_sos')

    def t_cmd_soshelp(self):
        self.do_test('t_cmd_soshelp')


def generate_report():
    report = [{'name': 'TOTAL', True: 0, False: 0, 'completed': True}]
    fail_messages = []

    if not os.path.isfile(summary_file):
        print('No summary file to process!')
        return

    with open(summary_file, 'r') as summary:
        for line in summary:
            if line.startswith('new_suite: '):
                report.append({'name': line.split()[-1], True: 0, False: 0,
                               'completed': False, 'timeout': False})
            elif line.startswith('True'):
                report[-1][True] += 1
            elif line.startswith('False'):
                report[-1][False] += 1
            elif line.startswith('Completed!'):
                report[-1]['completed'] = True
            elif line.startswith('Timeout!'):
                report[-1]['timeout'] = True
            elif line.startswith('!!! '):
                fail_messages.append(line.rstrip('\n'))

    for suite in report[1:]:
        report[0][True] += suite[True]
        report[0][False] += suite[False]
        report[0]['completed'] &= suite['completed']

    for line in fail_messages:
        print(line)

    print()
    print('=' * 79)
    print('{:72} {:6}'.format('Test suite', 'Result'))
    print('-' * 79)
    for suite in report[1:]:
        if suite['timeout']:
            result = 'Timeout'
        elif suite[False]:
            result = 'Fail'
        elif not suite['completed']:
            result = 'Crash'
        elif suite[True]:
            result = 'Success'
        else:
            result = 'Please, report'
        print('{:68} {:>10}'.format(suite['name'], result))
    print('=' * 79)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--lldb', default='lldb')
    parser.add_argument('--clrdir', default='.')
    parser.add_argument('--workdir', default='.')
    parser.add_argument('--assembly', default='Test.exe')
    parser.add_argument('--timeout', default=90)
    parser.add_argument('--regex', default='t_cmd_')
    parser.add_argument('--repeat', default=1)
    parser.add_argument('unittest_args', nargs='*')

    args = parser.parse_args()

    lldb = args.lldb
    clrdir = args.clrdir
    workdir = args.workdir
    assembly = args.assembly
    timeout = int(args.timeout)
    regex = args.regex
    repeat = int(args.repeat)
    print("lldb: %s" % lldb)
    print("clrdir: %s" % clrdir)
    print("workdir: %s" % workdir)
    print("assembly: %s" % assembly)
    print("timeout: %i" % timeout)
    print("regex: %s" % regex)
    print("repeat: %i" % repeat)

    corerun = os.path.join(clrdir, 'corerun')
    sosplugin = os.path.join(clrdir, 'libsosplugin.so')
    if os.name != 'posix':
        print('Not implemented: corerun.exe, sosplugin.dll?')
        exit(1)

    print("corerun: %s" % corerun)
    print("sosplugin: %s" % sosplugin)

    fail_flag = os.path.join(workdir, 'fail_flag')
    fail_flag_lldb = os.path.join(workdir, 'fail_flag.lldb')

    print("fail_flag: %s" % fail_flag)
    print("fail_flag_lldb: %s" % fail_flag_lldb)

    summary_file = os.path.join(workdir, 'summary')
    print("summary_file: %s" % summary_file)

    try:
        os.unlink(summary_file)
    except:
        pass

    sys.argv[1:] = args.unittest_args
    suite = unittest.TestSuite()
    all_tests = inspect.getmembers(TestSosCommands, predicate=inspect.ismethod)
    for (test_name, test_func) in all_tests:
        if re.match(regex, test_name):
            suite.addTest(TestSosCommands(test_name))
    unittest.TextTestRunner(verbosity=1).run(suite)

    generate_report()