1import os
2import infra.basetest
3import subprocess
4
5
6class TestZerofree(infra.basetest.BRTest):
7    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
8        """
9        BR2_PACKAGE_ZEROFREE=y
10        BR2_TARGET_ROOTFS_CPIO=y
11        # BR2_TARGET_ROOTFS_TAR is not set
12        """
13
14    def test_run(self):
15        # Prepare disk image.
16        # We keep it small (8 MB) for the sake of test time.
17        disk_file = os.path.join(self.outputdir, "disk.img")
18        subprocess.check_call(
19            ["dd", "if=/dev/zero", f"of={disk_file}", "bs=1M", "count=8"],
20            stdout=self.emulator.logfile,
21            stderr=self.emulator.logfile)
22
23        # Run the emulator with a drive.
24        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
25        self.emulator.boot(arch="armv5",
26                           kernel="builtin",
27                           options=[
28                                "-initrd", cpio_file,
29                                "-drive", f"file={disk_file},format=raw"])
30        self.emulator.login()
31
32        # Prepare filesystem.
33        output, exit_code = self.emulator.run("mkfs.ext4 /dev/sda")
34        self.assertEqual(exit_code, 0)
35        self.assertIn('Creating filesystem', output[2])
36
37        # Run zerofree on newly created filesystem.
38        cmd = "zerofree -v /dev/sda"
39        output, exit_code = self.emulator.run(cmd, timeout=60)
40        self.assertEqual(exit_code, 0)
41        self.assertIn('/8192', output[-1])  # total number of blocks
42