summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_util18.py
blob: c66bfca13b00733f0efa60fef40c8cb9ad9f3135 (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
#!/usr/bin/env python
#
# Public Domain 2014-2021 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.

import codecs, filecmp
from suite_subprocess import suite_subprocess
import wiredtiger, wttest
from wtscenario import make_scenarios

# test_util18.py
#   Utilities: wt printlog
class test_util18(wttest.WiredTigerTestCase, suite_subprocess):
    tablename = 'test_util18.a'
    uri = 'table:' + tablename
    logmax = 100
    nentries = 5
    create_params = 'key_format=S,value_format=S'
    key_prefix = 'KEY'
    val_prefix = 'VAL'

    # Whether user data is redacted or printed.
    print_user_data = [
        ('show_user_data', dict(print_user_data=True)),
        ('no_user_data', dict(print_user_data=False)),
    ]

    scenarios = make_scenarios(print_user_data)

    def conn_config(self):
        return 'log=(archive=false,enabled,file_max=%dK)' % self.logmax

    # Populate our test table with data we can check against in the printlog output.
    def populate(self):
        cursor = self.session.open_cursor(self.uri, None)
        for i in range(0, self.nentries):
            key = self.key_prefix + str(i)
            val = self.val_prefix + str(i)
            cursor[key] = val
        cursor.close()

    # Check the given printlog file reflects the data written by 'populate'.
    def check_populated_printlog(self, log_file, expect_keyval, expect_keyval_hex):
        for i in range(0, self.nentries):
            key = self.key_prefix + str(i)
            val = self.val_prefix + str(i)
            # Check if the KEY/VAL commits exist in the log file.
            if expect_keyval:
                self.check_file_contains(log_file, '"key": "%s\\u0000"' % key)
                self.check_file_contains(log_file, '"value": "%s\\u0000"' % val)
            else:
                self.check_file_not_contains(log_file, '"key": "%s\\u0000"' % key)
                self.check_file_not_contains(log_file, '"value": "%s\\u0000"' % val)

            # Convert our KEY/VAL strings to their expected hex value.
            hex_key = codecs.encode(key.encode(), 'hex')
            val_key = codecs.encode(val.encode(), 'hex')
            # Check if the KEY/VAL commits exist in the log file (in hex form).
            if expect_keyval_hex:
                self.check_file_contains(log_file, '"key-hex": "%s00"' % str(hex_key, 'ascii'))
                self.check_file_contains(log_file, '"value-hex": "%s00"' % str(val_key, 'ascii'))
            else:
                self.check_file_not_contains(log_file, '"key-hex": "%s00"' % str(hex_key, 'ascii'))
                self.check_file_not_contains(log_file, '"value-hex": "%s00"' % str(val_key, 'ascii'))

    def test_printlog_file(self):
        """
        Run printlog on a populated table.
        """
        self.session.create('table:' + self.tablename, self.create_params)
        self.populate()
        wt_args = ["printlog"]
        # Append "-u" if we expect printlog to print user data.
        if self.print_user_data:
            wt_args.append("-u")
        self.runWt(wt_args, outfilename='printlog.out')
        self.check_non_empty_file('printlog.out')
        self.check_populated_printlog('printlog.out', self.print_user_data, False)

    def test_printlog_hex_file(self):
        """
        Run printlog with hexadecimal formatting on a populated table.
        """
        self.session.create('table:' + self.tablename, self.create_params)
        self.populate()
        wt_args = ["printlog", "-x"]
        # Append "-u" if we expect printlog to print user data.
        if self.print_user_data:
            wt_args.append("-u")
        self.runWt(wt_args, outfilename='printlog-hex.out')
        self.check_non_empty_file('printlog-hex.out')
        self.check_populated_printlog('printlog-hex.out', self.print_user_data, self.print_user_data)

    def test_printlog_message(self):
        """
        Run printlog with messages-only formatting on a populated table.
        """
        self.session.create('table:' + self.tablename, self.create_params)
        self.populate()
        # Write a log message that we can specifically test the presence of.
        log_message = "Test Message: %s" % self.tablename
        self.session.log_printf(log_message)
        wt_args = ["printlog", "-m"]
        # Append "-u" if we expect printlog to print user data.
        if self.print_user_data:
            wt_args.append("-u")
        self.runWt(wt_args, outfilename='printlog-message.out')
        self.check_non_empty_file('printlog-message.out')
        self.check_file_contains('printlog-message.out', log_message)
        self.check_populated_printlog('printlog-message.out', False, False)

    def test_printlog_lsn_offset(self):
        """
        Run printlog with an LSN offset provided.
        """
        self.session.create('table:' + self.tablename, self.create_params)
        self.populate()

        # Open a log cursor to accurately extract the first, second and last LSN from our
        # log.
        c = self.session.open_cursor("log:", None, None)
        # Moving the cursor to the beginning of the file, extract our first LSN.
        c.next()
        first_lsn_keys = c.get_key()
        # Moving the cursor, extract our second LSN.
        c.next()
        second_lsn_keys = c.get_key()
        last_lsn_keys = []
        # Moving the cursor to the last available key, extract the last LSN value.
        while c.next() == 0:
            last_lsn_keys = c.get_key()
            c.next()
        c.close()

        # Construct the first, second and last LSN values, assuming the
        # key elements follow the following sequence: [lsn.file, lsn.offset, opcount].
        first_lsn = '%s,%s' % (first_lsn_keys[0], first_lsn_keys[1])
        second_lsn = '%s,%s' % (second_lsn_keys[0], second_lsn_keys[1])
        last_lsn = '%s,%s' % (last_lsn_keys[0], last_lsn_keys[1])

        # Test printlog on a bounded range that starts and ends on our first LSN record. In doing so we want
        # to assert that other log records won't be printed e.g. the second LSN record.
        wt_args = ["printlog", '-l %s,%s' % (first_lsn, first_lsn)]
        self.runWt(wt_args, outfilename='printlog-lsn-offset.out')
        self.check_file_contains('printlog-lsn-offset.out', '"lsn" : [%s]' % first_lsn)
        self.check_file_not_contains('printlog-lsn-offset.out', '"lsn" : [%s]' % second_lsn)
        self.check_populated_printlog('printlog-lsn-offset.out', False, False)

        # Test printlog from the starting LSN value to the end of the log. We expect to find the logs relating
        # to the population of our table.
        wt_args = ["printlog", '-l %s' % first_lsn]
        # Append "-u" if we expect printlog to print user data.
        if self.print_user_data:
            wt_args.append("-u")
        self.runWt(wt_args, outfilename='printlog-lsn-offset.out')
        self.check_populated_printlog('printlog-lsn-offset.out', self.print_user_data, False)

        # Test that using LSN '1,0' and our first LSN value produce the same output when passed to printlog.
        # We expect printing from LSN '1,0' (which should denote to the beginning of the first log file)
        # is equivalent to printing from our first extracted LSN value to the last LSN value.
        wt_args_beginning = ["printlog", '-l 1,0,%s' % last_lsn]
        wt_args_first = ["printlog", '-l %s,%s' % (first_lsn, last_lsn)]
        if self.print_user_data:
            wt_args_beginning.append("-u")
            wt_args_first.append("-u")
        self.runWt(wt_args_beginning, outfilename='printlog-lsn-offset-beginning.out')
        self.runWt(wt_args_first, outfilename='printlog-lsn-offset-first.out')
        self.assertTrue(filecmp.cmp('printlog-lsn-offset-beginning.out', 'printlog-lsn-offset-first.out'))

if __name__ == '__main__':
    wttest.run()