Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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# 

23 

24import os 

25import shutil 

26import tempfile 

27import unittest 

28from urllib.request import url2pathname 

29 

30import lsst.utils.tests 

31import lsst.daf.butler 

32from lsst.ap.verify.workspace import WorkspaceGen2, WorkspaceGen3 

33 

34 

35class WorkspaceGen2TestSuite(lsst.utils.tests.TestCase): 

36 

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) 

41 

42 def tearDown(self): 

43 shutil.rmtree(self._testWorkspace, ignore_errors=True) 

44 

45 def testRepr(self): 

46 # Required to match constructor call 

47 self.assertEqual(repr(self._testbed), "WorkspaceGen2(" + repr(self._testWorkspace) + ")") 

48 

49 def testEq(self): 

50 copied = WorkspaceGen2(self._testWorkspace) 

51 self.assertEqual(self._testbed, copied) 

52 

53 alternate = WorkspaceGen3(self._testWorkspace) 

54 self.assertNotEqual(self._testbed, alternate) 

55 self.assertNotEqual(copied, alternate) 

56 

57 with tempfile.TemporaryDirectory() as temp: 

58 different = WorkspaceGen2(temp) 

59 self.assertNotEqual(self._testbed, different) 

60 self.assertNotEqual(copied, different) 

61 

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) 

69 

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) 

77 

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.') 

84 

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) 

90 

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 

99 

100 def testDirectories(self): 

101 """Verify that a WorkspaceGen2 creates repositories in the target directory. 

102 

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) 

112 

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)) 

122 

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)) 

132 

133 

134class WorkspaceGen3TestSuite(lsst.utils.tests.TestCase): 

135 

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) 

140 

141 def tearDown(self): 

142 shutil.rmtree(self._testWorkspace, ignore_errors=True) 

143 

144 def testRepr(self): 

145 # Required to match constructor call 

146 self.assertEqual(repr(self._testbed), "WorkspaceGen3(" + repr(self._testWorkspace) + ")") 

147 

148 def testEq(self): 

149 copied = WorkspaceGen3(self._testWorkspace) 

150 self.assertEqual(self._testbed, copied) 

151 

152 alternate = WorkspaceGen2(self._testWorkspace) 

153 self.assertNotEqual(self._testbed, alternate) 

154 self.assertNotEqual(copied, alternate) 

155 

156 with tempfile.TemporaryDirectory() as temp: 

157 different = WorkspaceGen3(temp) 

158 self.assertNotEqual(self._testbed, different) 

159 self.assertNotEqual(copied, different) 

160 

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) 

168 

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) 

176 

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.') 

183 

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) 

189 

190 def testDirectories(self): 

191 """Verify that a WorkspaceGen3 creates subdirectories in the target directory. 

192 

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 self._assertInDir(self._testbed.pipelineDir, root) 

200 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

201 self._assertInDir(url2pathname(self._testbed.repo), root) 

202 

203 def testDatabase(self): 

204 """Verify that a WorkspaceGen3 requests a database file in the target 

205 directory, but not in any repository. 

206 """ 

207 root = self._testWorkspace 

208 self._assertInDir(self._testbed.dbLocation, root) 

209 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

210 self._assertNotInDir(self._testbed.dbLocation, url2pathname(self._testbed.repo)) 

211 

212 def testAlerts(self): 

213 """Verify that a WorkspaceGen3 requests an alert dump in the target 

214 directory, but not in any repository. 

215 """ 

216 root = self._testWorkspace 

217 self._assertInDir(self._testbed.alertLocation, root) 

218 # Workspace spec allows these to be URIs or paths, whatever the Butler accepts 

219 self._assertNotInDir(self._testbed.alertLocation, url2pathname(self._testbed.repo)) 

220 

221 def testWorkButler(self): 

222 """Verify that the Gen 3 Butler is available if and only if the repository is set up. 

223 """ 

224 with self.assertRaises(RuntimeError): 

225 self._testbed.workButler 

226 lsst.daf.butler.Butler.makeRepo(self._testbed.repo) 

227 # workButler definition currently requires valid collections; this 

228 # should be an implementation detail. 

229 registry = lsst.daf.butler.Butler(self._testbed.repo, writeable=True).registry 

230 registry.registerCollection('refcats', lsst.daf.butler.CollectionType.RUN) 

231 registry.registerCollection('skymaps', lsst.daf.butler.CollectionType.RUN) 

232 registry.registerCollection('templates/foo', lsst.daf.butler.CollectionType.RUN) 

233 

234 # Can't really test Butler's state, so just make sure it exists 

235 self.assertTrue(self._testbed.workButler.isWriteable()) 

236 

237 def testAnalysisButler(self): 

238 """Verify that the Gen 3 Butler is available if and only if the repository is set up. 

239 """ 

240 with self.assertRaises(RuntimeError): 

241 self._testbed.analysisButler 

242 lsst.daf.butler.Butler.makeRepo(self._testbed.repo) 

243 # Can't really test Butler's state, so just make sure it exists 

244 self.assertFalse(self._testbed.analysisButler.isWriteable()) 

245 

246 

247class MemoryTester(lsst.utils.tests.MemoryTestCase): 

248 pass 

249 

250 

251def setup_module(module): 

252 lsst.utils.tests.init() 

253 

254 

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

256 lsst.utils.tests.init() 

257 unittest.main()