<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for ``admin.packaging``.
"""

import errno
from glob import glob
import os
import socket
from subprocess import check_output, CalledProcessError, check_call
from textwrap import dedent
from unittest import skipIf

from twisted.python.filepath import FilePath
from twisted.python.procutils import which
from twisted.python.usage import UsageError
from twisted.trial.unittest import TestCase

from virtualenv import REQUIRED_MODULES as VIRTUALENV_REQUIRED_MODULES

from flocker.testtools import FakeSysModule
from flocker import __version__

from .. import packaging
from ..packaging import (
    omnibus_package_builder, InstallVirtualEnv, InstallApplication,
    BuildPackage, BuildSequence, BuildOptions, BuildScript, DockerBuildOptions,
    DockerBuildScript, GetPackageVersion, DelayedRpmVersion, CreateLinks,
    PythonPackage, create_virtualenv, VirtualEnv, PackageTypes, Distribution,
    Dependency, build_in_docker, DockerBuild, DockerRun,
    PACKAGE, PACKAGE_PYTHON, PACKAGE_CLI, PACKAGE_NODE,
    make_dependencies,
)
from ..release import rpm_version, make_rpm_version

FLOCKER_PATH = FilePath(__file__).parent().parent().parent()

# XXX: Get fpm installed on the build slaves.
# See https://github.com/ClusterHQ/build.clusterhq.com/issues/32
require_fpm = skipIf(not which('fpm'), "Tests require the ``fpm`` command.")
require_rpm = skipIf(not which('rpm'), "Tests require the ``rpm`` command.")
require_rpmlint = skipIf(not which('rpmlint'),
                         "Tests require the ``rpmlint`` command.")
require_dpkg = skipIf(not which('dpkg'), "Tests require the ``dpkg`` command.")
require_lintian = skipIf(not which('lintian'),
                         "Tests require the ``lintian`` command.")

DOCKER_SOCK = '/var/run/docker.sock'


def docker_accessible():
    """
    Attempt to connect to the Docker control socket.

    This may address https://clusterhq.atlassian.net/browse/FLOC-85 and could
    perhaps be moved to ``flocker.node.testtools``.

    :return: ``True`` if the current user has permission to connect, else
        ``False``.
    """
    try:
        socket.socket(family=socket.AF_UNIX).connect(DOCKER_SOCK)
    except socket.error as e:
        if e.errno == errno.EACCES:
            return False
        raise
    else:
        return True

require_docker_access = skipIf(
    not docker_accessible(),
    "User '{}' does not have permission "
    "to access the Docker server socket '{}'".format(os.getlogin(), DOCKER_SOCK)
)

def assert_equal_steps(test_case, expected, actual):
    """
    Assert that the list of provided steps are the same.
    If they are not, display the differences intelligently.

    :param test_case: The ``TestCase`` whose assert methods will be called.
    :param expected: The expected build step instance.<commit_msg>oops. getlogin won't work if there's no terminal<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for ``admin.packaging``.
"""

import errno
from glob import glob
import os
import pwd
import socket
from subprocess import check_output, CalledProcessError, check_call
from textwrap import dedent
from unittest import skipIf

from twisted.python.filepath import FilePath
from twisted.python.procutils import which
from twisted.python.usage import UsageError
from twisted.trial.unittest import TestCase

from virtualenv import REQUIRED_MODULES as VIRTUALENV_REQUIRED_MODULES

from flocker.testtools import FakeSysModule
from flocker import __version__

from .. import packaging
from ..packaging import (
    omnibus_package_builder, InstallVirtualEnv, InstallApplication,
    BuildPackage, BuildSequence, BuildOptions, BuildScript, DockerBuildOptions,
    DockerBuildScript, GetPackageVersion, DelayedRpmVersion, CreateLinks,
    PythonPackage, create_virtualenv, VirtualEnv, PackageTypes, Distribution,
    Dependency, build_in_docker, DockerBuild, DockerRun,
    PACKAGE, PACKAGE_PYTHON, PACKAGE_CLI, PACKAGE_NODE,
    make_dependencies,
)
from ..release import rpm_version, make_rpm_version

FLOCKER_PATH = FilePath(__file__).parent().parent().parent()

# XXX: Get fpm installed on the build slaves.
# See https://github.com/ClusterHQ/build.clusterhq.com/issues/32
require_fpm = skipIf(not which('fpm'), "Tests require the ``fpm`` command.")
require_rpm = skipIf(not which('rpm'), "Tests require the ``rpm`` command.")
require_rpmlint = skipIf(not which('rpmlint'),
                         "Tests require the ``rpmlint`` command.")
require_dpkg = skipIf(not which('dpkg'), "Tests require the ``dpkg`` command.")
require_lintian = skipIf(not which('lintian'),
                         "Tests require the ``lintian`` command.")

DOCKER_SOCK = '/var/run/docker.sock'


def docker_accessible():
    """
    Attempt to connect to the Docker control socket.

    This may address https://clusterhq.atlassian.net/browse/FLOC-85 and could
    perhaps be moved to ``flocker.node.testtools``.

    :return: ``True`` if the current user has permission to connect, else
        ``False``.
    """
    try:
        socket.socket(family=socket.AF_UNIX).connect(DOCKER_SOCK)
    except socket.error as e:
        if e.errno == errno.EACCES:
            return False
        raise
    else:
        return True

require_docker_access = skipIf(
    not docker_accessible(),
    "User '{}' does not have permission "
    "to access the Docker server socket '{}'".format(
        pwd.getpwuid(os.getuid())[0], DOCKER_SOCK)
)

def assert_equal_steps(test_case, expected, actual):
    """
    Assert that the list of provided steps are the same.
    If they are not, display the differences intelligently.

    :param test_case: The ``TestCase`` whose assert methods will be called.
    :param expected: The expected build step instance.
{'fraction_non_alphanumeric': 0.09461775797051765, 'fraction_numerical': 0.0015426808364758314, 'mean_word_length': 4.0871839581517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292011', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1655, 'n_words': 533}