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 self._testWorkspace = tempfile.mkdtemp() 

39 self._testbed = WorkspaceGen2(self._testWorkspace) 

40 

41 def tearDown(self): 

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

43 

44 def _assertInDir(self, path, baseDir): 

45 """Test that ``path`` is a subpath of ``baseDir``. 

46 """ 

47 _canonPath = os.path.abspath(os.path.realpath(path)) 

48 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

49 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

50 self.assertEqual(ancestor, _canonDir) 

51 

52 def _assertNotInDir(self, path, baseDir): 

53 """Test that ``path`` is not a subpath of ``baseDir``. 

54 """ 

55 _canonPath = os.path.abspath(os.path.realpath(path)) 

56 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

57 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

58 self.assertNotEqual(ancestor, _canonDir) 

59 

60 def testMakeDir(self): 

61 """Verify that a Workspace creates the workspace directory if it does not exist. 

62 """ 

63 newPath = '_temp2' # can't use mkdtemp because creation is what we're testing 

64 shutil.rmtree(newPath, ignore_errors=True) 

65 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.') 

66 

67 try: 

68 WorkspaceGen2(newPath) 

69 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.') 

70 finally: 

71 shutil.rmtree(newPath, ignore_errors=True) 

72 

73 @staticmethod 

74 def _allRepos(workspace): 

75 """An iterator over all repos exposed by a WorkspaceGen2. 

76 """ 

77 yield workspace.dataRepo 

78 yield workspace.calibRepo 

79 yield workspace.templateRepo 

80 yield workspace.outputRepo 

81 

82 def testDirectories(self): 

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

84 

85 The exact repository locations are not tested, as they are likely to change. 

86 """ 

87 # Workspace should report all paths as absolute 

88 root = os.path.abspath(os.path.realpath(self._testWorkspace)) 

89 self.assertEqual(self._testbed.workDir, root) 

90 self._assertInDir(self._testbed.configDir, root) 

91 for repo in self._allRepos(self._testbed): 

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

93 self._assertInDir(url2pathname(repo), root) 

94 

95 def testDatabase(self): 

96 """Verify that a WorkspaceGen2 requests a database file in the target 

97 directory, but not in any repository. 

98 """ 

99 root = self._testWorkspace 

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

101 for repo in self._allRepos(self._testbed): 

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

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

104 

105 def testAlerts(self): 

106 """Verify that a WorkspaceGen2 requests an alert dump in the target 

107 directory, but not in any repository. 

108 """ 

109 root = self._testWorkspace 

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

111 for repo in self._allRepos(self._testbed): 

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

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

114 

115 

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

117 

118 def setUp(self): 

119 self._testWorkspace = tempfile.mkdtemp() 

120 self._testbed = WorkspaceGen3(self._testWorkspace) 

121 

122 def tearDown(self): 

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

124 

125 def _assertInDir(self, path, baseDir): 

126 """Test that ``path`` is a subpath of ``baseDir``. 

127 """ 

128 _canonPath = os.path.abspath(os.path.realpath(path)) 

129 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

130 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

131 self.assertEqual(ancestor, _canonDir) 

132 

133 def _assertNotInDir(self, path, baseDir): 

134 """Test that ``path`` is not a subpath of ``baseDir``. 

135 """ 

136 _canonPath = os.path.abspath(os.path.realpath(path)) 

137 _canonDir = os.path.abspath(os.path.realpath(baseDir)) 

138 ancestor = os.path.commonprefix([_canonPath, _canonDir]) 

139 self.assertNotEqual(ancestor, _canonDir) 

140 

141 def testMakeDir(self): 

142 """Verify that a Workspace creates the workspace directory if it does not exist. 

143 """ 

144 newPath = '_temp3' # can't use mkdtemp because creation is what we're testing 

145 shutil.rmtree(newPath, ignore_errors=True) 

146 self.assertFalse(os.path.exists(newPath), 'Workspace directory must not exist before test.') 

147 

148 try: 

149 WorkspaceGen3(newPath) 

150 self.assertTrue(os.path.exists(newPath), 'Workspace directory must exist.') 

151 finally: 

152 shutil.rmtree(newPath, ignore_errors=True) 

153 

154 def testDirectories(self): 

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

156 

157 The exact locations are not tested, as they are likely to change. 

158 """ 

159 # Workspace should report all paths as absolute 

160 root = os.path.abspath(os.path.realpath(self._testWorkspace)) 

161 self.assertEqual(self._testbed.workDir, root) 

162 self._assertInDir(self._testbed.configDir, root) 

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

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

165 

166 def testDatabase(self): 

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

168 directory, but not in any repository. 

169 """ 

170 root = self._testWorkspace 

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

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

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

174 

175 def testAlerts(self): 

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

177 directory, but not in any repository. 

178 """ 

179 root = self._testWorkspace 

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

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

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

183 

184 def testWorkButler(self): 

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

186 """ 

187 with self.assertRaises(RuntimeError): 

188 self._testbed.workButler 

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

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

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

192 

193 def testAnalysisButler(self): 

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

195 """ 

196 with self.assertRaises(RuntimeError): 

197 self._testbed.analysisButler 

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

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

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

201 

202 

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

204 pass 

205 

206 

207def setup_module(module): 

208 lsst.utils.tests.init() 

209 

210 

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

212 lsst.utils.tests.init() 

213 unittest.main()