-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_conda_channel.py
362 lines (300 loc) · 12.4 KB
/
test_conda_channel.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import hashlib
import json
from unittest import TestCase
from unittest.mock import Mock, call, mock_open, patch
import pytest
import yaml
from requests import Response
from requests.adapters import urldefragauth
from yaml import safe_load
from yaml.loader import SafeLoader
from conda_vendor.conda_channel import CondaChannel, improved_download
from conda_vendor.manifest import LockWrapper, MetaManifest
from .conftest import mock_response
@patch("yaml.load")
def test_load_manifest(mock, conda_channel_fixture, tmp_path):
test_manifest_path = tmp_path / "test_manifest.yml"
with open(test_manifest_path, "w") as y:
y.write("test")
conda_channel_fixture.load_manifest(test_manifest_path)
mock.assert_called_once_with = [test_manifest_path, yaml.SafeLoader]
@patch("requests.Session.get")
def test_improved_download(mock) -> None:
mock.return_value = Mock(Response)
test_url = "https://NOT_REAL.com"
result = improved_download(test_url)
result_called_with = mock.call_args[0][0]
assert result_called_with == test_url
assert mock.call_count == 1
assert isinstance(result, Response)
@patch("conda_vendor.conda_channel.CondaChannel.load_manifest")
def test_CondaChannel_init_(mock, tmp_path, get_path_location_for_manifest_fixture):
test_channel_root = tmp_path / "test_channel_root"
test_manifest_path = get_path_location_for_manifest_fixture
test_manifest = {
"main": {"superman": "is_awesome"},
"conda-forge": {"batman": "is_better"},
}
expected_manifest = test_manifest
expected_channels = ["main", "conda-forge"]
mock.return_value = test_manifest
expected_platform = "superman"
test_conda_channel = CondaChannel(
channel_root=test_channel_root, meta_manifest_path=test_manifest_path
)
result_channels = test_conda_channel.channels
result_manifest = test_conda_channel.meta_manifest
mock.assert_called_once_with(test_manifest_path)
assert result_manifest == expected_manifest
assert sorted(result_channels) == sorted(expected_channels)
assert test_conda_channel.platform == expected_platform
@patch("conda_vendor.conda_channel.improved_download")
def test_CondaChannel_fetch_and_filter_repodata(mock_download, conda_channel_fixture):
fake_manifest_subset_metadata = {
"repodata_url": "https://url1",
"entries": [{"fn": "file1"}, {"fn": "file2"}, {"fn": "file3"},],
}
fake_live_repo_data_json = {
"info": {"subdir": "fake_subdir"},
"packages": {
"file1": {"id": 1},
"badfile1": {"id": 2},
"file2": {"id": 3},
"badfile2": {"id": 4},
},
"packages.conda": {
"file3": {"id": 5},
"badfile3": {"id": 6},
"file4": {"id": 7},
"badfile4": {"id": 8},
},
}
mock_download.return_value = mock_response(json_data=fake_live_repo_data_json)
fake_conda_subdir = "fake_subdir"
expected = {
"info": {"subdir": "fake_subdir"},
"packages": {"file1": {"id": 1}, "file2": {"id": 3},},
"packages.conda": {"file3": {"id": 5}},
}
expected_mock_call = call("https://url1")
result = conda_channel_fixture.fetch_and_filter_repodata(
fake_conda_subdir, fake_manifest_subset_metadata
)
assert mock_download.call_args == expected_mock_call
assert mock_download.call_count == 1
TestCase().assertDictEqual(result, expected)
@patch("conda_vendor.conda_channel.CondaChannel.fetch_and_filter_repodata")
def test_CondaChannel_get_all_repo_data(mock, conda_channel_fixture):
platform = conda_channel_fixture.platform
fake_repo_data_entry = {
"info": {"subdir": f"{platform}"},
"packages": {},
"packages.conda": {},
}
expected_calls = [
call(
f"{platform}",
{
"repodata_url": f"https://conda.anaconda.org/main/{platform}/repodata.json",
"entries": [
{
"url": f"https://conda.anaconda.org/main/{platform}/brotlipy-0.7.0-py39h27cfd23_1003.tar.bz2",
"name": "brotlipy",
"version": "0.7.0",
"channel": f"https://conda.anaconda.org/main/{platform}",
"purl": f"pkg:conda/brotlipy@0.7.0?url=https://conda.anaconda.org/main/{platform}/brotlipy-0.7.0-py39h27cfd23_1003.tar.bz2",
}
],
},
),
call("noarch", {"repodata_url": None, "entries": []}),
call(f"{platform}", {"repodata_url": None, "entries": []}),
call(
"noarch",
{
"repodata_url": "https://conda.anaconda.org/conda-forge/noarch/repodata.json",
"entries": [
{
"url": "https://conda.anaconda.org/conda-forge/noarch/ensureconda-1.4.1-pyhd8ed1ab_0.tar.bz2",
"name": "ensureconda",
"version": "1.4.1",
"channel": "https://conda.anaconda.org/conda-forge/noarch",
"purl": "pkg:conda/ensureconda@1.4.1?url=https://conda.anaconda.org/conda-forge/noarch/ensureconda-1.4.1-pyhd8ed1ab_0.tar.bz2",
}
],
},
),
]
expected_all_repodata = {
"main": {
f"{platform}": {
"info": {"subdir": f"{platform}"},
"packages": {},
"packages.conda": {},
},
"noarch": {
"info": {"subdir": f"{platform}"},
"packages": {},
"packages.conda": {},
},
},
"conda-forge": {
f"{platform}": {
"info": {"subdir": f"{platform}"},
"packages": {},
"packages.conda": {},
},
"noarch": {
"info": {"subdir": f"{platform}"},
"packages": {},
"packages.conda": {},
},
},
}
mock.return_value = fake_repo_data_entry
result = conda_channel_fixture.get_all_repo_data()
TestCase().assertListEqual(mock.call_args_list, expected_calls)
TestCase().assertDictEqual(result, expected_all_repodata)
def test_CondaChannel_local_channel_name(conda_channel_fixture):
expected = "local_TEST_CHANNEL_NAME"
result = conda_channel_fixture.local_channel_name(chan="TEST_CHANNEL_NAME")
assert expected == result
@patch("conda_vendor.conda_channel.CondaChannel.local_channel_name")
def test_CondaChannel_local_dir(mock, conda_channel_fixture):
mock_local_dir_name = "local_TEST_CHANNEL_NAME"
mock.return_value = mock_local_dir_name
test_channel_name = "TEST_CHANNEL_NAME"
test_subdir = "dummy-64"
expected = conda_channel_fixture.channel_root / mock_local_dir_name / test_subdir
result = conda_channel_fixture.local_dir(chan=test_channel_name, subdir=test_subdir)
assert result == expected
mock.assert_called_with(test_channel_name)
@patch("conda_vendor.conda_channel.CondaChannel.local_dir")
def test_CondaChannel_make_local_dir(mock, conda_channel_fixture):
mock_local_dir_name = "local_TEST_CHANNEL_NAME"
test_local_dir_subdir = "dummy-64"
expected_path = (
conda_channel_fixture.channel_root / mock_local_dir_name / test_local_dir_subdir
)
test_channel_name = "TEST_CHANNEL_NAME"
mock.return_value = expected_path
result = conda_channel_fixture.make_local_dir(
chan=test_channel_name, subdir=test_local_dir_subdir
)
assert result == expected_path
assert expected_path.exists()
mock.assert_called_with("TEST_CHANNEL_NAME", "dummy-64")
@patch("conda_vendor.conda_channel.CondaChannel.make_local_dir")
def test_CondaChannel_write_arch_repo_data(mock_mkdir, conda_channel_fixture):
new_dir = conda_channel_fixture.channel_root / "tmp"
new_dir.mkdir(exist_ok=True, parents=True)
new_file = new_dir / "repodata.json"
assert not new_file.exists()
expected_chan = "dummy_chan"
expected_subdir = "dummy_subdir"
expected = {"data": "garbage"}
def on_mkdir(chan, subdir):
assert chan == expected_chan
assert subdir == expected_subdir
return new_dir
mock_mkdir.side_effect = on_mkdir
conda_channel_fixture.write_arch_repo_data(
chan=expected_chan, subdir=expected_subdir, repo_data=expected
)
with new_file.open("r") as f:
result = json.load(f)
TestCase().assertDictEqual(result, expected)
@patch("conda_vendor.conda_channel.CondaChannel.write_arch_repo_data")
@patch("conda_vendor.conda_channel.CondaChannel.get_all_repo_data")
def test_CondaChannel_write_repo_data(mock_get, mock_write, conda_channel_fixture):
platform = conda_channel_fixture.platform
fake_repo_data = {
"chan1": {platform: {"data": 1}, "noarch": {"data": 2}},
"chan2": {platform: {"data": 3}, "noarch": {"data": 4}},
}
expected_calls = [
call("chan1", platform, {"data": 1}),
call("chan1", "noarch", {"data": 2}),
call("chan2", platform, {"data": 3}),
call("chan2", "noarch", {"data": 4}),
]
mock_get.return_value = fake_repo_data
conda_channel_fixture.write_repo_data()
TestCase().assertListEqual(mock_write.call_args_list, expected_calls)
def test_CondaChannel__calc_sha256():
test_data = b"DUMMY"
expected = hashlib.sha256(b"DUMMY").hexdigest()
result = CondaChannel._calc_sha256(test_data)
assert expected == result
@patch("conda_vendor.conda_channel.improved_download")
def test_CondaChannel_download_and_validate(mock, tmp_path):
expected_raw = b"DUMMY_DATA"
expected_path = tmp_path / "dummy.data"
expected_hash = hashlib.sha256(expected_raw).hexdigest()
expected_url = "https://should_have_been_a_doctor.com"
mock.return_value = mock_response(content=expected_raw)
CondaChannel.download_and_validate(
out=expected_path, url=expected_url, sha256=expected_hash
)
with open(expected_path, "rb") as f:
assert f.read() == expected_raw
mock.assert_called_with(expected_url)
assert mock.call_count == 1
@patch("conda_vendor.conda_channel.CondaChannel.make_local_dir")
@patch("conda_vendor.conda_channel.CondaChannel.download_and_validate")
def test_CondaChannel_download_arch_binaries(
mock_download_and_validate, mock_make_local_dir, conda_channel_fixture, tmp_path
):
platform = conda_channel_fixture.platform
test_channel = "dummy"
test_subdir = "dummy-64"
test_entries = [
{
"channel": f"http://fake.com/main/{platform}",
"url": f"https://fake.com/main/{platform}/name1",
"fn": "name1",
"sha256": "sha1",
}
]
mock_path = tmp_path / test_channel / test_subdir
expected_name = "name1"
expected_destination = mock_path / expected_name
expected_download_and_validate_calls = (
expected_destination,
f"https://fake.com/main/{platform}/name1",
"sha1",
)
mock_make_local_dir.return_value = mock_path
conda_channel_fixture.download_arch_binaries(
chan=test_channel, subdir=test_subdir, entries=test_entries
)
mock_download_and_validate.call_count == 1
mock_make_local_dir.call_count == 1
mock_make_local_dir.assert_called_with("dummy", "dummy-64")
mock_download_and_validate.assert_called_with(*expected_download_and_validate_calls)
@patch("conda_vendor.conda_channel.CondaChannel.download_arch_binaries")
def test_CondaChannel_download_binaries(mock_download, conda_channel_fixture):
platform = conda_channel_fixture.platform
mock_extended_data = {
"main": {
f"{platform}": {
"repodata_url": "https://url1",
"entries": [{"id": 1}, {"id": 2}],
},
"noarch": {"repodata_url": "https://url2", "entries": [{"id": 3}]},
},
"conda-forge": {
f"{platform}": {"repodata_url": None, "entries": []},
"noarch": {"repodata_url": "https://url3", "entries": [{"id": 4}]},
},
}
conda_channel_fixture.meta_manifest = mock_extended_data
expected_calls = [
call("main", platform, [{"id": 1}, {"id": 2}]),
call("main", "noarch", [{"id": 3}]),
call("conda-forge", platform, []),
call("conda-forge", "noarch", [{"id": 4}]),
]
conda_channel_fixture.download_binaries()
actual_calls = mock_download.call_args_list
TestCase().assertListEqual(expected_calls, actual_calls)