Coverage for tests/test_cliCmdPurge.py: 26%
58 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-09 03:40 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-09 03:40 -0700
1# This file is part of ctrl_mpexec.
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 ctrl_mpexec CLI purge subcommand."""
30import os
31import unittest
33from lsst.ctrl.mpexec.cli.pipetask import cli as pipetask_cli
34from lsst.daf.butler.cli.butler import cli as butler_cli
35from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
36from lsst.daf.butler.tests.utils import MetricTestRepo, makeTestTempDir, removeTestTempDir
38TESTDIR = os.path.abspath(os.path.dirname(__file__))
41class PurgeTest(unittest.TestCase):
42 """Test executing "pipetask purge" commands."""
44 def setUp(self):
45 self.runner = LogCliRunner()
47 # this creates a repo with collections:
48 # Name Type
49 # ---------- ------
50 # ingest TAGGED
51 # ingest/run RUN
52 self.root = makeTestTempDir(TESTDIR)
53 self.testRepo = MetricTestRepo(
54 self.root,
55 configFile=os.path.join(TESTDIR, "config/metricTestRepoButler.yaml"),
56 )
58 def tearDown(self):
59 removeTestTempDir(self.root)
61 def test_singleChain_yesNo(self):
62 """Test removing a chain with one child, and the yes/no
63 confirmation.
64 """
65 # add the collection ingest/run to a CHAINED collection called "in"
66 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ingest/run"])
67 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
69 # purge the CHAINED collection called "in", but say "no", check for
70 # expected outputs.
71 result = self.runner.invoke(pipetask_cli, ["purge", "-b", self.root, "in"], input="no")
72 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
73 self.assertIn("Will remove:\n runs: ingest/run\n chains: in\n others: \n", result.output)
74 self.assertIn("Aborted.", result.output)
76 # purge the CHAINED collection called "in", and say "yes", check for
77 # expected outputs.
78 result = self.runner.invoke(pipetask_cli, ["purge", "-b", self.root, "in"], input="yes")
79 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
80 self.assertIn("Will remove:\n runs: ingest/run\n chains: in\n others: \n", result.output)
81 self.assertIn("Done.", result.output)
83 def test_granparentChain_noConfirm(self):
84 """Test removing a chain with children and grandchildren, and the
85 --no-confirm option.
86 """
87 # add the collection ingest/run to a CHAINED collection called "ing"
88 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "ing", "ingest/run"])
89 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
91 # add the CHAINED collectin "ing" a CHAINED collection called "in"
92 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ing"])
93 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
95 # purge the CHAINED collection called "in" with --no-confirm and check
96 # for expected outputs.
97 result = self.runner.invoke(
98 pipetask_cli,
99 ["purge", "-b", self.root, "in", "--recursive", "--no-confirm"],
100 )
101 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
102 self.assertIn("Removed:\n runs: ingest/run\n chains: in, ing\n others: \n", result.output)
104 def test_topParentWithParent(self):
105 """Test that purging a chain with a parent fails."""
106 # add the collection ingest/run to a CHAINED collection called "ing"
107 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "ing", "ingest/run"])
108 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
110 # add the CHAINED collectin "ing" a CHAINED collection called "in"
111 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ing"])
112 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
114 # purge the CHAINED collection called "ing" and check for expected
115 # outputs.
116 result = self.runner.invoke(
117 pipetask_cli,
118 ["purge", "-b", self.root, "ing"],
119 )
120 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
121 self.assertIn(
122 'The passed-in collection "ing" must not be contained in other collections but '
123 'is contained in collection(s) "in".',
124 result.output,
125 )
127 def test_childWithMultipleParents(self):
128 """Test that a child chain with multiple parents fails."""
129 # add the collection ingest/run to a CHAINED collection called "ing"
130 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "ing", "ingest/run"])
131 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
133 # add the collectin ingest/run to a CHAINED collection called "foo"
134 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "foo", "ingest/run"])
135 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
137 # purge the CHAINED collection called "ing" and check for expected
138 # outputs.
139 result = self.runner.invoke(
140 pipetask_cli,
141 ["purge", "-b", self.root, "ing"],
142 )
143 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
144 self.assertIn(
145 'Collection "ingest/run" is in multiple chained collections:',
146 result.output,
147 )
148 self.assertIn('"foo"', result.output)
149 self.assertIn('"ing"', result.output)
151 def test_notFound_notChained(self):
152 """Test for failure when the top level collection is not found,
153 and when a top level connection is not a CHAINED collection.
154 """
155 # Test purging a collection that does not exist.
156 result = self.runner.invoke(
157 pipetask_cli,
158 ["purge", "-b", self.root, "notACollection"],
159 )
160 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
161 self.assertIn(
162 'The passed-in collection "notACollection" was not found.',
163 result.output,
164 )
166 # Test purging a collection that is not a CHAINED collection.
167 result = self.runner.invoke(
168 pipetask_cli,
169 ["purge", "-b", self.root, "ingest/run"],
170 )
171 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
172 self.assertIn(
173 'The passed-in collection must be a CHAINED collection; "ingest/run" is a RUN collection.',
174 result.output,
175 )
178if __name__ == "__main__":
179 unittest.main()