Coverage for tests/test_cliCmdRetrieveArtifacts.py: 35%

Shortcuts 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

61 statements  

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 program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

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

23""" 

24 

25import os 

26import unittest 

27from typing import List 

28 

29from lsst.daf.butler import ButlerURI, StorageClassFactory 

30from lsst.daf.butler.cli.butler import cli 

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

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

33 

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

35 

36 

37class CliRetrieveArtifactsTest(unittest.TestCase, ButlerTestHelper): 

38 

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

40 storageClassFactory = StorageClassFactory() 

41 

42 def setUp(self): 

43 self.root = makeTestTempDir(TESTDIR) 

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

45 

46 def tearDown(self): 

47 removeTestTempDir(self.root) 

48 

49 @staticmethod 

50 def find_files(root: str) -> List[ButlerURI]: 

51 return list(ButlerURI.findFileResources([root])) 

52 

53 def testRetrieveAll(self): 

54 runner = LogCliRunner() 

55 with runner.isolated_filesystem(): 

56 

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

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

59 # filename will have an underscore rather than dot. 

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

61 ( 

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

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

64 ) 

65 ): 

66 destdir = f"tmp{counter}/" 

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

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

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

70 

71 artifacts = self.find_files(destdir) 

72 self.assertEqual(len(artifacts), 6, f"Expected 6 artifacts: {artifacts}") 

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

74 

75 def testRetrieveSubset(self): 

76 runner = LogCliRunner() 

77 with runner.isolated_filesystem(): 

78 destdir = "tmp1/" 

79 result = runner.invoke( 

80 cli, 

81 [ 

82 "retrieve-artifacts", 

83 self.root, 

84 destdir, 

85 "--where", 

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

87 ], 

88 ) 

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

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

91 artifacts = self.find_files(destdir) 

92 self.assertEqual(len(artifacts), 3, f"Expected 3 artifacts: {artifacts}") 

93 

94 def testOverwriteLink(self): 

95 runner = LogCliRunner() 

96 with runner.isolated_filesystem(): 

97 destdir = "tmp2/" 

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

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

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

101 # tests for the command line itself. 

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

103 if result.exit_code != 0: 

104 raise unittest.SkipTest( 

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

106 f" {clickResultMsg(result)}" 

107 ) 

108 

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

110 # file. 

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

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

113 

114 def testClobber(self): 

115 runner = LogCliRunner() 

116 with runner.isolated_filesystem(): 

117 destdir = "tmp2/" 

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

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

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

121 

122 # Running again should fail 

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

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

125 

126 # But with clobber should pass 

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

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

129 

130 

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

132 unittest.main()