Coverage for tests/test_drivers.py: 39%
77 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 02:08 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 02:08 -0800
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <https://www.gnu.org/licenses/>.
27"""Unit tests for drivers.py."""
29import logging
30import os
31import shutil
32import tempfile
33import unittest
35import yaml
37from lsst.ctrl.bps import WmsStates
38from lsst.ctrl.bps.drivers import _init_submission_driver, ping_driver, status_driver
40TESTDIR = os.path.abspath(os.path.dirname(__file__))
43class TestInitSubmissionDriver(unittest.TestCase):
44 """Test submission."""
46 def setUp(self):
47 self.cwd = os.getcwd()
48 self.tmpdir = tempfile.mkdtemp(dir=TESTDIR)
50 def tearDown(self):
51 shutil.rmtree(self.tmpdir, ignore_errors=True)
53 @unittest.mock.patch("lsst.ctrl.bps.initialize.BPS_DEFAULTS", {})
54 def testDeprecatedOutCollection(self):
55 config = {
56 "submitPath": "bad",
57 "payload": {
58 "outCollection": "bad",
59 "outputRun": "bad",
60 },
61 }
62 with tempfile.NamedTemporaryFile(mode="w+", suffix=".yaml") as file:
63 yaml.dump(config, stream=file)
64 with self.assertRaisesRegex(KeyError, "outCollection"):
65 _init_submission_driver(file.name)
67 @unittest.mock.patch("lsst.ctrl.bps.initialize.BPS_DEFAULTS", {})
68 def testMissingOutputRun(self):
69 config = {"submitPath": "bad"}
70 with tempfile.NamedTemporaryFile(mode="w+", suffix=".yaml") as file:
71 yaml.dump(config, stream=file)
72 with self.assertRaisesRegex(KeyError, "outputRun"):
73 _init_submission_driver(file.name)
75 @unittest.mock.patch("lsst.ctrl.bps.initialize.BPS_DEFAULTS", {})
76 def testMissingSubmitPath(self):
77 config = {"payload": {"outputRun": "bad"}}
78 with tempfile.NamedTemporaryFile(mode="w+", suffix=".yaml") as file:
79 yaml.dump(config, stream=file)
80 with self.assertRaisesRegex(KeyError, "submitPath"):
81 _init_submission_driver(file.name)
84class TestPingDriver(unittest.TestCase):
85 """Test ping."""
87 def testWmsServiceSuccess(self):
88 retval = ping_driver("wms_test_utils.WmsServiceSuccess")
89 self.assertEqual(retval, 0)
91 def testWmsServiceFailure(self):
92 with self.assertLogs(level=logging.ERROR) as cm:
93 retval = ping_driver("wms_test_utils.WmsServiceFailure")
94 self.assertNotEqual(retval, 0)
95 self.assertEqual(cm.records[0].getMessage(), "Couldn't contact service X")
97 def testWmsServiceEnvVar(self):
98 with unittest.mock.patch.dict(
99 os.environ, {"BPS_WMS_SERVICE_CLASS": "wms_test_utils.WmsServiceSuccess"}
100 ):
101 retval = ping_driver()
102 self.assertEqual(retval, 0)
104 @unittest.mock.patch(
105 "lsst.ctrl.bps.drivers.BPS_DEFAULTS", {"wmsServiceClass": "wms_test_utils.WmsServiceDefault"}
106 )
107 def testWmsServiceNone(self):
108 with unittest.mock.patch.dict(os.environ, {}):
109 with self.assertLogs(level=logging.INFO) as cm:
110 retval = ping_driver()
111 self.assertEqual(retval, 0)
112 self.assertEqual(cm.records[0].getMessage(), "DEFAULT None")
114 def testWmsServicePassThru(self):
115 with self.assertLogs(level=logging.INFO) as cm:
116 retval = ping_driver("wms_test_utils.WmsServicePassThru", "EXTRA_VALUES")
117 self.assertEqual(retval, 0)
118 self.assertRegex(cm.output[0], "INFO.+EXTRA_VALUES")
121class TestStatusDriver(unittest.TestCase):
122 """Test status_driver function."""
124 def testWmsServiceSuccess(self):
125 with self.assertLogs(level=logging.INFO) as cm:
126 retval = status_driver("wms_test_utils.WmsServiceSuccess", run_id="/dummy/path", hist_days=3)
127 self.assertEqual(retval, WmsStates.SUCCEEDED.value)
128 self.assertEqual(cm.records[0].getMessage(), "status: SUCCEEDED")
130 def testWmsServiceFailure(self):
131 with self.assertLogs(level=logging.WARNING) as cm:
132 retval = status_driver("wms_test_utils.WmsServiceFailure", run_id="/dummy/path", hist_days=3)
133 self.assertEqual(retval, WmsStates.FAILED.value)
134 self.assertEqual(cm.records[0].getMessage(), "Dummy error message.")
136 @unittest.mock.patch(
137 "lsst.ctrl.bps.drivers.BPS_DEFAULTS", {"wmsServiceClass": "wms_test_utils.WmsServiceDefault"}
138 )
139 def testWmsServiceNone(self):
140 with unittest.mock.patch.dict(os.environ, {}):
141 retval = status_driver(None, run_id="/dummy/path", hist_days=3)
142 self.assertEqual(retval, WmsStates.RUNNING.value)
145if __name__ == "__main__":
146 unittest.main()