1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28
29import wiredtiger, wttest
30from wtscenario import make_scenarios
31
32# test_metadata_cursor02.py
33#    Metadata cursor operations with invalid metadata
34#
35# Test metadata cursor semantics when the underlying metadata is invalid.
36# This can happen after a crash, or if part of a table is dropped separate
37# from dropping the whole table.
38class test_metadata_cursor02(wttest.WiredTigerTestCase):
39    """
40    Test metadata cursor operations with invalid metadata
41    """
42    table_name1 = 'table:t1'
43    table_name2 = 'table:t2'
44    table_name3 = 'table:t3'
45    tables = [table_name1, table_name2, table_name3]
46
47    scenarios = make_scenarios([
48        ('plain', {'metauri' : 'metadata:'}),
49        ('create', {'metauri' : 'metadata:create'}),
50    ], [
51        ('drop_colgroup', {'drop' : 'colgroup'}),
52        ('drop_file', {'drop' : 'file'}),
53    ])
54
55    # Create tables
56    def create_tables(self):
57        # Reopen to make sure we can drop anything left over from the last run
58        self.reopen_conn()
59        for name in self.tables:
60            self.session.drop(name, 'force=true')
61            self.session.create(name, 'key_format=S,value_format=S')
62
63    # Forward iteration.
64    def test_missing(self):
65        for name in self.tables:
66            self.create_tables()
67
68            # Invalidate the table by dropping part of it
69            if self.drop == 'colgroup':
70                self.session.drop('colgroup:' + name[-2:])
71            else:
72                self.session.drop('file:' + name[-2:] + '.wt')
73
74            cursor = self.session.open_cursor(self.metauri)
75            is_create_cursor = self.metauri.endswith('create')
76            count = 0
77            for k, v in cursor:
78                self.pr('Found metadata entry: ' + k)
79                if k.startswith('table:'):
80                    count += 1
81            cursor.close()
82
83            if is_create_cursor:
84                self.captureerr.checkAdditionalPattern(self, 'metadata information.*not found')
85
86            # Should include the metadata and the two valid tables
87            self.assertEqual(count, self.metauri.endswith('create') and 2 or 3)
88
89if __name__ == '__main__':
90    wttest.run()
91