Coverage for tests/test_drivers.py: 38%

52 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-24 02:03 -0700

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 def setUp(self): 

36 self.cwd = os.getcwd() 

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

38 

39 def tearDown(self): 

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

41 

42 def testDeprecatedOutCollection(self): 

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

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

45 

46 def testMissingOutputRun(self): 

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

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

49 

50 def testMissingSubmitPath(self): 

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

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

53 

54 

55class TestPingDriver(unittest.TestCase): 

56 def testWmsServiceSuccess(self): 

57 retval = ping_driver("wms_test_utils.WmsServiceSuccess") 

58 self.assertEqual(retval, 0) 

59 

60 def testWmsServiceFailure(self): 

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

62 retval = ping_driver("wms_test_utils.WmsServiceFailure") 

63 self.assertNotEqual(retval, 0) 

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

65 

66 def testWmsServiceEnvVar(self): 

67 with unittest.mock.patch.dict( 

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

69 ): 

70 retval = ping_driver() 

71 self.assertEqual(retval, 0) 

72 

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

74 def testWmsServiceNone(self): 

75 # Override default wms to be the test one 

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

77 mock_function.return_value = "wms_test_utils.WmsServiceDefault" 

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

79 retval = ping_driver() 

80 self.assertEqual(retval, 0) 

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

82 

83 def testWmsServicePassThru(self): 

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

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

86 self.assertEqual(retval, 0) 

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

88 

89 

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

91 unittest.main()