Coverage for tests/test_cliCmdPurge.py: 27%

59 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-03 02:52 -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/>. 

27 

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

29""" 

30 

31 

32import os 

33import unittest 

34 

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

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

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

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

39 

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

41 

42 

43class PurgeTest(unittest.TestCase): 

44 """Test executing "pipetask purge" commands.""" 

45 

46 def setUp(self): 

47 self.runner = LogCliRunner() 

48 

49 # this creates a repo with collections: 

50 # Name Type 

51 # ---------- ------ 

52 # ingest TAGGED 

53 # ingest/run RUN 

54 self.root = makeTestTempDir(TESTDIR) 

55 self.testRepo = MetricTestRepo( 

56 self.root, 

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

58 ) 

59 

60 def tearDown(self): 

61 removeTestTempDir(self.root) 

62 

63 def test_singleChain_yesNo(self): 

64 """Test removing a chain with one child, and the yes/no 

65 confirmation. 

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)) 

70 

71 # purge the CHAINED collection called "in", but say "no", check for 

72 # expected outputs. 

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

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

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

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

77 

78 # purge the CHAINED collection called "in", and say "yes", check for 

79 # expected outputs. 

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

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

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

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

84 

85 def test_granparentChain_noConfirm(self): 

86 """Test removing a chain with children and grandchildren, and the 

87 --no-confirm option. 

88 """ 

89 # add the collection ingest/run to a CHAINED collection called "ing" 

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

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

92 

93 # add the CHAINED collectin "ing" a CHAINED collection called "in" 

94 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ing"]) 

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

96 

97 # purge the CHAINED collection called "in" with --no-confirm and check 

98 # for expected outputs. 

99 result = self.runner.invoke( 

100 pipetask_cli, 

101 ["purge", "-b", self.root, "in", "--recursive", "--no-confirm"], 

102 ) 

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

104 self.assertIn("Removed:\n runs: ingest/run\n chains: in, ing\n others: \n", result.output) 

105 

106 def test_topParentWithParent(self): 

107 """Test that purging a chain with a parent fails.""" 

108 # add the collection ingest/run to a CHAINED collection called "ing" 

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

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

111 

112 # add the CHAINED collectin "ing" a CHAINED collection called "in" 

113 result = self.runner.invoke(butler_cli, ["collection-chain", self.root, "in", "ing"]) 

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

115 

116 # purge the CHAINED collection called "ing" and check for expected 

117 # outputs. 

118 result = self.runner.invoke( 

119 pipetask_cli, 

120 ["purge", "-b", self.root, "ing"], 

121 ) 

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

123 self.assertIn( 

124 'The passed-in collection "ing" must not be contained in other collections but ' 

125 'is contained in collection(s) "in".', 

126 result.output, 

127 ) 

128 

129 def test_childWithMultipleParents(self): 

130 """Test that a child chain with multiple parents fails.""" 

131 # add the collection ingest/run to a CHAINED collection called "ing" 

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

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

134 

135 # add the collectin ingest/run to a CHAINED collection called "foo" 

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

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

138 

139 # purge the CHAINED collection called "ing" and check for expected 

140 # outputs. 

141 result = self.runner.invoke( 

142 pipetask_cli, 

143 ["purge", "-b", self.root, "ing"], 

144 ) 

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

146 self.assertIn( 

147 'Collection "ingest/run" is in multiple chained collections:', 

148 result.output, 

149 ) 

150 self.assertIn('"foo"', result.output) 

151 self.assertIn('"ing"', result.output) 

152 

153 def test_notFound_notChained(self): 

154 """Test for failure when the top level collection is not found, 

155 and when a top level connection is not a CHAINED collection. 

156 """ 

157 # Test purging a collection that does not exist. 

158 result = self.runner.invoke( 

159 pipetask_cli, 

160 ["purge", "-b", self.root, "notACollection"], 

161 ) 

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

163 self.assertIn( 

164 'The passed-in collection "notACollection" was not found.', 

165 result.output, 

166 ) 

167 

168 # Test purging a collection that is not a CHAINED collection. 

169 result = self.runner.invoke( 

170 pipetask_cli, 

171 ["purge", "-b", self.root, "ingest/run"], 

172 ) 

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

174 self.assertIn( 

175 'The passed-in collection must be a CHAINED collection; "ingest/run" is a RUN collection.', 

176 result.output, 

177 ) 

178 

179 

180if __name__ == "__main__": 

181 unittest.main()