2 Test suite for config. Uses unittest module.
4 2011, Mariana Marasoiu, mariana.marasoiu@gmail.com
16 class SwarmDescriptionTest(unittest.TestCase):
18 Test suite for SwarmDescription class in config.py.
21 # Class specific variables. Initialized in setUp, used throughout tests.
22 config_file = "config.sample.ini"
27 self.test_data = {'swarm': {'torrent_filename': 'fedora-11-cds.torrent',
28 'data_size': '12345678',
29 'description': 'BitTorrent Distribution Experiment, Fedora 11, i386 netinst'},
30 'p2p-next-01': {'bittorrent_client': 'libtorrent-rasterbar',
32 'system_os_version': '2.6.26',
35 'public_ip': '141.85.224.201',
36 'public_port': '6881',
39 'start_time': 'Thu Aug 11 15:36:35 EEST 2010',
40 'dht_enabled': 'true',
41 'pxe_enabled': 'false',
42 'streaming_enabled': 'false',
43 'description': 'NCIT Cluster System',
44 'log_file': 'p2p-next-01.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz'},
45 'p2p-next-03': {'bittorrent_client': 'libtorrent-rasterbar',
47 'system_os_version': '2.6.26',
50 'public_ip': '141.85.224.209',
51 'public_port': '6881',
54 'start_time': 'Thu Aug 11 15:36:35 EEST 2010',
55 'dht_enabled': 'true',
56 'pxe_enabled': 'false',
57 'streaming_enabled': 'false',
58 'description': 'NCIT Cluster System',
59 'log_file': 'p2p-next-03.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz'},
60 'p2p-next-04': {'bittorrent_client': 'libtorrent-rasterbar',
62 'system_os_version': '2.6.26',
65 'public_ip': '141.85.224.204',
66 'public_port': '6881',
69 'start_time': 'Thu Aug 11 15:36:35 EEST 2010',
70 'dht_enabled': 'true',
71 'pxe_enabled': 'false',
72 'streaming_enabled': 'false',
73 'description': 'NCIT Cluster System',
74 'log_file': 'p2p-next-04.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz'},
75 'p2p-next-09': {'bittorrent_client': 'libtorrent-rasterbar',
77 'system_os_version': '2.6.26',
80 'public_ip': '141.85.224.209',
81 'public_port': '6881',
84 'start_time': 'Thu Aug 11 15:36:35 EEST 2010',
85 'dht_enabled': 'true',
86 'pxe_enabled': 'false',
87 'streaming_enabled': 'false',
88 'description': 'NCIT Cluster System',
89 'log_file': 'p2p-next-09.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz'}}
91 config_file_path = os.path.join(os.path.dirname(__file__),
93 self.sd = config.SwarmDescription()
94 self.sd.load(config_file_path)
97 # Check for the same sections and items.
98 # Use sort() method because self.test_data is unordered.
99 self.assertEqual(self.sd.data.sections().sort(),
100 list(self.test_data.keys()).sort())
101 for section in self.sd.data.sections():
102 self.assertEqual(self.sd.data.items(section).sort(),
103 list(self.test_data[section]).sort())
105 def test_store(self):
106 self.sd.store("session_config.ini")
107 sd2 = config.SwarmDescription()
108 sd2.load("session_config.ini")
110 # Check for the same sections and items.
111 # Use sort() method because self.test_data is unordered.
112 self.assertEqual(sd2.data.sections().sort(),
113 list(self.test_data.keys()).sort())
114 for section in sd2.data.sections():
115 self.assertEqual(sd2.data.items(section).sort(),
116 list(self.test_data[section]).sort())
117 os.remove("session_config.ini")
119 def test_get_swarm(self):
120 swarm = self.sd.get_swarm()
121 expected_swarm = storage.Swarm(
122 self.test_data['swarm']['torrent_filename'],
123 self.test_data['swarm']['data_size'],
124 self.test_data['swarm']['description'])
126 self.assertEqual(swarm.torrent_filename,
127 expected_swarm.torrent_filename)
128 self.assertEqual(swarm.data_size, expected_swarm.data_size)
129 self.assertEqual(swarm.description, expected_swarm.description)
131 def test_get_session_entries(self):
132 session_entries = self.sd.get_session_entries()
133 expected_session_list = self.test_data.keys()
134 expected_session_list.remove('swarm')
136 for entry in session_entries:
137 section = entry.data.sections()[0]
138 self.assertEqual(entry.data.items(section).sort(),
139 list(self.test_data[section]).sort())
141 def test_update_session_entry_id(self):
142 session_entries = self.sd.get_session_entries()
143 entry = session_entries[0]
145 self.sd.update_session_entry_id(entry, cs_id)
147 self.assertEqual(entry.get_session_id(), cs_id)
149 def test_get_file_archives(self):
150 archives = self.sd.get_file_archives()
151 expected_archives = [
152 'p2p-next-01.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz',
153 'p2p-next-03.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz',
154 'p2p-next-04.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz',
155 'p2p-next-09.grid.pub.ro/btex-fedora-11/dbex-fedora-cds.tar.gz']
157 self.assertEqual(archives.sort(),expected_archives.sort())
159 class SessionEntryTest(unittest.TestCase):
161 Test suite for SessionEntry class in config.py.
164 # Class specific variables. Initialized in setUp, used throughout tests.
167 config_file = "config.sample.ini"
170 config_file_path = os.path.join(os.path.dirname(__file__),
172 self.sd = config.SwarmDescription()
173 self.sd.load(config_file_path)
174 session_entries = self.sd.get_session_entries()
176 self.se = session_entries[2]
178 def test_get_session(self):
179 cs = self.se.get_session()
181 self.assertTrue(type(cs) is storage.ClientSession)
182 self.assertEqual(cs.system_os, 'Linux')
183 self.assertEqual(cs.system_ram, '2007')
185 def test_get_session_id(self):
186 # Check for no session id before update
187 #self.assertFalse(self.se.get_session_id())
189 self.sd.update_session_entry_id(self.se, test_cs_id)
190 cs_id = self.se.get_session_id()
191 self.assertEqual(str(cs_id), str(test_cs_id))
193 class AccessConfigTest(unittest.TestCase):
195 Test suite for AccessConfig class in config.py.
198 # Class specific variables. Initialized in setUp, used throughout tests.
199 access_file = "access.sample.ini"
204 self.test_data = {'mysql': {'user': 'root',
205 'database': 'p2p_test',
206 'password': 'p2p4th3m45535',
207 'host': 'localhost'},
208 'sqlite': {'database': 'test.db'}}
210 access_file_path = os.path.join(os.path.dirname(__file__),
212 self.ac = config.AccessConfig()
213 self.ac.load(access_file_path)
216 # Check for the same sections.
217 # Use sort() method because self.test_data is unordered.
218 self.assertEqual(self.ac.data.sections().sort(),
219 list(self.test_data.keys()).sort())
220 for section in self.ac.data.sections():
221 self.assertEqual(self.ac.data.items(section).sort(),
222 list(self.test_data[section]).sort())
224 def test_store(self):
225 self.ac.store("access_config.ini")
226 ac2 = config.AccessConfig()
227 ac2.load("access_config.ini")
229 # Check for the same sections and items.
230 # Use sort() method because self.test_data is unordered.
231 self.assertEqual(ac2.data.sections().sort(),
232 list(self.test_data.keys()).sort())
233 for section in ac2.data.sections():
234 self.assertEqual(ac2.data.items(section).sort(),
235 list(self.test_data[section]).sort())
236 os.remove("access_config.ini")
238 def test_get_swarm_writer(self):
239 sw = self.ac.get_swarm_writer()
240 expected_handlers = 2
241 handlers = len(sw.handlers)
243 self.assertEqual(expected_handlers, handlers)
245 if __name__ == "__main__":