def test_include_of_single_file(self): with open("test1.txt", "w") as f: f.write("hello world") filedata = pack_include_files(["test1.txt"]) os.makedirs("outdir") os.chdir("outdir") unpack_include_files(filedata) self.assertEquals(os.listdir("."), ["test1.txt"])
def test_include_of_single_file_with_explicit_location(self): os.makedirs("indir") os.makedirs("outdir") with open("indir/test1.txt", "w") as f: f.write("hello world") filedata = pack_include_files(["*.txt"], "./indir") unpack_include_files(filedata, "./outdir") self.assertEquals(os.listdir("outdir"), ["test1.txt"])
def _handle_commands(self, message): # we get the messages from the broker here data = message.data command = data['command'] logger.debug('Received command %s' % command) if command == 'RUN': test_dir = data['args'].get('test_dir') if test_dir is None: test_dir = tempfile.mkdtemp() else: test_dir += str(os.getpid()) if not os.path.exists(test_dir): os.makedirs(test_dir) data['args']['test_dir'] = test_dir # XXX should be done in _run or at least asynchronously filedata = data.get('filedata') if filedata: unpack_include_files(filedata, test_dir) args = data['args'] run_id = data.get('run_id') pid = self._run(args, run_id) return { 'result': { 'pids': [pid], 'agent_id': str(self.pid), 'command': command } } elif command in ('STATUS', '_STATUS'): return self._status(command, data) elif command == 'STOP': logger.debug('asked to STOP all runs') return self._stop_runs(command) elif command == 'QUIT': if len(self._workers) > 0 and not data.get('force', False): # if we're busy we won't quit - unless forced ! logger.info("Broker asked us to quit ! But we're busy...") logger.info("Cowardly refusing to die") return self._status(command, data) logger.debug('asked to QUIT') try: return self._stop_runs(command) finally: sys.exit(0) raise NotImplementedError(command)
def _handle_commands(self, message): # we get the messages from the broker here data = message.data command = data['command'] logger.debug('Received command %s' % command) if command == 'RUN': # XXX should be done in _run or at least asynchronously filedata = data.get('filedata') if filedata: test_dir = data['args'].get('test_dir') if test_dir is None: test_dir = '.' if not os.path.exists(test_dir): os.makedirs(test_dir) unpack_include_files(filedata, test_dir) args = data['args'] run_id = data.get('run_id') pid = self._run(args, run_id) return { 'result': { 'pids': [pid], 'agent_id': str(self.pid), 'command': command } } elif command in ('STATUS', '_STATUS'): status = {} run_id = data.get('run_id') if run_id is not None: status['run_id'] = run_id for pid, (proc, _run_id) in self._workers.items(): if run_id is not None and run_id != _run_id: continue if proc.poll() is None: status[pid] = 'running' else: status[pid] = 'terminated' res = {'result': {'status': status, 'command': command}} return res elif command == 'STOP': return self._stop_runs(command) elif command == 'QUIT': try: return self._stop_runs(command) finally: sys.exit(0) raise NotImplementedError(command)
def _handle_commands(self, message): # we get the messages from the broker here data = message.data command = data['command'] logger.debug('Received command %s' % command) if command == 'RUN': # XXX should be done in _run or at least asynchronously filedata = data.get('filedata') if filedata: test_dir = data['args'].get('test_dir') if test_dir is None: test_dir = '.' if not os.path.exists(test_dir): os.makedirs(test_dir) unpack_include_files(filedata, test_dir) args = data['args'] run_id = data.get('run_id') pid = self._run(args, run_id) return {'result': {'pids': [pid], 'agent_id': str(self.pid), 'command': command}} elif command in ('STATUS', '_STATUS'): status = {} run_id = data.get('run_id') if run_id is not None: status['run_id'] = run_id for pid, (proc, _run_id) in self._workers.items(): if run_id is not None and run_id != _run_id: continue if proc.poll() is None: status[pid] = 'running' else: status[pid] = 'terminated' res = {'result': {'status': status, 'command': command}} return res elif command == 'STOP': return self._stop_runs(command) elif command == 'QUIT': try: return self._stop_runs(command) finally: sys.exit(0) raise NotImplementedError(command)
def _handle_commands(self, message): # we get the messages from the broker here data = message.data command = data['command'] logger.debug('Received command %s' % command) if command == 'RUN': test_dir = data['args'].get('test_dir') if test_dir is None: test_dir = tempfile.mkdtemp() else: test_dir += str(os.getpid()) if not os.path.exists(test_dir): os.makedirs(test_dir) data['args']['test_dir'] = test_dir # XXX should be done in _run or at least asynchronously filedata = data.get('filedata') if filedata: unpack_include_files(filedata, test_dir) args = data['args'] run_id = data.get('run_id') pid = self._run(args, run_id) return {'result': {'pids': [pid], 'agent_id': str(self.pid), 'command': command}} elif command in ('STATUS', '_STATUS'): return self._status(command, data) elif command == 'STOP': logger.debug('asked to STOP all runs') return self._stop_runs(command) elif command == 'QUIT': if len(self._workers) > 0 and not data.get('force', False): # if we're busy we won't quit - unless forced ! logger.info("Broker asked us to quit ! But we're busy...") logger.info("Cowardly refusing to die") return self._status(command, data) logger.debug('asked to QUIT') try: return self._stop_runs(command) finally: sys.exit(0) raise NotImplementedError(command)
def test_preservation_of_file_mode(self): with open("test1.sh", "w") as f: f.write("#!/bin/sh\necho 'hello world'\n") os.chmod("test1.sh", 0755) with open("private.txt", "w") as f: f.write("TOP SECRET DATA\n") os.chmod("private.txt", 0600) filedata = pack_include_files(["*.*"]) os.unlink("test1.sh") os.unlink("private.txt") unpack_include_files(filedata) self.assertEquals(os.stat("test1.sh").st_mode & 0777, 0755) self.assertEquals(os.stat("private.txt").st_mode & 0777, 0600)
def test_relative_globbing_and_direcotry_includes(self): os.makedirs("indir") os.makedirs("outdir") os.chdir("indir") with open("test1.txt", "w") as f: f.write("hello world") with open("test2.txt", "w") as f: f.write("hello world") os.makedirs("subdir/subsubdir") os.chdir("subdir/subsubdir") with open("test3.txt", "w") as f: f.write("hello world") os.chdir("../../../outdir") filedata = pack_include_files(["../indir/*.txt", "../indir/*dir"]) unpack_include_files(filedata) self.assertEquals(sorted(os.listdir(".")), ["subdir", "test1.txt", "test2.txt"]) self.assertEquals(os.listdir("./subdir"), ["subsubdir"]) self.assertEquals(os.listdir("./subdir/subsubdir"), ["test3.txt"])
def _prepare_filesystem(self): test_dir = self.args.get('test_dir') # in standalone mode we take care of creating # the files if test_dir is not None: if not self.slave: test_dir = test_dir + '-%d' % os.getpid() if not os.path.exists(test_dir): os.makedirs(test_dir) # Copy over the include files, if any. # It's inefficient to package them up and then immediately # unpackage them, but this has the advantage of ensuring # consistency with how it's done in the distributed case. includes = self.args.get('include_file', []) filedata = pack_include_files(includes) unpack_include_files(filedata, test_dir) # change to execution directory if asked logger.debug('chdir %r' % test_dir) os.chdir(test_dir)
def test_unicode_unpack(self): # make sure we pass string data = (u'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00' '\x00\x00\x00\x00\x00\x00\x00\x00\x00') unpack_include_files(data.encode('base64'))