blob: fa86eb053737cba9cf5b0ddf643d21d08d3e9d5a [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007import os
8import fnmatch
Brad Bishop96ff1982019-08-19 13:50:42 -04009import time
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010
11from oeqa.runtime.case import OERuntimeTestCase
12from oeqa.core.decorator.depends import OETestDepends
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013from 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
Brad Bishopd5ae7d92018-06-14 09:52:03 -070019 @OEHasPackage(['rpm'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 @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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026 @OETestDepends(['rpm.RpmBasicTest.test_rpm_help'])
27 def test_rpm_query(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050028 status, output = self.target.run('ls /var/lib/rpm/')
29 if status != 0:
30 self.skipTest('No /var/lib/rpm on target')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 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
Brad Bishop96ff1982019-08-19 13:50:42 -040035 @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
Andrew Geissler517393d2023-01-13 08:55:19 -060054 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 = "There're %s 's process(es) still running: %s".format(u, "\n".join(user_pss))
63 assertTrue(True, msg=msg)
Brad Bishop96ff1982019-08-19 13:50:42 -040064
65 def unset_up_test_user(u):
66 # ensure no test1 process in running
Andrew Geissler517393d2023-01-13 08:55:19 -060067 wait_for_no_process_for_user(u)
Brad Bishop96ff1982019-08-19 13:50:42 -040068 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081class RpmInstallRemoveTest(OERuntimeTestCase):
82
83 @classmethod
84 def setUpClass(cls):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_')
86 rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080087 # Pick base-passwd-doc as a test file to get installed, because it's small
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 # and it will always be built for standard targets
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080089 rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch
Brad Bishop977dc1a2019-02-06 16:01:43 -050090 if not os.path.exists(rpmdir):
91 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092 for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc):
Brad Bishop977dc1a2019-02-06 16:01:43 -050093 cls.test_file = os.path.join(rpmdir, f)
94 cls.dst = '/tmp/base-passwd-doc.rpm'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095
Brad Bishop977dc1a2019-02-06 16:01:43 -050096 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 def test_rpm_install(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050098 self.tc.target.copyTo(self.test_file, self.dst)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080099 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
100 msg = 'Failed to install base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 self.assertEqual(status, 0, msg=msg)
Brad Bishop977dc1a2019-02-06 16:01:43 -0500102 self.tc.target.run('rm -f %s' % self.dst)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install'])
105 def test_rpm_remove(self):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800106 status,output = self.target.run('rpm -e base-passwd-doc')
107 msg = 'Failed to remove base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 self.assertEqual(status, 0, msg=msg)
109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_remove'])
111 def test_check_rpm_install_removal_log_file_size(self):
112 """
113 Summary: Check that rpm writes into /var/log/messages
114 Expected: There should be some RPM prefixed entries in the above file.
115 Product: BSPs
116 Author: Alexandru Georgescu <alexandru.c.georgescu@intel.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800117 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500118 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
119 """
Andrew Geisslereff27472021-10-29 15:35:00 -0500120 db_files_cmd = 'ls /var/lib/rpm/rpmdb.sqlite*'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 check_log_cmd = "grep RPM /var/log/messages | wc -l"
122
Andrew Geisslereff27472021-10-29 15:35:00 -0500123 # Make sure that some database files are under /var/lib/rpm as 'rpmdb.sqlite'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 status, output = self.target.run(db_files_cmd)
Andrew Geisslereff27472021-10-29 15:35:00 -0500125 msg = 'Failed to find database files under /var/lib/rpm/ as rpmdb.sqlite'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126 self.assertEqual(0, status, msg=msg)
127
Brad Bishop977dc1a2019-02-06 16:01:43 -0500128 self.tc.target.copyTo(self.test_file, self.dst)
129
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 # Remove the package just in case
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800131 self.target.run('rpm -e base-passwd-doc')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132
133 # Install/Remove a package 10 times
134 for i in range(10):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
136 msg = 'Failed to install base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 self.assertEqual(0, status, msg=msg)
138
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139 status, output = self.target.run('rpm -e base-passwd-doc')
140 msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500141 self.assertEqual(0, status, msg=msg)
142
Brad Bishop977dc1a2019-02-06 16:01:43 -0500143 self.tc.target.run('rm -f %s' % self.dst)
144
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145