#
# 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')

[((24, 10, 24, 15), 'test_tools.fio.fio.Fio', 'Fio', ({}, {}), '()', False, 'from test_tools.fio.fio import Fio\n'), ((29, 4, 29, 41), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(29, 24, 29, 40): '"""Killing all IO"""'}, {}), "('Killing all IO')", False, 'from core.test_run_utils import TestRun\n'), ((30, 4, 30, 17), 'utils.misc.kill_all_io', 'kill_all_io', ({}, {}), '()', False, 'from utils.misc import kill_all_io\n'), ((36, 4, 36, 43), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(36, 24, 36, 42): '"""Stopping fuzzing"""'}, {}), "('Stopping fuzzing')", False, 'from core.test_run_utils import TestRun\n'), ((37, 4, 37, 112), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', ({(37, 25, 37, 111): '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    )", False, 'from core.test_run_utils import TestRun\n'), ((39, 13, 39, 50), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', ({(39, 34, 39, 49): '"""pgrep iotrace"""'}, {}), "('pgrep iotrace')", False, 'from core.test_run_utils import TestRun\n'), ((43, 4, 43, 51), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(43, 24, 43, 50): '"""Removing existing traces"""'}, {}), "('Removing existing traces')", False, 'from core.test_run_utils import TestRun\n'), ((45, 4, 45, 81), 'core.test_run_utils.TestRun.executor.run_expect_success', 'TestRun.executor.run_expect_success', ({(45, 40, 45, 80): 'f"""rm -rf {trace_repository_path}/kernel"""'}, {}), "(f'rm -rf {trace_repository_path}/kernel')", False, 'from core.test_run_utils import TestRun\n'), ((15, 8, 15, 50), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(15, 28, 15, 49): '"""Installing iotrace:"""'}, {}), "('Installing iotrace:')", False, 'from core.test_run_utils import TestRun\n'), ((16, 8, 16, 25), 'utils.installer.install_iotrace', 'install_iotrace', ({}, {}), '()', False, 'from utils.installer import install_iotrace, check_if_installed\n'), ((18, 8, 18, 76), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(18, 28, 18, 75): '"""iotrace is already installed by previous test"""'}, {}), "('iotrace is already installed by previous test')", False, 'from core.test_run_utils import TestRun\n'), ((26, 8, 26, 45), 'core.test_run_utils.TestRun.LOGGER.info', 'TestRun.LOGGER.info', ({(26, 28, 26, 44): '"""Installing fio"""'}, {}), "('Installing fio')", False, 'from core.test_run_utils import TestRun\n'), ((41, 8, 41, 56), 'core.test_run_utils.TestRun.executor.run', 'TestRun.executor.run', ({(41, 29, 41, 55): 'f"""kill -9 {output.stdout}"""'}, {}), "(f'kill -9 {output.stdout}')", False, 'from core.test_run_utils import TestRun\n'), ((14, 11, 14, 31), 'utils.installer.check_if_installed', 'check_if_installed', ({}, {}), '()', False, 'from utils.installer import install_iotrace, check_if_installed\n')]