def test_check_exit_code_boolean(self): processutils.execute('/usr/bin/env', 'false', check_exit_code=False) self.assertRaises(processutils.ProcessExecutionError, processutils.execute, '/usr/bin/env', 'false', check_exit_code=True)
def try_linkloop(loop_dev): path = '/root/blocks/' if not os.path.exists(path): putils.execute('mkdir', '-p', path, run_as_root=True, root_helper=rootwrap.root_helper()) path = path + 'snap' + loop_dev[9:] + '.blk' if not os.path.exists(path): putils.execute('dd', 'if=/dev/zero', 'of=' + path, 'bs=1M', 'count=512', run_as_root=True, root_helper=rootwrap.root_helper()) linkloop(loop_dev, path)
def tearDown(self): if self.reactor: self.reactor.close() try: processutils.execute('rm', '-rf', FLAGS.rpc_zmq_ipc_dir) except exception.Error: pass super(_RpcZmqBaseTestCase, self).tearDown()
def test_check_exit_code_list(self): processutils.execute('/usr/bin/env', 'sh', '-c', 'exit 101', check_exit_code=(101, 102)) processutils.execute('/usr/bin/env', 'sh', '-c', 'exit 102', check_exit_code=(101, 102)) self.assertRaises(processutils.ProcessExecutionError, processutils.execute, '/usr/bin/env', 'sh', '-c', 'exit 103', check_exit_code=(101, 102)) self.assertRaises(processutils.ProcessExecutionError, processutils.execute, '/usr/bin/env', 'sh', '-c', 'exit 0', check_exit_code=(101, 102))
def is_looped(loop_dev): (out, err) = putils.execute("losetup", '-a', run_as_root=True, root_helper=rootwrap.root_helper()) looped_list = [line.split()[0].rstrip(":") for line in out.strip().split('\n')] if loop_dev in looped_list: return True return False
def test_retry_on_communicate_error(self): self.called = False def fake_communicate(*args, **kwargs): if self.called: return ('', '') self.called = True e = OSError('foo') e.errno = errno.EAGAIN raise e self.useFixture(fixtures.MonkeyPatch( 'subprocess.Popen.communicate', fake_communicate)) processutils.execute('/usr/bin/env', 'true', check_exit_code=False) self.assertTrue(self.called)
def consume_in_thread(self): """Runs the ZmqProxy service""" ipc_dir = CONF.rpc_zmq_ipc_dir consume_in = "tcp://%s:%s" % \ (CONF.rpc_zmq_bind_address, CONF.rpc_zmq_port) consumption_proxy = InternalContext(None) if not os.path.isdir(ipc_dir): try: utils.execute('mkdir', '-p', ipc_dir, run_as_root=True) utils.execute('chown', "%s:%s" % (os.getuid(), os.getgid()), ipc_dir, run_as_root=True) utils.execute('chmod', '750', ipc_dir, run_as_root=True) except utils.ProcessExecutionError: with excutils.save_and_reraise_exception(): LOG.error(_("Could not create IPC directory %s") % (ipc_dir, )) try: self.register(consumption_proxy, consume_in, zmq.PULL, out_bind=True) except zmq.ZMQError: with excutils.save_and_reraise_exception(): LOG.error(_("Could not create ZeroMQ receiver daemon. " "Socket may already be in use.")) super(ZmqProxy, self).consume_in_thread()
def consume_in_thread(self): """Runs the ZmqProxy service""" ipc_dir = CONF.rpc_zmq_ipc_dir consume_in = "tcp://%s:%s" % \ (CONF.rpc_zmq_bind_address, CONF.rpc_zmq_port) consumption_proxy = InternalContext(None) if not os.path.isdir(ipc_dir): try: utils.execute('mkdir', '-p', ipc_dir, run_as_root=True) utils.execute('chown', "%s:%s" % (os.getuid(), os.getgid()), ipc_dir, run_as_root=True) utils.execute('chmod', '750', ipc_dir, run_as_root=True) except utils.ProcessExecutionError: LOG.error(_("Could not create IPC directory %s") % (ipc_dir, )) raise try: self.register(consumption_proxy, consume_in, zmq.PULL, out_bind=True) except zmq.ZMQError: LOG.error( _("Could not create ZeroMQ receiver daemon. " "Socket may already be in use.")) raise super(ZmqProxy, self).consume_in_thread()
def test_no_retry_on_success(self): fd, tmpfilename = tempfile.mkstemp() _, tmpfilename2 = tempfile.mkstemp() try: fp = os.fdopen(fd, 'w+') fp.write("""#!/bin/sh # If we've already run, bail out. grep -q foo "$1" && exit 1 # Mark that we've run before. echo foo > "$1" # Check that stdin gets passed correctly. grep foo """) fp.close() os.chmod(tmpfilename, 0755) processutils.execute(tmpfilename, tmpfilename2, process_input='foo', attempts=2) finally: os.unlink(tmpfilename) os.unlink(tmpfilename2)
def unlinkloop(loop_dev): if is_looped(loop_dev): putils.execute('losetup', '-d', loop_dev, run_as_root=True, root_helper=rootwrap.root_helper())
def linkloop(loop_dev, path): putils.execute('losetup', loop_dev, path, run_as_root=True, root_helper=rootwrap.root_helper())
def findloop(): # Licyh NOTE: openstack.common.processutils & subprocess.Popen only # supports 'ls' not 'cat a.py' but 'cat','a.py', (out, err) = putils.execute('losetup', '-f', run_as_root=True, root_helper=rootwrap.root_helper()) return out.strip()
def test_with_env_variables(self): env_vars = {'SUPER_UNIQUE_VAR': 'The answer is 42'} out, err = processutils.execute('/usr/bin/env', env_variables=env_vars) self.assertIn('SUPER_UNIQUE_VAR=The answer is 42', out)