Skip to content

Commit

Permalink
Move _create_flaky_qstat to where it is used
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Sep 17, 2024
1 parent 8640476 commit 2dafc86
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 59 deletions.
58 changes: 0 additions & 58 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down
57 changes: 56 additions & 1 deletion tests/unit_tests/scheduler/test_openpbs_driver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import contextlib
import json
import logging
import os
import shlex
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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",
[
Expand Down

0 comments on commit 2dafc86

Please sign in to comment.