Skip to content

Commit e386498

Browse files
committed
Added test for bad keys.
1 parent cb2be11 commit e386498

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

stackinator/mirror.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,7 @@ def _key_setup(self, key_store: pathlib.Path):
246246
#try prepending system config path
247247
path = self._system_config_root/path
248248

249-
if path.exists():
250-
if not path.is_file():
251-
raise MirrorError(
252-
f"The key path '{path}' is not a file. \n"
253-
f"Check the key listed in mirrors.yaml in system config.")
254-
249+
if path.is_file():
255250
with open(path, 'rb') as reader:
256251
binary_key = reader.read()
257252

@@ -261,15 +256,15 @@ def _key_setup(self, key_store: pathlib.Path):
261256
binary_key = base64.b64decode(key)
262257
except ValueError:
263258
raise MirrorError(
264-
f"Key for mirror '{name}' is not valid. \n"
259+
f"Key for mirror '{name}' is not valid: '{path}'. \n"
265260
f"Must be a path to a GPG public key or a base64 encoded GPG public key. \n"
266261
f"Check the key listed in mirrors.yaml in system config.")
267262

268263
file_type = magic.from_buffer(binary_key, mime=True)
269-
print("magic type:" , file_type)
270264
if file_type not in ("application/x-gnupg-keyring", "application/pgp-keys"):
271265
raise MirrorError(
272266
f"Key for mirror {name} is not a valid GPG key. \n"
267+
f"The file (or base64) was readable, but the data itself was not a PGP key.\n"
273268
f"Check the key listed in mirrors.yaml in system config.")
274269

275270
# copy key to new destination in key store
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
- name: bad-key
1+
bad-key:
22
url: https://mirror.spack.io
3-
public_key: /bad_key.gpg
3+
public_key: bad_key.gpg
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
- name: bad-key-path
1+
bad-key-path:
22
url: https://mirror.spack.io
3-
public_key: /path/doesnt/exist
3+
public_key: /path/doesnt/exist

unittests/data/systems/mirror-basic/cache.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

unittests/data/systems/mirror-basic/mirrors.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

unittests/test_mirrors.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ def test_key_setup(systems_path, tmp_path):
6868
with key_file.open('rb') as file:
6969
key_file_data.append(file.read())
7070
assert key_file_data[0] == key_file_data[1]
71+
72+
@pytest.mark.parametrize("system_name", [
73+
'mirror-bad-key',
74+
'mirror-bad-keypath',
75+
])
76+
def test_key_setup_bad_key(tmp_path, systems_path, system_name):
77+
"""asdfasdf"""
78+
79+
mirrors = mirror.Mirrors(systems_path/system_name)
80+
with pytest.raises(mirror.MirrorError):
81+
mirrors._key_setup(tmp_path)
82+
83+
84+

0 commit comments

Comments
 (0)