[RFC-next,2/2] support/testing: add tests for the genimage generation logic

Message ID 20170823204547.16672-3-thomas.petazzoni@free-electrons.com
State New
Headers show

Commit Message

Thomas Petazzoni Aug. 23, 2017, 8:45 p.m.
This commit adds two new test cases, to validate that the genimage
related functionality in Buildroot is working as expected:

 - One test case with just one ext4 partition. We do the build, check
   in the image that one partition is available with the right type,
   and then boot the system under Qemu to validate that the root
   filesystem can be mounted from /dev/mmcblk0p1.

 - One test case with one VFAT partition and one ext4 partition. We do
   the build, check in the image that two partitions are available
   with the right types, and then boot the system under Qemu. This
   allows to validate that the root filesystem can be mounted from
   /dev/mmcblk0p2, and that /dev/mmcblk0p1 can be mounted as a VFAT
   filesystem and contains the expected file.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
---
 support/testing/tests/fs/genimage-simple.cfg |  9 ++++
 support/testing/tests/fs/genimage-vfat.cfg   | 25 ++++++++++
 support/testing/tests/fs/test_genimage.py    | 74 ++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 support/testing/tests/fs/genimage-simple.cfg
 create mode 100644 support/testing/tests/fs/genimage-vfat.cfg
 create mode 100644 support/testing/tests/fs/test_genimage.py

Comments

Ricardo Martincoski Aug. 26, 2017, 9:12 p.m. | #1
Hello,

Looks good. Only nits below.

On Wed, Aug 23, 2017 at 05:45 PM, Thomas Petazzoni wrote:

> This commit adds two new test cases, to validate that the genimage
> related functionality in Buildroot is working as expected:
> 
>  - One test case with just one ext4 partition. We do the build, check
>    in the image that one partition is available with the right type,
>    and then boot the system under Qemu to validate that the root
>    filesystem can be mounted from /dev/mmcblk0p1.

The root filesystem ends up mounted as read-only due to limitations in the
builtin kernel image. From the -run.log:
Filesystem with huge files cannot be mounted RDWR without CONFIG_LBDAF

But I think it is OK since whether it is writable or not is not the subject of
the test.

[snip]
> ---
>  support/testing/tests/fs/genimage-simple.cfg |  9 ++++
>  support/testing/tests/fs/genimage-vfat.cfg   | 25 ++++++++++
>  support/testing/tests/fs/test_genimage.py    | 74 ++++++++++++++++++++++++++++
>  3 files changed, 108 insertions(+)

I don't know if it was intended (due to RFC status) or not, but the update to
.gitlab-ci.yml is missing.

[snip]
> +++ b/support/testing/tests/fs/test_genimage.py

Since it's a new file, could fix the 9 code style warnings from flake8?

[snip]
> +    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
> +             """
> +             BR2_TARGET_ROOTFS_EXT2=y
> +             BR2_TARGET_ROOTFS_EXT2_4=y
> +             BR2_TARGET_ROOTFS_GENIMAGE=y
> +             BR2_TARGET_ROOTFS_GENIMAGE_CFG_FILE="{}"
> +             """.format(infra.filepath("tests/fs/genimage-simple.cfg"))

This is a triple-quoted string and the test infra removes the white space at
beginning of lines, so any indentation would be valid.
But perhaps we could adopt a default and use everywhere for consistency. See
support/testing/tests/init/test_none.py that indents one level in this case.
    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
        """

Regards,
Ricardo

Patch

diff --git a/support/testing/tests/fs/genimage-simple.cfg b/support/testing/tests/fs/genimage-simple.cfg
new file mode 100644
index 0000000..baafe31
--- /dev/null
+++ b/support/testing/tests/fs/genimage-simple.cfg
@@ -0,0 +1,9 @@ 
+image sdcard.img {
+	hdimage {
+	}
+
+	partition rootfs {
+		partition-type = 0x83
+		image = "rootfs.ext4"
+	}
+}
diff --git a/support/testing/tests/fs/genimage-vfat.cfg b/support/testing/tests/fs/genimage-vfat.cfg
new file mode 100644
index 0000000..5a5388b
--- /dev/null
+++ b/support/testing/tests/fs/genimage-vfat.cfg
@@ -0,0 +1,25 @@ 
+image boot.vfat {
+	vfat {
+		files = {
+			"rootfs.tar",
+		}
+	}
+	size = 16M
+}
+
+image sdcard.img {
+	hdimage {
+	}
+
+	partition boot {
+		partition-type = 0xC
+		bootable = "true"
+		image = "boot.vfat"
+		offset = 8M
+	}
+
+	partition rootfs {
+		partition-type = 0x83
+		image = "rootfs.ext4"
+	}
+}
diff --git a/support/testing/tests/fs/test_genimage.py b/support/testing/tests/fs/test_genimage.py
new file mode 100644
index 0000000..f2edd2b
--- /dev/null
+++ b/support/testing/tests/fs/test_genimage.py
@@ -0,0 +1,74 @@ 
+import json
+import os
+import subprocess
+import infra.basetest
+
+class TestGenimageSimple(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+             """
+             BR2_TARGET_ROOTFS_EXT2=y
+             BR2_TARGET_ROOTFS_EXT2_4=y
+             BR2_TARGET_ROOTFS_GENIMAGE=y
+             BR2_TARGET_ROOTFS_GENIMAGE_CFG_FILE="{}"
+             """.format(infra.filepath("tests/fs/genimage-simple.cfg"))
+
+    def test_run(self):
+        img = os.path.join(self.builddir, "images", "sdcard.img")
+        sfdisk_cmd = ["host/usr/sbin/sfdisk", "-J", img]
+        out = subprocess.check_output(sfdisk_cmd, cwd=self.builddir,
+                                      env={"LANG":"C"})
+        data = json.loads(out)
+        partitions = data['partitiontable']['partitions']
+        self.assertEqual(len(partitions), 1)
+        ext_part = partitions[0]
+        self.assertTrue(ext_part['node'].endswith("sdcard.img1"))
+        self.assertEqual(ext_part['type'], "83")
+
+        subprocess.call(["truncate", "-s", "%1M", img])
+
+        self.emulator.boot(arch="armv7",
+                           kernel="builtin",
+                           kernel_cmdline=["root=/dev/mmcblk0p1", "rootfstype=ext4"],
+                           options=["-drive", "file={},if=sd,format=raw".format(img)])
+        self.emulator.login()
+
+class TestGenimageVfat(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+             """
+             BR2_TARGET_ROOTFS_EXT2=y
+             BR2_TARGET_ROOTFS_EXT2_4=y
+             BR2_TARGET_ROOTFS_GENIMAGE=y
+             BR2_TARGET_ROOTFS_GENIMAGE_CFG_FILE="{}"
+             BR2_TARGET_ROOTFS_GENIMAGE_USES_VFAT=y
+             """.format(infra.filepath("tests/fs/genimage-vfat.cfg"))
+
+    def test_run(self):
+        img = os.path.join(self.builddir, "images", "sdcard.img")
+        sfdisk_cmd = ["host/usr/sbin/sfdisk", "-J", img]
+        out = subprocess.check_output(sfdisk_cmd, cwd=self.builddir,
+                                      env={"LANG":"C"})
+        data = json.loads(out)
+        partitions = data['partitiontable']['partitions']
+        self.assertEqual(len(partitions), 2)
+        vfat_part = partitions[0]
+        self.assertTrue(vfat_part['node'].endswith("sdcard.img1"))
+        self.assertEqual(vfat_part['type'], "c")
+        ext_part = partitions[1]
+        self.assertTrue(ext_part['node'].endswith("sdcard.img2"))
+        self.assertEqual(ext_part['type'], "83")
+
+        subprocess.call(["truncate", "-s", "%1M", img])
+
+        self.emulator.boot(arch="armv7",
+                           kernel="builtin",
+                           kernel_cmdline=["root=/dev/mmcblk0p2", "rootfstype=ext4"],
+                           options=["-drive", "file={},if=sd,format=raw".format(img)])
+        self.emulator.login()
+
+        cmd = "mount -t vfat /dev/mmcblk0p1 /mnt"
+        _, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        cmd = "ls -l /mnt/rootfs.tar"
+        _, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+