Coverage for tests/test_cliCmdRetrieveArtifacts.py: 28%
59 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-14 09:11 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-14 09:11 +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/>.
22"""Unit tests for daf_butler CLI retrieve-artifacts command.
23"""
25import os
26import unittest
28from lsst.daf.butler import StorageClassFactory
29from lsst.daf.butler.cli.butler import cli
30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
31from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir
32from lsst.resources import ResourcePath
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class CliRetrieveArtifactsTest(unittest.TestCase, ButlerTestHelper):
38 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
39 storageClassFactory = StorageClassFactory()
41 def setUp(self):
42 self.root = makeTestTempDir(TESTDIR)
43 self.testRepo = MetricTestRepo(self.root, configFile=self.configFile)
45 def tearDown(self):
46 removeTestTempDir(self.root)
48 @staticmethod
49 def find_files(root: str) -> list[ResourcePath]:
50 return list(ResourcePath.findFileResources([root]))
52 def testRetrieveAll(self):
53 runner = LogCliRunner()
54 with runner.isolated_filesystem():
55 # When preserving the path the run will be in the directory along
56 # with a . in the component name. When not preserving paths the
57 # filename will have an underscore rather than dot.
58 for counter, (preserve_path, prefix) in enumerate(
59 (
60 ("--preserve-path", "ingest/run/test_metric_comp."),
61 ("--no-preserve-path", "test_metric_comp_"),
62 )
63 ):
64 destdir = f"tmp{counter}/"
65 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, preserve_path])
66 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
67 self.assertTrue(result.stdout.endswith(": 6\n"), f"Expected 6 got: {result.stdout}")
69 artifacts = self.find_files(destdir)
70 self.assertEqual(len(artifacts), 6, f"Expected 6 artifacts: {artifacts}")
71 self.assertIn(f"{destdir}{prefix}", str(artifacts[1]))
73 def testRetrieveSubset(self):
74 runner = LogCliRunner()
75 with runner.isolated_filesystem():
76 destdir = "tmp1/"
77 result = runner.invoke(
78 cli,
79 [
80 "retrieve-artifacts",
81 self.root,
82 destdir,
83 "--where",
84 "instrument='DummyCamComp' AND visit=423",
85 ],
86 )
87 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
88 self.assertTrue(result.stdout.endswith(": 3\n"), f"Expected 3 got: {result.stdout}")
89 artifacts = self.find_files(destdir)
90 self.assertEqual(len(artifacts), 3, f"Expected 3 artifacts: {artifacts}")
92 def testOverwriteLink(self):
93 runner = LogCliRunner()
94 with runner.isolated_filesystem():
95 destdir = "tmp2/"
96 # Force hardlink -- if this fails assume that it is because
97 # hardlinks are not supported (/tmp and TESTDIR are on
98 # different file systems) and skip the test. There are other
99 # tests for the command line itself.
100 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "hardlink"])
101 if result.exit_code != 0:
102 raise unittest.SkipTest(
103 "hardlink not supported between these directories for this test:"
104 f" {clickResultMsg(result)}"
105 )
107 # Running again should pass because hard links are the same
108 # file.
109 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir])
110 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
112 def testClobber(self):
113 runner = LogCliRunner()
114 with runner.isolated_filesystem():
115 destdir = "tmp2/"
116 # Force copy so we can ensure that overwrite tests will trigger.
117 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "copy"])
118 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
120 # Running again should fail
121 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir])
122 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
124 # But with clobber should pass
125 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--clobber"])
126 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
129if __name__ == "__main__":
130 unittest.main()