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