Exemplo n.º 1
0
 def test_mprocess_timeout(self):
     port = self.pp.port()
     cfg = self.cfg.copy()
     cfg['journal'] = True
     config_path = process.write_config(cfg)
     self.tmp_files.append(config_path)
     proc, host = process.mprocess(self.bin_path, config_path, port, 0)
     self.assertTrue(isinstance(proc, subprocess.Popen))
     self.assertTrue(isinstance(host, str))
     process.kill_mprocess(proc)
     if platform.system() == 'Windows':
         raise SkipTest("Cannot test mongod startup timeout on Windows.")
     with self.assertRaises(TimeoutError):
         result = process.mprocess(self.bin_path, config_path, port, 0.1)
         print(result)
Exemplo n.º 2
0
 def test_mprocess_timeout(self):
     port = self.pp.port()
     cfg = self.cfg.copy()
     cfg['journal'] = True
     config_path = process.write_config(cfg)
     self.tmp_files.append(config_path)
     proc, host = process.mprocess(self.bin_path, config_path, port, 0)
     self.assertTrue(isinstance(proc, subprocess.Popen))
     self.assertTrue(isinstance(host, str))
     process.kill_mprocess(proc)
     if platform.system() == 'Windows':
         raise SkipTest("Cannot test mongod startup timeout on Windows.")
     with self.assertRaises(TimeoutError):
         result = process.mprocess(self.bin_path, config_path, port, 0.1)
         print(result)
Exemplo n.º 3
0
    def start(self, timeout=300):
        """start server
        return True of False"""
        if self.is_alive:
            return True
        try:
            if self.cfg.get('dbpath', None) and self._is_locked:
                # repair if needed
                process.repair_mongo(self.name, self.cfg['dbpath'])

            self.proc, self.hostname = process.mprocess(self.name, self.config_path, self.cfg.get('port', None), timeout)
            self.pid = self.proc.pid
            logger.debug("pid={pid}, hostname={hostname}".format(pid=self.pid, hostname=self.hostname))
            self.host = self.hostname.split(':')[0]
            self.port = int(self.hostname.split(':')[1])

            # Wait for Server to respond to isMaster.
            for i in range(timeout):
                try:
                    self.run_command('isMaster')
                    break
                except pymongo.errors.ConnectionFailure:
                    time.sleep(1)
            else:
                raise TimeoutError(
                    "Server did not respond to 'isMaster' after %d attempts."
                    % timeout)
        except (OSError, TimeoutError):
            logger.exception("Could not start Server.")
            raise
        if not self.admin_added and self.login:
            self._add_auth()
            self.admin_added = True
        return True
Exemplo n.º 4
0
 def test_repair(self):
     port = self.pp.port(check=True)
     # Assume we're testing on 64-bit machines.
     self.cfg['nojournal'] = True
     lock_file = os.path.join(self.cfg['dbpath'], 'mongod.lock')
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     proc, host = process.mprocess(self.bin_path,
                                   config_path,
                                   port=port,
                                   timeout=60)
     self.assertTrue(os.path.exists(lock_file))
     if platform.system() == 'Windows':
         # mongod.lock cannot be read by any external process on Windows.
         with self.assertRaises(IOError):
             open(lock_file, 'r')
     else:
         with open(lock_file, 'r') as fd:
             self.assertGreater(len(fd.read()), 0)
     proc.terminate()
     proc.communicate()
     process.repair_mongo(self.bin_path, self.cfg['dbpath'])
     with open(lock_file, 'r') as fd:
         contents = fd.read()
         self.assertEqual(len(contents), 0,
                          "lock_file contains: " + contents)
 def test_mprocess(self):
     port = self.pp.port(check=True)
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     result = process.mprocess(self.bin_path, config_path, port=port, timeout=60)
     self.assertTrue(isinstance(result, tuple))
     proc, host = result
     self.assertTrue(isinstance(proc, subprocess.Popen))
     self.assertTrue(isinstance(host, str))
     process.kill_mprocess(proc)
Exemplo n.º 6
0
 def test_mprocess(self):
     port = self.pp.port(check=True)
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     result = process.mprocess(self.bin_path, config_path, port=port)
     self.assertTrue(isinstance(result, tuple))
     proc, host = result
     self.assertTrue(isinstance(proc, subprocess.Popen))
     self.assertTrue(isinstance(host, str))
     process.kill_mprocess(proc)
 def test_mprocess_busy_port(self):
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     port = self.pp.port()
     self.listen_port(port, max_connection=0)
     proc, host = process.mprocess(self.executable, config_path,
                                   port=port, timeout=2)
     self.assertTrue(proc.pid > 0)
     self.assertEqual(host, self.hostname + ':' + str(port))
     self.sockets.pop(port).close()
     self.assertRaises(OSError, process.mprocess,
                       self.executable, '', port, 1)
 def test_mprocess_busy_port(self):
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     port = self.pp.port()
     self.listen_port(port, max_connection=0)
     proc, host = process.mprocess(self.executable, config_path,
                                   port=port, timeout=2)
     self.assertTrue(proc.pid > 0)
     self.assertEqual(host, self.hostname + ':' + str(port))
     self.sockets.pop(port).close()
     self.assertRaises(OSError, process.mprocess,
                       self.executable, '', port, 1)
Exemplo n.º 9
0
    def start(self, timeout=300):
        """start server
        return True of False"""
        if self.is_alive:
            return True
        try:
            if self.cfg.get('dbpath', None) and self._is_locked:
                # repair if needed
                process.repair_mongo(self.name, self.cfg['dbpath'])

            self.proc, self.hostname = process.mprocess(
                self.name, self.config_path, self.cfg.get('port', None),
                timeout, self.silence_stdout)
            self.pid = self.proc.pid
            logger.debug("pid={pid}, hostname={hostname}".format(
                pid=self.pid, hostname=self.hostname))
            self.host = self.hostname.split(':')[0]
            self.port = int(self.hostname.split(':')[1])

            # Wait for Server to respond to isMaster.
            for i in range(timeout):
                try:
                    self.run_command('isMaster')
                    break
                except pymongo.errors.ConnectionFailure:
                    time.sleep(0.1)
            else:
                raise TimeoutError(
                    "Server did not respond to 'isMaster' after %d attempts." %
                    timeout)
        except (OSError, TimeoutError):
            logger.exception("Could not start Server.")
            raise
        if self.restart_required:
            if self.login:
                # Add users to the appropriate database.
                self._add_users()
            self.stop()

            # Restart with keyfile and auth.
            if self.is_mongos:
                self.config_path, self.cfg = self.__init_mongos(self.cfg)
            else:
                # Add auth options to this Server's config file.
                self.config_path, self.cfg = self.__init_mongod(self.cfg,
                                                                add_auth=True)
            self.restart_required = False
            self.start()

        return True
Exemplo n.º 10
0
    def start(self, timeout=300):
        """start server
        return True of False"""
        if self.is_alive:
            return True
        try:
            if self.cfg.get('dbpath', None) and self._is_locked:
                # repair if needed
                process.repair_mongo(self.name, self.cfg['dbpath'])

            self.proc, self.hostname = process.mprocess(
                self.name, self.config_path, self.cfg.get('port', None),
                timeout, self.silence_stdout)
            self.pid = self.proc.pid
            logger.debug("pid={pid}, hostname={hostname}".format(pid=self.pid, hostname=self.hostname))
            self.host = self.hostname.split(':')[0]
            self.port = int(self.hostname.split(':')[1])

            # Wait for Server to respond to isMaster.
            for i in range(timeout):
                try:
                    self.run_command('isMaster')
                    break
                except pymongo.errors.ConnectionFailure:
                    time.sleep(0.1)
            else:
                raise TimeoutError(
                    "Server did not respond to 'isMaster' after %d attempts."
                    % timeout)
        except (OSError, TimeoutError):
            logger.exception("Could not start Server.")
            raise
        if self.restart_required:
            if self.login:
                # Add users to the appropriate database.
                self._add_users()
            self.stop()

            # Restart with keyfile and auth.
            if self.is_mongos:
                self.config_path, self.cfg = self.__init_mongos(self.cfg)
            else:
                # Add auth options to this Server's config file.
                self.config_path, self.cfg = self.__init_mongod(
                    self.cfg, add_auth=True)
            self.restart_required = False
            self.start()

        return True
Exemplo n.º 11
0
 def test_repair(self):
     port = self.pp.port(check=True)
     # Assume we're testing on 64-bit machines.
     self.cfg['nojournal'] = True
     lock_file = os.path.join(self.cfg['dbpath'], 'mongod.lock')
     config_path = process.write_config(self.cfg)
     self.tmp_files.append(config_path)
     proc, host = process.mprocess(self.bin_path, config_path, port=port, timeout=60)
     self.assertTrue(os.path.exists(lock_file))
     if platform.system() == 'Windows':
         # mongod.lock cannot be read by any external process on Windows.
         with self.assertRaises(IOError):
             open(lock_file, 'r')
     else:
         self.assertTrue(len(open(lock_file, 'r').read()) > 0)
     proc.terminate()
     process.repair_mongo(self.bin_path, self.cfg['dbpath'])
     self.assertFalse(len(open(lock_file, 'r').read()) > 0, "lock_file contains: {0}".format(open(lock_file, 'r').read()))
Exemplo n.º 12
0
    def start(self, timeout=300):
        """start server
        return True of False"""
        if self.is_alive:
            return True
        try:
            if self.cfg.get('dbpath', None) and self._is_locked:
                # repair if needed
                process.repair_mongo(self.name, self.cfg['dbpath'])

            self.proc, self.hostname = process.mprocess(
                self.name, self.config_path, self.cfg.get('port', None),
                timeout, self.silence_stdout)
            self.pid = self.proc.pid
            logger.debug("pid={pid}, hostname={hostname}".format(pid=self.pid, hostname=self.hostname))
            self.host = self.hostname.split(':')[0]
            self.port = int(self.hostname.split(':')[1])

            # Wait for Server to respond to isMaster.
            for i in range(timeout):
                try:
                    self.run_command('isMaster')
                    break
                except pymongo.errors.ConnectionFailure:
                    logger.exception('isMaster command failed:')
                    time.sleep(0.1)
            else:
                raise TimeoutError(
                    "Server did not respond to 'isMaster' after %d attempts."
                    % timeout)
        except (OSError, TimeoutError):
            logpath = self.cfg.get('logpath')
            if logpath:
                # Copy the server logs into the mongo-orchestration logs.
                logger.error(
                    "Could not start Server. Please find server log below.\n"
                    "=====================================================")
                with open(logpath) as lp:
                    logger.error(lp.read())
            else:
                logger.exception(
                    'Could not start Server, and no logpath was provided!')
            reraise(TimeoutError,
                    'Could not start Server. '
                    'Please check server log located in ' +
                    self.cfg.get('logpath', '<no logpath given>') +
                    ' or the mongo-orchestration log in ' +
                    LOG_FILE + ' for more details.')
        if self.restart_required:
            if self.login:
                # Add users to the appropriate database.
                self._add_users()
            self.stop()

            # Restart with keyfile and auth.
            if self.is_mongos:
                self.config_path, self.cfg = self.__init_mongos(self.cfg)
            else:
                # Add auth options to this Server's config file.
                self.config_path, self.cfg = self.__init_mongod(
                    self.cfg, add_auth=True)
            self.restart_required = False
            self.start()

        return True
Exemplo n.º 13
0
    def start(self, timeout=300):
        """start server
        return True of False"""
        if self.is_alive:
            return True
        try:
            if self.cfg.get('dbpath', None) and self._is_locked:
                # repair if needed
                process.repair_mongo(self.name, self.cfg['dbpath'])

            self.proc, self.hostname = process.mprocess(
                self.name, self.config_path, self.cfg.get('port', None),
                timeout, self.silence_stdout)
            self.pid = self.proc.pid
            logger.debug("pid={pid}, hostname={hostname}".format(pid=self.pid, hostname=self.hostname))
            self.host = self.hostname.split(':')[0]
            self.port = int(self.hostname.split(':')[1])

            # Wait for Server to respond to isMaster.
            for i in range(timeout):
                try:
                    self.run_command('isMaster')
                    break
                except pymongo.errors.ConnectionFailure:
                    time.sleep(0.1)
            else:
                raise TimeoutError(
                    "Server did not respond to 'isMaster' after %d attempts."
                    % timeout)
        except (OSError, TimeoutError):
            logpath = self.cfg.get('logpath')
            if logpath:
                # Copy the server logs into the mongo-orchestration logs.
                logger.error(
                    "Could not start Server. Please find server log below.\n"
                    "=====================================================")
                with open(logpath) as lp:
                    logger.error(lp.read())
            else:
                logger.exception(
                    'Could not start Server, and no logpath was provided!')
            reraise(TimeoutError,
                    'Could not start Server. '
                    'Please check server log located in ' +
                    self.cfg.get('logpath', '<no logpath given>') +
                    ' or the mongo-orchestration log in ' +
                    LOG_FILE + ' for more details.')
        if self.restart_required:
            if self.login:
                # Add users to the appropriate database.
                self._add_users()
            self.stop()

            # Restart with keyfile and auth.
            if self.is_mongos:
                self.config_path, self.cfg = self.__init_mongos(self.cfg)
            else:
                # Add auth options to this Server's config file.
                self.config_path, self.cfg = self.__init_mongod(
                    self.cfg, add_auth=True)
            self.restart_required = False
            self.start()

        return True