# (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

[((24, 1, 24, 32), 'pytest.fixture', 'pytest.fixture', (), '', False, 'import pytest\n'), ((26, 19, 26, 71), 'os.path.join', 'os.path.join', ({(26, 32, 26, 36): 'HERE', (26, 38, 26, 47): '"""compose"""', (26, 49, 26, 70): '"""docker-compose.yaml"""'}, {}), "(HERE, 'compose', 'docker-compose.yaml')", False, 'import os\n'), ((36, 8, 38, 9), 'datadog_checks.dev.subprocess.run_command', 'run_command', ({(37, 12, 37, 115): "['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'])", False, 'from datadog_checks.dev.subprocess import run_command\n'), ((40, 20, 40, 75), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', ({(40, 36, 40, 48): 'compose_file', (40, 50, 40, 74): '"""Cluster is now healthy"""'}, {}), "(compose_file, 'Cluster is now healthy')", False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n'), ((31, 12, 31, 84), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', (), '', False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n'), ((32, 12, 32, 76), 'datadog_checks.dev.conditions.CheckDockerLogs', 'CheckDockerLogs', ({(32, 28, 32, 40): 'compose_file', (32, 42, 32, 75): '"""Running on http://0.0.0.0:5000/"""'}, {}), "(compose_file, 'Running on http://0.0.0.0:5000/')", False, 'from datadog_checks.dev.conditions import CheckDockerLogs\n')]