rpm.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. import fnmatch
  3. from oeqa.runtime.case import OERuntimeTestCase
  4. from oeqa.core.decorator.depends import OETestDepends
  5. from oeqa.core.decorator.data import skipIfDataVar
  6. from oeqa.runtime.decorator.package import OEHasPackage
  7. from oeqa.core.utils.path import findFile
  8. class RpmBasicTest(OERuntimeTestCase):
  9. @OEHasPackage(['rpm'])
  10. @OETestDepends(['ssh.SSHTest.test_ssh'])
  11. def test_rpm_help(self):
  12. status, output = self.target.run('rpm --help')
  13. msg = 'status and output: %s and %s' % (status, output)
  14. self.assertEqual(status, 0, msg=msg)
  15. @OETestDepends(['rpm.RpmBasicTest.test_rpm_help'])
  16. def test_rpm_query(self):
  17. status, output = self.target.run('ls /var/lib/rpm/')
  18. if status != 0:
  19. self.skipTest('No /var/lib/rpm on target')
  20. status, output = self.target.run('rpm -q rpm')
  21. msg = 'status and output: %s and %s' % (status, output)
  22. self.assertEqual(status, 0, msg=msg)
  23. class RpmInstallRemoveTest(OERuntimeTestCase):
  24. @classmethod
  25. def setUpClass(cls):
  26. pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_')
  27. rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch)
  28. # Pick base-passwd-doc as a test file to get installed, because it's small
  29. # and it will always be built for standard targets
  30. rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch
  31. if not os.path.exists(rpmdir):
  32. return
  33. for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc):
  34. cls.test_file = os.path.join(rpmdir, f)
  35. cls.dst = '/tmp/base-passwd-doc.rpm'
  36. @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
  37. def test_rpm_install(self):
  38. self.tc.target.copyTo(self.test_file, self.dst)
  39. status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
  40. msg = 'Failed to install base-passwd-doc package: %s' % output
  41. self.assertEqual(status, 0, msg=msg)
  42. self.tc.target.run('rm -f %s' % self.dst)
  43. @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install'])
  44. def test_rpm_remove(self):
  45. status,output = self.target.run('rpm -e base-passwd-doc')
  46. msg = 'Failed to remove base-passwd-doc package: %s' % output
  47. self.assertEqual(status, 0, msg=msg)
  48. @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
  49. def test_rpm_query_nonroot(self):
  50. def set_up_test_user(u):
  51. status, output = self.target.run('id -u %s' % u)
  52. if status:
  53. status, output = self.target.run('useradd %s' % u)
  54. msg = 'Failed to create new user: %s' % output
  55. self.assertTrue(status == 0, msg=msg)
  56. def exec_as_test_user(u):
  57. status, output = self.target.run('su -c id %s' % u)
  58. msg = 'Failed to execute as new user'
  59. self.assertTrue("({0})".format(u) in output, msg=msg)
  60. status, output = self.target.run('su -c "rpm -qa" %s ' % u)
  61. msg = 'status: %s. Cannot run rpm -qa: %s' % (status, output)
  62. self.assertEqual(status, 0, msg=msg)
  63. def unset_up_test_user(u):
  64. status, output = self.target.run('userdel -r %s' % u)
  65. msg = 'Failed to erase user: %s' % output
  66. self.assertTrue(status == 0, msg=msg)
  67. tuser = 'test1'
  68. try:
  69. set_up_test_user(tuser)
  70. exec_as_test_user(tuser)
  71. finally:
  72. unset_up_test_user(tuser)
  73. @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_remove'])
  74. def test_check_rpm_install_removal_log_file_size(self):
  75. """
  76. Summary: Check that rpm writes into /var/log/messages
  77. Expected: There should be some RPM prefixed entries in the above file.
  78. Product: BSPs
  79. Author: Alexandru Georgescu <alexandru.c.georgescu@intel.com>
  80. Author: Alexander Kanavin <alex.kanavin@gmail.com>
  81. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  82. """
  83. db_files_cmd = 'ls /var/lib/rpm/__db.*'
  84. check_log_cmd = "grep RPM /var/log/messages | wc -l"
  85. # Make sure that some database files are under /var/lib/rpm as '__db.xxx'
  86. status, output = self.target.run(db_files_cmd)
  87. msg = 'Failed to find database files under /var/lib/rpm/ as __db.xxx'
  88. self.assertEqual(0, status, msg=msg)
  89. self.tc.target.copyTo(self.test_file, self.dst)
  90. # Remove the package just in case
  91. self.target.run('rpm -e base-passwd-doc')
  92. # Install/Remove a package 10 times
  93. for i in range(10):
  94. status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
  95. msg = 'Failed to install base-passwd-doc package. Reason: {}'.format(output)
  96. self.assertEqual(0, status, msg=msg)
  97. status, output = self.target.run('rpm -e base-passwd-doc')
  98. msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output)
  99. self.assertEqual(0, status, msg=msg)
  100. self.tc.target.run('rm -f %s' % self.dst)
  101. # if using systemd this should ensure all entries are flushed to /var
  102. status, output = self.target.run("journalctl --sync")
  103. # Get the amount of entries in the log file
  104. status, output = self.target.run(check_log_cmd)
  105. msg = 'Failed to get the final size of the log file.'
  106. self.assertEqual(0, status, msg=msg)
  107. # Check that there's enough of them
  108. self.assertGreaterEqual(int(output), 80,
  109. 'Cound not find sufficient amount of rpm entries in /var/log/messages, found {} entries'.format(output))