summaryrefslogtreecommitdiff
path: root/uitest/mass-testing/run.py
blob: d1bec88b208c529c5afb9bb055f66d758bf4f287 (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
#!/usr/bin/env python3
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#

import os
import argparse
import glob
import shutil
from subprocess import Popen, PIPE, TimeoutExpired
import sys
import signal
import logging
from shutil import copyfile
import time
import fcntl
import tempfile
import magic
import multiprocessing
from multiprocessing_logging import install_mp_handler

extensions = {
    'writer' : [ "odt", "doc", "docx", "rtf" ],
    'calc' : [ "ods", "xls", "xlsx" ],
    'impress' : [ "odp", "ppt", "pptx" ]
    }

def signal_handler(sig, frame):
        print('Ctrl+C pressed! Killing it!')
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

class DefaultHelpParser(argparse.ArgumentParser):
    def error(self, message):
        sys.stderr.write('error: %s\n' % message)
        self.print_help()
        sys.exit(2)

def kill_soffice():
    p = Popen(['ps', '-A'], stdout=PIPE)
    out, err = p.communicate()
    for line in out.splitlines():
        if b'soffice' in line:
            pid = int(line.split(None, 1)[0])
            print("Killing process: " + str(pid))
            try:
                os.kill(pid, signal.SIGKILL)
            except ProcessLookupError:
                pass

    # Also clean leftovers in /tmp
    for filename in glob.glob("/tmp/OSL_PIPE_*"):
        os.remove(filename)

    for filename in glob.glob("/tmp/lu*.tmp"):
        if os.path.isfile(filename):
            os.remove(filename)
        else:
            shutil.rmtree(filename)

def start_logger(name):
    rootLogger = logging.getLogger()
    rootLogger.setLevel(os.environ.get("LOGLEVEL", "INFO"))

    logFormatter = logging.Formatter("%(asctime)s %(message)s")
    fileHandler = logging.FileHandler(name)
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)

    streamHandler = logging.StreamHandler(sys.stdout)
    rootLogger.addHandler(streamHandler)

    return rootLogger

def get_file_names(filesPath):
    auxNames = []
    for fileName in os.listdir(filesPath):
        for key, val in extensions.items():
            extension = os.path.splitext(fileName)[1][1:]
            if extension in val:
                fullName = filesPath + fileName
                mimetype = magic.from_file(fullName, mime=True)

                # Ignore CSV files since they prompt the CSV import dialog
                if mimetype == "application/csv":
                    continue
                auxNames.append("file:///" + fullName)

                #Remove previous lock files
                lockFilePath = filesPath + '.~lock.' + fileName + '#'
                if os.path.isfile(lockFilePath):
                    os.remove(lockFilePath)
                break

    return auxNames

def launchLibreOffice(logger, fileName, sofficePath, component, countInfo, isDebug):
    #Create temp directory for the user profile
    with tempfile.TemporaryDirectory() as tmpdirname:
        profilePath = os.path.join(tmpdirname, 'libreoffice/4')
        userPath = os.path.join(profilePath, 'user')
        os.makedirs(userPath)

        # Replace the profile file with
        # 1. DisableMacrosExecution = True
        # 2. IgnoreProtectedArea = True
        # 3. AutoPilot = False
        copyfile(os.getcwd() + '/registrymodifications.xcu', userPath + '/registrymodifications.xcu')

        #TODO: Find a better way to pass fileName parameter
        os.environ["TESTFILENAME"] = fileName

        process = Popen(["python3",
                    './uitest/test_main.py',
                    "--soffice=path:" + sofficePath,
                    "--userdir=file://" + profilePath,
                    "--file=" + component + ".py"], stdin=PIPE, stdout=PIPE, stderr=PIPE,
                    preexec_fn=os.setsid)

        # Do not block on process.stdout
        fcntl.fcntl(process.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

        # Kill the process if:
        # 1. The file can't be loaded in 'fileInterval' seconds
        # 2. The test can't be executed in 'testInterval' seconds
        fileInterval = 30
        testInterval = 20
        timeout = time.time() + fileInterval
        notLoaded = True
        while True:
            time.sleep(0.1)

            if time.time() > timeout:
                if notLoaded:
                    logger.info(countInfo + " - SKIP: " + fileName)
                else:
                    logger.info(countInfo + " - TIMEOUT: " + fileName)

                # kill popen process
                os.killpg(process.pid, signal.SIGKILL)
                break

            try:
                outputLines = process.stdout.readlines()
            except IOError:
                pass

            importantInfo = ''
            isFailure = False
            for line in outputLines:
                line = line.decode("utf-8").strip()

                if not line:
                    continue

                if isDebug:
                    print(line)

                if line.startswith("mass-uitesting:"):
                    message = line.split(":")[1]
                    if message == 'skipped':
                        logger.info(countInfo + " - SKIP: " + fileName + " : " + importantInfo)

                        # kill popen process
                        os.killpg(process.pid, signal.SIGKILL)

                        break
                    elif message == 'loaded':
                        notLoaded = False

                        #Extend timeout
                        timeout += testInterval

                elif 'Execution time' in line:
                    importantInfo = line.split('for ')[1]

                elif importantInfo and 'error' == line.lower() or 'fail' == line.lower():
                    isFailure = True

            if importantInfo:
                if isFailure:
                    logger.info(countInfo + " - FAIL: " + fileName + " : " + importantInfo)
                else:
                    # No error found between the Execution time line and the end of stdout
                    logger.info(countInfo + " - PASS: " + fileName + " : " + str(importantInfo))

            if process.poll() is not None:
                break

def run_tests_and_get_results(sofficePath, listFiles, isDebug):

    process = Popen([sofficePath, "--version"], stdout=PIPE, stderr=PIPE)
    stdout = process.communicate()[0].decode("utf-8")
    sourceHash = stdout.split(" ")[2].strip()

    if not os.path.exists('./logs'):
        os.makedirs('./logs')

    logName = './logs/' + sourceHash + ".log"
    logger = start_logger(logName)

    previousLog = ""
    if os.path.exists(logName):
        with open(logName, 'r') as file:
            previousLog = file.read()

    kill_soffice()
    cpuCount = int(multiprocessing.cpu_count() / 2) #Use half of the CPUs
    chunkSplit = cpuCount * 16
    chunks = [listFiles[x:x+chunkSplit] for x in range(0, len(listFiles), chunkSplit)]
    totalCount = len(listFiles)

    count = 0
    for chunk in chunks:
        install_mp_handler()
        pool = multiprocessing.Pool(cpuCount)
        for fileName in chunk:
            count += 1
            countInfo = str(count) + '/' + str(totalCount)

            if fileName in previousLog:
                print(countInfo + " - SKIP: " + fileName)
                continue

            extension = os.path.splitext(fileName)[1][1:]

            for key, val in extensions.items():
                if extension in val:
                    component = key
                    break

            pool.apply_async(launchLibreOffice,
                    args=(logger, fileName, sofficePath, component, countInfo, isDebug))

        pool.close()
        pool.join()

        kill_soffice()


if __name__ == '__main__':
    currentPath = os.path.dirname(os.path.realpath(__file__))
    uitestPath = os.path.join(currentPath, 'uitest/test_main.py')
    if not os.path.exists(uitestPath):
        print("ERROR: " + uitestPath + " doesn't exists. " + \
                "Copy uitest folder from LibreOffice codebase and paste it here")
        sys.exit(1)

    pythonPath = os.path.join(currentPath, 'python/')
    if not os.path.exists(pythonPath):
        print("ERROR: " + pythonPath + " doesn't exists. " + \
                "Copy unotest/source/python/ folder from LibreOffice codebase and paste it here")
        sys.exit(1)

    if sys.version_info >= (3, 11):
        print("This script doesn't work with Python 3.11 or newer.\n"
            "See https://lists.freedesktop.org/archives/libreoffice/2023-December/091319.html")
        sys.exit(1)

    parser = DefaultHelpParser()

    parser.add_argument(
            '--dir', required=True, help="Path to the files directory")
    parser.add_argument(
            '--soffice', required=True, help="Path to the LibreOffice directory")
    parser.add_argument(
            '--debug', action='store_true', help="Flag to print output")

    argument = parser.parse_args()

    filesPath = os.path.join(argument.dir, '')
    if not os.path.exists(filesPath):
        parser.error(filesPath + " is an invalid directory path")

    sofficePath = argument.soffice
    if not os.path.exists(sofficePath) or not sofficePath.endswith('/soffice'):
        parser.error(sofficePath + " is an invalid LibreOffice path")

    os.environ["PYTHONPATH"] = sofficePath.split('/soffice')[0] + os.pathsep + pythonPath
    os.environ["URE_BOOTSTRAP"] = "file://" + sofficePath.split('/soffice')[0] + '/fundamentalrc'
    os.environ["SAL_USE_VCLPLUGIN"] = "gen"

    listFiles = get_file_names(filesPath)
    listFiles.sort()

    run_tests_and_get_results(sofficePath, listFiles, argument.debug)

# vim: set shiftwidth=4 softtabstop=4 expandtab: