2 Test suite for storage. Uses unittest module.
4 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro.
5 2011, Mariana Marasoiu, mariana.marasoiu@gmail.com
20 def create_non_numeric_subfolders(path):
21 os.mkdir(os.path.join(path, "ana"))
22 os.mkdir(os.path.join(path, "are"))
23 os.mkdir(os.path.join(path, "mere"))
24 os.mkdir(os.path.join(path, "si"))
25 os.mkdir(os.path.join(path, "pere"))
27 def create_numeric_subfolders_sequential(path):
28 os.mkdir(os.path.join(path, "1"))
29 os.mkdir(os.path.join(path, "2"))
30 os.mkdir(os.path.join(path, "3"))
31 os.mkdir(os.path.join(path, "4"))
32 os.mkdir(os.path.join(path, "5"))
34 def create_numeric_subfolders_non_sequential(path):
35 os.mkdir(os.path.join(path, "32"))
36 os.mkdir(os.path.join(path, "5"))
37 os.mkdir(os.path.join(path, "423"))
38 os.mkdir(os.path.join(path, "1401"))
39 os.mkdir(os.path.join(path, "92"))
41 def create_numeric_files(path):
42 f = open(os.path.join(path, "33"), 'w')
44 f = open(os.path.join(path, "6"), 'w')
46 f = open(os.path.join(path, "424"), 'w')
48 f = open(os.path.join(path, "1402"), 'w')
50 f = open(os.path.join(path, "93"), 'w')
53 class StorageTest(unittest.TestCase):
55 """Test suite for stand alone functions in storage.py."""
57 # Class specific variables. Initialized in setUp, used throughout tests.
58 path = "/tmp/ttfa-test"
65 """Remove base folder."""
66 shutil.rmtree(self.path)
68 def test_find_last_numeric_subfolder_no_subfolders(self):
69 """Test return of None when no subfolders are present."""
70 id = storage.find_last_numeric_subfolder(self.path)
71 self.assertEqual(id, None)
73 def test_find_last_numeric_subfolder_no_numeric_subfolders(self):
74 """Test return of None when no numeric subfolders are present."""
75 create_non_numeric_subfolders(self.path)
77 id = storage.find_last_numeric_subfolder(self.path)
78 self.assertEqual(id, None)
80 def test_find_last_numeric_subfolder_only_numeric_subfolders_sequential(
83 Test return of correct id when sequential numeric named
84 subfolders only are present (1, 2, 3, ...).
86 create_numeric_subfolders_sequential(self.path)
88 id = storage.find_last_numeric_subfolder(self.path)
89 self.assertEqual(id, 5)
91 def test_find_last_numeric_subfolder_only_numeric_subfolders_nonsequential(
94 Test return of correct id when nonsequential numeric named
95 subfolders only are present (32, 5, 423, ...).
97 create_numeric_subfolders_non_sequential(self.path)
99 id = storage.find_last_numeric_subfolder(self.path)
100 self.assertEqual(id, 1401)
102 def test_find_last_numeric_subfolder_mixed_subfolders_sequential(self):
104 Test return of None when mixed named subfolders (numeric and
105 others) are present. Numeric folders are sequential.
107 create_non_numeric_subfolders(self.path)
108 create_numeric_subfolders_sequential(self.path)
110 id = storage.find_last_numeric_subfolder(self.path)
111 self.assertEqual(id, 5)
113 def test_find_last_numeric_subfolder_mixed_subfolders_nonsequential(self):
115 Test return of None when mixed named subfolders (numeric and
116 others) are present. Numeric folders are non-sequential.
118 create_non_numeric_subfolders(self.path)
119 create_numeric_subfolders_non_sequential(self.path)
121 id = storage.find_last_numeric_subfolder(self.path)
122 self.assertEqual(id, 1401)
124 def test_find_last_numeric_subfolder_mixed_subfolders_files(self):
126 Test return of None when mixed named subfolders (numeric and
127 others) together with files are present. Numeric folders are
130 create_non_numeric_subfolders(self.path)
131 create_numeric_subfolders_non_sequential(self.path)
132 create_numeric_files(self.path)
134 id = storage.find_last_numeric_subfolder(self.path)
135 self.assertEqual(id, 1401)
138 class TreeTextFileAccessTest(unittest.TestCase):
140 """Test suite for TreeTextFileAccess class in storage.py."""
142 # Class specific variables. Initialized in setUp, used throughout tests.
143 path = "/tmp/ttfa-test"
146 """Create folder. Initialize object. """
150 """Remove base folder."""
151 shutil.rmtree(self.path)
153 def test_add_swarm_folder_and_file_are_created(self):
154 # Create instance of class and add swarm.
155 a = storage.TreeTextFileAccess(self.path)
156 s = storage.Swarm(torrent_filename="fedora.torrent",
160 expected_swarm_subfolder = 1
161 expected_swarm_path = os.path.join(self.path,
162 str(expected_swarm_subfolder))
163 expected_swarm_config = os.path.join(expected_swarm_path, "swarm.conf")
165 self.assertEqual(os.path.isdir(expected_swarm_path), True)
166 self.assertEqual(os.path.isfile(expected_swarm_config), True)
168 def test_add_client_session_folder_and_file_are_created(self):
169 a = storage.TreeTextFileAccess(self.path)
172 s = storage.Swarm(torrent_filename="fedora.torrent",
176 # Add client session.
177 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
179 system_os_version="2.6.26",
180 system_ram=2048, system_cpu=3000,
181 public_ip="141.85.224.201",
182 public_port="50500", ds_limit=300,
184 a.add_client_session(cs)
186 expected_swarm_subfolder = 1
187 expected_session_subfolder = 1
188 expected_swarm_path = os.path.join(self.path,
189 str(expected_swarm_subfolder))
190 expected_session_path = os.path.join(expected_swarm_path,
191 str(expected_session_subfolder))
192 expected_session_config = os.path.join(expected_session_path,
193 "client_session.conf")
195 self.assertEqual(os.path.isdir(expected_session_path), True)
196 self.assertEqual(os.path.isfile(expected_session_config), True)
198 def test_add_peer_status_message_file_is_created(self):
199 a = storage.TreeTextFileAccess(self.path)
202 s = storage.Swarm(torrent_filename="fedora.torrent",
206 # Add client session.
207 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
209 system_os_version="2.6.26",
210 system_ram=2048, system_cpu=3000,
211 public_ip="141.85.224.201",
212 public_port="50500", ds_limit=300,
214 a.add_client_session(cs)
216 # Add peer status message.
217 msg = storage.PeerStatusMessage(swarm_id=1, client_session_id=1,
218 peer_ip="141.85.224.202",
219 peer_port="12345", download_speed=13,
221 a.add_peer_status_message(msg)
223 expected_swarm_subfolder = 1
224 expected_session_subfolder = 1
225 expected_swarm_path = os.path.join(self.path,
226 str(expected_swarm_subfolder))
227 expected_session_path = os.path.join(expected_swarm_path,
228 str(expected_session_subfolder))
229 expected_message_file = os.path.join(expected_session_path,
232 self.assertEqual(os.path.isfile(expected_message_file), True)
234 def test_add_peer_status_message_number_of_lines(self):
235 self.assertEqual(True, True)
237 def test_add_status_message_file_is_created(self):
238 a = storage.TreeTextFileAccess(self.path)
241 s = storage.Swarm(torrent_filename="fedora.torrent",
245 # Add client session.
246 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
248 system_os_version="2.6.26",
249 system_ram=2048, system_cpu=3000,
250 public_ip="141.85.224.201",
251 public_port="50500", ds_limit=300,
253 a.add_client_session(cs)
255 # Add status message.
256 msg = storage.StatusMessage(swarm_id=1, client_session_id=1,
257 num_peers=10, num_dht_peers=3,
258 download_speed=102, upload_speed=99,
259 download_size=10213, upload_size=3301)
260 a.add_status_message(msg)
262 expected_swarm_subfolder = 1
263 expected_session_subfolder = 1
264 expected_swarm_path = os.path.join(self.path,
265 str(expected_swarm_subfolder))
266 expected_session_path = os.path.join(expected_swarm_path,
267 str(expected_session_subfolder))
268 expected_message_file = os.path.join(expected_session_path,
271 self.assertEqual(os.path.isfile(expected_message_file), True)
273 def test_add_status_message_number_of_lines(self):
274 self.assertEqual(True, True)
276 def test_add_verbose_message_file_is_created(self):
277 a = storage.TreeTextFileAccess(self.path)
280 s = storage.Swarm(torrent_filename="fedora.torrent",
284 # Add client session.
285 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
287 system_os_version="2.6.26",
288 system_ram=2048, system_cpu=3000,
289 public_ip="141.85.224.201",
290 public_port="50500", ds_limit=300,
292 a.add_client_session(cs)
294 # Add verbose message.
295 msg = storage.VerboseMessage(swarm_id=1, client_session_id=1,
296 transfer_direction="send",
297 peer_ip="141.85.224.202",
298 peer_port="12345", message_type="CHOKE")
299 a.add_verbose_message(msg)
301 expected_swarm_subfolder = 1
302 expected_session_subfolder = 1
303 expected_swarm_path = os.path.join(self.path,
304 str(expected_swarm_subfolder))
305 expected_session_path = os.path.join(expected_swarm_path,
306 str(expected_session_subfolder))
307 expected_message_file = os.path.join(expected_session_path,
310 self.assertEqual(os.path.isfile(expected_message_file), True)
312 def test_add_verbose_message_number_of_lines(self):
313 self.assertEqual(True, True)
316 class SQLiteDatabaseAccessTest(unittest.TestCase):
318 """Test suite for SQLiteDatabaseAccess class in storage.py."""
320 # Class specific variables. Initialized in setUp, used throughout tests.
322 sql_create_script = "p2p-log-sqlite.sql"
323 sql_init_script = "p2p-init-test.sql"
324 expected_swarm_count = 0
325 expected_session_count = 0
326 expected_statmsg_count = 0
327 expected_pstatmsg_count = 0
328 expected_verbmsg_count = 0
330 def get_swarm_count(self):
331 """Retrieve number of entries in `swarms` table."""
332 conn = sqlite3.connect(self.database)
333 cursor = conn.cursor()
335 cursor.execute('SELECT COUNT(id) FROM swarms')
336 count = cursor.fetchone()[0]
343 def get_session_count(self):
344 """Retrieve number of entries in `client_sessions` table."""
345 conn = sqlite3.connect(self.database)
346 cursor = conn.cursor()
348 cursor.execute('SELECT COUNT(id) FROM client_sessions')
349 count = cursor.fetchone()[0]
356 def get_statmsg_count(self):
357 """Retrieve number of entries in `status_messages` table."""
358 conn = sqlite3.connect(self.database)
359 cursor = conn.cursor()
361 cursor.execute('SELECT COUNT(id) FROM status_messages')
362 count = cursor.fetchone()[0]
369 def get_pstatmsg_count(self):
370 """Retrieve number of entries in `peer_status_messages` table."""
371 conn = sqlite3.connect(self.database)
372 cursor = conn.cursor()
374 cursor.execute('SELECT COUNT(id) FROM peer_status_messages')
375 count = cursor.fetchone()[0]
382 def get_verbmsg_count(self):
383 """Retrieve number of entries in `verbose_messages` table."""
384 conn = sqlite3.connect(self.database)
385 cursor = conn.cursor()
387 cursor.execute('SELECT COUNT(id) FROM verbose_messages')
388 count = cursor.fetchone()[0]
396 """Create database file and instantiate objects."""
397 # Create database file. Create tables and indices.
398 # TODO: Check exceptions.
399 sql_create_script_path = os.path.join(os.path.dirname(__file__),
400 self.sql_create_script)
401 f = open(sql_create_script_path, 'r')
402 p = subprocess.Popen(["sqlite3", self.database], stdin=f)
403 ret = os.waitpid(p.pid, 0)[1]
407 # TODO: Check exceptions.
408 sql_init_script_path = os.path.join(os.path.dirname(__file__),
409 self.sql_init_script)
410 f = open(sql_init_script_path, 'r')
411 p = subprocess.Popen(["sqlite3", self.database], stdin=f)
412 ret = os.waitpid(p.pid, 0)[1]
415 # Initialize to number of table entries inserted in the init script.
416 self.expected_swarm_count = 2
417 self.expected_session_count = 2
418 self.expected_statmsg_count = 2
419 self.expected_pstatmsg_count = 2
420 self.expected_verbmsg_count = 2
423 """Close connection and remove database file."""
424 os.remove(self.database)
426 def test_add_swarm_new_entry_in_table(self):
428 s = storage.Swarm(torrent_filename="fedora.torrent",
430 a = storage.SQLiteDatabaseAccess(self.database)
434 self.expected_swarm_count = self.expected_swarm_count + 1
436 # Select number of swarms.
437 swarm_count = self.get_swarm_count()
439 self.assertEqual(swarm_count, self.expected_swarm_count)
441 def test_add_client_session_new_entry_in_table(self):
442 # Add new client session.
443 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
445 system_os_version="2.6.26",
446 system_ram=2048, system_cpu=3000,
447 public_ip="141.85.224.201",
448 public_port="50500", ds_limit=300,
450 a = storage.SQLiteDatabaseAccess(self.database)
452 a.add_client_session(cs)
454 self.expected_session_count = self.expected_session_count + 1
456 # Select number of swarms.
457 session_count = self.get_session_count()
459 self.assertEqual(session_count, self.expected_session_count)
461 def test_add_status_message_new_entry_in_table(self):
462 # Add new status message.
463 ts = datetime.datetime.strptime("2010-09-12 08:43:15",
465 msg = storage.StatusMessage(client_session_id=1, timestamp=ts,
466 num_peers=10, num_dht_peers=3,
467 download_speed=102, upload_speed=99,
468 download_size=10213, upload_size=3301)
469 a = storage.SQLiteDatabaseAccess(self.database)
471 a.add_status_message(msg)
473 self.expected_statmsg_count = self.expected_statmsg_count + 1
475 # Select number of status messages.
476 statmsg_count = self.get_statmsg_count()
478 self.assertEqual(statmsg_count, self.expected_statmsg_count)
480 def test_add_peer_status_message_new_entry_in_table(self):
481 # Add new peer status message.
482 ts = datetime.datetime.strptime("2010-09-12 13:43:25",
484 msg = storage.PeerStatusMessage(client_session_id=1, timestamp=ts,
485 peer_ip="141.85.224.202",
486 peer_port="12345", download_speed=13,
488 a = storage.SQLiteDatabaseAccess(self.database)
490 a.add_peer_status_message(msg)
492 self.expected_pstatmsg_count = self.expected_pstatmsg_count + 1
494 # Select number of peer status messages.
495 pstatmsg_count = self.get_pstatmsg_count()
497 self.assertEqual(pstatmsg_count, self.expected_pstatmsg_count)
499 def test_add_verbose_message_new_entry_in_database(self):
500 # Add new verbose message.
501 ts = datetime.datetime.strptime("2010-09-12 13:43:24",
503 msg = storage.VerboseMessage(client_session_id=1, timestamp=ts,
504 transfer_direction="send",
505 peer_ip="141.85.224.202",
507 message_type="CHOKE")
508 a = storage.SQLiteDatabaseAccess(self.database)
510 a.add_verbose_message(msg)
512 self.expected_verbmsg_count = self.expected_verbmsg_count + 1
514 # Select number of verbose messages.
515 verbmsg_count = self.get_verbmsg_count()
517 self.assertEqual(verbmsg_count, self.expected_verbmsg_count)
520 class MySQLDatabaseAccessTest(unittest.TestCase):
522 """Test suite for MySQLDatabaseAccess class in storage.py."""
524 # Class specific variables. Initialized in setUp, used throughout tests.
525 database = "p2p_test"
527 password = "p2p4th3m45535"
530 sql_create_script = "p2p-log-mysql.sql"
531 sql_init_script = "p2p-init-test-mysql.sql"
532 expected_swarm_count = 0
533 expected_session_count = 0
534 expected_statmsg_count = 0
535 expected_pstatmsg_count = 0
536 expected_verbmsg_count = 0
538 def get_swarm_count(self):
539 """Retrieve number of entries in `swarms` table."""
540 self.cursor.execute('SELECT COUNT(id) FROM swarms')
541 count = self.cursor.fetchone()[0]
545 def get_session_count(self):
546 """Retrieve number of entries in `client_sessions` table."""
547 self.cursor.execute('SELECT COUNT(id) FROM client_sessions')
548 count = self.cursor.fetchone()[0]
552 def get_statmsg_count(self):
553 """Retrieve number of entries in `status_messages` table."""
554 self.cursor.execute('SELECT COUNT(id) FROM status_messages')
555 count = self.cursor.fetchone()[0]
559 def get_pstatmsg_count(self):
560 """Retrieve number of entries in `peer_status_messages` table."""
561 self.cursor.execute('SELECT COUNT(id) FROM peer_status_messages')
562 count = self.cursor.fetchone()[0]
566 def get_verbmsg_count(self):
567 """Retrieve number of entries in `verbose_messages` table."""
568 self.cursor.execute('SELECT COUNT(id) FROM verbose_messages')
569 count = self.cursor.fetchone()[0]
574 """Create database and instantiate objects."""
575 # Create database. Create tables and indices.
576 # TODO: Check exceptions.
578 sql_create_script_path = os.path.join(os.path.dirname(__file__),
579 self.sql_create_script)
581 self.conn = MySQLdb.Connection(user=self.user, passwd=self.password)
582 self.cursor = self.conn.cursor()
584 self.cursor.execute("CREATE DATABASE %s" %self.database)
585 self.cursor.execute("USE %s" %self.database)
587 f = open(sql_create_script_path, 'r')
588 p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user,
589 "--password=%s" %self.password], stdin=f)
590 ret = os.waitpid(p.pid, 0)[1]
594 # TODO: Check exceptions.
595 sql_init_script_path = os.path.join(os.path.dirname(__file__),
596 self.sql_init_script)
598 f = open(sql_init_script_path, 'r')
599 p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user,
600 "--password=%s" %self.password], stdin=f)
601 ret = os.waitpid(p.pid, 0)[1]
604 # Initialize to number of table entries inserted in the init script.
605 self.expected_swarm_count = 2
606 self.expected_session_count = 2
607 self.expected_statmsg_count = 2
608 self.expected_pstatmsg_count = 2
609 self.expected_verbmsg_count = 2
612 """ Delete database and close connection. """
613 self.cursor.execute("DROP DATABASE %s" %self.database)
617 def test_add_swarm_new_entry_in_table(self):
619 s = storage.Swarm(torrent_filename="fedora.torrent",
621 a = storage.MySQLDatabaseAccess(self.database)
625 self.expected_swarm_count = self.expected_swarm_count + 1
627 # Select number of swarms.
628 swarm_count = self.get_swarm_count()
630 self.assertEqual(swarm_count, self.expected_swarm_count)
632 def test_add_client_session_new_entry_in_table(self):
633 # Add new client session.
634 cs = storage.ClientSession(swarm_id=1, btclient="Tribler",
636 system_os_version="2.6.26",
637 system_ram=2048, system_cpu=3000,
638 public_ip="141.85.224.201",
639 public_port="50500", ds_limit=300,
641 a = storage.MySQLDatabaseAccess(self.database)
643 a.add_client_session(cs)
645 self.expected_session_count = self.expected_session_count + 1
647 # Select number of swarms.
648 session_count = self.get_session_count()
650 self.assertEqual(session_count, self.expected_session_count)
652 def test_add_status_message_new_entry_in_table(self):
653 # Add new status message.
654 ts = datetime.datetime.strptime("2010-09-12 08:43:15",
656 msg = storage.StatusMessage(client_session_id=1, timestamp=ts,
657 num_peers=10, num_dht_peers=3,
658 download_speed=102, upload_speed=99,
659 download_size=10213, upload_size=3301)
660 a = storage.MySQLDatabaseAccess(self.database)
662 a.add_status_message(msg)
664 self.expected_statmsg_count = self.expected_statmsg_count + 1
666 # Select number of status messages.
667 statmsg_count = self.get_statmsg_count()
669 self.assertEqual(statmsg_count, self.expected_statmsg_count)
671 def test_add_peer_status_message_new_entry_in_table(self):
672 # Add new peer status message.
673 ts = datetime.datetime.strptime("2010-09-12 13:43:25",
675 msg = storage.PeerStatusMessage(client_session_id=1, timestamp=ts,
676 peer_ip="141.85.224.202",
677 peer_port="12345", download_speed=13,
679 a = storage.MySQLDatabaseAccess(self.database)
681 a.add_peer_status_message(msg)
683 self.expected_pstatmsg_count = self.expected_pstatmsg_count + 1
685 # Select number of peer status messages.
686 pstatmsg_count = self.get_pstatmsg_count()
688 self.assertEqual(pstatmsg_count, self.expected_pstatmsg_count)
690 def test_add_verbose_message_new_entry_in_database(self):
691 # Add new verbose message.
692 ts = datetime.datetime.strptime("2010-09-12 13:43:24",
694 msg = storage.VerboseMessage(client_session_id=1, timestamp=ts,
695 transfer_direction="send",
696 peer_ip="141.85.224.202",
698 message_type="CHOKE")
699 a = storage.MySQLDatabaseAccess(self.database)
701 a.add_verbose_message(msg)
703 self.expected_verbmsg_count = self.expected_verbmsg_count + 1
705 # Select number of verbose messages.
706 verbmsg_count = self.get_verbmsg_count()
708 self.assertEqual(verbmsg_count, self.expected_verbmsg_count)
711 if __name__ == "__main__":