summaryrefslogtreecommitdiff
path: root/tests/functional-tests/store/200-backup-restore.py
blob: 6d58f5ba196ed17eef6914c0254a1596b497bc67 (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
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/python
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import os
import dbus  # For the exception handling

from common.utils.system import TrackerSystemAbstraction
from common.utils.helpers import StoreHelper
from common.utils import configuration as cfg
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
from common.utils.expectedFailure import expectedFailureBug, expectedFailureJournal
import unittest as ut


"""
Call backup, restore, force the journal replay and check the data is correct afterwards
"""


class BackupRestoreTest (CommonTrackerStoreTest):

    """
    Backup and restore to/from valid/invalid files
    """

    def setUp(self):
        self.TEST_INSTANCE = "test://backup-restore/1"
        self.BACKUP_FILE = "file://" + \
            os.path.join(cfg.TEST_TMP_DIR, "tracker-backup-test-1")

        if (os.path.exists(self.BACKUP_FILE)):
            os.unlink(self.BACKUP_FILE)

    def __insert_test_instance(self):
        self.tracker.update("INSERT { <%s> a nco:Contact; nco:fullname 'test-backup' } "
                            % (self.TEST_INSTANCE))

    def __delete_test_instance(self):
        self.tracker.update(
            "DELETE { <%s> a rdfs:Resource } " % (self.TEST_INSTANCE))

    def __is_test_instance_there(self):
        result = self.tracker.query(
            "SELECT ?u WHERE { ?u a nco:Contact; nco:fullname 'test-backup'}")
        if (len(result) == 1 and len(result[0]) == 1 and result[0][0] == self.TEST_INSTANCE):
            return True
        return False

    def test_backup_01(self):
        """
        Inserted data is restored after backup

        1.Insert contact
        2.Take backup.
        3.Delete contact. (check it is not there)
        4.Restore the file.
        5.Check the contact is back there
        """

        self.__insert_test_instance()
        instances_before = self.tracker.count_instances("nco:Contact")

        self.tracker.backup(self.BACKUP_FILE)

        self.__delete_test_instance()
        instances_now = self.tracker.count_instances("nco:Contact")

        self.assertEquals(instances_before - 1, instances_now)

        self.tracker.restore(self.BACKUP_FILE)

        instances_after = self.tracker.count_instances("nco:Contact")

        self.assertEquals(instances_before, instances_after)
        self.assertTrue(self.__is_test_instance_there())

        # Clean the DB for the next test
        self.__delete_test_instance()

    def test_backup_02(self):
        """
        Data inserted after backup is lost in restore

        1.Take backup of db.
        2.Insert a contact.
        3.Restore the db.
        4.Search for the contact inserted.
        """

        # Precondition: test backup contact shouldn't be there
        self.assertFalse(self.__is_test_instance_there())

        self.tracker.backup(self.BACKUP_FILE)

        self.__insert_test_instance()
        self.assertTrue(self.__is_test_instance_there())

        self.tracker.restore(self.BACKUP_FILE)

        self.assertFalse(self.__is_test_instance_there())

    def test_backup_03(self):
        """
        Restore from a random text file
        """
        TEST_FILE = os.path.join(cfg.TEST_TMP_DIR, "trash_file")
        trashfile = open(TEST_FILE, "w")
        trashfile.write(
            "Here some useless text that obviously is NOT a backup")
        trashfile.close()

        self.assertRaises(dbus.DBusException,
                          self.tracker.restore,
                          "file://" + TEST_FILE)
        os.unlink(TEST_FILE)

    def test_backup_04(self):
        """
        Restore from a random binary file
        """
        TEST_FILE = os.path.join(cfg.TEST_TMP_DIR, "trash_file.dat")

        import struct
        trashfile = open(TEST_FILE, "wb")
        for n in range(0, 50):
            data = struct.pack('i', n)
            trashfile.write(data)
        trashfile.close()

        instances_before = self.tracker.count_instances(
            "nie:InformationElement")
        self.assertRaises(dbus.DBusException,
                          self.tracker.restore,
                          "file://" + TEST_FILE)

        os.unlink(TEST_FILE)

    def test_backup_05(self):
        """
        Take backup of db to a invalid path.
        Expected: Backup should not be taken and tracker should behave normally.
        """
        self.assertRaises(dbus.DBusException,
                          self.tracker.backup,
                          "file://%s/this/is/a/non-existant/folder/backup" % (cfg.TEST_TMP_DIR))

    def test_backup_06(self):
        """
        Try to restore an invalid path
        """
        self.assertRaises(dbus.DBusException,
                          self.tracker.restore,
                          "file://%s/this/is/a/non-existant/folder/backup" % (cfg.TEST_TMP_DIR))

    def test_backup_07(self):
        """
        Restore after removing the DBs and journal

        1.Insert a contact.
        2.Take backup of db.
        4.Delete the database
        5.Restore the db.
        6.Search for the contact inserted.
        """
        self.__insert_test_instance()
        instances_before = self.tracker.count_instances("nco:Contact")
        self.tracker.backup(self.BACKUP_FILE)

        self.system.tracker_store_stop_nicely()
        self.system.tracker_store_remove_dbs()
        self.system.tracker_store_remove_journal()
        self.system.tracker_store_start()

        instances_before_restore = self.tracker.count_instances("nco:Contact")
        self.assertNotEqual(instances_before_restore, instances_before)

        self.tracker.restore(self.BACKUP_FILE)
        self.assertTrue(self.__is_test_instance_there())

        self.__delete_test_instance()

    def test_backup_08(self):
        """
        Restore after corrupting DB

        1.Insert a contact.
        2.Take backup of db.
        5.Restore the db.
        6.Search for the contact inserted.
        """
        self.__insert_test_instance()
        instances_before = self.tracker.count_instances("nco:Contact")
        self.tracker.backup(self.BACKUP_FILE)

        self.system.tracker_store_stop_brutally()
        self.system.tracker_store_corrupt_dbs()
        self.system.tracker_store_remove_journal()
        self.system.tracker_store_start()

        instances_before_restore = self.tracker.count_instances("nco:Contact")
        self.assertNotEqual(instances_before_restore, instances_before)

        self.tracker.restore(self.BACKUP_FILE)
        self.assertTrue(self.__is_test_instance_there())

        # DB to the original state
        self.__delete_test_instance()

    def test_backup_11(self):
        """
        Backup ignores the file extension

        1.Insert a contact.
        2.Take backup of db in .jpg format.
        3.Restore the db.
        4.Search for the contact inserted.
        """
        BACKUP_JPG_EXT = "file://%s/tracker-test-backup.jpg" % (
            cfg.TEST_TMP_DIR)

        self.__insert_test_instance()

        instances_before = self.tracker.count_instances("nco:Contact")

        self.tracker.backup(BACKUP_JPG_EXT)

        self.__delete_test_instance()
        instances_now = self.tracker.count_instances("nco:Contact")
        self.assertEquals(instances_before, instances_now + 1)

        self.tracker.restore(BACKUP_JPG_EXT)
        instances_after = self.tracker.count_instances("nco:Contact")
        self.assertEquals(instances_before, instances_after)

        # Restore the DB to the original state
        self.__delete_test_instance()


class JournalReplayTest (CommonTrackerStoreTest):

    """
    Force journal replaying and check that the DB is correct aftewards
    """
    @expectedFailureJournal()
    def test_journal_01(self):
        """
        Journal replaying when the DB is corrupted

        Insert few data (to have more than the pre-defined instances)
        Check instances of different classes
        Kill the store (SIGKILL)
        Replace the DB with a random file
        Start the store
        Check instances of different classes
        """
        self.tracker.update(
            "INSERT { <test://journal-replay/01> a nco:Contact. }")

        emails = self.tracker.count_instances("nmo:Email")
        ie = self.tracker.count_instances("nie:InformationElement")
        contacts = self.tracker.count_instances("nco:Contact")

        self.system.tracker_store_stop_brutally()
        self.system.tracker_store_corrupt_dbs()
        self.system.tracker_store_start()

        emails_now = self.tracker.count_instances("nmo:Email")
        ie_now = self.tracker.count_instances("nie:InformationElement")
        contacts_now = self.tracker.count_instances("nco:Contact")

        self.assertEquals(emails, emails_now)
        self.assertEquals(ie, ie_now)
        self.assertEquals(contacts, contacts_now)

        self.tracker.update(
            "DELETE { <test://journal-replay/01> a rdfs:Resource. }")

    @expectedFailureJournal()
    def test_journal_02(self):
        """
        Journal replaying when the DB disappears

        Insert few data (to have more than the pre-defined instances)
        Check instances of different classes
        KILL the store
        Force a journal replay (DB missing, .meta.isrunning exists, journal OK)
        Start the store
        Check instances of different classes
        """
        self.tracker.update(
            "INSERT { <test://journal-replay/02> a nco:Contact. }")

        emails = self.tracker.count_instances("nmo:Email")
        ie = self.tracker.count_instances("nie:InformationElement")
        contacts = self.tracker.count_instances("nco:Contact")

        self.system.tracker_store_stop_brutally()
        self.system.tracker_store_prepare_journal_replay()
        self.system.tracker_store_start()

        emails_now = self.tracker.count_instances("nmo:Email")
        ie_now = self.tracker.count_instances("nie:InformationElement")
        contacts_now = self.tracker.count_instances("nco:Contact")

        self.assertEquals(emails, emails_now)
        self.assertEquals(ie, ie_now)
        self.assertEquals(contacts, contacts_now)

        self.tracker.update(
            "DELETE { <test://journal-replay/02> a rdfs:Resource. }")

if __name__ == "__main__":
    ut.main()