def testOpenportApp(self): port = get_open_port() s = SimpleTcpServer(port) s.runThreaded() os.chdir(os.path.dirname(os.path.dirname(__file__))) p = subprocess.Popen(['env/bin/python', 'apps/openport_app.py', '--local-port', '%s' % port, '--no-gui', '--no-tray'], stderr=subprocess.PIPE, stdout=subprocess.PIPE) self.processes_to_kill.append(p) sleep(5) process_output = nonBlockRead(p.stdout) print 'output: ', process_output if p.poll() is not None: # process terminated print 'error: %s' % nonBlockRead(p.stderr) self.fail('p.poll() should be None but was %s' % p.poll()) remote_host, remote_port = self.getRemoteHostAndPort(process_output) c = SimpleTcpClient(remote_host, remote_port) request = 'hello' response = c.send(request) self.assertEqual(request, response.strip()) c.close() s.close() p.kill()
def testTraySpawning(self): self.assertFalse(self.trayIsRunning()) db_file = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp_openport.db') try: os.remove(db_file) except OSError: pass port = get_open_port() os.chdir(os.path.dirname(os.path.dirname(__file__))) p_app = subprocess.Popen(['env/bin/python', 'apps/openport_app.py', '--local-port', '%s' % port, '--no-gui', '--verbose'], stderr=subprocess.PIPE, stdout=subprocess.PIPE) self.processes_to_kill.append(p_app) sleep(5) process_output = nonBlockRead(p_app.stdout) print lineNumber(), 'output: ', process_output self.check_application_is_still_alive(p_app) self.assertTrue(self.trayIsRunning()) remote_host, remote_port = self.getRemoteHostAndPort(process_output) print lineNumber(), "remote port:", remote_port os.kill(p_app.pid, signal.SIGKILL) p_app.wait() sleep(1) self.assertTrue(self.trayIsRunning()) self.killTray() self.assertFalse(self.trayIsRunning())
def restart_sharing(self): shares = self.dbhandler.get_shares() logger.debug('restarting shares - amount: %s' % len(list(shares))) for share in shares: if self.os_interaction.pid_is_running(share.pid): logger.debug('share still running: %s' % share.restart_command) self.onNewShare(share) else: try: logger.debug('starting share: %s' % share.restart_command) p = self.os_interaction.start_openport_process(share) sleep(1) if p.poll() is not None: logger.debug('could not start openport process: StdOut:%s\nStdErr:%s' % (nonBlockRead(p.stdout), nonBlockRead(p.stderr) ) ) else: logger.debug('started app %s' % share.restart_command) self.share_processes[p.pid]=p except Exception, e: tb = traceback.format_exc() logger.error(tb)
def testTray(self): db_file = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp_openport.db') try: os.remove(db_file) except OSError: pass port = get_open_port() print 'localport :', port s = SimpleTcpServer(port) s.runThreaded() os.chdir(os.path.dirname(os.path.dirname(__file__))) p_tray = subprocess.Popen(['env/bin/python', 'tray/openporttray.py', '--no-gui', '--database', db_file], stderr=subprocess.PIPE, stdout=subprocess.PIPE) p_app = subprocess.Popen(['env/bin/python', 'apps/openport_app.py', '--local-port', '%s' % port, '--no-gui', '--no-tray', '--verbose'], stderr=subprocess.PIPE, stdout=subprocess.PIPE) self.processes_to_kill.append(p_tray) self.processes_to_kill.append(p_app) sleep(10) process_output = nonBlockRead(p_app.stdout) print lineNumber(), 'output: ', process_output self.check_application_is_still_alive(p_tray) self.check_application_is_still_alive(p_app) remote_host, remote_port = self.getRemoteHostAndPort(process_output) print lineNumber(), "remote port:", remote_port c = SimpleTcpClient(remote_host, remote_port) request = 'hello' response = c.send(request) self.assertEqual(request, response.strip()) c.close() # s.close() os.kill(p_app.pid, signal.SIGKILL) self.assertNotEqual(p_app.wait(), None) while self.osinteraction.pid_is_running(p_app.pid): print "waiting for pid to be killed: %s" % p_app.pid sleep(1) os.kill(p_tray.pid, signal.SIGINT) print 'waiting for tray to be killed' p_tray.wait() print lineNumber(), "tray.stdout:", nonBlockRead(p_tray.stdout) print lineNumber(), "tray.stderr:", nonBlockRead(p_tray.stderr) print lineNumber(), "p_app stdout:", nonBlockRead(p_app.stdout) sleep(5) # s = SimpleTcpServer(port) # s.runThreaded() p_tray2 = subprocess.Popen(['env/bin/python', 'tray/openporttray.py', '--no-gui', '--database', db_file, '--verbose'], stderr=subprocess.PIPE, stdout=subprocess.PIPE) self.processes_to_kill.append(p_tray2) sleep(10) print lineNumber(), "tray.stdout:", nonBlockRead(p_tray2.stdout) print lineNumber(), "tray.stderr:",nonBlockRead(p_tray2.stderr) self.check_application_is_still_alive(p_tray2) c = SimpleTcpClient(remote_host, remote_port) request = 'hello' response = c.send(request) self.assertEqual(request, response.strip()) os.kill(p_tray2.pid, signal.SIGINT) p_tray2.wait() print lineNumber(), "tray.stdout:", nonBlockRead(p_tray2.stdout) print lineNumber(), "tray.stderr:",nonBlockRead(p_tray2.stderr) response = c.send(request) self.assertNotEqual(request, response.strip()) c.close() s.close()
def check_application_is_still_alive(self, p): if p.poll() is not None: # process terminated print 'error: %s' % nonBlockRead(p.stderr) self.fail('p_app.poll() should be None but was %s' % p.poll())