* Re: [Buildroot] [PATCH 1/1] support/testing: add a "file" package runtime test
2024-01-12 20:49 [Buildroot] [PATCH 1/1] support/testing: add a "file" package runtime test Julien Olivain
@ 2024-01-21 9:38 ` Yann E. MORIN
0 siblings, 0 replies; 2+ messages in thread
From: Yann E. MORIN @ 2024-01-21 9:38 UTC (permalink / raw)
To: Julien Olivain; +Cc: buildroot
Julien, All,
On 2024-01-12 21:49 +0100, Julien Olivain spake thusly:
> Signed-off-by: Julien Olivain <ju.o@free.fr>
> ---
[--SNIP--]
> diff --git a/support/testing/tests/package/test_file.py b/support/testing/tests/package/test_file.py
> new file mode 100644
> index 0000000000..686fa52d72
> --- /dev/null
> +++ b/support/testing/tests/package/test_file.py
> @@ -0,0 +1,81 @@
> +import os
> +
> +import infra.basetest
> +
> +
> +class TestFile(infra.basetest.BRTest):
> + config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
> + """
> + BR2_PACKAGE_FILE=y
> + BR2_TARGET_ROOTFS_CPIO=y
> + # BR2_TARGET_ROOTFS_TAR is not set
> + """
> +
> + def generate_test_data(self):
> + cmd = "echo 'Hello Buildroot!' > file.txt"
> + self.assertRunOk(cmd)
> +
> + cmd = "gzip -k file.txt"
> + self.assertRunOk(cmd)
> +
> + cmd = "ln -s file.txt link.txt"
> + self.assertRunOk(cmd)
> +
> + cmd = "echo 'void main(void) { printf(\"hello\\nBuildroot!\"); }' > file.c"
> + self.assertRunOk(cmd)
> +
> + cmd = "echo -e '#!/bin/sh\necho Hello Buildroot' > file.sh"
> + self.assertRunOk(cmd)
> +
> + cmd = "echo -e '"
> + cmd += "#!/usr/bin/env python3\nprint(\"Hello Buildroot!\")'"
> + cmd += " > file.py"
> + self.assertRunOk(cmd)
> +
> + cmd = "dd if=/dev/urandom of=file.bin bs=1k count=4"
> + self.assertRunOk(cmd)
The test data corpus should be in an overlay, committed to the git tree
(yes, even the gzip data).
> + def test_run(self):
> + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
> + self.emulator.boot(arch="armv5",
> + kernel="builtin",
> + options=["-initrd", cpio_file])
> + self.emulator.login()
> +
> + self.assertRunOk("file --version")
> +
> + self.generate_test_data()
> +
> + tests = [
> + ["file.txt", "ASCII text"],
> + ["file.txt.gz", "gzip compressed data"],
> + ["file.c", "C source"],
> + ["file.sh", "POSIX shell script"],
> + ["file.py", "Python script"],
> + ["file.bin", "data"],
> + ["/usr/share/misc/magic.mgc", "magic binary file for file"],
> + ["/usr/bin/file", "ELF"],
> + ["/dev/zero", "character special"],
> + ["/", "directory"]
> + ]
> + for test_file, expected_type in tests:
> + cmd = f"file '{test_file}' | grep -F '{expected_type}'"
> + self.assertRunOk(cmd)
> +
> + tests = [
> + ["file.txt", "text/plain"],
> + ["file.txt.gz", "application/gzip"],
> + ["file.bin", "application/octet-stream"],
> + ]
> + for test_file, expected_type in tests:
> + cmd = f"file -i '{test_file}' | grep -F '{expected_type}'"
> + self.assertRunOk(cmd)
> +
> + cmd = "file -h link.txt | grep -F 'symbolic link'"
> + self.assertRunOk(cmd)
> +
> + cmd = "file -L link.txt | grep -F 'ASCII text'"
> + self.assertRunOk(cmd)
> +
> + cmd = "file -z file.txt.gz | grep -F 'ASCII text'"
> + self.assertRunOk(cmd)
Ah, I like the array option with a loop, and it can all be made a
single loop; also I prefer the pattern test to be done in python:
tests = [
("", "file.txt", "ASCII text"),
("-i", "file.txt", "text/plain"),
("", "file.txt.gz", "gzip compressed data"),
("-i", "file.txt.gz", ""pplication/gzip),
("-h", "link.txt", "symbolic link"),
("-h", "link.txt", "symbolic link"),
...
]
for opt_str, path, pattern in tests:
cmd = f"file {opt_str} '{path}'"
out, ret = self.emulator.run(cmd)
self.assertEqual(ret, 0, f"Failed to run '{cmd}'")
self.assertIn(pattern, out)
Regards,
Yann E. MORIN.
> --
> 2.43.0
>
> _______________________________________________
> buildroot mailing list
> buildroot@buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
--
.-----------------.--------------------.------------------.--------------------.
| Yann E. MORIN | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software Designer | \ / CAMPAIGN | ___ |
| +33 561 099 427 `------------.-------: X AGAINST | \e/ There is no |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL | v conspiracy. |
'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot
^ permalink raw reply [flat|nested] 2+ messages in thread