Example #1
0
 def _get_client(cls, rootwrap_config):
     try:
         return cls._clients[rootwrap_config]
     except KeyError:
         from oslo_rootwrap import client
         new_client = client.Client(
             ["sudo", "nova-rootwrap-daemon", rootwrap_config])
         cls._clients[rootwrap_config] = new_client
         return new_client
Example #2
0
 def get_client(cls):
     with cls.__lock:
         if cls.__client is None:
             if xenapi_root_helper.ROOT_HELPER_DAEMON_TOKEN == \
                 cfg.CONF.AGENT.root_helper_daemon:
                 cls.__client = xenapi_root_helper.XenAPIClient()
             else:
                 cls.__client = client.Client(
                     shlex.split(cfg.CONF.AGENT.root_helper_daemon))
         return cls.__client
Example #3
0
 def get_client(cls):
     LOG.info('%s() caller: %s()',
              sys._getframe(0).f_code.co_name,
              sys._getframe(1).f_code.co_name)
     with cls.__lock:
         if cls.__client is None:
             if (xenapi_root_helper.ROOT_HELPER_DAEMON_TOKEN ==
                     cfg.CONF.AGENT.root_helper_daemon):
                 cls.__client = xenapi_root_helper.XenAPIClient()
             else:
                 cls.__client = client.Client(
                     shlex.split(cfg.CONF.AGENT.root_helper_daemon))
         return cls.__client
    def setUp(self):
        self.assert_unpatched()

        super(RootwrapDaemonTest, self).setUp()

        # Collect daemon logs
        daemon_log = io.BytesIO()
        p = mock.patch('oslo_rootwrap.subprocess.Popen',
                       run_daemon.forwarding_popen(daemon_log))
        p.start()
        self.addCleanup(p.stop)

        # Collect client logs
        client_log = six.StringIO()
        handler = logging.StreamHandler(client_log)
        log_format = run_daemon.log_format.replace('+', ' ')
        handler.setFormatter(logging.Formatter(log_format))
        logger = logging.getLogger('oslo_rootwrap')
        logger.addHandler(handler)
        logger.setLevel(logging.DEBUG)
        self.addCleanup(logger.removeHandler, handler)

        # Add all logs as details
        @self.addCleanup
        def add_logs():
            self.addDetail(
                'daemon_log',
                content.Content(content.UTF8_TEXT,
                                lambda: [daemon_log.getvalue()]))
            self.addDetail(
                'client_log',
                content.Content(
                    content.UTF8_TEXT,
                    lambda: [client_log.getvalue().encode('utf-8')]))

        # Create client
        self.client = client.Client(
            [sys.executable, run_daemon.__file__, self.config_file])

        # _finalize is set during Client.execute()
        @self.addCleanup
        def finalize_client():
            if self.client._initialized:
                self.client._finalize()

        self.execute = self.client.execute
Example #5
0
 def get_client(cls):
     with cls.__lock:
         if cls.__client is None:
             cls.__client = client.Client(
                 shlex.split(cfg.CONF.COMMON.root_helper_daemon))
         return cls.__client
Example #6
0
    return obj.returncode, out, err


def run_sudo(cmd):
    return run_plain(["sudo"] + cmd)


def run_rootwrap(cmd):
    return run_plain([
        "sudo", sys.executable, "-c",
        "from oslo_rootwrap import cmd; cmd.main()", config_path
    ] + cmd)


run_daemon = client.Client([
    "sudo", sys.executable, "-c",
    "from oslo_rootwrap import cmd; cmd.daemon()", config_path
]).execute


def run_one(runner, cmd):
    def __inner():
        code, out, err = runner(cmd)
        assert err == "", "Stderr not empty:\n" + err
        assert code == 0, "Command failed"

    return __inner


runners = [
    ("{0}", run_plain),
    ("sudo {0}", run_sudo),