All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Yann E. MORIN" <yann.morin.1998@free.fr>
To: Julien Olivain <ju.o@free.fr>
Cc: buildroot@buildroot.org
Subject: Re: [Buildroot] [PATCH 1/1] support/testing: add a "file" package runtime test
Date: Sun, 21 Jan 2024 10:38:07 +0100	[thread overview]
Message-ID: <Zazl_wVaiiEwpI9N@landeda> (raw)
In-Reply-To: <20240112204952.229925-1-ju.o@free.fr>

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

      reply	other threads:[~2024-01-21  9:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zazl_wVaiiEwpI9N@landeda \
    --to=yann.morin.1998@free.fr \
    --cc=buildroot@buildroot.org \
    --cc=ju.o@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.