Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 5 | import os |
| 6 | import fnmatch |
| 7 | |
| 8 | from oeqa.runtime.case import OERuntimeTestCase |
| 9 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | from oeqa.core.decorator.data import skipIfDataVar |
| 11 | from oeqa.runtime.decorator.package import OEHasPackage |
| 12 | from oeqa.core.utils.path import findFile |
| 13 | |
| 14 | class RpmBasicTest(OERuntimeTestCase): |
| 15 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 16 | @OEHasPackage(['rpm']) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
| 18 | def test_rpm_help(self): |
| 19 | status, output = self.target.run('rpm --help') |
| 20 | msg = 'status and output: %s and %s' % (status, output) |
| 21 | self.assertEqual(status, 0, msg=msg) |
| 22 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 23 | @OETestDepends(['rpm.RpmBasicTest.test_rpm_help']) |
| 24 | def test_rpm_query(self): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 25 | status, output = self.target.run('ls /var/lib/rpm/') |
| 26 | if status != 0: |
| 27 | self.skipTest('No /var/lib/rpm on target') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 28 | status, output = self.target.run('rpm -q rpm') |
| 29 | msg = 'status and output: %s and %s' % (status, output) |
| 30 | self.assertEqual(status, 0, msg=msg) |
| 31 | |
| 32 | class RpmInstallRemoveTest(OERuntimeTestCase): |
| 33 | |
| 34 | @classmethod |
| 35 | def setUpClass(cls): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_') |
| 37 | rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 38 | # 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] | 39 | # and it will always be built for standard targets |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 40 | rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 41 | if not os.path.exists(rpmdir): |
| 42 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 43 | for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 44 | cls.test_file = os.path.join(rpmdir, f) |
| 45 | cls.dst = '/tmp/base-passwd-doc.rpm' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 47 | @OETestDepends(['rpm.RpmBasicTest.test_rpm_query']) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | def test_rpm_install(self): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 49 | self.tc.target.copyTo(self.test_file, self.dst) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 50 | status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm') |
| 51 | msg = 'Failed to install base-passwd-doc package: %s' % output |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | self.assertEqual(status, 0, msg=msg) |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 53 | self.tc.target.run('rm -f %s' % self.dst) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 54 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install']) |
| 56 | def test_rpm_remove(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 57 | status,output = self.target.run('rpm -e base-passwd-doc') |
| 58 | msg = 'Failed to remove base-passwd-doc package: %s' % output |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 59 | self.assertEqual(status, 0, msg=msg) |
| 60 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | @OETestDepends(['rpm.RpmBasicTest.test_rpm_query']) |
| 62 | def test_rpm_query_nonroot(self): |
| 63 | |
| 64 | def set_up_test_user(u): |
| 65 | status, output = self.target.run('id -u %s' % u) |
| 66 | if status: |
| 67 | status, output = self.target.run('useradd %s' % u) |
| 68 | msg = 'Failed to create new user: %s' % output |
| 69 | self.assertTrue(status == 0, msg=msg) |
| 70 | |
| 71 | def exec_as_test_user(u): |
| 72 | status, output = self.target.run('su -c id %s' % u) |
| 73 | msg = 'Failed to execute as new user' |
| 74 | self.assertTrue("({0})".format(u) in output, msg=msg) |
| 75 | |
| 76 | status, output = self.target.run('su -c "rpm -qa" %s ' % u) |
| 77 | msg = 'status: %s. Cannot run rpm -qa: %s' % (status, output) |
| 78 | self.assertEqual(status, 0, msg=msg) |
| 79 | |
| 80 | def unset_up_test_user(u): |
| 81 | status, output = self.target.run('userdel -r %s' % u) |
| 82 | msg = 'Failed to erase user: %s' % output |
| 83 | self.assertTrue(status == 0, msg=msg) |
| 84 | |
| 85 | tuser = 'test1' |
| 86 | |
| 87 | try: |
| 88 | set_up_test_user(tuser) |
| 89 | exec_as_test_user(tuser) |
| 90 | finally: |
| 91 | unset_up_test_user(tuser) |
| 92 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_remove']) |
| 94 | def test_check_rpm_install_removal_log_file_size(self): |
| 95 | """ |
| 96 | Summary: Check that rpm writes into /var/log/messages |
| 97 | Expected: There should be some RPM prefixed entries in the above file. |
| 98 | Product: BSPs |
| 99 | Author: Alexandru Georgescu <alexandru.c.georgescu@intel.com> |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 100 | Author: Alexander Kanavin <alex.kanavin@gmail.com> |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 102 | """ |
| 103 | db_files_cmd = 'ls /var/lib/rpm/__db.*' |
| 104 | check_log_cmd = "grep RPM /var/log/messages | wc -l" |
| 105 | |
| 106 | # Make sure that some database files are under /var/lib/rpm as '__db.xxx' |
| 107 | status, output = self.target.run(db_files_cmd) |
| 108 | msg = 'Failed to find database files under /var/lib/rpm/ as __db.xxx' |
| 109 | self.assertEqual(0, status, msg=msg) |
| 110 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 111 | self.tc.target.copyTo(self.test_file, self.dst) |
| 112 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | # Remove the package just in case |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 114 | self.target.run('rpm -e base-passwd-doc') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 115 | |
| 116 | # Install/Remove a package 10 times |
| 117 | for i in range(10): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 118 | status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm') |
| 119 | msg = 'Failed to install base-passwd-doc package. Reason: {}'.format(output) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 120 | self.assertEqual(0, status, msg=msg) |
| 121 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 122 | status, output = self.target.run('rpm -e base-passwd-doc') |
| 123 | msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | self.assertEqual(0, status, msg=msg) |
| 125 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 126 | self.tc.target.run('rm -f %s' % self.dst) |
| 127 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | # if using systemd this should ensure all entries are flushed to /var |
| 129 | status, output = self.target.run("journalctl --sync") |
| 130 | # Get the amount of entries in the log file |
| 131 | status, output = self.target.run(check_log_cmd) |
| 132 | msg = 'Failed to get the final size of the log file.' |
| 133 | self.assertEqual(0, status, msg=msg) |
| 134 | |
| 135 | # Check that there's enough of them |
| 136 | self.assertGreaterEqual(int(output), 80, |
| 137 | 'Cound not find sufficient amount of rpm entries in /var/log/messages, found {} entries'.format(output)) |