/
test_cli.py
255 lines (173 loc) · 9.16 KB
/
test_cli.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import threading
import time
from unittest.mock import patch
import pytest
from aria2p import cli
from . import BUNSENLABS_MAGNET, SESSIONS_DIR, TESTS_DATA_DIR, Aria2Server
def out_lines(cs):
return cs.readouterr().out.split("\n")
def err_lines(cs):
return cs.readouterr().err.split("\n")
def first_out_line(cs):
return out_lines(cs)[0]
def first_err_line(cs):
return err_lines(cs)[0]
def test_main_returns_2_when_no_remote_running():
assert cli.main(["--port=7549"]) == 2
def test_parser_error_when_gids_and_all_option(capsys):
with pytest.raises(SystemExit) as e:
cli.main(["pause", "-a", "2089b05ecca3d829"])
assert e.value.code == 2
lines = err_lines(capsys)
assert lines[0].startswith("usage: aria2p pause")
assert lines[1].endswith("-a/--all: not allowed with arguments gids")
def test_parser_error_when_no_gid_and_no_all_option(capsys):
def assert_func(command, alias):
with pytest.raises(SystemExit) as e:
cli.main([alias])
assert e.value.code == 2
lines = err_lines(capsys)
assert lines[0].startswith("usage: aria2p " + command)
assert lines[1].endswith("the following arguments are required: gids or --all")
for command, aliases in [
("pause", ["stop"]),
("remove", ["rm", "del", "delete"]),
("resume", ["start"]),
("purge", ["clear"]),
]:
assert_func(command, command)
for alias in aliases:
assert_func(command, alias)
@patch("aria2p.cli.subcommand_purge")
def test_parser_no_error(mocked_function):
with Aria2Server(port=7550) as server:
cli.main(["-p", str(server.port), "purge", "-a"])
assert mocked_function.called
@patch("aria2p.cli.subcommand_show")
def test_main_no_command_defaults_to_show(mocked_function):
with Aria2Server(port=7500) as server:
cli.main(["-p", str(server.port)])
assert mocked_function.called
def test_main_show_subcommand(capsys):
with Aria2Server(port=7501) as server:
cli.main(["-p", str(server.port), "show"])
first_line = first_out_line(capsys)
for word in ("GID", "STATUS", "PROGRESS", "DOWN_SPEED", "UP_SPEED", "ETA", "NAME"):
assert word in first_line
def test_errors_and_print_message(capsys):
with Aria2Server(port=7502) as server:
assert cli.main(["-p", str(server.port), "call", "tellstatus", "-P", "invalid gid"]) > 0
assert capsys.readouterr().err == "Invalid GID invalid gid\n"
def test_show_subcommand(capsys):
with Aria2Server(port=7503, session=SESSIONS_DIR / "dl-aria2-1.34.0-paused.txt") as server:
assert cli.subcommand_show(server.api) == 0
assert len(capsys.readouterr().out.rstrip("\n").split("\n")) == 2
def test_call_subcommand(capsys):
with Aria2Server(port=7504) as server:
assert cli.subcommand_call(server.api, "wrongMethod", []) == 1
assert (
capsys.readouterr().err == "aria2p: call: Unknown method wrongMethod.\n"
" Run 'aria2p call listmethods' to list the available methods.\n"
)
def test_call_subcommand_with_json_params():
with Aria2Server(port=7505, session=SESSIONS_DIR / "dl-aria2-1.34.0-paused.txt") as server:
assert cli.subcommand_call(server.api, "tellstatus", '["2089b05ecca3d829"]') == 0
def test_call_subcommand_with_no_params():
with Aria2Server(port=7506) as server:
assert cli.subcommand_call(server.api, "listmethods", None) == 0
def test_add_magnet_subcommand():
with Aria2Server(port=7507) as server:
assert cli.subcommand_add_magnet(server.api, BUNSENLABS_MAGNET) == 0
def test_add_torrent_subcommand():
with Aria2Server(port=7508) as server:
assert cli.subcommand_add_torrent(server.api, TESTS_DATA_DIR / "bunsenlabs-helium-4.iso.torrent") == 0
def test_add_metalink_subcommand():
with Aria2Server(port=7509) as server:
assert cli.subcommand_add_metalink(server.api, TESTS_DATA_DIR / "debian.metalink") == 0
def test_pause_subcommand(capsys):
with Aria2Server(port=7510, session=SESSIONS_DIR / "dl-aria2-1.34.0.txt") as server:
assert cli.subcommand_pause(server.api, ["2089b05ecca3d829"]) == 0
def test_pause_subcommand_already_paused(capsys):
with Aria2Server(port=7511, session=SESSIONS_DIR / "2-dl-in-queue.txt") as server:
assert cli.subcommand_pause(server.api, ["2089b05ecca3d829", "cca3d8292089b05e"]) == 1
assert (
capsys.readouterr().err
== "GID#2089b05ecca3d829 cannot be paused now\nGID#cca3d8292089b05e cannot be paused now\n"
)
def test_pause_subcommand_one_paused(capsys):
with Aria2Server(port=7512, session=SESSIONS_DIR / "one-active-one-paused.txt") as server:
assert cli.subcommand_pause(server.api, ["2089b05ecca3d829", "cca3d8292089b05e"]) == 1
assert capsys.readouterr().err == "GID#cca3d8292089b05e cannot be paused now\n"
def test_pause_all_subcommand():
with Aria2Server(port=7513) as server:
assert cli.subcommand_pause(server.api, do_all=True) == 0
def test_pause_all_subcommand_doesnt_fail_with_already_paused_downloads():
with Aria2Server(port=7514, session=SESSIONS_DIR / "2-dl-in-queue.txt") as server:
assert cli.subcommand_pause(server.api, do_all=True) == 0
def test_resume_subcommand(capsys):
with Aria2Server(port=7515, session=SESSIONS_DIR / "dl-aria2-1.34.0-paused.txt") as server:
assert cli.subcommand_resume(server.api, ["2089b05ecca3d829"]) == 0
def test_resume_subcommand_already_unpaused(capsys):
with Aria2Server(port=7516, session=SESSIONS_DIR / "dl-2-aria2.txt") as server:
assert cli.subcommand_resume(server.api, ["2089b05ecca3d829", "cca3d8292089b05e"]) == 1
assert (
capsys.readouterr().err
== "GID#2089b05ecca3d829 cannot be unpaused now\nGID#cca3d8292089b05e cannot be unpaused now\n"
)
def test_resume_subcommand_one_unpaused(capsys):
with Aria2Server(port=7517, session=SESSIONS_DIR / "one-active-one-paused.txt") as server:
assert cli.subcommand_resume(server.api, ["2089b05ecca3d829", "cca3d8292089b05e"]) == 1
assert capsys.readouterr().err == "GID#2089b05ecca3d829 cannot be unpaused now\n"
def test_resume_all_subcommand():
with Aria2Server(port=7518) as server:
assert cli.subcommand_resume(server.api, do_all=True) == 0
def test_resume_all_subcommand_doesnt_fail_with_already_active_downloads():
with Aria2Server(port=7519, session=SESSIONS_DIR / "dl-2-aria2.txt") as server:
assert cli.subcommand_resume(server.api, do_all=True) == 0
def test_remove_subcommand():
with Aria2Server(port=7520, session=SESSIONS_DIR / "dl-aria2-1.34.0-paused.txt") as server:
assert cli.subcommand_remove(server.api, ["2089b05ecca3d829"]) == 0
def test_remove_subcommand_one_failure(capsys):
with Aria2Server(port=7521, session=SESSIONS_DIR / "dl-aria2-1.34.0-paused.txt") as server:
assert cli.subcommand_remove(server.api, ["2089b05ecca3d829", "cca3d8292089b05e"]) == 1
assert capsys.readouterr().err == "GID cca3d8292089b05e is not found\n"
def test_remove_all_subcommand():
with Aria2Server(port=7522) as server:
assert cli.subcommand_remove(server.api, do_all=True) == 0
# def test_remove_all_subcommand_fails():
# with Aria2Server(port=7523) as server:
# assert cli.subcommand_remove_all(server.api) == 0
def test_purge_subcommand():
with Aria2Server(port=7524, session=SESSIONS_DIR / "very-small-remote-file.txt") as server:
while not server.api.get_download("2089b05ecca3d829").is_complete:
time.sleep(0.2)
assert cli.subcommand_purge(server.api, ["2089b05ecca3d829"]) == 0
def test_purge_subcommand_one_failure(capsys):
with Aria2Server(port=7525, session=SESSIONS_DIR / "small-file-and-paused-file.txt") as server:
while not server.api.get_download("2089b05ecca3d829").is_complete:
time.sleep(0.2)
assert cli.subcommand_purge(server.api, ["2089b05ecca3d829", "208a3d8299b05ecc"]) == 1
lines = err_lines(capsys)
assert lines[1] == "Could not remove download result of GID#208a3d8299b05ecc"
def test_purge_all_subcommand():
with Aria2Server(port=7526) as server:
assert cli.subcommand_purge(server.api, do_all=True) == 0
def test_autopurge_subcommand():
with Aria2Server(port=7527, session=SESSIONS_DIR / "very-small-remote-file.txt") as server:
assert cli.subcommand_autopurge(server.api) == 0
# def test_purge_all_subcommand_fails():
# with Aria2Server(port=7528) as server:
# assert cli.subcommand_purge_all(server.api) == 0
def test_listen_subcommand(capsys):
with Aria2Server(port=7529, session=SESSIONS_DIR / "2-dl-in-queue.txt") as server:
def thread_target():
time.sleep(2)
server.api.resume_all()
time.sleep(3)
server.api.stop_listening()
thread = threading.Thread(target=thread_target)
thread.start()
cli.subcommand_listen(server.api, callbacks_module=TESTS_DATA_DIR / "callbacks.py", event_types=["start"])
captured = capsys.readouterr()
assert captured.err == ""
assert captured.out == "started 2089b05ecca3d829\nstarted cca3d8292089b05e\n"