Skip to content

Commit

Permalink
Moved code for checking host to function
Browse files Browse the repository at this point in the history
Added some tests
  • Loading branch information
Nina.Hakansson committed Jan 26, 2024
1 parent 509319f commit a78d2f8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
38 changes: 38 additions & 0 deletions nwcsafpps_runner/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
from nwcsafpps_runner.utils import (create_xml_timestat_from_lvl1c,
get_lvl1c_file_from_msg,
find_product_statistics_from_lvl1c,
ready2run,
publish_pps_files)
from nwcsafpps_runner.utils import FindTimeControlFileError

TEST_MSG = """pytroll://segment/EPSSGA/1B/ file [email protected] 2023-02-17T08:18:15.748831 v1.01 application/json {"start_time": "2023-02-17T08:03:25", "end_time": "2023-02-17T08:15:25", "orbit_number": 99999, "platform_name": "Metop-SG-A1", "sensor": "metimage", "format": "X", "type": "NETCDF", "data_processing_level": "1b", "variant": "DR", "orig_orbit_number": 23218, "uri": "/san1/polar_in/direct_readout/metimage/W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc", "uid": "W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc"}""" # noqa: E501

TEST_MSG_UID = """pytroll://segment/EPSSGA/1B/ file [email protected] 2023-02-17T08:18:15.748831 v1.01 application/json {"start_time": "2023-02-17T08:03:25", "end_time": "2023-02-17T08:15:25", "orbit_number": 99999, "platform_name": "Metop-SG-A1", "sensor": "metimage", "format": "X", "type": "NETCDF", "data_processing_level": "1b", "variant": "DR", "orig_orbit_number": 23218, "uid": "W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc", "uid": "W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc", "destination": "/san1/polar_in/direct_readout/metimage"}""" # noqa: E501

TEST_MSG_BAD = """pytroll://segment/EPSSGA/1B/ dataset [email protected] 2023-02-17T08:18:15.748831 v1.01 application/json {"start_time": "2023-02-17T08:03:25", "end_time": "2023-02-17T08:15:25", "orbit_number": 99999, "platform_name": "Metop-SG-A1", "sensor": "metimage", "format": "X", "type": "NETCDF", "data_processing_level": "1b", "variant": "DR", "orig_orbit_number": 23218, "uid": "W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc", "uid": "W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_20210314224906_G_D_20070912101704_20070912101804_T_B____.nc", "destination": "/san1/polar_in/direct_readout/metimage"}""" # noqa: E501


@pytest.fixture
def fake_file_dir(tmp_path):
Expand Down Expand Up @@ -120,6 +125,21 @@ def test_xml_for_products_no_file4pps(self, fake_file_dir):
assert res == []


class TestReady2Run(unittest.TestCase):
"""Test ready2run function."""

def test_ready2run(self):
from posttroll.message import Message
input_msg = Message.decode(rawstr=TEST_MSG)
mymodule = MagicMock()
import sys
sys.modules["check_host_ok"] = mymodule
ok2run = ready2run(input_msg, {"file4pps": "dummy"})
self.assertTrue(ok2run)
ok2run = ready2run(input_msg, {"file4pps": None})
self.assertFalse(ok2run)


class TestPublishPPSFiles(unittest.TestCase):
"""Test publish pps files."""

Expand Down Expand Up @@ -155,6 +175,24 @@ def test_get_lvl1c_file_from_msg(self):
"20210314224906_G_D_20070912101704_20070912101804_T_B____.nc")
self.assertEqual(file1, file_exp)

def test_get_lvl1c_file_from_msg_uid(self):
"""Test get_lvl1c_file_from_message."""
from posttroll.message import Message
input_msg = Message.decode(rawstr=TEST_MSG_UID)
file1 = get_lvl1c_file_from_msg(input_msg)
file_exp = (
"/san1/polar_in/direct_readout/metimage/" +
"W_XX-EUMETSAT-Darmstadt,SAT,SGA1-VII-1B-RAD_C_EUMT_" +
"20210314224906_G_D_20070912101704_20070912101804_T_B____.nc")
self.assertEqual(file1, file_exp)

def test_get_lvl1c_file_from_msg_bad(self):
"""Test get_lvl1c_file_from_message."""
from posttroll.message import Message
input_msg = Message.decode(rawstr=TEST_MSG_BAD)
file1 = get_lvl1c_file_from_msg(input_msg)
self.assertEqual(file1, None)


if __name__ == "__main__":
pass
26 changes: 16 additions & 10 deletions nwcsafpps_runner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,20 @@ def get_lvl1c_file_from_msg(msg):
else:
LOG.debug(
"Ignoring this type of message data: type = " + str(msg.type))
return []
return None

try:
level1c_files = check_uri(uris)
except IOError:
LOG.info('One or more files not present on this host!')
return []
return None

Check warning on line 184 in nwcsafpps_runner/utils.py

View check run for this annotation

Codecov / codecov/patch

nwcsafpps_runner/utils.py#L184

Added line #L184 was not covered by tests

LOG.debug("files4pps: %s", str(level1c_files))
if len(level1c_files) < 1:
LOG.info("No level1c files!")
return []

return level1c_files[0]


def ready2run(msg, scene, **kwargs):
"""Check whether pps is ready to run or not."""

LOG.info("Got message: " + str(msg))
def check_host_ok(msg):
"""Check that host is ok."""
try:
url_ip = socket.gethostbyname(msg.host)
if url_ip not in get_local_ips():
Expand All @@ -203,6 +197,18 @@ def ready2run(msg, scene, **kwargs):
except (AttributeError, socket.gaierror) as err:
LOG.error("Failed checking host! Hostname = %s", socket.gethostname())
LOG.exception(err)
return True


def ready2run(msg, scene, **kwargs):
"""Check whether pps is ready to run or not."""

LOG.info("Got message: " + str(msg))
if not check_host_ok(msg):
return False

Check warning on line 208 in nwcsafpps_runner/utils.py

View check run for this annotation

Codecov / codecov/patch

nwcsafpps_runner/utils.py#L208

Added line #L208 was not covered by tests

if scene['file4pps'] is None:
return False

if msg.data['platform_name'] in SUPPORTED_PPS_SATELLITES:
LOG.info(
Expand Down

0 comments on commit a78d2f8

Please sign in to comment.