diff mbox series

[1/1] support/testing: add parted runtime test

Message ID 20240825115239.158016-1-ju.o@free.fr
State Accepted
Headers show
Series [1/1] support/testing: add parted runtime test | expand

Commit Message

Julien Olivain Aug. 25, 2024, 11:52 a.m. UTC
Signed-off-by: Julien Olivain <ju.o@free.fr>
---
 DEVELOPERS                                   |   1 +
 support/testing/tests/package/test_parted.py | 107 +++++++++++++++++++
 2 files changed, 108 insertions(+)
 create mode 100644 support/testing/tests/package/test_parted.py

Comments

Thomas Petazzoni Aug. 26, 2024, 4:55 p.m. UTC | #1
On Sun, 25 Aug 2024 13:52:39 +0200
Julien Olivain <ju.o@free.fr> wrote:

> Signed-off-by: Julien Olivain <ju.o@free.fr>
> ---
>  DEVELOPERS                                   |   1 +
>  support/testing/tests/package/test_parted.py | 107 +++++++++++++++++++
>  2 files changed, 108 insertions(+)
>  create mode 100644 support/testing/tests/package/test_parted.py

Thanks, applied to next! Just one comment below.

> +        # For each partition, we create a mount point directory, mount
> +        # the filesystem, copy the reference data file in it, sync the
> +        # filesystem, and compute the sha256 hash of the file. This
> +        # sequence will exercise a bit the partitions and filesystems
> +        # in read/write operations.
> +        for part in range(1, 4):
> +            self.assertRunOk(f"mkdir -p /tmp/MyPart{part}")
> +            self.assertRunOk(f"mount {dev}{part} /tmp/MyPart{part}")
> +            self.assertRunOk(f"cp /tmp/{data_file} /tmp/MyPart{part}/")
> +            self.assertRunOk("sync")

I wasn't sure whether sync or a umount/mount cycle was more appropriate here.

> +            out, ret = self.emulator.run(f"( cd /tmp/MyPart{part} && {hash_cmd} )")

You could have used "cmp" instead to compare files. No need to compute
the hash ahead of time, no need to parse the output of sha256sum, etc:
cmp returns a 0 error code when the files are identical, a non-zero
error code otherwise, so self.assertRunOk() would have worked.

Thanks!

Thomas
diff mbox series

Patch

diff --git a/DEVELOPERS b/DEVELOPERS
index 426590d5c5..208edc724c 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1933,6 +1933,7 @@  F:	support/testing/tests/package/test_octave.py
 F:	support/testing/tests/package/test_ola.py
 F:	support/testing/tests/package/test_ola/
 F:	support/testing/tests/package/test_openblas.py
+F:	support/testing/tests/package/test_parted.py
 F:	support/testing/tests/package/test_pciutils.py
 F:	support/testing/tests/package/test_perftest.py
 F:	support/testing/tests/package/test_pigz.py
diff --git a/support/testing/tests/package/test_parted.py b/support/testing/tests/package/test_parted.py
new file mode 100644
index 0000000000..ca585b7e02
--- /dev/null
+++ b/support/testing/tests/package/test_parted.py
@@ -0,0 +1,107 @@ 
+import os
+import subprocess
+
+import infra.basetest
+
+
+class TestParted(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_PACKAGE_E2FSPROGS=y
+        BR2_PACKAGE_PARTED=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def test_run(self):
+        # Prepare the disk image.
+        disk_file = os.path.join(self.builddir, "images", "disk.img")
+        self.emulator.logfile.write(f"Creating disk image: {disk_file}\n")
+        self.emulator.logfile.flush()
+        subprocess.check_call(
+            ["dd", "if=/dev/zero", f"of={disk_file}", "bs=1M", "count=256"],
+            stdout=self.emulator.logfile,
+            stderr=self.emulator.logfile)
+
+        # Run the emulator with a drive.
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=[
+                                "-initrd", cpio_file,
+                                "-drive", f"file={disk_file},format=raw"])
+        self.emulator.login()
+
+        # We check the program can run.
+        self.assertRunOk("parted --version")
+
+        dev = "/dev/sda"
+        parted = f"parted {dev}"
+
+        # We print the partition list of our drive. Since the drive is
+        # supposed to be blank, it should not have a partition table.
+        part_list_cmd = f"{parted} print list"
+        out, ret = self.emulator.run(part_list_cmd, timeout=10)
+        self.assertEqual(ret, 0)
+        self.assertIn("Partition Table: unknown", out)
+
+        # We create a GPT partition table.
+        mklabel_cmd = f"{parted} mklabel gpt"
+        self.assertRunOk(mklabel_cmd, timeout=10)
+
+        # We print again the partition list. We should now see our
+        # partition table.
+        out, ret = self.emulator.run(part_list_cmd, timeout=10)
+        self.assertEqual(ret, 0)
+        self.assertIn("Partition Table: gpt", out)
+
+        # We create 3 partitions on our drive.
+        partitions = [
+            "MyPart1 ext2 1MiB 25%",
+            "MyPart2 ext4 25% 50%",
+            "MyPart3 ext4 50% 100%"
+        ]
+        for part in partitions:
+            mkpart_cmd = f"{parted} mkpart {part}"
+            self.assertRunOk(mkpart_cmd, timeout=10)
+
+        # We print again the list of partitions, this time in machine
+        # parseable format. We check we have our 3 partitions.
+        cmd = f"parted -m {dev} print list"
+        out, ret = self.emulator.run(cmd, timeout=10)
+        self.assertEqual(ret, 0)
+        for part in range(1, 4):
+            self.assertTrue(out[1+part].startswith(f"{part}:"))
+            self.assertTrue(out[1+part].endswith(f":MyPart{part}:;"))
+
+        # We format our partitions.
+        self.assertRunOk(f"mkfs.ext2 {dev}1", timeout=10)
+        self.assertRunOk(f"mkfs.ext4 {dev}2", timeout=10)
+        self.assertRunOk(f"mkfs.ext4 {dev}3", timeout=10)
+
+        # We create a random data file in the temporary directory. It
+        # will be the reference source file that will be copied later
+        # on each of our filesystems.
+        data_file = "data.bin"
+        cmd = f"dd if=/dev/urandom of=/tmp/{data_file} bs=1M count=10"
+        self.assertRunOk(cmd)
+
+        # We compute the sha256 hash and save it for later.
+        hash_cmd = f"sha256sum {data_file}"
+        out, ret = self.emulator.run(f"( cd /tmp && {hash_cmd} )")
+        self.assertEqual(ret, 0)
+        data_sha256 = out[0]
+
+        # For each partition, we create a mount point directory, mount
+        # the filesystem, copy the reference data file in it, sync the
+        # filesystem, and compute the sha256 hash of the file. This
+        # sequence will exercise a bit the partitions and filesystems
+        # in read/write operations.
+        for part in range(1, 4):
+            self.assertRunOk(f"mkdir -p /tmp/MyPart{part}")
+            self.assertRunOk(f"mount {dev}{part} /tmp/MyPart{part}")
+            self.assertRunOk(f"cp /tmp/{data_file} /tmp/MyPart{part}/")
+            self.assertRunOk("sync")
+            out, ret = self.emulator.run(f"( cd /tmp/MyPart{part} && {hash_cmd} )")
+            self.assertEqual(ret, 0)
+            self.assertEqual(out[0], data_sha256)