Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

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 daf_butler CLI prune-collections subcommand. 

23""" 

24 

25from astropy.table import Table 

26from numpy import array 

27import os 

28import shutil 

29import tempfile 

30import unittest 

31 

32from lsst.daf.butler import Butler 

33from lsst.daf.butler.cli.butler import cli as butlerCli 

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

35from lsst.daf.butler.tests.utils import ButlerTestHelper, MetricTestRepo, readTable 

36 

37 

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

39 

40 

41class PruneCollectionsTest(unittest.TestCase): 

42 

43 def setUp(self): 

44 self.runner = LogCliRunner() 

45 

46 def testPruneCollections(self): 

47 """Test removing a collection and run from a repository using the 

48 butler prune-collection subcommand.""" 

49 with self.runner.isolated_filesystem(): 

50 repoName = "myRepo" 

51 runName = "myRun" 

52 taggedName = "taggedCollection" 

53 

54 # Add the run and the tagged collection to the repo: 

55 result = self.runner.invoke(butlerCli, ["create", repoName]) 

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

57 # Use the butler initalizer to create the run and tagged 

58 # collection. 

59 Butler(repoName, run=runName, tags=[taggedName]) 

60 

61 # Verify the run and tag show up in query-collections: 

62 result = self.runner.invoke(butlerCli, ["query-collections", repoName]) 

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

64 self.assertIn(runName, result.output) 

65 self.assertIn(taggedName, result.output) 

66 

67 # Verify the tagged collection can be removed: 

68 result = self.runner.invoke(butlerCli, ["prune-collection", repoName, 

69 taggedName, 

70 "--unstore"]) 

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

72 result = self.runner.invoke(butlerCli, ["query-collections", repoName]) 

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

74 self.assertIn(runName, result.output) 

75 self.assertNotIn(taggedName, result.output) 

76 

77 # Verify the run can be removed: 

78 result = self.runner.invoke(butlerCli, ["prune-collection", repoName, 

79 runName, 

80 "--purge", 

81 "--unstore"]) 

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

83 self.assertNotIn(runName, result.output) 

84 self.assertNotIn(taggedName, result.output) 

85 

86 

87class PruneCollectionExecutionTest(unittest.TestCase, ButlerTestHelper): 

88 """Test executing a small number of basic prune-collections commands to 

89 verify collections can be pruned. 

90 """ 

91 

92 def setUp(self): 

93 self.runner = LogCliRunner() 

94 

95 self.root = tempfile.mkdtemp(dir=TESTDIR) 

96 self.testRepo = MetricTestRepo(self.root, 

97 configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")) 

98 

99 def tearDown(self): 

100 if os.path.exists(self.root): 

101 shutil.rmtree(self.root, ignore_errors=True) 

102 

103 def testPruneRun(self): 

104 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

106 expected = Table(array((("ingest/run", "RUN"), 

107 ("ingest", "TAGGED"))), 

108 names=("Name", "Type")) 

109 self.assertAstropyTablesEqual(readTable(result.output), expected) 

110 

111 # Try pruning RUN without purge or unstore, should fail. 

112 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run"]) 

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

114 

115 # Try pruning RUN without unstore, should fail. 

116 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

117 "--purge"]) 

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

119 

120 # Try pruning RUN without purge, should fail. 

121 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

122 "--unstore"]) 

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

124 

125 # Try pruning RUN with purge and unstore, should succeed. 

126 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest/run", 

127 "--purge", "--unstore"]) 

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

129 

130 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

132 expected = Table((["ingest"], ["TAGGED"]), 

133 names=("Name", "Type")) 

134 self.assertAstropyTablesEqual(readTable(result.output), expected) 

135 

136 def testPruneTagged(self): 

137 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

139 expected = Table(array((("ingest/run", "RUN"), 

140 ("ingest", "TAGGED"))), 

141 names=("Name", "Type")) 

142 self.assertAstropyTablesEqual(readTable(result.output), expected) 

143 

144 # Try pruning TAGGED, should succeed. 

145 result = self.runner.invoke(butlerCli, ["prune-collection", self.root, "ingest", "--unstore"]) 

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

147 

148 result = self.runner.invoke(butlerCli, ["query-collections", self.root]) 

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

150 expected = Table((["ingest/run"], ["RUN"]), 

151 names=("Name", "Type")) 

152 self.assertAstropyTablesEqual(readTable(result.output), expected) 

153 

154 

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

156 unittest.main()