blob: d8cabd3640e7207b66873103754a6fb161f0d7a8 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005import os
6import fnmatch
7
8from oeqa.runtime.case import OERuntimeTestCase
9from oeqa.core.decorator.depends import OETestDepends
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010from oeqa.core.decorator.data import skipIfDataVar
11from oeqa.runtime.decorator.package import OEHasPackage
12from oeqa.core.utils.path import findFile
13
14class RpmBasicTest(OERuntimeTestCase):
15
Brad Bishopd5ae7d92018-06-14 09:52:03 -070016 @OEHasPackage(['rpm'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017 @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 Bishop6e60e8b2018-02-01 10:27:11 -050023 @OETestDepends(['rpm.RpmBasicTest.test_rpm_help'])
24 def test_rpm_query(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050025 status, output = self.target.run('ls /var/lib/rpm/')
26 if status != 0:
27 self.skipTest('No /var/lib/rpm on target')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050028 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
32class RpmInstallRemoveTest(OERuntimeTestCase):
33
34 @classmethod
35 def setUpClass(cls):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_')
37 rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038 # Pick base-passwd-doc as a test file to get installed, because it's small
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 # and it will always be built for standard targets
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080040 rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch
Brad Bishop977dc1a2019-02-06 16:01:43 -050041 if not os.path.exists(rpmdir):
42 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc):
Brad Bishop977dc1a2019-02-06 16:01:43 -050044 cls.test_file = os.path.join(rpmdir, f)
45 cls.dst = '/tmp/base-passwd-doc.rpm'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046
Brad Bishop977dc1a2019-02-06 16:01:43 -050047 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 def test_rpm_install(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050049 self.tc.target.copyTo(self.test_file, self.dst)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080050 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
51 msg = 'Failed to install base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -050052 self.assertEqual(status, 0, msg=msg)
Brad Bishop977dc1a2019-02-06 16:01:43 -050053 self.tc.target.run('rm -f %s' % self.dst)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054
Brad Bishop6e60e8b2018-02-01 10:27:11 -050055 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install'])
56 def test_rpm_remove(self):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 status,output = self.target.run('rpm -e base-passwd-doc')
58 msg = 'Failed to remove base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059 self.assertEqual(status, 0, msg=msg)
60
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 @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 Bishop6e60e8b2018-02-01 10:27:11 -050093 @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 Bishop1a4b7ee2018-12-16 17:11:34 -0800100 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 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 Bishop977dc1a2019-02-06 16:01:43 -0500111 self.tc.target.copyTo(self.test_file, self.dst)
112
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 # Remove the package just in case
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800114 self.target.run('rpm -e base-passwd-doc')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115
116 # Install/Remove a package 10 times
117 for i in range(10):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 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 Bishop6e60e8b2018-02-01 10:27:11 -0500120 self.assertEqual(0, status, msg=msg)
121
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 status, output = self.target.run('rpm -e base-passwd-doc')
123 msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 self.assertEqual(0, status, msg=msg)
125
Brad Bishop977dc1a2019-02-06 16:01:43 -0500126 self.tc.target.run('rm -f %s' % self.dst)
127
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500128 # 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))