# Path: cloudify_agent/tests/daemon.py
# def wait_for_daemon_alive(name, timeout=10):
#     deadline = time.time() + timeout
# 
#     while time.time() < deadline:
#         if _is_agent_alive(name, timeout=5):
#             return
#         logger.info('Waiting for daemon {0} to start...'
#                     .format(name))
#         time.sleep(1)
#     raise RuntimeError('Failed waiting for daemon {0} to start. Waited '
#                        'for {1} seconds'.format(name, timeout))
# 
# def wait_for_daemon_dead(name, timeout=20):
#     deadline = time.time() + timeout
# 
#     while time.time() < deadline:
#         if not _is_agent_alive(name, timeout=5):
#             return
#         logger.info('Waiting for daemon {0} to stop...'
#                     .format(name))
#         time.sleep(1)
#     raise RuntimeError('Failed waiting for daemon {0} to stop. Waited '
#                        'for {1} seconds'.format(name, timeout))
# 
# Path: cloudify_agent/tests/api/pm/shared.py
# def patch_get_source():
# def patch_no_managed_plugin():
# def _test_create(daemon_fixture):
# def _test_create_overwrite(daemon_fixture):
# def _test_start(daemon_fixture):
# def _test_start_delete_amqp_queue(daemon_fixture):
# def _test_start_with_error(daemon_fixture):
# def _test_start_short_timeout(daemon_fixture):
# def _test_status(daemon_fixture):
# def _test_stop(daemon_fixture):
# def _test_stop_short_timeout(daemon_fixture):
# def _test_restart(daemon_fixture):
# def _test_two_daemons(daemon_fixture):
# def _test_conf_env_variables(daemon_fixture):
#     def _get_env_var(var):
#     def _check_env_var(var, expected_value):
# def _test_extra_env(daemon_fixture):
# def _test_execution_env(daemon_fixture):
# def _test_delete_before_stop(daemon_fixture):
# def _test_delete_before_stop_with_force(daemon_fixture):
# def _test_logging(daemon_fixture):
#     def log_and_assert(_message, _deployment_id=None):


import os
import pytest
from cloudify_agent.tests.daemon import (
    wait_for_daemon_alive,
    wait_for_daemon_dead,
)
from cloudify_agent.tests.api.pm import shared
    daemon.delete()
    assert not os.path.exists(daemon.script_path)
    assert not os.path.exists(daemon.config_path)
    assert not os.path.exists(daemon.pid_file)


@pytest.mark.only_rabbit
@pytest.mark.only_posix
def test_cron_respawn(detach_daemon):
    daemon = detach_daemon.create_daemon(cron_respawn=True,
                                         cron_respawn_delay=1)
    daemon.create()
    daemon.configure()
    daemon.start()

    crontab = detach_daemon.runner.run('crontab -l').std_out
    assert daemon.cron_respawn_path in crontab

    wait_for_daemon_alive(daemon.queue)

    # lets kill the process
    detach_daemon.runner.run("pkill -9 -f 'cloudify_agent.worker'")
    wait_for_daemon_dead(daemon.queue)

    # check it was respawned
    # mocking cron - respawn it using the cron respawn script
    detach_daemon.runner.run(daemon.cron_respawn_path)
    wait_for_daemon_alive(daemon.queue)

    daemon.stop()

# Path: cloudify_agent/tests/daemon.py
# def wait_for_daemon_alive(name, timeout=10):
#     deadline = time.time() + timeout
# 
#     while time.time() < deadline:
#         if _is_agent_alive(name, timeout=5):
#             return
#         logger.info('Waiting for daemon {0} to start...'
#                     .format(name))
#         time.sleep(1)
#     raise RuntimeError('Failed waiting for daemon {0} to start. Waited '
#                        'for {1} seconds'.format(name, timeout))
# 
# def wait_for_daemon_dead(name, timeout=20):
#     deadline = time.time() + timeout
# 
#     while time.time() < deadline:
#         if not _is_agent_alive(name, timeout=5):
#             return
#         logger.info('Waiting for daemon {0} to stop...'
#                     .format(name))
#         time.sleep(1)
#     raise RuntimeError('Failed waiting for daemon {0} to stop. Waited '
#                        'for {1} seconds'.format(name, timeout))
# 
# Path: cloudify_agent/tests/api/pm/shared.py
# def patch_get_source():
# def patch_no_managed_plugin():
# def _test_create(daemon_fixture):
# def _test_create_overwrite(daemon_fixture):
# def _test_start(daemon_fixture):
# def _test_start_delete_amqp_queue(daemon_fixture):
# def _test_start_with_error(daemon_fixture):
# def _test_start_short_timeout(daemon_fixture):
# def _test_status(daemon_fixture):
# def _test_stop(daemon_fixture):
# def _test_stop_short_timeout(daemon_fixture):
# def _test_restart(daemon_fixture):
# def _test_two_daemons(daemon_fixture):
# def _test_conf_env_variables(daemon_fixture):
#     def _get_env_var(var):
#     def _check_env_var(var, expected_value):
# def _test_extra_env(daemon_fixture):
# def _test_execution_env(daemon_fixture):
# def _test_delete_before_stop(daemon_fixture):
# def _test_delete_before_stop_with_force(daemon_fixture):
# def _test_logging(daemon_fixture):
#     def log_and_assert(_message, _deployment_id=None):


# Path: cloudify_agent/tests/api/pm/test_detach.py
import os
import pytest
from cloudify_agent.tests.daemon import (
    wait_for_daemon_alive,
    wait_for_daemon_dead,
)
from cloudify_agent.tests.api.pm import shared

    daemon.delete()
    assert not os.path.exists(daemon.script_path)
    assert not os.path.exists(daemon.config_path)
    assert not os.path.exists(daemon.pid_file)


@pytest.mark.only_rabbit
@pytest.mark.only_posix
def test_cron_respawn(detach_daemon):
    daemon = detach_daemon.create_daemon(cron_respawn=True,
                                         cron_respawn_delay=1)
    daemon.create()
    daemon.configure()
    daemon.start()

    crontab = detach_daemon.runner.run('crontab -l').std_out
    assert daemon.cron_respawn_path in crontab

    wait_for_daemon_alive(daemon.queue)

    # lets kill the process
    detach_daemon.runner.run("pkill -9 -f 'cloudify_agent.worker'")
    wait_for_daemon_dead(daemon.queue)

    # check it was respawned
    # mocking cron - respawn it using the cron respawn script
    detach_daemon.runner.run(daemon.cron_respawn_path)
    wait_for_daemon_alive(daemon.queue)

    daemon.stop()

    wait_for_daemon_dead(daemon.queue)