summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/suite_subprocess.py
blob: 507c647757f6be66bbf6ce02fa695339478795f4 (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
#!/usr/bin/env python
#
# Public Domain 2014-present MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# 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 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.

from __future__ import print_function
import os, re, subprocess, sys
from run import wt_builddir
from wttest import WiredTigerTestCase
import wttest

# suite_subprocess.py
#    Run a subprocess within the test suite
# Used as a 'mixin' class along with a WiredTigerTestCase class
class suite_subprocess:
    subproc = None

    def has_error_in_file(self, filename):
        """
        Return whether the file contains 'ERROR'.
        WT utilities issue a 'WT_ERROR' output string upon error.
        """
        with open(filename, 'r') as f:
            for line in f:
                if 'ERROR' in line:
                    return True
        return False

    def check_no_error_in_file(self, filename, match='ERROR'):
        """
        Raise an error and show output context if the file contains 'ERROR'.
        WT utilities issue a 'WT_ERROR' output string upon error.
        """
        lines = []
        hasError = False
        hasPrevious = False  # do we need to prefix an ellipsis?
        hasNext = False  # do we need to suffix an ellipsis?
        with open(filename, 'r') as f:
            for line in f:
                lines.append(line)
                hasError = hasError or match in line
                if hasError:
                    if len(lines) > 10:
                        hasNext = True
                        break
                else:
                    if len(lines) > 5:
                        lines.pop(0)
                        hasPrevious = True
        if hasError:
            print('**************** ' + match + ' in output file: ' + filename + ' ****************')
            if hasPrevious:
                print('...')
            for line in lines:
                print(line, end=' ')
            if hasNext:
                print('...')
            print('********************************')
            self.fail('ERROR found in output file: ' + filename)

    # If the string is of the form '/.../', then return just the embedded
    # pattern, otherwise, return None
    def convert_to_pattern(self, s):
        if len(s) >= 2 and s[0] == '/' and s[-1] == '/':
            return s[1:-1]
        else:
            return None

    def check_file_content(self, filename, expect):
        with open(filename, 'r') as f:
            got = f.read(len(expect) + 100)
            self.assertEqual(got, expect, filename + ': does not contain expected:\n\'' + expect + '\', but contains:\n\'' + got + '\'.')

    # Check contents of the file against a provided checklist. Expected is used as a bool to either
    # ensure checklist is included or ensure the checklist is not included in the file.
    def check_file_contains_one_of(self, filename, checklist, expected):
        """
        Check that the file contains the expected string in the first 100K bytes
        """
        maxbytes = 1024*100
        with open(filename, 'r') as f:
            got = f.read(maxbytes)
            found = False
            for expect in checklist:
                pat = self.convert_to_pattern(expect)
                if pat == None:
                    if expect in got:
                        found = True
                        if expected:
                            break
                        else:
                            self.fail("Did not expect: " + got)
                else:
                    if re.search(pat, got):
                        found = True
                        if expected:
                            break
                        else:
                            self.fail("Did not expect: " + got)
            if not found and expected:
                if len(checklist) == 1:
                    expect = '\'' + checklist[0] + '\''
                else:
                    expect = str(checklist)
                gotstr = '\'' + \
                    (got if len(got) < 1000 else (got[0:1000] + '...')) + '\''
                if len(got) >= maxbytes:
                    self.fail(filename + ': does not contain expected ' + expect + ', or output is too large, got ' + gotstr)
                else:
                    self.fail(filename + ': does not contain expected ' + expect + ', got ' + gotstr)

    def check_file_contains(self, filename, content):
        self.check_file_contains_one_of(filename, [content], True)

    def check_file_not_contains(self, filename, content):
        self.check_file_contains_one_of(filename, [content], False)

    def check_empty_file(self, filename):
        """
        Raise an error if the file is not empty
        """
        filesize = os.path.getsize(filename)
        if filesize > 0:
            with open(filename, 'r') as f:
                contents = f.read(1000)
                print('ERROR: ' + filename + ' expected to be empty, but contains:\n')
                print(contents + '...\n')
        self.assertEqual(filesize, 0, filename + ': expected to be empty')

    def check_non_empty_file(self, filename):
        """
        Raise an error if the file is empty
        """
        filesize = os.path.getsize(filename)
        if filesize == 0:
            print('ERROR: ' + filename + ' should not be empty (this command expected error output)')
        self.assertNotEqual(filesize, 0, filename + ': expected to not be empty')

    def verbose_env(self, envvar):
        return envvar + '=' + str(os.environ.get(envvar)) + '\n'

    def show_outputs(self, procargs, message, filenames):
        out = message + ': ' + \
              str(procargs) + '\n' + \
              self.verbose_env('PATH') + \
              self.verbose_env('LD_LIBRARY_PATH') + \
              self.verbose_env('DYLD_LIBRARY_PATH') + \
              self.verbose_env('PYTHONPATH') + \
              'output files follow:'
        WiredTigerTestCase.prout(out)
        for filename in filenames:
            maxbytes = 1024*100
            with open(filename, 'r') as f:
                contents = f.read(maxbytes)
                if len(contents) > 0:
                    if len(contents) >= maxbytes:
                        contents += '...\n'
                    sepline = '*' * 50 + '\n'
                    out = sepline + filename + '\n' + sepline + contents
                    WiredTigerTestCase.prout(out)

    # Run a method as a subprocess using the run.py machinery.
    # Return the process exit status and the WiredTiger home
    # directory used by the subprocess.
    def run_subprocess_function(self, directory, funcname):
        testparts = funcname.split('.')
        if len(testparts) != 3:
            raise ValueError('bad function name "' + funcname +
                '", should be three part dotted name')
        topdir = os.path.dirname(self.buildDirectory())
        runscript = os.path.join(topdir, 'test', 'suite', 'run.py')
        procargs = [ sys.executable, runscript, '-p', '--dir', directory,
            funcname]

        # scenario_number is only set if we are running in a scenario
        try:
            scennum = self.scenario_number
            procargs.append('-s')
            procargs.append(str(scennum))
        except:
            scennum = 0

        returncode = -1
        os.makedirs(directory)

        # We cannot put the output/error files in the subdirectory, as
        # that will be cleared by the run.py script.
        with open("subprocess.err", "w") as wterr:
            with open("subprocess.out", "w") as wtout:
                returncode = subprocess.call(
                    procargs, stdout=wtout, stderr=wterr)
                if returncode != 0:
                    # This is not necessarily an error, the primary reason to
                    # run in a subprocess is that it may crash.
                    self.show_outputs(procargs,
                        "Warning: run_subprocess_function " + funcname + \
                        " returned error code " + str(returncode),
                        [ "subprocess.out", "subprocess.err" ])

        new_home_dir = os.path.join(directory,
            testparts[1] + '.' + str(scennum))
        return [ returncode, new_home_dir ]

    # Run the wt utility.

    # FIXME-WT-9808:
    # The tiered hook silently interjects tiered configuration and extensions,
    # these are not yet dealt with when running the external 'wt' process.
    @wttest.skip_for_hook("tiered", "runWt cannot add needed extensions")
    def runWt(self, args, infilename=None,
        outfilename=None, errfilename=None, closeconn=True,
        reopensession=True, failure=False):

        # FIXME-WT-9809:
        if 'timestamp' in self.hook_names and args[0] == 'load':
            self.skipTest("the load utility cannot be run when timestamps are already set")

        # Close the connection to guarantee everything is flushed, and that
        # we can open it from another process.
        if closeconn:
            self.close_conn()

        wtoutname = outfilename or "wt.out"
        wterrname = errfilename or "wt.err"
        with open(wterrname, "w") as wterr:
            with open(wtoutname, "w") as wtout:
                # Prefer running the actual 'wt' binary rather than the
                # 'wt' script created by libtool. On OS/X with System Integrity
                # Protection enabled, running a shell script strips
                # environment variables needed to run 'wt'. There are
                # also test environments that work better with the binary.
                libs_wt = os.path.join(wt_builddir, ".libs", "wt")
                if os.path.isfile(libs_wt):
                    wtexe = libs_wt
                else:
                    wtexe = os.path.join(wt_builddir, "wt")
                procargs = [ wtexe ]
                if self._gdbSubprocess:
                    procargs = [ "gdb", "--args" ] + procargs
                elif self._lldbSubprocess:
                    procargs = [ "lldb", "--" ] + procargs
                procargs.extend(args)
                if self._gdbSubprocess:
                    infilepart = ""
                    if infilename != None:
                        infilepart = "<" + infilename + " "
                    print(str(procargs))
                    print("*********************************************")
                    print("**** Run 'wt' via: run " + \
                        " ".join(procargs[3:]) + infilepart + \
                        ">" + wtoutname + " 2>" + wterrname)
                    print("*********************************************")
                    returncode = subprocess.call(procargs)
                elif self._lldbSubprocess:
                    infilepart = ""
                    if infilename != None:
                        infilepart = "<" + infilename + " "
                    print(str(procargs))
                    print("*********************************************")
                    print("**** Run 'wt' via: run " + \
                        " ".join(procargs[3:]) + infilepart + \
                        ">" + wtoutname + " 2>" + wterrname)
                    print("*********************************************")
                    returncode = subprocess.call(procargs)
                elif infilename:
                    with open(infilename, "r") as wtin:
                        returncode = subprocess.call(
                            procargs, stdin=wtin, stdout=wtout, stderr=wterr)
                else:
                    returncode = subprocess.call(
                        procargs, stdout=wtout, stderr=wterr)
        if failure:
            if returncode == 0:
                self.show_outputs(procargs,
                    "ERROR: wt command expected failure, got success",
                    [wtoutname, wterrname])
            self.assertNotEqual(returncode, 0,
                'expected failure: "' + \
                str(procargs) + '": exited ' + str(returncode))
        else:
            if returncode != 0:
                self.show_outputs(procargs,
                    "ERROR: wt command expected success, got failure",
                    [wtoutname, wterrname])
            self.assertEqual(returncode, 0,
                'expected success: "' + \
                str(procargs) + '": exited ' + str(returncode))
        if errfilename == None:
            self.check_empty_file(wterrname)
        if outfilename == None:
            self.check_empty_file(wtoutname)

        # Reestablish the connection if needed
        if reopensession and closeconn:
            self.open_conn()