Skip to content

Commit 93d9419

Browse files
author
Alan Christie
committed
feat: now offers get_file_assets()
1 parent 3a75314 commit 93d9419

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

decoder/decoder.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def get_job_doc_url(
212212

213213

214214
def get_pull_secret_names(job_definition: Dict[str, Any]) -> Set[str]:
215-
""" "Given a Job definition this function returns the unique list of the
215+
"""Given a Job definition this function returns the unique list of the
216216
pull-secrets declared.
217217
"""
218218
names: Set[str] = set()
@@ -226,7 +226,7 @@ def get_pull_secret_names(job_definition: Dict[str, Any]) -> Set[str]:
226226

227227

228228
def get_asset_names(job_definition: Dict[str, Any]) -> Set[str]:
229-
""" "Given a Job definition this function returns the unique list of all the
229+
"""Given a Job definition this function returns the unique list of all the
230230
asset names declared. Asset names can be used in image environment
231231
variables and files.
232232
"""
@@ -248,6 +248,27 @@ def get_asset_names(job_definition: Dict[str, Any]) -> Set[str]:
248248
return asset_names
249249

250250

251+
def get_file_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str]]:
252+
"""Given a Job definition this function returns the list of all the
253+
file-based assets declared. What's returned is the asset 'name' and the
254+
'image file' the asset is expected to mapped to.
255+
"""
256+
file_assets: List[Dict[str, str]] = []
257+
258+
# Iterate through the file block...
259+
file_block: List[Dict[str, Any]] = job_definition.get("image", {}).get("file", [])
260+
for item in file_block:
261+
if "account-server-asset" in item["content-from"]:
262+
file_assets.append(
263+
{
264+
"asset": item["content-from"]["account-server-asset"]["name"],
265+
"image-file": item["name"],
266+
}
267+
)
268+
269+
return file_assets
270+
271+
251272
def decode(
252273
template_text: str,
253274
variable_map: Optional[Dict[str, str]],

tests/test_get_asset_names.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def test_get_asset_names():
2020
],
2121
"file": [
2222
{
23-
"name": "/tmp/blob.txt",
23+
"name": "/tmp/blob-1.txt",
2424
"content-from": {"account-server-asset": {"name": "asset-a"}},
2525
},
2626
{
27-
"name": "/tmp/blob.txt",
27+
"name": "/tmp/blob-2.txt",
2828
"content-from": {"account-server-asset": {"name": "asset-b"}},
2929
},
3030
],

tests/test_get_file_assets.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Tests for the decoder's get_job_doc_url() function.
2+
from typing import Dict
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.unit
7+
8+
from decoder import decoder
9+
10+
11+
def test_get_file_assets():
12+
# Arrange
13+
job_definition: Dict = {
14+
"image": {
15+
"environment": [
16+
{
17+
"name": "BLOB",
18+
"value-from": {"account-server-asset": {"name": "asset-a"}},
19+
}
20+
],
21+
"file": [
22+
{
23+
"name": "/tmp/blob-1.txt",
24+
"content-from": {"account-server-asset": {"name": "asset-a"}},
25+
},
26+
{
27+
"name": "/tmp/blob-2.txt",
28+
"content-from": {"account-server-asset": {"name": "asset-b"}},
29+
},
30+
],
31+
},
32+
}
33+
34+
# Act
35+
file_assets = decoder.get_file_assets(job_definition)
36+
37+
# Assert
38+
assert len(file_assets) == 2
39+
print(file_assets)
40+
for file_asset in file_assets:
41+
if file_asset["image-file"] == "/tmp/blob-1.txt":
42+
assert file_asset["asset"] == "asset-a"
43+
elif file_asset["image-file"] == "/tmp/blob-2.txt":
44+
assert file_asset["asset"] == "asset-b"
45+
else:
46+
# How did we get here?
47+
assert False

0 commit comments

Comments
 (0)