Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | import os |
| 8 | import fnmatch |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 9 | import time |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | |
| 11 | from oeqa.runtime.case import OERuntimeTestCase |
| 12 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | from oeqa.core.decorator.data import skipIfDataVar |
| 14 | from oeqa.runtime.decorator.package import OEHasPackage |
| 15 | from oeqa.core.utils.path import findFile |
| 16 | |
| 17 | class RpmBasicTest(OERuntimeTestCase): |
| 18 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 19 | @OEHasPackage(['rpm']) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 26 | @OETestDepends(['rpm.RpmBasicTest.test_rpm_help']) |
| 27 | def test_rpm_query(self): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 28 | status, output = self.target.run('ls /var/lib/rpm/') |
| 29 | if status != 0: |
| 30 | self.skipTest('No /var/lib/rpm on target') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 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 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 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] |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 62 | msg = "User %s has processes still running: %s" % (u, "\n".join(user_pss)) |
| 63 | self.fail(msg=msg) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 64 | |
| 65 | def unset_up_test_user(u): |
| 66 | # ensure no test1 process in running |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 67 | wait_for_no_process_for_user(u) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | class RpmInstallRemoveTest(OERuntimeTestCase): |
| 82 | |
| 83 | @classmethod |
| 84 | def setUpClass(cls): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 85 | pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_') |
| 86 | rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 87 | # Pick base-passwd-doc as a test file to get installed, because it's small |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 88 | # and it will always be built for standard targets |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 89 | rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 90 | if not os.path.exists(rpmdir): |
| 91 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 93 | cls.test_file = os.path.join(rpmdir, f) |
| 94 | cls.dst = '/tmp/base-passwd-doc.rpm' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 96 | @OETestDepends(['rpm.RpmBasicTest.test_rpm_query']) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 97 | def test_rpm_install(self): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 98 | self.tc.target.copyTo(self.test_file, self.dst) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 99 | status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm') |
| 100 | msg = 'Failed to install base-passwd-doc package: %s' % output |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | self.assertEqual(status, 0, msg=msg) |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 102 | self.tc.target.run('rm -f %s' % self.dst) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 103 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install']) |
| 105 | def test_rpm_remove(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 106 | status,output = self.target.run('rpm -e base-passwd-doc') |
| 107 | msg = 'Failed to remove base-passwd-doc package: %s' % output |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 108 | self.assertEqual(status, 0, msg=msg) |
| 109 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | @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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 117 | Author: Alexander Kanavin <alex.kanavin@gmail.com> |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 118 | AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 119 | """ |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 120 | db_files_cmd = 'ls /var/lib/rpm/rpmdb.sqlite*' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | check_log_cmd = "grep RPM /var/log/messages | wc -l" |
| 122 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 123 | # Make sure that some database files are under /var/lib/rpm as 'rpmdb.sqlite' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | status, output = self.target.run(db_files_cmd) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 125 | msg = 'Failed to find database files under /var/lib/rpm/ as rpmdb.sqlite' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 126 | self.assertEqual(0, status, msg=msg) |
| 127 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 128 | self.tc.target.copyTo(self.test_file, self.dst) |
| 129 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 130 | # Remove the package just in case |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 131 | self.target.run('rpm -e base-passwd-doc') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 132 | |
| 133 | # Install/Remove a package 10 times |
| 134 | for i in range(10): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 135 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | self.assertEqual(0, status, msg=msg) |
| 138 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 139 | status, output = self.target.run('rpm -e base-passwd-doc') |
| 140 | msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 141 | self.assertEqual(0, status, msg=msg) |
| 142 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 143 | self.tc.target.run('rm -f %s' % self.dst) |
| 144 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 145 | |