-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_cliCmdRemoveRuns.py
167 lines (147 loc) · 7.86 KB
/
test_cliCmdRemoveRuns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for daf_butler CLI remove-runs subcommand."""
import os
import unittest
from lsst.daf.butler import DatasetType
from lsst.daf.butler.cli import butler
from lsst.daf.butler.cli.cmd._remove_runs import (
abortedMsg,
didRemoveDatasetsMsg,
didRemoveRunsMsg,
mustBeUnlinkedMsg,
noRunCollectionsMsg,
removedRunsMsg,
willRemoveDatasetsMsg,
willRemoveRunsMsg,
willUnlinkMsg,
)
from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
from lsst.daf.butler.tests.utils import MetricTestRepo
TESTDIR = os.path.abspath(os.path.dirname(__file__))
class RemoveCollectionTest(unittest.TestCase):
"""Test the butler remove-runs command."""
def setUp(self):
self.runner = LogCliRunner()
def test_removeRuns(self):
with self.runner.isolated_filesystem():
root = "repo"
repo = MetricTestRepo(root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml"))
# Add a dataset type that will have no datasets to make sure it
# isn't printed.
repo.butler.registry.registerDatasetType(
DatasetType("no_datasets", repo.butler.dimensions.empty, "StructuredDataDict")
)
# Execute remove-runs but say no, check for expected outputs.
result = self.runner.invoke(butler.cli, ["remove-runs", root, "ingest*"], input="no")
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(willRemoveRunsMsg, result.output)
self.assertIn(abortedMsg, result.output)
self.assertNotIn("no_datasets", result.output)
self.assertIn(
"ingest/run",
list(repo.butler.registry.queryCollections()),
)
# Add the run to a CHAINED collection.
parentCollection = "aParentCollection"
result = self.runner.invoke(
butler.cli, f"collection-chain {root} {parentCollection} ingest/run".split()
)
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(parentCollection, result.output)
# Execute remove-runs but say no, check for expected outputs
# including the CHAINED collection.
result = self.runner.invoke(butler.cli, ["remove-runs", root, "ingest*"], input="no")
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(willRemoveRunsMsg, result.output)
self.assertIn(willRemoveDatasetsMsg, result.output)
self.assertIn(
willUnlinkMsg.format(run="ingest/run", parents=f'"{parentCollection}"'), result.output
)
self.assertIn(abortedMsg, result.output)
self.assertNotIn("no_datasets", result.output)
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn("ingest/run", result.output)
self.assertIn(parentCollection, result.output)
# Do the same remove-runs command, but say yes.
result = self.runner.invoke(butler.cli, ["remove-runs", root, "ingest*"], input="yes")
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(willRemoveRunsMsg, result.output)
self.assertIn(willRemoveDatasetsMsg, result.output)
self.assertIn(removedRunsMsg, result.output)
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertNotIn("ingest/run", result.output)
self.assertIn(parentCollection, result.output)
# Now they've been deleted, try again and check for "none found".
result = self.runner.invoke(butler.cli, ["remove-runs", root, "ingest*"])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(noRunCollectionsMsg, result.output)
# Remake the repo and check --no-confirm option.
root = "repo1"
MetricTestRepo(root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml"))
# Add the run to a CHAINED collection.
parentCollection = "parent"
result = self.runner.invoke(
butler.cli, f"collection-chain {root} {parentCollection} ingest/run".split()
)
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn("ingest/run", result.output)
self.assertIn(parentCollection, result.output)
# Execute remove-runs with --no-confirm, should fail because there
# is a parent CHAINED collection.
result = self.runner.invoke(butler.cli, ["remove-runs", root, "ingest*", "--no-confirm"])
self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(
mustBeUnlinkedMsg.format(run="ingest/run", parents=f'"{parentCollection}"'), result.output
)
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn("ingest/run", result.output)
self.assertIn(parentCollection, result.output)
# Execute remove-runs with --no-confirm and --force
result = self.runner.invoke(
butler.cli, ["remove-runs", root, "ingest*", "--no-confirm", "--force"]
)
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(didRemoveRunsMsg, result.output)
self.assertIn(didRemoveDatasetsMsg, result.output)
result = self.runner.invoke(butler.cli, ["query-collections", root])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertNotIn("ingest/run", result.output)
self.assertIn(parentCollection, result.output)
# Execute cmd looking for a non-existant collection
result = self.runner.invoke(butler.cli, ["remove-runs", root, "foo"])
self.assertEqual(result.exit_code, 0, clickResultMsg(result))
self.assertIn(noRunCollectionsMsg, result.output)
if __name__ == "__main__":
unittest.main()