tracker-0.16.2/tests/libtracker-data/tracker-backup-test.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 
 24 #include <glib.h>
 25 #include <gio/gio.h>
 26 
 27 #include <libtracker-common/tracker-common.h>
 28 
 29 #include <libtracker-data/tracker-data-backup.h>
 30 #include <libtracker-data/tracker-data-manager.h>
 31 #include <libtracker-data/tracker-data-query.h>
 32 #include <libtracker-data/tracker-data-update.h>
 33 #include <libtracker-data/tracker-data.h>
 34 #include <libtracker-data/tracker-sparql-query.h>
 35 
 36 static gint backup_calls = 0;
 37 static GMainLoop *loop = NULL;
 38 
 39 static void
 40 backup_finished_cb (GError *error, gpointer user_data)
 41 {
 42 	g_assert (TRUE);
 43 	backup_calls += 1;
 44 
 45 	if (loop != NULL) {
 46 		/* backup callback, quit main loop */
 47 		g_main_loop_quit (loop);
 48 	}
 49 }
 50 
 51 static gboolean
 52 check_content_in_db (gint expected_instances, gint expected_relations)
 53 {
 54 	GError *error = NULL;
 55 	const gchar  *query_instances_1 = "SELECT ?u WHERE { ?u a foo:class1. }";
 56 	const gchar  *query_relation = "SELECT ?a ?b WHERE { ?a foo:propertyX ?b }";
 57 	TrackerDBCursor *cursor;
 58 	gint n_rows;
 59 
 60 	cursor = tracker_data_query_sparql_cursor (query_instances_1, &error);
 61 	g_assert_no_error (error);
 62 	n_rows = 0;
 63 	while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 64 		n_rows++;
 65 	}
 66 	g_assert_no_error (error);
 67 	g_assert_cmpint (n_rows, ==, expected_instances);
 68 	g_object_unref (cursor);
 69 
 70 	cursor = tracker_data_query_sparql_cursor (query_relation, &error);
 71 	g_assert_no_error (error);
 72 	n_rows = 0;
 73 	while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 74 		n_rows++;
 75 	}
 76 	g_assert_no_error (error);
 77 	g_assert_cmpint (n_rows, ==, expected_relations);
 78 	g_object_unref (cursor);
 79 
 80 	return TRUE;
 81 }
 82 /*
 83  * Load ontology a few instances
 84  * Run a couple of queries to check it is ok
 85  * Back-up. 
 86  * Remove the DB.
 87  * Restore
 88  * Run again the queries
 89  */
 90 static void
 91 test_backup_and_restore_helper (gboolean journal)
 92 {
 93 	gchar  *data_prefix, *data_filename, *backup_location, *backup_filename, *db_location, *meta_db;
 94 	GError *error = NULL;
 95 	GFile  *backup_file;
 96 	gchar *test_schemas[5] = { NULL, NULL, NULL, NULL, NULL };
 97 
 98 	db_location = g_build_path (G_DIR_SEPARATOR_S, g_get_current_dir (), "tracker", NULL);
 99 	data_prefix = g_build_path (G_DIR_SEPARATOR_S, 
100 	                            TOP_SRCDIR, "tests", "libtracker-data", "backup", "backup",
101 	                            NULL);
102 
103 	/*
104 	 * This function uses $(data_prefix).ontology
105 	 */ 
106 	test_schemas[0] = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", "ontologies", "20-dc", NULL);
107 	test_schemas[1] = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", "ontologies", "31-nao", NULL);
108 	test_schemas[2] = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", "ontologies", "90-tracker", NULL);
109 	test_schemas[3] = data_prefix;
110 
111 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
112 
113 	tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
114 	                           (const gchar **) test_schemas,
115 	                           NULL, FALSE, FALSE,
116 	                           100, 100, NULL, NULL, NULL, &error);
117 
118 	g_assert_no_error (error);
119 
120 	/* load data set */
121 	data_filename = g_strconcat (data_prefix, ".data", NULL);
122 	if (g_file_test (data_filename, G_FILE_TEST_IS_REGULAR)) {
123 		tracker_turtle_reader_load (data_filename, &error);
124 		g_assert_no_error (error);
125 	} else {
126 		g_assert_not_reached ();
127 	}
128 	g_free (data_filename);
129 
130 
131 	/* Check everything is correct */
132 	check_content_in_db (3, 1);
133 
134 	backup_location = g_build_filename (db_location, "backup", NULL);
135 	g_mkdir (backup_location, 0777);
136 	backup_filename = g_build_filename (backup_location, "tracker.dump", NULL);
137 	backup_file = g_file_new_for_path (backup_filename);
138 	g_free (backup_filename);
139 	g_free (backup_location);
140 	tracker_data_backup_save (backup_file,
141 	                          backup_finished_cb,
142 	                          NULL,
143 	                          NULL);
144 
145 	/* Backup is asynchronous, wait until it is finished */
146 	loop = g_main_loop_new (NULL, FALSE);
147 	g_main_loop_run (loop);
148 	g_main_loop_unref (loop);
149 	loop = NULL;
150 
151 	tracker_data_manager_shutdown ();
152 
153 	meta_db = g_build_path (G_DIR_SEPARATOR_S, db_location, "meta.db", NULL);
154 	g_unlink (meta_db);
155 	g_free (meta_db);
156 
157 #ifndef DISABLE_JOURNAL
158 	if (!journal) {
159 		meta_db = g_build_path (G_DIR_SEPARATOR_S, db_location, "data", "tracker-store.journal", NULL);
160 		g_unlink (meta_db);
161 		g_free (meta_db);
162 
163 		meta_db = g_build_path (G_DIR_SEPARATOR_S, db_location, "data", "tracker-store.ontology.journal", NULL);
164 		g_unlink (meta_db);
165 		g_free (meta_db);
166 	}
167 #endif /* DISABLE_JOURNAL */
168 
169 	meta_db = g_build_path (G_DIR_SEPARATOR_S, db_location, "data", ".meta.isrunning", NULL);
170 	g_unlink (meta_db);
171 	g_free (meta_db);
172 
173 #ifndef DISABLE_JOURNAL
174 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
175 #endif /* DISABLE_JOURNAL */
176 
177 	tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
178 	                           (const gchar **) test_schemas,
179 	                           NULL, FALSE, FALSE,
180 	                           100, 100, NULL, NULL, NULL, &error);
181 
182 	g_assert_no_error (error);
183 
184 	check_content_in_db (0, 0);
185 
186 	tracker_data_backup_restore (backup_file, (const gchar **) test_schemas, NULL, NULL, &error);
187 	g_assert_no_error (error);
188 	check_content_in_db (3, 1);
189 
190 	g_free (test_schemas[0]);
191 	g_free (test_schemas[1]);
192 	g_free (test_schemas[2]);
193 	g_free (test_schemas[3]);
194 
195 	g_assert_cmpint (backup_calls, ==, 1);
196 
197 	tracker_data_manager_shutdown ();
198 }
199 
200 static void
201 test_backup_and_restore (void)
202 {
203 	test_backup_and_restore_helper (FALSE);
204 	backup_calls = 0;
205 }
206 
207 static void
208 test_journal_then_backup_and_restore (void)
209 {
210 	test_backup_and_restore_helper (TRUE);
211 	backup_calls = 0;
212 }
213 
214 int
215 main (int argc, char **argv)
216 {
217 	gint result;
218 	gchar *current_dir;
219 
220 	g_test_init (&argc, &argv, NULL);
221 
222 	current_dir = g_get_current_dir ();
223 
224 	g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
225 	g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
226 	g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
227 
228 	g_free (current_dir);
229 
230 	g_test_add_func ("/tracker/libtracker-data/backup/journal_then_save_and_restore",
231 	                 test_journal_then_backup_and_restore);
232 
233 	g_test_add_func ("/tracker/libtracker-data/backup/save_and_restore",
234 	                 test_backup_and_restore);
235 
236 	/* run tests */
237 	result = g_test_run ();
238 
239 	/* clean up */
240 	g_print ("Removing temporary data\n");
241 	g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
242 
243 	return result;
244 }