Coverage for tests/test_cliCmdCleanup.py: 33%

46 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 01:37 -0800

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/>. 

27 

28"""Unit tests for ctrl_mpexec CLI cleanup subcommand.""" 

29 

30import os 

31import unittest 

32 

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 

38 

39TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

40 

41 

42class CleanupCollectionTest(unittest.TestCase): 

43 """Test executing "pipetask cleanup" commands.""" 

44 

45 def setUp(self): 

46 self.runner = LogCliRunner() 

47 

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 

59 def tearDown(self): 

60 removeTestTempDir(self.root) 

61 

62 def test_cleanup_yesNo(self): 

63 """Test cleaning up a non-chained collection and that other collections 

64 in the chain remian. Verify the yes/no dialog works. 

65 """ 

66 # add the collection ingest/run to a CHAINED collection called "in" 

67 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ingest/run"]) 

68 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

69 

70 # clean up the CHAINED collection "in", verify that the TAGGED 

71 # collection "ingest" is removed, but the child RUN collection 

72 # "ingest/run" is not removed. 

73 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"], input="n") 

74 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

75 self.assertIn("Will remove:\n runs: \n others: ingest\n", result.output) 

76 self.assertIn("Aborted.", result.output) 

77 

78 # run cleanup again but say "y" to continue, and check for expected 

79 # outputs. 

80 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"], input="y") 

81 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

82 self.assertIn("Will remove:\n runs: \n others: ingest\n", result.output) 

83 self.assertIn("Done.", result.output) 

84 

85 butler = Butler.from_config(self.root) 

86 self.assertEqual(set(butler.registry.queryCollections()), {"in", "ingest/run"}) 

87 

88 def test_nonExistantCollection(self): 

89 """Test running cleanup on a collection that has never existed.""" 

90 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "foo"]) 

91 self.assertEqual(result.exit_code, 1, clickResultMsg(result)) 

92 self.assertIn('Did not find a collection named "foo"', result.output) 

93 

94 def test_removedCollection(self): 

95 """Test cleaning up a collection that used to exist.""" 

96 # Remove the TAGGED collection called "ingest" 

97 result = self.runner.invoke(butler_cli, ["remove-collections", self.root, "ingest"], input="y") 

98 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

99 

100 # Add the collection ingest/run to a CHAINED collection called "in" 

101 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ingest/run"]) 

102 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

103 # Purge the collection 

104 result = self.runner.invoke(pipetask_cli, ["purge", "-b", self.root, "in"], input="no") 

105 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

106 

107 # Attempt to clean up the collection, expect a message that there was 

108 # nothing to do. 

109 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "in"]) 

110 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

111 self.assertIn("Did not find any collections to remove.", result.output) 

112 

113 def test_cleanupNonChained(self): 

114 result = self.runner.invoke(pipetask_cli, ["cleanup", "-b", self.root, "ingest"]) 

115 self.assertEqual(result.exit_code, 1, clickResultMsg(result)) 

116 self.assertIn( 

117 'Error: COLLECTION must be a CHAINED collection, "ingest" is a "TAGGED" collection.', 

118 result.output, 

119 ) 

120 

121 

122if __name__ == "__main__": 

123 unittest.main()