Coverage for tests/test_drivers.py: 36%

52 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-23 10:45 +0000

1# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21"""Unit tests for drivers.py.""" 

22import logging 

23import os 

24import shutil 

25import tempfile 

26import unittest 

27 

28from lsst.ctrl.bps import BpsConfig 

29from lsst.ctrl.bps.drivers import _init_submission_driver, ping_driver 

30 

31TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

32 

33 

34class TestInitSubmissionDriver(unittest.TestCase): 

35 """Test submission.""" 

36 

37 def setUp(self): 

38 self.cwd = os.getcwd() 

39 self.tmpdir = tempfile.mkdtemp(dir=TESTDIR) 

40 

41 def tearDown(self): 

42 shutil.rmtree(self.tmpdir, ignore_errors=True) 

43 

44 def testDeprecatedOutCollection(self): 

45 with self.assertRaisesRegex(KeyError, "outCollection"): 

46 _init_submission_driver({"payload": {"outCollection": "bad"}}) 

47 

48 def testMissingOutputRun(self): 

49 with self.assertRaisesRegex(KeyError, "outputRun"): 

50 _init_submission_driver({"payload": {"inCollection": "bad"}}) 

51 

52 def testMissingSubmitPath(self): 

53 with self.assertRaisesRegex(KeyError, "submitPath"): 

54 _init_submission_driver({"payload": {"outputRun": "bad"}}) 

55 

56 

57class TestPingDriver(unittest.TestCase): 

58 """Test ping.""" 

59 

60 def testWmsServiceSuccess(self): 

61 retval = ping_driver("wms_test_utils.WmsServiceSuccess") 

62 self.assertEqual(retval, 0) 

63 

64 def testWmsServiceFailure(self): 

65 with self.assertLogs(level=logging.ERROR) as cm: 

66 retval = ping_driver("wms_test_utils.WmsServiceFailure") 

67 self.assertNotEqual(retval, 0) 

68 self.assertEqual(cm.records[0].getMessage(), "Couldn't contact service X") 

69 

70 def testWmsServiceEnvVar(self): 

71 with unittest.mock.patch.dict( 

72 os.environ, {"BPS_WMS_SERVICE_CLASS": "wms_test_utils.WmsServiceSuccess"} 

73 ): 

74 retval = ping_driver() 

75 self.assertEqual(retval, 0) 

76 

77 @unittest.mock.patch.dict(os.environ, {}) 

78 def testWmsServiceNone(self): 

79 # Override default wms to be the test one 

80 with unittest.mock.patch.object(BpsConfig, "__getitem__") as mock_function: 

81 mock_function.return_value = "wms_test_utils.WmsServiceDefault" 

82 with self.assertLogs(level=logging.INFO) as cm: 

83 retval = ping_driver() 

84 self.assertEqual(retval, 0) 

85 self.assertEqual(cm.records[0].getMessage(), "DEFAULT None") 

86 

87 def testWmsServicePassThru(self): 

88 with self.assertLogs(level=logging.INFO) as cm: 

89 retval = ping_driver("wms_test_utils.WmsServicePassThru", "EXTRA_VALUES") 

90 self.assertEqual(retval, 0) 

91 self.assertRegex(cm.output[0], "INFO.+EXTRA_VALUES") 

92 

93 

94if __name__ == "__main__": 94 ↛ 95line 94 didn't jump to line 95, because the condition on line 94 was never true

95 unittest.main()