summaryrefslogtreecommitdiff
path: root/test/suite/test_readonly02.py
blob: 0df5465642d6f62b44babab8eb72719e11b32445 (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
#!/usr/bin/env python
#
# Public Domain 2016-2016 MongoDB, Inc.
# Public Domain 2008-2016 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.
#
# test_readonly02.py
#   Readonly: Test readonly mode with illegal config combinations
#   and error checking during updates.
#

from helper import copy_wiredtiger_home
from suite_subprocess import suite_subprocess
import os, wiredtiger, wttest

class test_readonly02(wttest.WiredTigerTestCase, suite_subprocess):
    tablename = 'table:test_readonly02'
    create = True
    create_params = 'key_format=i,value_format=i'
    entries = 10

    conn_params = \
        'create,statistics=(fast),log=(enabled,file_max=100K,zero_fill=true),'
    conn_params_rd = \
        'create,readonly=true,statistics=(fast),log=(enabled,zero_fill=false),'
    conn_params_rdcfg = \
        'create,readonly=true,statistics=(fast),log=(enabled),'

    #
    # Run to make sure incompatible configuration options return an error.
    # The situations that cause failures (instead of silent overrides) are:
    #   1. setting readonly on a new database directory
    #   2. an unclean shutdown and reopening readonly
    #   3. logging with zero-fill enabled and readonly
    #
    badcfg1 = 'log=(enabled,zero_fill=true)'

    def setUpConnectionOpen(self, dir):
        self.home = dir
        rdonlydir = dir + '.rdonly'
        #
        # First time through check readonly on a non-existent database.
        #
        if self.create:
            #   1. setting readonly on a new database directory
            # Setting readonly prevents creation so we should see an
            # error because the lock file does not exist.
            msg = '/No such file/'
            if os.name != 'posix':
                msg = '/cannot find the file/'
            os.mkdir(rdonlydir)
            self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
                lambda: self.wiredtiger_open(
                rdonlydir, self.conn_params_rd), msg)

        self.create = False
        conn = self.wiredtiger_open(dir, self.conn_params)
        return conn

    def check_unclean(self):
        backup = "WT_COPYDIR"
        copy_wiredtiger_home(self.home, backup, True)
        msg = '/needs recovery/'
        #   2. an unclean shutdown and reopening readonly
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
            lambda: self.wiredtiger_open(backup, self.conn_params_rd), msg)

    def close_checkerror(self, cfg):
        ''' Close the connection and reopen readonly'''
        #
        # Close the original connection.  Reopen readonly and also with
        # the given configuration string.
        #
        self.close_conn()
        conn_params = self.conn_params_rd + cfg
        msg = '/Invalid argument/'
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
            lambda: self.wiredtiger_open(self.home, conn_params), msg)

    def test_readonly(self):
        tablearg = self.tablename
        self.session.create(tablearg, self.create_params)
        c = self.session.open_cursor(tablearg, None, None)
        for i in range(self.entries):
            c[i+1] = i % 255
        # Check for an error on an unclean recovery/restart.
        self.check_unclean()

        # Close the connection.  Reopen readonly with other bad settings.
        #   3. logging with zero-fill enabled and readonly
        self.close_checkerror(self.badcfg1)

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