Coverage for tests/test_cliCmdRetrieveArtifacts.py: 30%

62 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-07 09:47 +0000

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

33from lsst.resources import ResourcePath 

34 

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

36 

37 

38class CliRetrieveArtifactsTest(unittest.TestCase, ButlerTestHelper): 

39 

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

41 storageClassFactory = StorageClassFactory() 

42 

43 def setUp(self): 

44 self.root = makeTestTempDir(TESTDIR) 

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

46 

47 def tearDown(self): 

48 removeTestTempDir(self.root) 

49 

50 @staticmethod 

51 def find_files(root: str) -> List[ResourcePath]: 

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

53 

54 def testRetrieveAll(self): 

55 runner = LogCliRunner() 

56 with runner.isolated_filesystem(): 

57 

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

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

60 # filename will have an underscore rather than dot. 

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

62 ( 

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

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

65 ) 

66 ): 

67 destdir = f"tmp{counter}/" 

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

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

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

71 

72 artifacts = self.find_files(destdir) 

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

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

75 

76 def testRetrieveSubset(self): 

77 runner = LogCliRunner() 

78 with runner.isolated_filesystem(): 

79 destdir = "tmp1/" 

80 result = runner.invoke( 

81 cli, 

82 [ 

83 "retrieve-artifacts", 

84 self.root, 

85 destdir, 

86 "--where", 

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

88 ], 

89 ) 

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

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

92 artifacts = self.find_files(destdir) 

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

94 

95 def testOverwriteLink(self): 

96 runner = LogCliRunner() 

97 with runner.isolated_filesystem(): 

98 destdir = "tmp2/" 

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

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

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

102 # tests for the command line itself. 

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

104 if result.exit_code != 0: 

105 raise unittest.SkipTest( 

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

107 f" {clickResultMsg(result)}" 

108 ) 

109 

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

111 # file. 

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

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

114 

115 def testClobber(self): 

116 runner = LogCliRunner() 

117 with runner.isolated_filesystem(): 

118 destdir = "tmp2/" 

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

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

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

122 

123 # Running again should fail 

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

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

126 

127 # But with clobber should pass 

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

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

130 

131 

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

133 unittest.main()