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