# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

import os

import pytest

from datadog_checks.dev import docker_run
from datadog_checks.dev.conditions import CheckDockerLogs
from datadog_checks.dev.subprocess import run_command

from .common import BASIC_CONFIG, HERE

E2E_METADATA = {
    'start_commands': [
        'apt-get update',
        'apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y docker.io',
    ],
    'docker_volumes': ['/var/run/docker.sock:/var/run/docker.sock'],
}


@pytest.fixture(scope="session")
def dd_environment():
    compose_file = os.path.join(HERE, 'compose', 'docker-compose.yaml')
    # We need a custom condition to wait a bit longer
    with docker_run(
        compose_file=compose_file,
        conditions=[
            CheckDockerLogs(compose_file, 'spawning ceph --cluster ceph -w', wait=5),
            CheckDockerLogs(compose_file, 'Running on http://0.0.0.0:5000/'),
        ],
    ):
        # Clean the disk space warning
        run_command(
            ['docker', 'exec', 'dd-test-ceph', 'ceph', 'tell', 'mon.*', 'injectargs', '--mon_data_avail_warn', '5']
        )
        # Wait a bit for the change to take effect
        condition = CheckDockerLogs(compose_file, 'Cluster is now healthy')
        condition()
        yield BASIC_CONFIG, E2E_METADATA

[((599, 630), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""'}), "(scope='session')\n", (613, 630), False, 'import pytest\n'), ((672, 724), 'os.path.join', 'os.path.join', (['HERE', '"""compose"""', '"""docker-compose.yaml"""'], {}), "(HERE, 'compose', 'docker-compose.yaml')\n", (684, 724), False, 'import os\n'), ((1085, 1205), 'datadog_checks.dev.subprocess.run_command', 'run_command', (["['docker', 'exec', 'dd-test-ceph', 'ceph', 'tell', 'mon.*', 'injectargs',\n    '--mon_data_avail_warn', '5']"], {}), "(['docker', 'exec', 'dd-test-ceph', 'ceph', 'tell', 'mon.*',\n    'injectargs', '--mon_data_avail_warn', '5'])\n", (1096, 1205), False, 'from datadog_checks.dev.subprocess import run_command\n'), ((1295, 1350), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', (['compose_file', '"""Cluster is now healthy"""'], {}), "(compose_file, 'Cluster is now healthy')\n", (1310, 1350), False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n'), ((868, 940), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', (['compose_file', '"""spawning ceph --cluster ceph -w"""'], {'wait': '(5)'}), "(compose_file, 'spawning ceph --cluster ceph -w', wait=5)\n", (883, 940), False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n'), ((954, 1018), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', (['compose_file', '"""Running on http://0.0.0.0:5000/"""'], {}), "(compose_file, 'Running on http://0.0.0.0:5000/')\n", (969, 1018), False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n')]