#
# Copyright(c) 2020 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause-Clear
#

from core.test_run_utils import TestRun
from utils.installer import install_iotrace, check_if_installed
from utils.iotrace import IotracePlugin
from utils.misc import kill_all_io
from test_tools.fio.fio import Fio


def dut_prepare(reinstall: bool):
    if not check_if_installed() or reinstall:
        TestRun.LOGGER.info("Installing iotrace:")
        install_iotrace()
    else:
        TestRun.LOGGER.info("iotrace is already installed by previous test")

    # Call it after installing iotrace because we need iotrace
    # to get valid paths
    dut_cleanup()

    fio = Fio()
    if not fio.is_installed():
        TestRun.LOGGER.info("Installing fio")
        fio.install()

    TestRun.LOGGER.info("Killing all IO")
    kill_all_io()


def dut_cleanup():
    iotrace: IotracePlugin = TestRun.plugins['iotrace']

    TestRun.LOGGER.info("Stopping fuzzing")
    TestRun.executor.run(f'{iotrace.working_dir}/standalone-linux-io-tracer/tests/security/fuzzy/fuzz.sh clean')

    output = TestRun.executor.run('pgrep iotrace')
    if output.stdout != "":
        TestRun.executor.run(f'kill -9 {output.stdout}')

    TestRun.LOGGER.info("Removing existing traces")
    trace_repository_path: str = iotrace.get_trace_repository_path()
    TestRun.executor.run_expect_success(f'rm -rf {trace_repository_path}/kernel')

[((667, 672), 'test_tools.fio.fio.Fio', 'Fio', ([], {}), '()\n', (670, 672), False, 'from test_tools.fio.fio import Fio\n'), ((777, 814), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""Killing all IO"""'], {}), "('Killing all IO')\n", (796, 814), False, 'from core.test_run_utils import TestRun\n'), ((819, 832), 'utils.misc.kill_all_io', 'kill_all_io', ([], {}), '()\n', (830, 832), False, 'from utils.misc import kill_all_io\n'), ((915, 954), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""Stopping fuzzing"""'], {}), "('Stopping fuzzing')\n", (934, 954), False, 'from core.test_run_utils import TestRun\n'), ((959, 1077), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', (['f"""{iotrace.working_dir}/standalone-linux-io-tracer/tests/security/fuzzy/fuzz.sh clean"""'], {}), "(\n    f'{iotrace.working_dir}/standalone-linux-io-tracer/tests/security/fuzzy/fuzz.sh clean'\n    )\n", (979, 1077), False, 'from core.test_run_utils import TestRun\n'), ((1082, 1119), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', (['"""pgrep iotrace"""'], {}), "('pgrep iotrace')\n", (1102, 1119), False, 'from core.test_run_utils import TestRun\n'), ((1210, 1257), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""Removing existing traces"""'], {}), "('Removing existing traces')\n", (1229, 1257), False, 'from core.test_run_utils import TestRun\n'), ((1331, 1408), 'core.test_run_utils.TestRun.executor.run_expect_success', 'TestRun.executor.run_expect_success', (['f"""rm -rf {trace_repository_path}/kernel"""'], {}), "(f'rm -rf {trace_repository_path}/kernel')\n", (1366, 1408), False, 'from core.test_run_utils import TestRun\n'), ((393, 435), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""Installing iotrace:"""'], {}), "('Installing iotrace:')\n", (412, 435), False, 'from core.test_run_utils import TestRun\n'), ((444, 461), 'utils.installer.install_iotrace', 'install_iotrace', ([], {}), '()\n', (459, 461), False, 'from utils.installer import install_iotrace, check_if_installed\n'), ((480, 548), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""iotrace is already installed by previous test"""'], {}), "('iotrace is already installed by previous test')\n", (499, 548), False, 'from core.test_run_utils import TestRun\n'), ((712, 749), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', (['"""Installing fio"""'], {}), "('Installing fio')\n", (731, 749), False, 'from core.test_run_utils import TestRun\n'), ((1156, 1204), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', (['f"""kill -9 {output.stdout}"""'], {}), "(f'kill -9 {output.stdout}')\n", (1176, 1204), False, 'from core.test_run_utils import TestRun\n'), ((350, 370), 'utils.installer.check_if_installed', 'check_if_installed', ([], {}), '()\n', (368, 370), False, 'from utils.installer import install_iotrace, check_if_installed\n')]