Exemplo n.º 1
0
    def test_missing_log_directory(self):
        """
        Test creating directory for log directory
        """
        temp_dir_path = tempfile.mkdtemp()

        old_dir_path = logutil.LOGFILE_DIR
        old_file_path = logutil.LOGFILE_PATH

        # Set logutil to uninitialized state
        logutil._rhsm_log_handler = None
        logutil._subman_debug_handler = None
        logutil.LOGFILE_DIR = temp_dir_path
        logutil.LOGFILE_PATH = os.path.join(temp_dir_path, "rhsm.log")

        self.assertTrue(os.path.exists(temp_dir_path))

        # Simulate deleting of directory /var/log/rhsm
        os.rmdir(temp_dir_path)

        logutil.init_logger()

        # init_logger should automatically create directory /var/log/rhsm,
        # when it doesn't exist
        self.assertTrue(os.path.exists(temp_dir_path))

        os.remove(logutil.LOGFILE_PATH)
        os.rmdir(logutil.LOGFILE_DIR)

        logutil.LOGFILE_DIR = old_dir_path
        logutil.LOGFILE_PATH = old_file_path
Exemplo n.º 2
0
    def test_not_possible_to_create_log_dir_due_to_access_perm(self):
        """
        Test that it is not possible to create log directory due to access permission
        """
        temp_dir_path = tempfile.mkdtemp()
        os.chmod(temp_dir_path, 444)

        old_dir_path = logutil.LOGFILE_DIR
        old_file_path = logutil.LOGFILE_PATH

        # Set logutil to uninitialized state
        logutil._rhsm_log_handler = None
        logutil._subman_debug_handler = None
        logutil.LOGFILE_DIR = os.path.join(temp_dir_path, "rhsm")
        logutil.LOGFILE_PATH = os.path.join(logutil.LOGFILE_DIR, "rhsm.log")

        self.assertTrue(os.path.exists(temp_dir_path))

        with fixture.Capture() as cap:
            logutil.init_logger()

        self.assertTrue("Further logging output will be written to stderr" in cap.err)

        # init_logger should not be able to automatically create directory /var/log/rhsm,
        # when user does not have access permission for that
        self.assertFalse(os.path.exists(logutil.LOGFILE_DIR))

        os.chmod(temp_dir_path, 744)
        os.rmdir(temp_dir_path)

        logutil.LOGFILE_DIR = old_dir_path
        logutil.LOGFILE_PATH = old_file_path
def main():
    logutil.init_logger()
    log = logging.getLogger('rhsm-app.' + __name__)

    parser = OptionParser(usage=USAGE,
                          formatter=WrappedIndentedHelpFormatter())
    parser.add_option("--autoheal", dest="autoheal", action="store_true",
            default=False, help="perform an autoheal check")
    parser.add_option("--force", dest="force", action="store_true",
            default=False, help=SUPPRESS_HELP)
    parser.add_option(
            "--auto-register", dest="auto_register", action="store_true",
            default=False, help="perform auto-registration"
    )

    (options, args) = parser.parse_args()
    try:
        _main(options, log)
    except SystemExit as se:
        # sys.exit triggers an exception in older Python versions, which
        # in this case  we can safely ignore as we do not want to log the
        # stack trace. We need to check the code, since we want to signal
        # exit with failure to the caller. Otherwise, we will exit with 0
        if se.code:
            sys.exit(-1)
    except Exception as e:
        log.error("Error while updating certificates using daemon")
        print(_('Unable to update entitlement certificates and repositories'))
        log.exception(e)
        sys.exit(-1)
Exemplo n.º 4
0
    def test_missing_user_log_directory(self, MockGetUID):
        """
        Test creating directory for user log directory
        """
        MockGetUID.return_value = 1000

        temp_dir_path = tempfile.mkdtemp()

        old_dir_path = logutil.USER_LOGFILE_DIR
        old_file_path = logutil.USER_LOGFILE_PATH

        # Set logutil to uninitialized state
        logutil._rhsm_log_handler = None
        logutil._subman_debug_handler = None
        logutil.USER_LOGFILE_DIR = temp_dir_path
        logutil.USER_LOGFILE_PATH = os.path.join(temp_dir_path, "rhsm.log")

        self.assertTrue(os.path.exists(temp_dir_path))

        # Simulate deleting of directory $XDG_CACHE_HOME/rhsm
        os.rmdir(temp_dir_path)

        logutil.init_logger()

        # init_logger should automatically create directory $XDG_CACHE_HOME/rhsm,
        # when it doesn't exist
        self.assertTrue(os.path.exists(temp_dir_path))

        os.remove(logutil.USER_LOGFILE_PATH)
        os.rmdir(logutil.USER_LOGFILE_DIR)

        logutil.USER_LOGFILE_DIR = old_dir_path
        logutil.USER_LOGFILE_PATH = old_file_path
Exemplo n.º 5
0
    def func_wrapper(*args, **kwargs):
        global injected
        if not injected:
            logutil.init_logger()

            from subscription_manager.injectioninit import init_dep_injection
            init_dep_injection()
            injected = True
        return func(*args, **kwargs)
Exemplo n.º 6
0
    def test_set_invalid_logger_level(self):
        test_logger_name = 'foobar'
        initial_level = logging.ERROR
        test_logger = logging.getLogger(test_logger_name)
        test_logger.setLevel(initial_level)
        config_level = logging.DEBUG
        self.rhsm_config.set('logging', test_logger_name, config_level)

        logutil.init_logger()

        self.assertNotEqual(test_logger.getEffectiveLevel(), config_level)
Exemplo n.º 7
0
 def test_log_init(self):
     logutil.init_logger()
     sm_logger = logging.getLogger("subscription_manager")
     rhsm_logger = logging.getLogger("rhsm-app")
     sm_effective = sm_logger.getEffectiveLevel()
     rhsm_effective = rhsm_logger.getEffectiveLevel()
     # Fun hack for 2.6/2.7 interoperability
     self.assertTrue(logging.DEBUG == sm_effective
                     or logging._levelNames[sm_effective] == logging.DEBUG)
     self.assertTrue(
         logging.DEBUG == rhsm_effective
         or logging._levelNames[rhsm_effective] == logging.DEBUG)
Exemplo n.º 8
0
    def test_log_init_invalid_default_log_level(self):
        self.rhsm_config.set("logging", "default_log_level", "FOO")

        logutil.init_logger()
        sm_logger = logging.getLogger("subscription_manager")
        rhsm_logger = logging.getLogger("rhsm-app")
        sm_effective = sm_logger.getEffectiveLevel()
        rhsm_effective = rhsm_logger.getEffectiveLevel()
        # Fun hack for 2.6/2.7 interoperability
        self.assertTrue(logging.INFO == sm_effective
                        or logging._levelNames[sm_effective] == logging.INFO)
        self.assertTrue(logging.INFO == rhsm_effective
                        or logging._levelNames[rhsm_effective] == logging.INFO)
Exemplo n.º 9
0
    def test_set_valid_logger_level(self):
        logging_conf = [('subscription_manager.managercli', "ERROR"),
                        ('rhsm', "WARNING"), ('rhsm-app', "CRITICAL")]

        for logger_name, log_level in logging_conf:
            self.rhsm_config.set('logging', logger_name, log_level)

        logutil.init_logger()

        for logger_name, log_level in logging_conf:
            real_log_level = logging.getLogger(logger_name).getEffectiveLevel()
            self.assertTrue(
                logging.getLevelName(log_level) == real_log_level
                or log_level == real_log_level)
Exemplo n.º 10
0
    def test_wrong_rhsm_log_priv(self, MockGetUID):
        """
        Test that error messages are not printed to stderr, when it is not possible
        to print error messages to rhsm.log, during initialization of logger
        """
        MockGetUID.return_value = 0

        # Create temporary log directory
        temp_dir_path = tempfile.mkdtemp()
        # Create temporary log file
        temp_log_file = os.path.join(temp_dir_path, "rhsm.log")
        with open(temp_log_file, "w") as fp:
            fp.write("")
        # Change permission to directory /var/log/rhsm and log file
        os.chmod(temp_log_file, 444)
        os.chmod(temp_dir_path, 444)

        old_dir_path = logutil.LOGFILE_DIR
        old_file_path = logutil.LOGFILE_PATH

        # Set logutil to uninitialized state
        logutil._rhsm_log_handler = None
        logutil._subman_debug_handler = None
        logutil.LOGFILE_DIR = temp_dir_path
        logutil.LOGFILE_PATH = temp_log_file

        with fixture.Capture() as cap:
            logutil.init_logger()

        self.assertTrue(
            "Further logging output will be written to stderr" in cap.err)

        os.chmod(temp_dir_path, 744)
        os.chmod(temp_log_file, 744)
        os.remove(logutil.LOGFILE_PATH)
        os.rmdir(logutil.LOGFILE_DIR)

        logutil.LOGFILE_DIR = old_dir_path
        logutil.LOGFILE_PATH = old_file_path
Exemplo n.º 11
0
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

# hack to allow bytes/strings to be interpolated w/ unicode values (gettext gives us bytes)
# Without this, for example, "Формат: %s\n" % "foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import sys

from subscription_manager.i18n import configure_i18n, ugettext as _
from rhsm import logutil
from rct.cli import RctCLI

configure_i18n()
logutil.init_logger()


def main():
    return RctCLI().main()


if __name__ == "__main__":
    try:
        sys.exit(abs(main() or 0))
    except SystemExit as err:
        # This is a non-exceptional exception thrown by Python 2.4, just
        # re-raise, bypassing handle_exception
        try:
            sys.stdout.flush()
            sys.stderr.flush()
Exemplo n.º 12
0
 def test_do_not_modify_root_logger(self):
     root_handlers = logging.getLogger().handlers
     logutil.init_logger()
     self.assert_items_equals(logging.getLogger().handlers, root_handlers)
Exemplo n.º 13
0
from subscription_manager.ga import GLib
from functools import partial
from rhsmlib.services import config
from rhsm.config import get_config_parser
from rhsmlib.file_monitor import create_filesystem_watcher, DirectoryWatch
from rhsmlib.file_monitor import CONSUMER_WATCHER, ENTITLEMENT_WATCHER, CONFIG_WATCHER, PRODUCT_WATCHER, \
    SYSPURPOSE_WATCHER
from subscription_manager import injection as inj
from rhsm.logutil import init_logger

log = logging.getLogger(__name__)

parser = get_config_parser()
conf = config.Config(parser)

init_logger(parser)


class Server(object):
    """
    Class used for rhsm.service providing D-Bus API
    """

    INSTANCE = None

    def __new__(cls, *args, **kwargs):
        """
        Function called, when new instance of Server is requested
        """
        cls.INSTANCE = object.__new__(cls)
        return cls.INSTANCE