test_attr.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import os
  2. import infra.basetest
  3. class TestAttr(infra.basetest.BRTest):
  4. # Note: this test uses extended attributes (xattr). We use a ext4
  5. # rootfs (which fully supports xattrs). Note that tmpfs has
  6. # partial support of xattrs, and cpio initrd has not.
  7. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  8. """
  9. BR2_PACKAGE_ATTR=y
  10. BR2_TARGET_ROOTFS_EXT2=y
  11. BR2_TARGET_ROOTFS_EXT2_4=y
  12. # BR2_TARGET_ROOTFS_TAR is not set
  13. """
  14. def test_run(self):
  15. disk_file = os.path.join(self.builddir, "images", "rootfs.ext4")
  16. self.emulator.boot(arch="armv5",
  17. kernel="builtin",
  18. kernel_cmdline=["rootwait", "root=/dev/sda"],
  19. options=["-drive", f"file={disk_file},if=scsi,format=raw"])
  20. self.emulator.login()
  21. # Check the programs can execute.
  22. self.assertRunOk("getfattr --version")
  23. self.assertRunOk("setfattr --version")
  24. test_file = "/root/file.txt"
  25. attr_name = "buildroot"
  26. attr_value = "is-great"
  27. # Create a test file.
  28. self.assertRunOk(f"echo 'Hello Buildroot!' > {test_file}")
  29. # Set an extended attribute.
  30. cmd = f"setfattr -n user.{attr_name} -v {attr_value} {test_file}"
  31. self.assertRunOk(cmd)
  32. # Read back the attribute value. We add an extra "echo" to add
  33. # a new line. getfattr --only-values prints raw attribute
  34. # values and lack of a new line.
  35. cmd = "getfattr"
  36. cmd += f" -n user.{attr_name} --absolute-names --only-values"
  37. cmd += f" {test_file} && echo"
  38. out, ret = self.emulator.run(cmd)
  39. self.assertEqual(ret, 0)
  40. self.assertEqual(out[0], attr_value)
  41. # We read back the attribute value again, but with the "attr"
  42. # command this time.
  43. cmd = f"attr -q -g {attr_name} {test_file} && echo"
  44. out, ret = self.emulator.run(cmd)
  45. self.assertEqual(ret, 0)
  46. self.assertEqual(out[0], attr_value)
  47. # List extended attributes with "attr", and check we see our
  48. # test attribute.
  49. cmd = f"attr -l {test_file}"
  50. out, ret = self.emulator.run(cmd)
  51. self.assertEqual(ret, 0)
  52. self.assertIn(attr_name, "\n".join(out))
  53. # Remove the test attribute with setfattr.
  54. cmd = f"setfattr -x user.{attr_name} {test_file}"
  55. self.assertRunOk(cmd)
  56. # We check the test attribute is no longer listed by the attr
  57. # command.
  58. cmd = f"attr -l {test_file}"
  59. out, ret = self.emulator.run(cmd)
  60. self.assertEqual(ret, 0)
  61. self.assertNotIn(attr_name, "\n".join(out))