Coverage for tests/test_cliCmdCleanup.py: 34%

47 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-01-30 10:53 +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/>. 

27 

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

29""" 

30 

31 

32import os 

33import unittest 

34 

35from lsst.ctrl.mpexec.cli.pipetask import cli as pipetask_cli 

36from lsst.daf.butler import Butler 

37from lsst.daf.butler.cli.butler import cli as butler_cli 

38from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg 

39from lsst.daf.butler.tests.utils import MetricTestRepo, makeTestTempDir, removeTestTempDir 

40 

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

42 

43 

44class CleanupCollectionTest(unittest.TestCase): 

45 """Test executing "pipetask cleanup" commands.""" 

46 

47 def setUp(self): 

48 self.runner = LogCliRunner() 

49 

50 # this creates a repo with collections: 

51 # Name Type 

52 # ---------- ------ 

53 # ingest TAGGED 

54 # ingest/run RUN 

55 self.root = makeTestTempDir(TESTDIR) 

56 self.testRepo = MetricTestRepo( 

57 self.root, 

58 configFile=os.path.join(TESTDIR, "config/metricTestRepoButler.yaml"), 

59 ) 

60 

61 def tearDown(self): 

62 removeTestTempDir(self.root) 

63 

64 def test_cleanup_yesNo(self): 

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

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

67 """ 

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

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

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

71 

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

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

74 # "ingest/run" is not removed. 

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

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

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

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

79 

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

81 # outputs. 

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

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

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

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

86 

87 butler = Butler.from_config(self.root) 

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

89 

90 def test_nonExistantCollection(self): 

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

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

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

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

95 

96 def test_removedCollection(self): 

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

98 # Remove the TAGGED collection called "ingest" 

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

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

101 

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

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

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

105 # Purge the collection 

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

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

108 

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

110 # nothing to do. 

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

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

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

114 

115 def test_cleanupNonChained(self): 

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

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

118 self.assertIn( 

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

120 result.output, 

121 ) 

122 

123 

124if __name__ == "__main__": 

125 unittest.main()