Skip to content

Commit

Permalink
Handle empty list or string of filenames from PPS
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina.Hakansson committed Aug 13, 2024
1 parent 92e49dc commit 113862d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 6 additions & 1 deletion nwcsafpps_runner/pps_posttroll_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def send(self):
# Error
# pubmsg = self.create_message("FAILED", self.metadata)
LOG.warning("Module %s failed, so no message sent", self.metadata.get('module', 'unknown'))
elif self.metadata["filename"] == "" or self.metadata["filename"] == []:
LOG.info("Module %s did not create any files, so no message sent", self.metadata.get('module', 'unknown'))
else:
# Ok
pubmsg = self.create_message("OK")
Expand Down Expand Up @@ -352,7 +354,10 @@ def get_message_with_uri_and_uid(self):
LOG.debug("Servername = %s", str(servername))

msg = {}
if isinstance(self.metadata['filename'], list):

if self.metadata['filename'] == '':
return {}
elif isinstance(self.metadata['filename'], list):
dataset = []
for filename in self.metadata['filename']:
uri = os.path.abspath(filename)
Expand Down
30 changes: 28 additions & 2 deletions nwcsafpps_runner/tests/test_pps_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,19 @@ def test_send_method(self, mandatory_param, filename):

mandatory_param.return_value = True
filename.return_value = True
my_metadata = self.metadata.copy()
my_metadata["filename"] = "dummy"

with patch.object(PostTrollMessage, 'publish_message', return_value=None) as mock_method_publish:
with patch.object(PostTrollMessage, 'create_message', return_value=None) as mock_method_create:
posttroll_message = PostTrollMessage(0, self.metadata)
posttroll_message = PostTrollMessage(0, my_metadata)
posttroll_message.send()
self.assertEqual(mock_method_publish.call_count, 1)
self.assertEqual(mock_method_create.call_count, 1)

with patch.object(PostTrollMessage, 'publish_message', return_value=None) as mock_method_publish:
with patch.object(PostTrollMessage, 'create_message', return_value=None) as mock_method_create:
posttroll_message = PostTrollMessage(1, self.metadata)
posttroll_message = PostTrollMessage(1, my_metadata)
posttroll_message.send()
self.assertEqual(mock_method_publish.call_count, 0)
self.assertEqual(mock_method_create.call_count, 0)
Expand Down Expand Up @@ -344,6 +346,30 @@ def test_create_message_with_topic(self, socket_gethostname):
expected_message_header = "/my/pps/publish/topic/UNKNOWN/"
self.assertEqual(expected_message_header, result_message['header'])

@patch('socket.gethostname')
def test_create_message_without_filename(self, socket_gethostname):
"""Test creating a message with header/topic, type and content."""
from nwcsafpps_runner.pps_posttroll_hook import PostTrollMessage

socket_gethostname.return_value = 'TEST_SERVERNAME'

metadata = {'publish_topic': '/my/pps/publish/topic/{pps_product}/',
'output_format': 'CF',
'level': '2',
'variant': 'DR',
'geo_or_polar': 'polar',
'software': 'NWCSAF-PPSv2018',
'start_time': START_TIME1, 'end_time': END_TIME1,
'sensor': 'viirs',
'filename': '',
'platform_name': 'npp'}

posttroll_message = PostTrollMessage(0, metadata)
with patch.object(PostTrollMessage, 'is_segment', return_value=False):
result_message = posttroll_message.create_message('OK')
self.assertFalse("uri" in result_message["content"])


@patch('socket.gethostname')
def test_create_message_with_topic_pattern(self, socket_gethostname):
"""Test creating a message with header/topic that is a pattern, type and content."""
Expand Down

0 comments on commit 113862d

Please sign in to comment.