Coverage for tests/test_cliCmdCleanup.py: 38%

49 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-26 09:47 +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/>. 

21 

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

23""" 

24 

25 

26import os 

27import unittest 

28 

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 

34 

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

36 

37 

38class CleanupCollectionTest(unittest.TestCase): 

39 """Test executing "pipetask cleanup" commands.""" 

40 

41 def setUp(self): 

42 self.runner = LogCliRunner() 

43 

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 ) 

54 

55 def tearDown(self): 

56 removeTestTempDir(self.root) 

57 

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 # add the collection ingest/run to a CHAINED collection called "in" 

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

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

64 

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

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

67 # "ingest/run" is not removed. 

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

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

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

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

72 

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

74 # outputs. 

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

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("Done.", result.output) 

79 

80 butler = Butler(self.root) 

81 self.assertEqual(set(butler.registry.queryCollections()), set(["in", "ingest/run"])) 

82 

83 def test_nonExistantCollection(self): 

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

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

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

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

88 

89 def test_removedCollection(self): 

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

91 # Remove the TAGGED collection called "ingest" 

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

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

94 

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

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

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

98 # Purge the collection 

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

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

101 

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

103 # nothing to do. 

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

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

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

107 

108 def test_cleanupNonChained(self): 

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

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

111 self.assertIn( 

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

113 result.output, 

114 ) 

115 

116 

117if __name__ == "__main__": 117 ↛ 118line 117 didn't jump to line 118, because the condition on line 117 was never true

118 unittest.main()