Coverage for tests / test_cliCmdCleanup.py: 32%
47 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:48 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:48 +0000
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 cleanup subcommand."""
30import os
31import unittest
33from lsst.ctrl.mpexec.cli.pipetask import cli as pipetask_cli
34from lsst.daf.butler import Butler
35from lsst.daf.butler.cli.butler import cli as butler_cli
36from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
37from lsst.daf.butler.tests.utils import MetricTestRepo, makeTestTempDir, removeTestTempDir
39TESTDIR = os.path.abspath(os.path.dirname(__file__))
42class CleanupCollectionTest(unittest.TestCase):
43 """Test executing "pipetask cleanup" commands."""
45 def setUp(self):
46 self.runner = LogCliRunner()
48 # this creates a repo with collections:
49 # Name Type
50 # ---------- ------
51 # ingest TAGGED
52 # ingest/run RUN
53 self.root = makeTestTempDir(TESTDIR)
54 self.testRepo = MetricTestRepo(
55 self.root,
56 configFile=os.path.join(TESTDIR, "config/metricTestRepoButler.yaml"),
57 )
58 self.enterContext(self.testRepo.butler)
60 def tearDown(self):
61 removeTestTempDir(self.root)
63 def test_cleanup_yesNo(self):
64 """Test cleaning up a non-chained collection and that other collections
65 in the chain remian. Verify the yes/no dialog works.
66 """
67 # add the collection ingest/run to a CHAINED collection called "in"
68 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ingest/run"])
69 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
71 # clean up the CHAINED collection "in", verify that the TAGGED
72 # collection "ingest" is removed, but the child RUN collection
73 # "ingest/run" is not removed.
74 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"], input="n")
75 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
76 self.assertIn("Will remove:\n runs: \n others: ingest\n", result.output)
77 self.assertIn("Aborted.", result.output)
79 # run cleanup again but say "y" to continue, and check for expected
80 # outputs.
81 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"], input="y")
82 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
83 self.assertIn("Will remove:\n runs: \n others: ingest\n", result.output)
84 self.assertIn("Done.", result.output)
86 with Butler.from_config(self.root) as butler:
87 self.assertEqual(set(butler.registry.queryCollections()), {"in", "ingest/run"})
89 def test_nonExistantCollection(self):
90 """Test running cleanup on a collection that has never existed."""
91 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "foo"])
92 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
93 self.assertIn('Did not find a collection named "foo"', result.output)
95 def test_removedCollection(self):
96 """Test cleaning up a collection that used to exist."""
97 # Remove the TAGGED collection called "ingest"
98 result = self.runner.invoke(butler_cli, ["remove-collections", self.root, "ingest"], input="y")
99 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
101 # Add the collection ingest/run to a CHAINED collection called "in"
102 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ingest/run"])
103 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
104 # Purge the collection
105 result = self.runner.invoke(pipetask_cli, ["purge", "-b", self.root, "in"], input="no")
106 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
108 # Attempt to clean up the collection, expect a message that there was
109 # nothing to do.
110 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"])
111 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
112 self.assertIn("Did not find any collections to remove.", result.output)
114 def test_cleanupNonChained(self):
115 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "ingest"])
116 self.assertEqual(result.exit_code, 1, clickResultMsg(result))
117 self.assertIn(
118 'Error: COLLECTION must be a CHAINED collection, "ingest" is a "TAGGED" collection.',
119 result.output,
120 )
123if __name__ == "__main__":
124 unittest.main()