Coverage for tests/test_cliCmdRetrieveArtifacts.py: 30%
59 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 08:00 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 08:00 +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 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/>.
28"""Unit tests for daf_butler CLI retrieve-artifacts command.
29"""
31import os
32import unittest
34from lsst.daf.butler import StorageClassFactory
35from lsst.daf.butler.cli.butler import cli
36from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
37from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, makeTestTempDir, removeTestTempDir
38from lsst.resources import ResourcePath
40TESTDIR = os.path.abspath(os.path.dirname(__file__))
43class CliRetrieveArtifactsTest(unittest.TestCase, ButlerTestHelper):
44 """Test the retrieve-artifacts command-line."""
46 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
47 storageClassFactory = StorageClassFactory()
49 def setUp(self):
50 self.root = makeTestTempDir(TESTDIR)
51 self.testRepo = MetricTestRepo(self.root, configFile=self.configFile)
53 def tearDown(self):
54 removeTestTempDir(self.root)
56 @staticmethod
57 def find_files(root: str) -> list[ResourcePath]:
58 return list(ResourcePath.findFileResources([root]))
60 def testRetrieveAll(self):
61 runner = LogCliRunner()
62 with runner.isolated_filesystem():
63 # When preserving the path the run will be in the directory along
64 # with a . in the component name. When not preserving paths the
65 # filename will have an underscore rather than dot.
66 for counter, (preserve_path, prefix) in enumerate(
67 (
68 ("--preserve-path", "ingest/run/test_metric_comp."),
69 ("--no-preserve-path", "test_metric_comp_"),
70 )
71 ):
72 destdir = f"tmp{counter}/"
73 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, preserve_path])
74 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
75 self.assertTrue(result.stdout.endswith(": 6\n"), f"Expected 6 got: {result.stdout}")
77 artifacts = self.find_files(destdir)
78 self.assertEqual(len(artifacts), 6, f"Expected 6 artifacts: {artifacts}")
79 self.assertIn(f"{destdir}{prefix}", str(artifacts[1]))
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 self.assertEqual(len(artifacts), 3, f"Expected 3 artifacts: {artifacts}")
100 def testOverwriteLink(self):
101 runner = LogCliRunner()
102 with runner.isolated_filesystem():
103 destdir = "tmp2/"
104 # Force hardlink -- if this fails assume that it is because
105 # hardlinks are not supported (/tmp and TESTDIR are on
106 # different file systems) and skip the test. There are other
107 # tests for the command line itself.
108 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "hardlink"])
109 if result.exit_code != 0:
110 raise unittest.SkipTest(
111 "hardlink not supported between these directories for this test:"
112 f" {clickResultMsg(result)}"
113 )
115 # Running again should pass because hard links are the same
116 # file.
117 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir])
118 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
120 def testClobber(self):
121 runner = LogCliRunner()
122 with runner.isolated_filesystem():
123 destdir = "tmp2/"
124 # Force copy so we can ensure that overwrite tests will trigger.
125 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--transfer", "copy"])
126 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
128 # Running again should fail
129 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir])
130 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
132 # But with clobber should pass
133 result = runner.invoke(cli, ["retrieve-artifacts", self.root, destdir, "--clobber"])
134 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
137if __name__ == "__main__":
138 unittest.main()