# Path: ceph_deploy/cli.py
# @catches((KeyboardInterrupt, RuntimeError, exc.DeployError,), handle_all=True)
# def _main(args=None, namespace=None):
#     # Set console logging first with some defaults, to prevent having exceptions
#     # before hitting logging configuration. The defaults can/will get overridden
#     # later.
# 
#     # Console Logger
#     sh = logging.StreamHandler()
#     sh.setFormatter(log.color_format())
#     sh.setLevel(logging.WARNING)
# 
#     # because we're in a module already, __name__ is not the ancestor of
#     # the rest of the package; use the root as the logger for everyone
#     root_logger = logging.getLogger()
# 
#     # allow all levels at root_logger, handlers control individual levels
#     root_logger.setLevel(logging.DEBUG)
#     root_logger.addHandler(sh)
# 
#     parser = get_parser()
#     if len(sys.argv) < 2:
#         parser.print_help()
#         sys.exit()
#     else:
#         args = parser.parse_args(args=args, namespace=namespace)
# 
#     console_loglevel = logging.DEBUG  # start at DEBUG for now
#     if args.quiet:
#         console_loglevel = logging.WARNING
#     if args.verbose:
#         console_loglevel = logging.DEBUG
# 
#     # Console Logger
#     sh.setLevel(console_loglevel)
# 
#     # File Logger
#     fh = logging.FileHandler('ceph-deploy-{cluster}.log'.format(cluster=args.cluster))
#     fh.setLevel(logging.DEBUG)
#     fh.setFormatter(logging.Formatter(log.FILE_FORMAT))
# 
#     root_logger.addHandler(fh)
# 
#     # Reads from the config file and sets values for the global
#     # flags and the given sub-command
#     # the one flag that will never work regardless of the config settings is
#     # logging because we cannot set it before hand since the logging config is
#     # not ready yet. This is the earliest we can do.
#     args = ceph_deploy.conf.cephdeploy.set_overrides(args)
# 
#     LOG.info("Invoked (%s): %s" % (
#         ceph_deploy.__version__,
#         ' '.join(sys.argv))
#     )
#     log_flags(args)
# 
#     return args.func(args)
# 
# Path: ceph_deploy/hosts/remotes.py
# def platform_information(_linux_distribution=None):
# def parse_os_release(release_path='/etc/os-release'):
# def machine_type():
# def write_sources_list(url, codename, filename='ceph.list', mode=0o644):
# def write_sources_list_content(content, filename='ceph.list', mode=0o644):
# def write_yum_repo(content, filename='ceph.repo'):
# def set_apt_priority(fqdn, path='/etc/apt/preferences.d/ceph.pref'):
# def set_repo_priority(sections, path='/etc/yum.repos.d/ceph.repo', priority='1'):
#     def remove_whitespace_from_assignments():
# def write_conf(cluster, conf, overwrite):
# def write_keyring(path, key, uid=-1, gid=-1):
# def create_mon_path(path, uid=-1, gid=-1):
# def create_done_path(done_path, uid=-1, gid=-1):
# def create_init_path(init_path, uid=-1, gid=-1):
# def append_to_file(file_path, contents):
# def path_getuid(path):
# def path_getgid(path):
# def readline(path):
# def path_exists(path):
# def get_realpath(path):
# def listdir(path):
# def makedir(path, ignored=None, uid=-1, gid=-1):
# def unlink(_file):
# def write_monitor_keyring(keyring, monitor_keyring, uid=-1, gid=-1):
# def write_file(path, content, mode=0o644, directory=None, uid=-1, gid=-1):
# def touch_file(path):
# def get_file(path):
# def object_grep(term, file_object):
# def grep(term, file_path):
# def shortname():
# def which_service():
# def which(executable):
# def make_mon_removed_dir(path, file_name):
# def safe_mkdir(path, uid=-1, gid=-1):
# def safe_makedirs(path, uid=-1, gid=-1):
# def zeroing(dev):
# def enable_yum_priority_obsoletes(path="/etc/yum/pluginconf.d/priorities.conf"):


import os
import subprocess
import pytest
from mock import patch, MagicMock, Mock
from ceph_deploy.cli import _main as main
from ceph_deploy.hosts import remotes
from ceph_deploy.tests.directory import directory
            result = p.stderr.read().decode('utf-8')
    assert 'No such file or directory: \'ceph.conf\'' in result
    assert err.value.status == 1


def test_bad_no_key(tmpdir, cli):
    with tmpdir.join('ceph.conf').open('w'):
        pass
    with pytest.raises(cli.Failed) as err:
        with cli(
            args=['ceph-deploy', 'admin', 'host1'],
            stderr=subprocess.PIPE,
            ) as p:
            result = p.stderr.read().decode('utf-8')
    assert 'ceph.client.admin.keyring not found' in result
    assert err.value.status == 1


def test_write_keyring(tmpdir):
    with tmpdir.join('ceph.conf').open('w'):
        pass
    with tmpdir.join('ceph.client.admin.keyring').open('wb'):
        pass

    etc_ceph = os.path.join(str(tmpdir), 'etc', 'ceph')
    os.makedirs(etc_ceph)

    distro = MagicMock()
    distro.conn = MagicMock()
    remotes.write_file.__defaults__ = (0o644, str(tmpdir), -1, -1)

# Path: ceph_deploy/cli.py
# @catches((KeyboardInterrupt, RuntimeError, exc.DeployError,), handle_all=True)
# def _main(args=None, namespace=None):
#     # Set console logging first with some defaults, to prevent having exceptions
#     # before hitting logging configuration. The defaults can/will get overridden
#     # later.
# 
#     # Console Logger
#     sh = logging.StreamHandler()
#     sh.setFormatter(log.color_format())
#     sh.setLevel(logging.WARNING)
# 
#     # because we're in a module already, __name__ is not the ancestor of
#     # the rest of the package; use the root as the logger for everyone
#     root_logger = logging.getLogger()
# 
#     # allow all levels at root_logger, handlers control individual levels
#     root_logger.setLevel(logging.DEBUG)
#     root_logger.addHandler(sh)
# 
#     parser = get_parser()
#     if len(sys.argv) < 2:
#         parser.print_help()
#         sys.exit()
#     else:
#         args = parser.parse_args(args=args, namespace=namespace)
# 
#     console_loglevel = logging.DEBUG  # start at DEBUG for now
#     if args.quiet:
#         console_loglevel = logging.WARNING
#     if args.verbose:
#         console_loglevel = logging.DEBUG
# 
#     # Console Logger
#     sh.setLevel(console_loglevel)
# 
#     # File Logger
#     fh = logging.FileHandler('ceph-deploy-{cluster}.log'.format(cluster=args.cluster))
#     fh.setLevel(logging.DEBUG)
#     fh.setFormatter(logging.Formatter(log.FILE_FORMAT))
# 
#     root_logger.addHandler(fh)
# 
#     # Reads from the config file and sets values for the global
#     # flags and the given sub-command
#     # the one flag that will never work regardless of the config settings is
#     # logging because we cannot set it before hand since the logging config is
#     # not ready yet. This is the earliest we can do.
#     args = ceph_deploy.conf.cephdeploy.set_overrides(args)
# 
#     LOG.info("Invoked (%s): %s" % (
#         ceph_deploy.__version__,
#         ' '.join(sys.argv))
#     )
#     log_flags(args)
# 
#     return args.func(args)
# 
# Path: ceph_deploy/hosts/remotes.py
# def platform_information(_linux_distribution=None):
# def parse_os_release(release_path='/etc/os-release'):
# def machine_type():
# def write_sources_list(url, codename, filename='ceph.list', mode=0o644):
# def write_sources_list_content(content, filename='ceph.list', mode=0o644):
# def write_yum_repo(content, filename='ceph.repo'):
# def set_apt_priority(fqdn, path='/etc/apt/preferences.d/ceph.pref'):
# def set_repo_priority(sections, path='/etc/yum.repos.d/ceph.repo', priority='1'):
#     def remove_whitespace_from_assignments():
# def write_conf(cluster, conf, overwrite):
# def write_keyring(path, key, uid=-1, gid=-1):
# def create_mon_path(path, uid=-1, gid=-1):
# def create_done_path(done_path, uid=-1, gid=-1):
# def create_init_path(init_path, uid=-1, gid=-1):
# def append_to_file(file_path, contents):
# def path_getuid(path):
# def path_getgid(path):
# def readline(path):
# def path_exists(path):
# def get_realpath(path):
# def listdir(path):
# def makedir(path, ignored=None, uid=-1, gid=-1):
# def unlink(_file):
# def write_monitor_keyring(keyring, monitor_keyring, uid=-1, gid=-1):
# def write_file(path, content, mode=0o644, directory=None, uid=-1, gid=-1):
# def touch_file(path):
# def get_file(path):
# def object_grep(term, file_object):
# def grep(term, file_path):
# def shortname():
# def which_service():
# def which(executable):
# def make_mon_removed_dir(path, file_name):
# def safe_mkdir(path, uid=-1, gid=-1):
# def safe_makedirs(path, uid=-1, gid=-1):
# def zeroing(dev):
# def enable_yum_priority_obsoletes(path="/etc/yum/pluginconf.d/priorities.conf"):


# Path: ceph_deploy/tests/test_cli_admin.py
import os
import subprocess
import pytest
from mock import patch, MagicMock, Mock
from ceph_deploy.cli import _main as main
from ceph_deploy.hosts import remotes
from ceph_deploy.tests.directory import directory

            result = p.stderr.read().decode('utf-8')
    assert 'No such file or directory: \'ceph.conf\'' in result
    assert err.value.status == 1


def test_bad_no_key(tmpdir, cli):
    with tmpdir.join('ceph.conf').open('w'):
        pass
    with pytest.raises(cli.Failed) as err:
        with cli(
            args=['ceph-deploy', 'admin', 'host1'],
            stderr=subprocess.PIPE,
            ) as p:
            result = p.stderr.read().decode('utf-8')
    assert 'ceph.client.admin.keyring not found' in result
    assert err.value.status == 1


def test_write_keyring(tmpdir):
    with tmpdir.join('ceph.conf').open('w'):
        pass
    with tmpdir.join('ceph.client.admin.keyring').open('wb'):
        pass

    etc_ceph = os.path.join(str(tmpdir), 'etc', 'ceph')
    os.makedirs(etc_ceph)

    distro = MagicMock()
    distro.conn = MagicMock()
    remotes.write_file.__defaults__ = (0o644, str(tmpdir), -1, -1)

    distro.conn.remote_module = remotes