class ServersTestCase(unittest.TestCase): def setUp(self): PortPool().change_range() self.path = tempfile.mktemp(prefix="test-storage") self.servers = Servers() self.servers.set_settings(os.environ.get('MONGOBIN', "")) def remove_path(self, path): onerror = lambda func, filepath, exc_info: (os.chmod( filepath, stat.S_IWUSR), func(filepath)) if os.path.isfile(path): try: os.remove(path) except OSError: time.sleep(2) onerror(os.remove, path, None) def tearDown(self): self.servers.cleanup() self.remove_path(self.path) def test_singleton(self): self.assertEqual(id(self.servers), id(Servers())) def test_bool(self): self.assertEqual(False, bool(self.servers)) self.servers.create('mongod', {}, autostart=False) self.assertTrue(True, bool(self.servers)) def test_operations(self): server_id = self.servers.create('mongod', {}, autostart=False) self.assertTrue(len(self.servers) == 1) self.assertTrue(server_id in self.servers) server_id2 = 'server-id2' server2 = Server( os.path.join(os.environ.get('MONGOBIN', ''), 'mongod'), {}) server2.start(30) server2_pid = server2.info()['procInfo']['pid'] self.servers[server_id2] = server2 self.assertTrue( self.servers[server_id2]['procInfo']['pid'] == server2_pid) self.assertTrue(server_id2 in self.servers) for h_id in self.servers: self.assertTrue(h_id in (server_id, server_id2)) operator.delitem(self.servers, server_id2) self.assertFalse(server_id2 in self.servers) server2.stop() server2.cleanup() def test_cleanup(self): self.servers.create('mongod', {}, autostart=False) self.servers.create('mongod', {}, autostart=True) self.assertTrue(len(self.servers) == 2) self.servers.cleanup() self.assertTrue(len(self.servers) == 0) def test_new_server(self): self.assertTrue(len(self.servers) == 0) server_id = self.servers.create('mongod', {}, autostart=False) info = self.servers.info(server_id) self.assertTrue(len(self.servers) == 1) self.assertNotIn('pid', info['procInfo']) server_id2 = self.servers.create('mongod', {}, autostart=True) info = self.servers.info(server_id2) self.assertTrue(info['procInfo']['pid'] > 0) self.assertRaises(OSError, self.servers.create, 'fake_process_', {}) def test_new_server_with_auth(self): server_id = self.servers.create('mongod', {}, login='******', password='******', autostart=True) hostname = self.servers.hostname(server_id) c = pymongo.MongoClient(hostname) self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names) self.assertTrue(c.admin.authenticate('adminko', 'password')) self.assertTrue(isinstance(c.admin.collection_names(), list)) self.assertTrue(c.admin.logout() is None) self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names) def test_hdel(self): self.assertEqual(len(self.servers), 0) h_id = self.servers.create('mongod', {}, autostart=True) self.assertEqual(len(self.servers), 1) h_info = self.servers.info(h_id)['procInfo'] self.assertTrue(os.path.exists(h_info['params']['dbpath'])) self.assertTrue(os.path.exists(h_info['optfile'])) self.servers.remove(h_id) self.assertEqual(len(self.servers), 0) # check length # check cleanup self.assertFalse(os.path.exists(h_info['params']['dbpath'])) self.assertFalse(os.path.exists(h_info['optfile'])) def test_hcommand(self): h_id = self.servers.create('mongod', {}, autostart=False) self.assertTrue(self.servers.command(h_id, 'start')) self.assertTrue(self.servers.command(h_id, 'stop')) self.assertTrue(self.servers.command(h_id, 'start')) self.assertTrue(self.servers.command(h_id, 'restart')) self.assertRaises(ValueError, self.servers.command, h_id, 'fake') def test_hinfo(self): h_id = self.servers.create('mongod', {}, autostart=False) info = self.servers.info(h_id) self.assertEqual(info['id'], h_id) self.assertNotIn('pid', info['procInfo']) self.assertEqual(info['statuses'], {}) self.assertEqual(info['serverInfo'], {}) def test_host_to_server_id(self): h_id = self.servers.create('mongod', {}, autostart=True) h_uri = self.servers.hostname(h_id) h2_id = self.servers.create('mongod', {}, autostart=True) h2_uri = self.servers.hostname(h2_id) self.assertEqual(self.servers.host_to_server_id(h_uri), h_id) self.assertEqual(self.servers.host_to_server_id(h2_uri), h2_id) def test_hostname(self): h_id = self.servers.create('mongod', {}, autostart=True) h_uri = self.servers.hostname(h_id) self.assertEqual(self.servers.hostname(h_id), h_uri) def test_is_alive(self): h_id = self.servers.create('mongod', {}, autostart=True) self.assertEqual(self.servers.is_alive(h_id), True) self.servers.command(h_id, 'stop') self.assertEqual(self.servers.is_alive(h_id), False) def test_db_command(self): h_id = self.servers.create('mongod', {}, autostart=False) self.assertRaises(pymongo.errors.PyMongoError, self.servers.db_command, h_id, 'serverStatus', None, False) self.servers.command(h_id, 'start', 10) self.assertEqual( self.servers.db_command(h_id, 'serverStatus', arg=None, is_eval=False).get('ok', -1), 1) self.assertEqual( self.servers.db_command(h_id, 'db.getName()', arg=None, is_eval=True), 'admin') def test_id_specified(self): id = 'xyzzy' h_id = self.servers.create('mongod', {}, autostart=False, server_id=id) self.assertEqual(id, h_id)
class ServersTestCase(unittest.TestCase): def setUp(self): PortPool().change_range() self.path = tempfile.mktemp(prefix="test-storage") self.servers = Servers() self.servers.set_settings(os.environ.get('MONGOBIN', "")) def remove_path(self, path): onerror = lambda func, filepath, exc_info: (os.chmod(filepath, stat.S_IWUSR), func(filepath)) if os.path.isfile(path): try: os.remove(path) except OSError: time.sleep(2) onerror(os.remove, path, None) def tearDown(self): self.servers.cleanup() self.remove_path(self.path) def test_singleton(self): self.assertEqual(id(self.servers), id(Servers())) def test_set_settings(self): default_release = 'old-release' releases = {default_release: os.path.join(os.getcwd(), 'bin')} self.servers.set_settings(releases, default_release) self.assertEqual(releases, self.servers.releases) self.assertEqual(default_release, self.servers.default_release) def test_bool(self): self.assertEqual(False, bool(self.servers)) self.servers.create('mongod', {}, autostart=False) self.assertTrue(True, bool(self.servers)) def test_operations(self): server_id = self.servers.create('mongod', {}, autostart=False) self.assertTrue(len(self.servers) == 1) self.assertTrue(server_id in self.servers) server_id2 = 'server-id2' server2 = Server(os.path.join(os.environ.get('MONGOBIN', ''), 'mongod'), {}) server2.start(30) server2_pid = server2.info()['procInfo']['pid'] self.servers[server_id2] = server2 self.assertTrue(self.servers[server_id2]['procInfo']['pid'] == server2_pid) self.assertTrue(server_id2 in self.servers) for h_id in self.servers: self.assertTrue(h_id in (server_id, server_id2)) operator.delitem(self.servers, server_id2) self.assertFalse(server_id2 in self.servers) server2.stop() server2.cleanup() def test_cleanup(self): self.servers.create('mongod', {}, autostart=False) self.servers.create('mongod', {}, autostart=True) self.assertTrue(len(self.servers) == 2) self.servers.cleanup() self.assertTrue(len(self.servers) == 0) def test_new_server(self): self.assertTrue(len(self.servers) == 0) server_id = self.servers.create('mongod', {}, autostart=False) info = self.servers.info(server_id) self.assertTrue(len(self.servers) == 1) self.assertNotIn('pid', info['procInfo']) server_id2 = self.servers.create('mongod', {}, autostart=True) info = self.servers.info(server_id2) self.assertTrue(info['procInfo']['pid'] > 0) self.assertRaises(OSError, self.servers.create, 'fake_process_', {}) def test_new_server_with_auth(self): server_id = self.servers.create('mongod', {}, login='******', password='******', autostart=True) hostname = self.servers.hostname(server_id) c = pymongo.MongoClient(hostname) self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names) self.assertTrue(c.admin.authenticate('adminko', 'password')) self.assertTrue(isinstance(c.admin.collection_names(), list)) self.assertTrue(c.admin.logout() is None) self.assertRaises(pymongo.errors.OperationFailure, c.admin.collection_names) def test_hdel(self): self.assertEqual(len(self.servers), 0) h_id = self.servers.create('mongod', {}, autostart=True) self.assertEqual(len(self.servers), 1) h_info = self.servers.info(h_id)['procInfo'] self.assertTrue(os.path.exists(h_info['params']['dbpath'])) self.assertTrue(os.path.exists(h_info['optfile'])) self.servers.remove(h_id) self.assertEqual(len(self.servers), 0) # check length # check cleanup self.assertFalse(os.path.exists(h_info['params']['dbpath'])) self.assertFalse(os.path.exists(h_info['optfile'])) def test_hcommand(self): h_id = self.servers.create('mongod', {}, autostart=False) self.assertTrue(self.servers.command(h_id, 'start')) self.assertTrue(self.servers.command(h_id, 'stop')) self.assertTrue(self.servers.command(h_id, 'start')) self.assertTrue(self.servers.command(h_id, 'restart')) self.assertRaises(ValueError, self.servers.command, h_id, 'fake') def test_hinfo(self): h_id = self.servers.create('mongod', {}, autostart=False) info = self.servers.info(h_id) self.assertEqual(info['id'], h_id) self.assertNotIn('pid', info['procInfo']) self.assertEqual(info['statuses'], {}) self.assertEqual(info['serverInfo'], {}) def test_id_by_hostname(self): h_id = self.servers.create('mongod', {}, autostart=True) h_uri = self.servers.info(h_id)['uri'] h2_id = self.servers.create('mongod', {}, autostart=True) h2_uri = self.servers.info(h2_id)['uri'] self.assertTrue(self.servers.id_by_hostname(h_uri) == h_id) self.assertTrue(self.servers.id_by_hostname(h2_uri) == h2_id) def test_hostname(self): h_id = self.servers.create('mongod', {}, autostart=True) h_uri = self.servers.info(h_id)['uri'] self.assertEqual(self.servers.hostname(h_id), h_uri) def test_is_alive(self): h_id = self.servers.create('mongod', {}, autostart=True) self.assertEqual(self.servers.is_alive(h_id), True) self.servers.command(h_id, 'stop') self.assertEqual(self.servers.is_alive(h_id), False) def test_db_command(self): h_id = self.servers.create('mongod', {}, autostart=False) self.assertRaises(pymongo.errors.PyMongoError, self.servers.db_command, h_id, 'serverStatus', None, False) self.servers.command(h_id, 'start', 10) self.assertEqual(self.servers.db_command(h_id, 'serverStatus', arg=None, is_eval=False).get('ok', -1), 1) self.assertEqual(self.servers.db_command(h_id, 'db.getName()', arg=None, is_eval=True), 'admin') def test_id_specified(self): id = 'xyzzy' h_id = self.servers.create('mongod', {}, autostart=False, server_id=id) self.assertEqual(id, h_id)