Skip to content

Commit 04e17b4

Browse files
committed
examples/elf: extend nxpkg validation coverage
Extend the ELF package fixtures with both success and failure repository data so nxpkg validation paths are exercised from the same example flow. Format the helper generator with black so it matches the current apps lint checks. Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 97802bd commit 04e17b4

2 files changed

Lines changed: 147 additions & 1 deletion

File tree

examples/elf/main/Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,24 @@ ifeq ($(CONFIG_EXAMPLES_ELF_ROMFS),y)
6969
ROMFSIMG = elf_romfs.img
7070
ROMFSSRC = elf_romfs.c
7171
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
72+
ifdef CONFIG_SYSTEM_NXPKG
73+
PKGINDEX = $(BINDIR)/index.json
74+
PKGBADINDEX = $(BINDIR)/bad-index.json
75+
PKGTEST = $(BINDIR)/pkgtest.nsh
76+
PKGFAIL = $(BINDIR)/pkgfail.nsh
77+
PKG_FIXTURE_GEN = $(APPDIR)/examples/elf/main/mk_pkg_fixture.py
78+
PKG_FIXTURE_OUTPUTS = $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL)
79+
PKG_FIXTURE_INPUTS = $(BINDIR)/hello
80+
PKG_FIXTURE_ARGS = \
81+
$(BINDIR)/hello $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) \
82+
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)
83+
84+
$(PKG_FIXTURE_OUTPUTS) &: $(PKG_FIXTURE_INPUTS) $(PKG_FIXTURE_GEN)
85+
$(Q) python3 $(PKG_FIXTURE_GEN) \
86+
$(PKG_FIXTURE_ARGS)
87+
endif
7288

73-
$(ROMFSIMG):
89+
$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS)
7490
$(Q) genromfs -d $(BINDIR) -f $@
7591

7692
$(ROMFSSRC): $(ROMFSIMG)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env python3
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import hashlib
6+
import json
7+
import pathlib
8+
import sys
9+
10+
11+
def sha256_file(path: pathlib.Path) -> str:
12+
digest = hashlib.sha256()
13+
with path.open("rb") as infile:
14+
while True:
15+
chunk = infile.read(4096)
16+
if not chunk:
17+
break
18+
digest.update(chunk)
19+
20+
return digest.hexdigest()
21+
22+
23+
def write_index(
24+
path: pathlib.Path,
25+
arch: str,
26+
compat: str,
27+
artifact: str,
28+
digest: str,
29+
) -> None:
30+
payload = {
31+
"packages": [
32+
{
33+
"name": "hello",
34+
"version": "1.0.0",
35+
"arch": arch,
36+
"compat": compat,
37+
"artifact": artifact,
38+
"sha256": digest,
39+
"type": "elf",
40+
},
41+
{
42+
"name": "hello",
43+
"version": "9.9.9",
44+
"arch": "arm",
45+
"compat": "stm32f4discovery",
46+
"artifact": artifact,
47+
"sha256": digest,
48+
"type": "elf",
49+
},
50+
]
51+
}
52+
53+
path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8")
54+
55+
56+
def write_bad_index(path: pathlib.Path, arch: str, compat: str) -> None:
57+
payload = {
58+
"packages": [
59+
{
60+
"name": "hello-missing",
61+
"version": "1.0.0",
62+
"arch": arch,
63+
"compat": compat,
64+
"artifact": "/mnt/elf/romfs/hello-missing",
65+
"sha256": "0" * 64,
66+
"type": "elf",
67+
}
68+
]
69+
}
70+
71+
path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8")
72+
73+
74+
def write_script(path: pathlib.Path) -> None:
75+
script = "\n".join(
76+
[
77+
"mount -t tmpfs /etc",
78+
"mount -t tmpfs /var",
79+
"mkdir /etc/nxpkg",
80+
"cp /mnt/elf/romfs/index.json /etc/nxpkg/index.json",
81+
"nxpkg install hello",
82+
"nxpkg list",
83+
"",
84+
]
85+
)
86+
path.write_text(script, encoding="utf-8")
87+
88+
89+
def write_fail_script(path: pathlib.Path) -> None:
90+
script = "\n".join(
91+
[
92+
"mount -t tmpfs /etc",
93+
"mount -t tmpfs /var",
94+
"mkdir /etc/nxpkg",
95+
"cp /mnt/elf/romfs/bad-index.json /etc/nxpkg/index.json",
96+
"nxpkg install hello-missing",
97+
"",
98+
]
99+
)
100+
path.write_text(script, encoding="utf-8")
101+
102+
103+
def main() -> int:
104+
if len(sys.argv) != 8:
105+
print(
106+
"usage: mk_pkg_fixture.py <hello-bin> <index-json> <bad-index-json> "
107+
"<pkgtest-nsh> <pkgfail-nsh> <arch> <compat>",
108+
file=sys.stderr,
109+
)
110+
return 1
111+
112+
hello = pathlib.Path(sys.argv[1])
113+
index = pathlib.Path(sys.argv[2])
114+
bad_index = pathlib.Path(sys.argv[3])
115+
script = pathlib.Path(sys.argv[4])
116+
fail_script = pathlib.Path(sys.argv[5])
117+
arch = sys.argv[6]
118+
compat = sys.argv[7]
119+
artifact = "/mnt/elf/romfs/hello"
120+
121+
digest = sha256_file(hello)
122+
write_index(index, arch, compat, artifact, digest)
123+
write_bad_index(bad_index, arch, compat)
124+
write_script(script)
125+
write_fail_script(fail_script)
126+
return 0
127+
128+
129+
if __name__ == "__main__":
130+
raise SystemExit(main())

0 commit comments

Comments
 (0)