diff --git a/tests/conftest.py b/tests/conftest.py index 1b162cd5dc6..c3757c41473 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,14 +1,11 @@ import fileinput -import json import logging import os import resource import shutil -import stat import sys from argparse import ArgumentParser from pathlib import Path -from textwrap import dedent from typing import List, Optional from unittest.mock import MagicMock @@ -485,61 +482,6 @@ def __init__(self, *args, **kwargs): monkeypatch.setattr("ert.cli.main.EvaluatorServerConfig", MockESConfig) -QSTAT_HEADER = ( - "Job id Name User Time Use S Queue\n" - "----------------------------- --------------- --------------- -------- - ---------------\n" -) -QSTAT_HEADER_FORMAT = "%-30s %-15s %-15s %-8s %-1s %-5s" - - -@pytest.fixture -def create_mock_flaky_qstat(monkeypatch, tmp_path): - bin_path = tmp_path / "bin" - bin_path.mkdir() - monkeypatch.chdir(bin_path) - monkeypatch.setenv("PATH", f"{bin_path}:{os.environ['PATH']}") - yield _mock_flaky_qstat - - -def _mock_flaky_qstat(error_message_to_output: str): - qsub_path = Path("qsub") - qsub_path.write_text("#!/bin/sh\necho '1'") - qsub_path.chmod(qsub_path.stat().st_mode | stat.S_IEXEC) - qstat_path = Path("qstat") - qstat_path.write_text( - "#!/bin/sh" - + dedent( - f""" - count=0 - if [ -f counter_file ]; then - count=$(cat counter_file) - fi - echo "$((count+1))" > counter_file - if [ $count -ge 3 ]; then - json_flag_set=false; - while [ "$#" -gt 0 ]; do - case "$1" in - -Fjson) - json_flag_set=true - ;; - esac - shift - done - if [ "$json_flag_set" = true ]; then - echo '{json.dumps({"Jobs": {"1": {"Job_Name": "1", "job_state": "E", "Exit_status": "0"}}})}' - else - echo "{QSTAT_HEADER}"; printf "{QSTAT_HEADER_FORMAT}" 1 foo someuser 0 E normal - fi - else - echo "{error_message_to_output}" >&2 - exit 2 - fi - """ - ) - ) - qstat_path.chmod(qstat_path.stat().st_mode | stat.S_IEXEC) - - @pytest.fixture def run_paths(): def func(ert_config: ErtConfig): diff --git a/tests/unit_tests/scheduler/test_openpbs_driver.py b/tests/unit_tests/scheduler/test_openpbs_driver.py index 31e7e3f32cc..23977a92f37 100644 --- a/tests/unit_tests/scheduler/test_openpbs_driver.py +++ b/tests/unit_tests/scheduler/test_openpbs_driver.py @@ -1,5 +1,6 @@ import asyncio import contextlib +import json import logging import os import shlex @@ -31,7 +32,6 @@ _create_job_class, _parse_jobs_dict, ) -from tests.conftest import QSTAT_HEADER, QSTAT_HEADER_FORMAT from tests.ui_tests.cli.run_cli import run_cli from tests.utils import poll @@ -40,6 +40,13 @@ pytestmark = pytest.mark.xdist_group("openpbs") +QSTAT_HEADER = ( + "Job id Name User Time Use S Queue\n" + "----------------------------- --------------- --------------- -------- - ---------------\n" +) +QSTAT_HEADER_FORMAT = "%-30s %-15s %-15s %-8s %-1s %-5s" + + @given(st.lists(st.sampled_from(JOB_STATES))) async def test_events_produced_from_jobstate_updates(jobstate_sequence: List[str]): # Determine what to expect from the sequence: @@ -507,6 +514,54 @@ async def test_keep_qsub_output(value: bool): ) +@pytest.fixture +def create_mock_flaky_qstat(monkeypatch, tmp_path): + bin_path = tmp_path / "bin" + bin_path.mkdir() + monkeypatch.chdir(bin_path) + monkeypatch.setenv("PATH", f"{bin_path}:{os.environ['PATH']}") + yield _mock_flaky_qstat + + +def _mock_flaky_qstat(error_message_to_output: str): + qsub_path = Path("qsub") + qsub_path.write_text("#!/bin/sh\necho '1'") + qsub_path.chmod(qsub_path.stat().st_mode | stat.S_IEXEC) + qstat_path = Path("qstat") + qstat_path.write_text( + "#!/bin/sh" + + dedent( + f""" + count=0 + if [ -f counter_file ]; then + count=$(cat counter_file) + fi + echo "$((count+1))" > counter_file + if [ $count -ge 3 ]; then + json_flag_set=false; + while [ "$#" -gt 0 ]; do + case "$1" in + -Fjson) + json_flag_set=true + ;; + esac + shift + done + if [ "$json_flag_set" = true ]; then + echo '{json.dumps({"Jobs": {"1": {"Job_Name": "1", "job_state": "E", "Exit_status": "0"}}})}' + else + echo "{QSTAT_HEADER}"; printf "{QSTAT_HEADER_FORMAT}" 1 foo someuser 0 E normal + fi + else + echo "{error_message_to_output}" >&2 + exit 2 + fi + """ + ) + ) + qstat_path.chmod(qstat_path.stat().st_mode | stat.S_IEXEC) + + @pytest.mark.parametrize( "text_to_ignore", [