blob: e3cd818b2bceb8fccc961c206ac4e677602a1b1e [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
54 def check_no_process_for_user(u):
55 _, output = self.target.run(self.tc.target_cmds['ps'])
56 if u + ' ' in output:
57 return False
58 else:
59 return True
60
61 def unset_up_test_user(u):
62 # ensure no test1 process in running
63 timeout = time.time() + 30
64 while time.time() < timeout:
65 if check_no_process_for_user(u):
66 break
67 else:
68 time.sleep(1)
69 status, output = self.target.run('userdel -r %s' % u)
70 msg = 'Failed to erase user: %s' % output
71 self.assertTrue(status == 0, msg=msg)
72
73 tuser = 'test1'
74
75 try:
76 set_up_test_user(tuser)
77 exec_as_test_user(tuser)
78 finally:
79 unset_up_test_user(tuser)
80
81
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082class RpmInstallRemoveTest(OERuntimeTestCase):
83
84 @classmethod
85 def setUpClass(cls):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050086 pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_')
87 rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080088 # Pick base-passwd-doc as a test file to get installed, because it's small
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 # and it will always be built for standard targets
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080090 rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch
Brad Bishop977dc1a2019-02-06 16:01:43 -050091 if not os.path.exists(rpmdir):
92 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc):
Brad Bishop977dc1a2019-02-06 16:01:43 -050094 cls.test_file = os.path.join(rpmdir, f)
95 cls.dst = '/tmp/base-passwd-doc.rpm'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050096
Brad Bishop977dc1a2019-02-06 16:01:43 -050097 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050098 def test_rpm_install(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050099 self.tc.target.copyTo(self.test_file, self.dst)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
101 msg = 'Failed to install base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 self.assertEqual(status, 0, msg=msg)
Brad Bishop977dc1a2019-02-06 16:01:43 -0500103 self.tc.target.run('rm -f %s' % self.dst)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install'])
106 def test_rpm_remove(self):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800107 status,output = self.target.run('rpm -e base-passwd-doc')
108 msg = 'Failed to remove base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 self.assertEqual(status, 0, msg=msg)
110
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_remove'])
112 def test_check_rpm_install_removal_log_file_size(self):
113 """
114 Summary: Check that rpm writes into /var/log/messages
115 Expected: There should be some RPM prefixed entries in the above file.
116 Product: BSPs
117 Author: Alexandru Georgescu <alexandru.c.georgescu@intel.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
120 """
Andrew Geisslereff27472021-10-29 15:35:00 -0500121 db_files_cmd = 'ls /var/lib/rpm/rpmdb.sqlite*'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 check_log_cmd = "grep RPM /var/log/messages | wc -l"
123
Andrew Geisslereff27472021-10-29 15:35:00 -0500124 # Make sure that some database files are under /var/lib/rpm as 'rpmdb.sqlite'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500125 status, output = self.target.run(db_files_cmd)
Andrew Geisslereff27472021-10-29 15:35:00 -0500126 msg = 'Failed to find database files under /var/lib/rpm/ as rpmdb.sqlite'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 self.assertEqual(0, status, msg=msg)
128
Brad Bishop977dc1a2019-02-06 16:01:43 -0500129 self.tc.target.copyTo(self.test_file, self.dst)
130
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 # Remove the package just in case
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800132 self.target.run('rpm -e base-passwd-doc')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133
134 # Install/Remove a package 10 times
135 for i in range(10):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800136 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
137 msg = 'Failed to install base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138 self.assertEqual(0, status, msg=msg)
139
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800140 status, output = self.target.run('rpm -e base-passwd-doc')
141 msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142 self.assertEqual(0, status, msg=msg)
143
Brad Bishop977dc1a2019-02-06 16:01:43 -0500144 self.tc.target.run('rm -f %s' % self.dst)
145
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146