Skip to content

Commit

Permalink
remove duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
MehmedGIT committed Feb 12, 2024
1 parent eb0da74 commit 79c5b79
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 88 deletions.
41 changes: 7 additions & 34 deletions tests/network/test_ocrd_all_workflow.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
from time import sleep
from requests import get, post
from src.ocrd_network.models import StateEnum
from tests.base import assets
from tests.network.config import test_config
from tests.network.utils import (
poll_job_till_timeout_fail_or_success,
post_ps_workflow_request,
)

PROCESSING_SERVER_URL = test_config.PROCESSING_SERVER_URL


def poll_till_timeout_fail_or_success(test_url: str, tries: int, wait: int) -> StateEnum:
job_state = StateEnum.unset
while tries > 0:
sleep(wait)
response = get(url=test_url)
assert response.status_code == 200, f"Processing server: {test_url}, {response.status_code}"
job_state = response.json()["state"]
if job_state == StateEnum.success or job_state == StateEnum.failed:
break
tries -= 1
return job_state


def test_ocrd_all_workflow():
# This tests is supposed to with ocrd_all not with just core on its own
# This test is supposed to run with ocrd_all not with just core on its own
# Note: the used workflow path is volume mapped
path_to_wf = "/ocrd-data/assets/ocrd_all-test-workflow.txt"
path_to_mets = "/data/mets.xml"

# submit the workflow job
test_url = f"{PROCESSING_SERVER_URL}/workflow/run?mets_path={path_to_mets}&page_wise=True"
response = post(
url=test_url,
headers={"accept": "application/json"},
files={"workflow": open(path_to_wf, 'rb')}
)
# print(response.json())
assert response.status_code == 200, (
f"Processing server: {test_url}, {response.status_code}. "
f"Response text: {response.text}"
)
wf_job_id = response.json()["job_id"]
assert wf_job_id

job_state = poll_till_timeout_fail_or_success(
wf_job_id = post_ps_workflow_request(PROCESSING_SERVER_URL, path_to_wf, path_to_mets)
job_state = poll_job_till_timeout_fail_or_success(
test_url=f"{PROCESSING_SERVER_URL}/workflow/job-simple/{wf_job_id}",
tries=30,
wait=10
Expand Down
76 changes: 22 additions & 54 deletions tests/network/test_processing_server.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
from time import sleep
from requests import get, post
from requests import get
from src.ocrd_network import NETWORK_AGENT_WORKER
from src.ocrd_network.models import StateEnum
from tests.base import assets
from tests.network.config import test_config
from tests.network.utils import (
poll_job_till_timeout_fail_or_success,
post_ps_processing_request,
post_ps_workflow_request,
)

PROCESSING_SERVER_URL = test_config.PROCESSING_SERVER_URL


def poll_till_timeout_fail_or_success(test_url: str, tries: int, wait: int) -> StateEnum:
job_state = StateEnum.unset
while tries > 0:
sleep(wait)
response = get(url=test_url)
assert response.status_code == 200, f"Processing server: {test_url}, {response.status_code}"
job_state = response.json()["state"]
if job_state == StateEnum.success or job_state == StateEnum.failed:
break
tries -= 1
return job_state


def test_processing_server_connectivity():
test_url = f'{PROCESSING_SERVER_URL}/'
test_url = f"{PROCESSING_SERVER_URL}/"
response = get(test_url)
assert response.status_code == 200, \
f'Processing server is not reachable on: {test_url}, {response.status_code}'
message = response.json()['message']
assert message.startswith('The home page of'), \
f'Processing server home page message is corrupted'
f"Processing server is not reachable on: {test_url}, {response.status_code}"
message = response.json()["message"]
assert message.startswith("The home page of"), \
f"Processing server home page message is corrupted"


# TODO: The processing workers are still not registered when deployed separately.
# Fix that by extending the processing server.
def test_processing_server_deployed_processors():
test_url = f'{PROCESSING_SERVER_URL}/processor'
test_url = f"{PROCESSING_SERVER_URL}/processor"
response = get(test_url)
processors = response.json()
assert response.status_code == 200, \
f'Processing server: {test_url}, {response.status_code}'
assert processors == [], f'Mismatch in deployed processors'
f"Processing server: {test_url}, {response.status_code}"
assert processors == [], f"Mismatch in deployed processors"


def test_processing_server_processing_request():
path_to_mets = assets.path_to('kant_aufklaerung_1784/data/mets.xml')
path_to_mets = assets.path_to("kant_aufklaerung_1784/data/mets.xml")
test_processing_job_input = {
"path_to_mets": path_to_mets,
"input_file_grps": ['OCR-D-IMG'],
"output_file_grps": ['OCR-D-DUMMY'],
"input_file_grps": ["OCR-D-IMG"],
"output_file_grps": ["OCR-D-DUMMY"],
"agent_type": NETWORK_AGENT_WORKER,
"parameters": {}
}
test_processor = 'ocrd-dummy'
test_url = f'{PROCESSING_SERVER_URL}/processor/run/{test_processor}'
response = post(
url=test_url,
headers={"accept": "application/json"},
json=test_processing_job_input
)
# print(response.json())
assert response.status_code == 200, \
f'Processing server: {test_url}, {response.status_code}'
processing_job_id = response.json()["job_id"]
assert processing_job_id

job_state = poll_till_timeout_fail_or_success(
test_processor = "ocrd-dummy"
processing_job_id = post_ps_processing_request(PROCESSING_SERVER_URL, test_processor, test_processing_job_input)
job_state = poll_job_till_timeout_fail_or_success(
test_url=f"{PROCESSING_SERVER_URL}/processor/job/{processing_job_id}",
tries=10,
wait=10
Expand All @@ -76,20 +56,8 @@ def test_processing_server_workflow_request():
# Note: the used workflow path is volume mapped
path_to_dummy_wf = "/ocrd-data/assets/dummy-workflow.txt"
path_to_mets = assets.path_to('kant_aufklaerung_1784/data/mets.xml')

# submit the workflow job
test_url = f"{PROCESSING_SERVER_URL}/workflow/run?mets_path={path_to_mets}&page_wise=True"
response = post(
url=test_url,
headers={"accept": "application/json"},
files={"workflow": open(path_to_dummy_wf, 'rb')}
)
# print(response.json())
assert response.status_code == 200, f"Processing server: {test_url}, {response.status_code}"
wf_job_id = response.json()["job_id"]
assert wf_job_id

job_state = poll_till_timeout_fail_or_success(
wf_job_id = post_ps_workflow_request(PROCESSING_SERVER_URL, path_to_dummy_wf, path_to_mets)
job_state = poll_job_till_timeout_fail_or_success(
test_url=f"{PROCESSING_SERVER_URL}/workflow/job-simple/{wf_job_id}",
tries=30,
wait=10
Expand Down
50 changes: 50 additions & 0 deletions tests/network/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from requests import get, post
from time import sleep
from src.ocrd_network.models import StateEnum


def poll_job_till_timeout_fail_or_success(
test_url: str,
tries: int = 10,
wait: int = 10
) -> StateEnum:
job_state = StateEnum.unset
while tries > 0:
sleep(wait)
response = get(url=test_url)
assert response.status_code == 200, f"Processing server: {test_url}, {response.status_code}"
job_state = response.json()["state"]
if job_state == StateEnum.success or job_state == StateEnum.failed:
break
tries -= 1
return job_state


def post_ps_processing_request(ps_server_host: str, test_processor: str, test_job_input: dict) -> str:
test_url = f"{ps_server_host}/processor/run/{test_processor}"
response = post(
url=test_url,
headers={"accept": "application/json"},
json=test_job_input
)
# print(response.json())
assert response.status_code == 200, \
f"Processing server: {test_url}, {response.status_code}"
processing_job_id = response.json()["job_id"]
assert processing_job_id
return processing_job_id


# TODO: Can be extended to include other parameters such as page_wise
def post_ps_workflow_request(ps_server_host: str, path_to_test_wf: str, path_to_test_mets: str) -> str:
test_url = f"{ps_server_host}/workflow/run?mets_path={path_to_test_mets}&page_wise=True"
response = post(
url=test_url,
headers={"accept": "application/json"},
files={"workflow": open(path_to_test_wf, "rb")}
)
# print(response.json())
assert response.status_code == 200, f"Processing server: {test_url}, {response.status_code}"
wf_job_id = response.json()["job_id"]
assert wf_job_id
return wf_job_id

0 comments on commit 79c5b79

Please sign in to comment.