Skip to content
Snippets Groups Projects
test_default.py 4.14 KiB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
import os
Eliot Berriot's avatar
Eliot Berriot committed

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")
Eliot Berriot's avatar
Eliot Berriot committed


@pytest.mark.parametrize("service", ["redis-server", "postgresql", "nginx"])
def test_installed_services(host, service):
    service = host.service(service)
    assert service.is_running
    assert service.is_enabled


def test_database_created(host):
    cmd = """
        sudo -u postgres psql -A -t -c \
            "SELECT 1 FROM pg_catalog.pg_user u WHERE u.usename =  'funkwhale';"
    """
    result = host.run(cmd)
    assert result.stdout == "1"


def test_database_user_created(host):
    cmd = """
        sudo -u postgres psql -A -t -c "SELECT 1 FROM pg_database WHERE datname = 'funkwhale';"
    """
    result = host.run(cmd)
    assert result.stdout == "1"


def test_funkwhale_user_creation(host):
    user = host.user("funkwhale")
    assert user.home == "/srv/funkwhale"
    assert user.shell == "/bin/false"


@pytest.mark.parametrize(
    "path",
    [
        "/srv/funkwhale/",
        "/srv/funkwhale/data/media",
        "/srv/funkwhale/data/static",
        "/srv/funkwhale/data/music",
    ],
)
def test_funkwhale_directories_creation(path, host):
    dir = host.file(path)

    assert dir.exists is True
    assert dir.is_directory is True


def test_funkwhale_env_file(host):
Eliot Berriot's avatar
Eliot Berriot committed
    secret_key = host.file("/srv/funkwhale/config/django_secret_key").content.decode()
    assert len(secret_key) > 0
    f = host.file("/srv/funkwhale/config/.env")
Eliot Berriot's avatar
Eliot Berriot committed
    env_content = f.content.decode()
    assert "MEDIA_ROOT=/srv/funkwhale/data/media" in env_content
    assert "STATIC_ROOT=/srv/funkwhale/data/static" in env_content
    assert "MUSIC_DIRECTORY_PATH=/srv/funkwhale/data/music" in env_content
    assert "MUSIC_DIRECTORY_SERVE_PATH=/srv/funkwhale/data/music" in env_content
    assert "FUNKWHALE_HOSTNAME=yourdomain.funkwhale" in env_content
    assert "FUNKWHALE_PROTOCOL=https" in env_content
    assert "DJANGO_SECRET_KEY={}".format(secret_key) in env_content
    assert "FUNKWHALE_API_IP=127.0.0.1" in env_content
    assert "FUNKWHALE_API_PORT=5000" in env_content
    assert "REVERSE_PROXY_TYPE=nginx" in env_content
    assert "DATABASE_URL=postgresql://funkwhale@:5432/funkwhale" in env_content
    assert "CACHE_URL=redis://127.0.0.1:6379/0" in env_content
    assert "EMAIL_CONFIG=smtp+tls://user@:password@youremail.host:587" in env_content
    assert "DEFAULT_FROM_EMAIL=noreply@yourdomain" in env_content
    assert "FUNKWHALE_FRONTEND_PATH=/srv/funkwhale/front/dist" in env_content
    assert "FUNKWHALE_SPA_HTML_ROOT=/srv/funkwhale/front/dist/index.html" in env_content
    assert "NGINX_MAX_BODY_SIZE=100M" in env_content
    assert "DJANGO_SETTINGS_MODULE=config.settings.production" in env_content

    # additional vars
Eliot Berriot's avatar
Eliot Berriot committed
    assert "ADDITIONAL_VAR=1" in env_content
    assert "ADDITIONAL_VAR=2" in env_content


def test_frontend_download(host):
    f = host.file("/srv/funkwhale/front/dist/index.html")

    assert f.exists is True


def test_api_download(host):
    f = host.file("/srv/funkwhale/api/funkwhale_api/__init__.py")

    assert f.exists is True
    assert f.contains('__version__ = "0.19.0-rc2"') is True
Eliot Berriot's avatar
Eliot Berriot committed


def test_virtualenv(host):
    expected_packages = {"Django", "djangorestframework", "celery"}
    packages = host.pip_package.get_packages(
        pip_path="/srv/funkwhale/virtualenv/bin/pip"
    )
    names = set(packages.keys())

    intersection = expected_packages & names
    assert intersection == expected_packages


def test_static_files_copied(host):
    f = host.file("/srv/funkwhale/data/static/admin/css/base.css")

    assert f.exists is True


def test_migrations_applied(host):
    cmd = """
        sudo -u postgres psql funkwhale -A -t -c "SELECT 1 from django_migrations where app = 'music' and name = '0039_auto_20190423_0820';"
    """
    result = host.run(cmd)
    assert result.stdout == "1"
Eliot Berriot's avatar
Eliot Berriot committed


@pytest.mark.parametrize(
    "service",
    ["funkwhale-server", "funkwhale-worker", "funkwhale-beat", "funkwhale.target"],
)
def test_funkwhale_services(service, host):
    service = host.service(service)
    assert service.is_running
    assert service.is_enabled