1# 2# Copyright OpenEmbedded Contributors 3# 4# SPDX-License-Identifier: MIT 5# 6 7import os 8import fnmatch 9import time 10 11from oeqa.runtime.case import OERuntimeTestCase 12from oeqa.core.decorator.depends import OETestDepends 13from oeqa.core.decorator.data import skipIfDataVar 14from oeqa.runtime.decorator.package import OEHasPackage 15from oeqa.core.utils.path import findFile 16 17class RpmBasicTest(OERuntimeTestCase): 18 19 @OEHasPackage(['rpm']) 20 @OETestDepends(['ssh.SSHTest.test_ssh']) 21 def test_rpm_help(self): 22 status, output = self.target.run('rpm --help') 23 msg = 'status and output: %s and %s' % (status, output) 24 self.assertEqual(status, 0, msg=msg) 25 26 @OETestDepends(['rpm.RpmBasicTest.test_rpm_help']) 27 def test_rpm_query(self): 28 status, output = self.target.run('ls /var/lib/rpm/') 29 if status != 0: 30 self.skipTest('No /var/lib/rpm on target') 31 status, output = self.target.run('rpm -q rpm') 32 msg = 'status and output: %s and %s' % (status, output) 33 self.assertEqual(status, 0, msg=msg) 34 35 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query']) 36 def test_rpm_query_nonroot(self): 37 38 def set_up_test_user(u): 39 status, output = self.target.run('id -u %s' % u) 40 if status: 41 status, output = self.target.run('useradd %s' % u) 42 msg = 'Failed to create new user: %s' % output 43 self.assertTrue(status == 0, msg=msg) 44 45 def exec_as_test_user(u): 46 status, output = self.target.run('su -c id %s' % u) 47 msg = 'Failed to execute as new user' 48 self.assertTrue("({0})".format(u) in output, msg=msg) 49 50 status, output = self.target.run('su -c "rpm -qa" %s ' % u) 51 msg = 'status: %s. Cannot run rpm -qa: %s' % (status, output) 52 self.assertEqual(status, 0, msg=msg) 53 54 def wait_for_no_process_for_user(u, timeout = 120): 55 timeout_at = time.time() + timeout 56 while time.time() < timeout_at: 57 _, output = self.target.run(self.tc.target_cmds['ps']) 58 if u + ' ' not in output: 59 return 60 time.sleep(1) 61 user_pss = [ps for ps in output.split("\n") if u + ' ' in ps] 62 msg = "User %s has processes still running: %s" % (u, "\n".join(user_pss)) 63 self.fail(msg=msg) 64 65 def unset_up_test_user(u): 66 # ensure no test1 process in running 67 wait_for_no_process_for_user(u) 68 status, output = self.target.run('userdel -r %s' % u) 69 msg = 'Failed to erase user: %s' % output 70 self.assertTrue(status == 0, msg=msg) 71 72 tuser = 'test1' 73 74 try: 75 set_up_test_user(tuser) 76 exec_as_test_user(tuser) 77 finally: 78 unset_up_test_user(tuser) 79 80 81class RpmInstallRemoveTest(OERuntimeTestCase): 82 83 def _find_test_file(self): 84 pkgarch = self.td['TUNE_PKGARCH'].replace('-', '_') 85 rpmdir = os.path.join(self.tc.td['DEPLOY_DIR'], 'rpm', pkgarch) 86 # Pick base-passwd-doc as a test file to get installed, because it's small 87 # and it will always be built for standard targets 88 rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch 89 if not os.path.exists(rpmdir): 90 self.fail("Rpm directory {} does not exist".format(rpmdir)) 91 for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc): 92 self.test_file = os.path.join(rpmdir, f) 93 break 94 else: 95 self.fail("Couldn't find the test rpm file {} in {}".format(rpm_doc, rpmdir)) 96 self.dst = '/tmp/base-passwd-doc.rpm' 97 98 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query']) 99 def test_rpm_install(self): 100 self._find_test_file() 101 self.tc.target.copyTo(self.test_file, self.dst) 102 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm') 103 msg = 'Failed to install base-passwd-doc package: %s' % output 104 self.assertEqual(status, 0, msg=msg) 105 self.tc.target.run('rm -f %s' % self.dst) 106 107 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install']) 108 def test_rpm_remove(self): 109 status,output = self.target.run('rpm -e base-passwd-doc') 110 msg = 'Failed to remove base-passwd-doc package: %s' % output 111 self.assertEqual(status, 0, msg=msg) 112 113 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_remove']) 114 def test_check_rpm_install_removal_log_file_size(self): 115 """ 116 Summary: Check that rpm writes into /var/log/messages 117 Expected: There should be some RPM prefixed entries in the above file. 118 Product: BSPs 119 Author: Alexandru Georgescu <alexandru.c.georgescu@intel.com> 120 Author: Alexander Kanavin <alex.kanavin@gmail.com> 121 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> 122 """ 123 self._find_test_file() 124 db_files_cmd = 'ls /var/lib/rpm/rpmdb.sqlite*' 125 check_log_cmd = "grep RPM /var/log/messages | wc -l" 126 127 # Make sure that some database files are under /var/lib/rpm as 'rpmdb.sqlite' 128 status, output = self.target.run(db_files_cmd) 129 msg = 'Failed to find database files under /var/lib/rpm/ as rpmdb.sqlite' 130 self.assertEqual(0, status, msg=msg) 131 132 self.tc.target.copyTo(self.test_file, self.dst) 133 134 # Remove the package just in case 135 self.target.run('rpm -e base-passwd-doc') 136 137 # Install/Remove a package 10 times 138 for i in range(10): 139 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm') 140 msg = 'Failed to install base-passwd-doc package. Reason: {}'.format(output) 141 self.assertEqual(0, status, msg=msg) 142 143 status, output = self.target.run('rpm -e base-passwd-doc') 144 msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output) 145 self.assertEqual(0, status, msg=msg) 146 147 self.tc.target.run('rm -f %s' % self.dst) 148 149 150