Coverage for tests/test_cliCmdRetrieveArtifacts.py: 27%

69 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-07 01:48 -0800

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

27 

28"""Unit tests for daf_butler CLI retrieve-artifacts command.""" 

29 

30import os 

31import unittest 

32 

33from lsst.daf.butler import StorageClassFactory 

34from lsst.daf.butler.cli.butler import cli 

35from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg 

36from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir 

37from lsst.resources import ResourcePath 

38 

39TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

40 

41 

42class CliRetrieveArtifactsTest(unittest.TestCase, ButlerTestHelper): 

43 """Test the retrieve-artifacts command-line.""" 

44 

45 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

46 storageClassFactory = StorageClassFactory() 

47 

48 def setUp(self): 

49 self.root = makeTestTempDir(TESTDIR) 

50 self.testRepo = MetricTestRepo(self.root, configFile=self.configFile) 

51 

52 def tearDown(self): 

53 removeTestTempDir(self.root) 

54 

55 @staticmethod 

56 def find_files(root: str) -> list[ResourcePath]: 

57 return list(ResourcePath.findFileResources([root])) 

58 

59 def testRetrieveAll(self): 

60 runner = LogCliRunner() 

61 with runner.isolated_filesystem(): 

62 # When preserving the path the run will be in the directory along 

63 # with a . in the component name. When not preserving paths the 

64 # filename will have an underscore rather than dot. 

65 for counter, (preserve_path, prefix) in enumerate( 

66 ( 

67 ("--preserve-path", "ingest/run/test_metric_comp."), 

68 ("--no-preserve-path", "test_metric_comp_"), 

69 ) 

70 ): 

71 destdir = f"tmp{counter}/" 

72 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, preserve_path]) 

73 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

74 self.assertTrue(result.stdout.endswith(": 6\n"), f"Expected 6 got: {result.stdout}") 

75 

76 artifacts = self.find_files(destdir) 

77 artifacts.sort() 

78 self.assertEqual(len(artifacts), 7, f"Expected 7 artifacts including index: {artifacts}") 

79 self.assertIn(f"{destdir}{prefix}", str(artifacts[1])) 

80 

81 def testRetrieveSubset(self): 

82 runner = LogCliRunner() 

83 with runner.isolated_filesystem(): 

84 destdir = "tmp1/" 

85 result = runner.invoke( 

86 cli, 

87 [ 

88 "retrieve-artifacts", 

89 self.root, 

90 destdir, 

91 "--where", 

92 "instrument='DummyCamComp' AND visit=423", 

93 ], 

94 ) 

95 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

96 self.assertTrue(result.stdout.endswith(": 3\n"), f"Expected 3 got: {result.stdout}") 

97 artifacts = self.find_files(destdir) 

98 artifacts.sort() 

99 self.assertEqual(len(artifacts), 4, f"Expected 4 artifacts including index: {artifacts}") 

100 

101 def testRetrieveAsZip(self): 

102 runner = LogCliRunner() 

103 with runner.isolated_filesystem(): 

104 destdir = "tmp1/" 

105 result = runner.invoke( 

106 cli, 

107 [ 

108 "retrieve-artifacts", 

109 self.root, 

110 destdir, 

111 "--where", 

112 "instrument='DummyCamComp' AND visit=423", 

113 "--zip", 

114 ], 

115 ) 

116 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

117 self.assertIn(".zip", result.stdout) 

118 artifacts = self.find_files(destdir) 

119 self.assertEqual(len(artifacts), 1, f"Expected one zip file: {artifacts}") 

120 

121 def testOverwriteLink(self): 

122 runner = LogCliRunner() 

123 with runner.isolated_filesystem(): 

124 destdir = "tmp2/" 

125 # Force hardlink -- if this fails assume that it is because 

126 # hardlinks are not supported (/tmp and TESTDIR are on 

127 # different file systems) and skip the test. There are other 

128 # tests for the command line itself. 

129 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "hardlink"]) 

130 if result.exit_code != 0: 

131 raise unittest.SkipTest( 

132 "hardlink not supported between these directories for this test:" 

133 f" {clickResultMsg(result)}" 

134 ) 

135 

136 # Running again should pass because hard links are the same 

137 # file. 

138 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir]) 

139 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

140 

141 def testClobber(self): 

142 runner = LogCliRunner() 

143 with runner.isolated_filesystem(): 

144 destdir = "tmp2/" 

145 # Force copy so we can ensure that overwrite tests will trigger. 

146 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "copy"]) 

147 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

148 

149 # Running again should fail 

150 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir]) 

151 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

152 

153 # But with clobber should pass 

154 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--clobber"]) 

155 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

156 

157 

158if __name__ == "__main__": 

159 unittest.main()