Exemplo n.º 1
0
def test_recursion_ko(tmp_path, capsys):
    flag1 = tmp_path.joinpath("flag1")
    flag2 = tmp_path.joinpath("flag2")

    # here the first test will succeed but the second one will fail
    # so while the fix for the second test runs (1 second), the first test will be executed again 0.4 second later
    # but then it will fail, and the fix won't actually fix anything, so everything shuts down with an exception
    with pytest.raises(ChildProcessError):
        heal.test_and_fix(
            [
                {
                    "test": f"[ ! -f {flag1} ] && touch {flag1}",
                    "fix": "true",
                    "order": 6
                },  # succeeds then fails, but won't fix
                {
                    "test": f"[ -f {flag2} ]",
                    "fix": f"sleep 1 && touch {flag2}",
                    "order": 11
                }
            ],
            print_status,
            0.4)

    assert capsys.readouterr().out == RECURSION_KO_OUTPUT.format(flag1, flag2)
Exemplo n.º 2
0
def test_double_recursion_ok(tmp_path, capsys):
    log = tmp_path.joinpath("log")
    flag1 = tmp_path.joinpath("flag1")
    flag2 = tmp_path.joinpath("flag2")

    # here the first two tests will succeed, but not the third
    # so while the fix for the third test runs (1 second), the first two tests will be executed again 0.4 seconds later
    # but then only the first one will succeed, the second one will fail
    # so while the fix for the second test runs (1 second), the first one will be executed two more times
    heal.test_and_fix(
        [
            {
                "test": f"echo ping >> {log}"
            },  # succeeds everytime
            {
                "test": f"[ ! -f {flag1} ] && touch {flag1}",
                "fix": f"sleep 1 && rm {flag1}",
                "order": 7
            },  # succeeds then fails
            {
                "test": f"[ -f {flag2} ]",
                "fix": f"sleep 1 && touch {flag2}",
                "order": 9
            }  # fails every time but last
        ],
        print_status,
        0.4)

    assert capsys.readouterr().out == DOUBLE_RECURSION_OK_OUTPUT.format(
        flag1, flag2)
    assert log.read_text() == DOUBLE_RECURSION_OK_LOG
Exemplo n.º 3
0
def test_ko(capsys):
    with pytest.raises(ChildProcessError):
        heal.test_and_fix([{
            "test": "echo doomed && false",
            "fix": "false",
            "order": 5
        }], print_status)
    assert capsys.readouterr().out == KO_OUTPUT
Exemplo n.º 4
0
def test_ok(capsys):
    heal.test_and_fix([{
        "test": "echo one"
    }, {
        "test": "echo two"
    }], print_status)
    assert capsys.readouterr(
    ).out == "status: ok\n"  # output is not shown for checks that succeed
Exemplo n.º 5
0
def test_recursion_ok(tmp_path, capsys):
    log = tmp_path.joinpath("log")
    flag = tmp_path.joinpath("flag")

    # here the first test will be done once, then the second test will fail
    # while the fix for the second test runs (1 second), the previous successful test will run twice since the delay is set to 0.4 second
    heal.test_and_fix([{
        "test": f"echo ping >> {log}"
    }, {
        "test": f"[ -f {flag} ]",
        "fix": f"sleep 1 && touch {flag}",
        "order": 10
    }], print_status, 0.4)

    assert capsys.readouterr().out == RECURSION_OK_OUTPUT.format(flag)
    assert log.read_text() == RECURSION_OK_LOG
Exemplo n.º 6
0
def test_fix_with_progressive_output(tmp_path, capsys):
    flag = tmp_path.joinpath("flag")

    # here we run test_and_fix() in a thread to be able to capture the output along the way
    thread = threading.Thread(target=lambda: heal.test_and_fix([{
        "test": f"echo output of the failed test && [ -f {flag} ]",
        "fix": f"echo one && sleep 1 && echo two && touch {flag}",
        "order": 10
    }], print_status))
    thread.start()

    time.sleep(0.1)
    assert capsys.readouterr().out == FIX_WITH_PROGRESSIVE_OUTPUT_1.format(
        flag)

    thread.join()
    assert capsys.readouterr().out == FIX_WITH_PROGRESSIVE_OUTPUT_2