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