1
1
"""Tests for the pytroll runner."""
2
2
import os
3
+ import logging
3
4
from unittest import mock
4
5
5
6
import pytest
45
46
"""
46
47
47
48
49
+ log_config_content = """version: 1
50
+ disable_existing_loggers: false
51
+ handlers:
52
+ console:
53
+ class: logging.StreamHandler
54
+ level: DEBUG
55
+ stream: ext://sys.stdout
56
+ root:
57
+ level: DEBUG
58
+ handlers: [console]
59
+ """
60
+
61
+
62
+ @pytest .fixture ()
63
+ def log_config_file (tmp_path ):
64
+ """Write a log config file."""
65
+ log_config = tmp_path / "mylogconfig.yaml"
66
+ with open (log_config , "w" ) as fobj :
67
+ fobj .write (log_config_content )
68
+
69
+ return log_config
70
+
71
+
48
72
@pytest .fixture ()
49
73
def command (tmp_path ):
50
74
"""Make a command script that just prints out the files it got."""
@@ -266,6 +290,7 @@ def single_file_to_glob(tmp_path):
266
290
pattern = os .fspath (tmp_path / "file?" )
267
291
return pattern
268
292
293
+
269
294
@pytest .fixture ()
270
295
def old_generated_file (tmp_path ):
271
296
"""Create a single file to glob."""
@@ -276,7 +301,7 @@ def old_generated_file(tmp_path):
276
301
return filename
277
302
278
303
279
- def test_run_and_publish (tmp_path , command_bla , files_to_glob ):
304
+ def test_run_and_publish (caplog , tmp_path , command_bla , files_to_glob ):
280
305
"""Test run and publish."""
281
306
sub_config = dict (nameserver = False , addresses = ["ipc://bla" ])
282
307
pub_config = dict (publisher_settings = dict (nameservers = False , port = 1979 ),
@@ -294,12 +319,17 @@ def test_run_and_publish(tmp_path, command_bla, files_to_glob):
294
319
data = {"dataset" : [{"uri" : os .fspath (tmp_path / f ), "uid" : f } for f in some_files ]}
295
320
messages = [Message ("some_topic" , "dataset" , data = data )]
296
321
297
- with patched_subscriber_recv (messages ):
298
- with patched_publisher () as published_messages :
299
- run_and_publish (yaml_file )
300
- assert len (published_messages ) == 1
301
- for ds , filename in zip (published_messages [0 ].data ["dataset" ], some_files ):
302
- assert ds ["uid" ] == filename + ".bla"
322
+ with caplog .at_level (logging .DEBUG ):
323
+ with patched_subscriber_recv (messages ):
324
+ with patched_publisher () as published_messages :
325
+ run_and_publish (yaml_file )
326
+ assert len (published_messages ) == 1
327
+ for ds , filename in zip (published_messages [0 ].data ["dataset" ], some_files ):
328
+ assert ds ["uid" ] == filename + ".bla"
329
+
330
+ assert "Subscriber config settings: " in caplog .text
331
+ assert "addresses = ['ipc://bla']" in caplog .text
332
+ assert "nameserver = False" in caplog .text
303
333
304
334
305
335
def test_run_and_publish_does_not_pick_old_files (tmp_path , command_bla , files_to_glob , old_generated_file ):
@@ -370,7 +400,6 @@ def test_run_and_publish_with_files_from_log(tmp_path, command_aws):
370
400
assert published_messages [0 ].data ["uri" ] == "/local_disk/aws_test/test/RAD_AWS_1B/" + expected
371
401
372
402
373
-
374
403
def test_config_reader (command , tmp_path ):
375
404
"""Test the config reader."""
376
405
sub_config = dict (nameserver = False , addresses = ["ipc://bla" ])
@@ -383,6 +412,7 @@ def test_config_reader(command, tmp_path):
383
412
with open (yaml_file , "w" ) as fd :
384
413
fd .write (yaml .dump (test_config ))
385
414
command_to_call , subscriber_config , publisher_config = read_config (yaml_file )
415
+
386
416
assert subscriber_config == sub_config
387
417
assert command_to_call == command_path
388
418
assert publisher_config == pub_config
@@ -398,3 +428,27 @@ def test_main_crashes_when_config_file_missing():
398
428
"""Test that main crashes when the config file is missing."""
399
429
with pytest .raises (FileNotFoundError ):
400
430
main (["moose_config.yaml" ])
431
+
432
+
433
+ def test_main_parse_log_configfile (tmp_path , command , log_config_file ):
434
+ """Test that when parsing a log-config yaml file logging is being setup"""
435
+ sub_config = dict (nameserver = False , addresses = ["ipc://bla" ])
436
+ pub_settings = dict (nameserver = False , name = 'blabla' )
437
+ pub_config = dict (topic = "/hi/there/" ,
438
+ expected_files = '*.bufr' ,
439
+ publisher_settings = pub_settings )
440
+ command_path = os .fspath (command )
441
+ test_config = dict (subscriber_config = sub_config ,
442
+ script = command_path ,
443
+ publisher_config = pub_config )
444
+
445
+ yaml_file = tmp_path / "config.yaml"
446
+ with open (yaml_file , "w" ) as fd :
447
+ fd .write (yaml .dump (test_config ))
448
+
449
+ messages = []
450
+ with patched_subscriber_recv (messages ):
451
+ with patched_publisher () as published_messages :
452
+ main ([str (yaml_file ), '-l' , str (log_config_file )])
453
+
454
+ assert isinstance (logging .getLogger ('' ).handlers [0 ], logging .StreamHandler )
0 commit comments