Coverage for tests/test_workspace.py: 30%
136 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-17 10:21 +0000
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-17 10:21 +0000
1#
2# This file is part of ap_verify.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
24import os
25import shutil
26import tempfile
27import unittest
28from urllib.request import url2pathname
30import lsst.utils.tests
31import lsst.daf.butler
32from lsst.ap.verify.workspace import WorkspaceGen2, WorkspaceGen3
35class WorkspaceGen2TestSuite(lsst.utils.tests.TestCase):
37 def setUp(self):
38 # Use realpath to avoid link problems
39 self._testWorkspace = os.path.realpath(tempfile.mkdtemp())
40 self._testbed = WorkspaceGen2(self._testWorkspace)
42 def tearDown(self):
43 shutil.rmtree(self._testWorkspace, ignore_errors=True)
45 def testRepr(self):
46 # Required to match constructor call
47 self.assertEqual(repr(self._testbed), "WorkspaceGen2(" + repr(self._testWorkspace) + ")")
49 def testEq(self):
50 copied = WorkspaceGen2(self._testWorkspace)
51 self.assertEqual(self._testbed, copied)
53 alternate = WorkspaceGen3(self._testWorkspace)
54 self.assertNotEqual(self._testbed, alternate)
55 self.assertNotEqual(copied, alternate)
57 with tempfile.TemporaryDirectory() as temp:
58 different = WorkspaceGen2(temp)
59 self.assertNotEqual(self._testbed, different)
60 self.assertNotEqual(copied, different)
62 def _assertInDir(self, path, baseDir):
63 """Test that ``path`` is a subpath of ``baseDir``.
64 """
65 _canonPath = os.path.abspath(os.path.realpath(path))
66 _canonDir = os.path.abspath(os.path.realpath(baseDir))
67 ancestor = os.path.commonprefix([_canonPath, _canonDir])
68 self.assertEqual(ancestor, _canonDir)
70 def _assertNotInDir(self, path, baseDir):
71 """Test that ``path`` is not a subpath of ``baseDir``.
72 """
73 _canonPath = os.path.abspath(os.path.realpath(path))
74 _canonDir = os.path.abspath(os.path.realpath(baseDir))
75 ancestor = os.path.commonprefix([_canonPath, _canonDir])
76 self.assertNotEqual(ancestor, _canonDir)
78 def testMakeDir(self):
79 """Verify that a Workspace creates the workspace directory if it does not exist.
80 """
81 newPath = '_temp2' # can't use mkdtemp because creation is what we're testing
82 shutil.rmtree(newPath, ignore_errors=True)
83 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.')
85 try:
86 WorkspaceGen2(newPath)
87 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.')
88 finally:
89 shutil.rmtree(newPath, ignore_errors=True)
91 @staticmethod
92 def _allRepos(workspace):
93 """An iterator over all repos exposed by a WorkspaceGen2.
94 """
95 yield workspace.dataRepo
96 yield workspace.calibRepo
97 yield workspace.templateRepo
98 yield workspace.outputRepo
100 def testDirectories(self):
101 """Verify that a WorkspaceGen2 creates repositories in the target directory.
103 The exact repository locations are not tested, as they are likely to change.
104 """
105 # Workspace should report all paths as absolute
106 root = os.path.abspath(os.path.realpath(self._testWorkspace))
107 self.assertEqual(self._testbed.workDir, root)
108 self._assertInDir(self._testbed.configDir, root)
109 for repo in self._allRepos(self._testbed):
110 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
111 self._assertInDir(url2pathname(repo), root)
113 def testDatabase(self):
114 """Verify that a WorkspaceGen2 requests a database file in the target
115 directory, but not in any repository.
116 """
117 root = self._testWorkspace
118 self._assertInDir(self._testbed.dbLocation, root)
119 for repo in self._allRepos(self._testbed):
120 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
121 self._assertNotInDir(self._testbed.dbLocation, url2pathname(repo))
123 def testAlerts(self):
124 """Verify that a WorkspaceGen2 requests an alert dump in the target
125 directory, but not in any repository.
126 """
127 root = self._testWorkspace
128 self._assertInDir(self._testbed.alertLocation, root)
129 for repo in self._allRepos(self._testbed):
130 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
131 self._assertNotInDir(self._testbed.alertLocation, url2pathname(repo))
134class WorkspaceGen3TestSuite(lsst.utils.tests.TestCase):
136 def setUp(self):
137 # Use realpath to avoid link problems
138 self._testWorkspace = os.path.realpath(tempfile.mkdtemp())
139 self._testbed = WorkspaceGen3(self._testWorkspace)
141 def tearDown(self):
142 shutil.rmtree(self._testWorkspace, ignore_errors=True)
144 def testRepr(self):
145 # Required to match constructor call
146 self.assertEqual(repr(self._testbed), "WorkspaceGen3(" + repr(self._testWorkspace) + ")")
148 def testEq(self):
149 copied = WorkspaceGen3(self._testWorkspace)
150 self.assertEqual(self._testbed, copied)
152 alternate = WorkspaceGen2(self._testWorkspace)
153 self.assertNotEqual(self._testbed, alternate)
154 self.assertNotEqual(copied, alternate)
156 with tempfile.TemporaryDirectory() as temp:
157 different = WorkspaceGen3(temp)
158 self.assertNotEqual(self._testbed, different)
159 self.assertNotEqual(copied, different)
161 def _assertInDir(self, path, baseDir):
162 """Test that ``path`` is a subpath of ``baseDir``.
163 """
164 _canonPath = os.path.abspath(os.path.realpath(path))
165 _canonDir = os.path.abspath(os.path.realpath(baseDir))
166 ancestor = os.path.commonprefix([_canonPath, _canonDir])
167 self.assertEqual(ancestor, _canonDir)
169 def _assertNotInDir(self, path, baseDir):
170 """Test that ``path`` is not a subpath of ``baseDir``.
171 """
172 _canonPath = os.path.abspath(os.path.realpath(path))
173 _canonDir = os.path.abspath(os.path.realpath(baseDir))
174 ancestor = os.path.commonprefix([_canonPath, _canonDir])
175 self.assertNotEqual(ancestor, _canonDir)
177 def testMakeDir(self):
178 """Verify that a Workspace creates the workspace directory if it does not exist.
179 """
180 newPath = '_temp3' # can't use mkdtemp because creation is what we're testing
181 shutil.rmtree(newPath, ignore_errors=True)
182 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.')
184 try:
185 WorkspaceGen3(newPath)
186 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.')
187 finally:
188 shutil.rmtree(newPath, ignore_errors=True)
190 def testDirectories(self):
191 """Verify that a WorkspaceGen3 creates subdirectories in the target directory.
193 The exact locations are not tested, as they are likely to change.
194 """
195 # Workspace should report all paths as absolute
196 root = os.path.abspath(os.path.realpath(self._testWorkspace))
197 self.assertEqual(self._testbed.workDir, root)
198 self._assertInDir(self._testbed.configDir, root)
199 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
200 self._assertInDir(url2pathname(self._testbed.repo), root)
202 def testDatabase(self):
203 """Verify that a WorkspaceGen3 requests a database file in the target
204 directory, but not in any repository.
205 """
206 root = self._testWorkspace
207 self._assertInDir(self._testbed.dbLocation, root)
208 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
209 self._assertNotInDir(self._testbed.dbLocation, url2pathname(self._testbed.repo))
211 def testAlerts(self):
212 """Verify that a WorkspaceGen3 requests an alert dump in the target
213 directory, but not in any repository.
214 """
215 root = self._testWorkspace
216 self._assertInDir(self._testbed.alertLocation, root)
217 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts
218 self._assertNotInDir(self._testbed.alertLocation, url2pathname(self._testbed.repo))
220 def testWorkButler(self):
221 """Verify that the Gen 3 Butler is available if and only if the repository is set up.
222 """
223 with self.assertRaises(RuntimeError):
224 self._testbed.workButler
225 lsst.daf.butler.Butler.makeRepo(self._testbed.repo)
226 # workButler definition currently requires valid collections; this
227 # should be an implementation detail.
228 registry = lsst.daf.butler.Butler(self._testbed.repo, writeable=True).registry
229 registry.registerCollection('refcats', lsst.daf.butler.CollectionType.RUN)
230 registry.registerCollection('skymaps', lsst.daf.butler.CollectionType.RUN)
231 registry.registerCollection('templates/foo', lsst.daf.butler.CollectionType.RUN)
233 # Can't really test Butler's state, so just make sure it exists
234 self.assertTrue(self._testbed.workButler.isWriteable())
236 def testAnalysisButler(self):
237 """Verify that the Gen 3 Butler is available if and only if the repository is set up.
238 """
239 with self.assertRaises(RuntimeError):
240 self._testbed.analysisButler
241 lsst.daf.butler.Butler.makeRepo(self._testbed.repo)
242 # Can't really test Butler's state, so just make sure it exists
243 self.assertFalse(self._testbed.analysisButler.isWriteable())
246class MemoryTester(lsst.utils.tests.MemoryTestCase):
247 pass
250def setup_module(module):
251 lsst.utils.tests.init()
254if __name__ == "__main__": 254 ↛ 255line 254 didn't jump to line 255, because the condition on line 254 was never true
255 lsst.utils.tests.init()
256 unittest.main()