blob: 7a9d62c0038abea38b8123a5d6b144b09fe76a66 [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
Brad Bishop96ff1982019-08-19 13:50:42 -04007import time
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008
9from oeqa.runtime.case import OERuntimeTestCase
10from oeqa.core.decorator.depends import OETestDepends
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011from oeqa.core.decorator.data import skipIfDataVar
12from oeqa.runtime.decorator.package import OEHasPackage
13from oeqa.core.utils.path import findFile
14
15class RpmBasicTest(OERuntimeTestCase):
16
Brad Bishopd5ae7d92018-06-14 09:52:03 -070017 @OEHasPackage(['rpm'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018 @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 Bishop6e60e8b2018-02-01 10:27:11 -050024 @OETestDepends(['rpm.RpmBasicTest.test_rpm_help'])
25 def test_rpm_query(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050026 status, output = self.target.run('ls /var/lib/rpm/')
27 if status != 0:
28 self.skipTest('No /var/lib/rpm on target')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 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 Bishop96ff1982019-08-19 13:50:42 -040033 @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 Bishop6e60e8b2018-02-01 10:27:11 -050080class RpmInstallRemoveTest(OERuntimeTestCase):
81
82 @classmethod
83 def setUpClass(cls):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 pkgarch = cls.td['TUNE_PKGARCH'].replace('-', '_')
85 rpmdir = os.path.join(cls.tc.td['DEPLOY_DIR'], 'rpm', pkgarch)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080086 # Pick base-passwd-doc as a test file to get installed, because it's small
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 # and it will always be built for standard targets
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080088 rpm_doc = 'base-passwd-doc-*.%s.rpm' % pkgarch
Brad Bishop977dc1a2019-02-06 16:01:43 -050089 if not os.path.exists(rpmdir):
90 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 for f in fnmatch.filter(os.listdir(rpmdir), rpm_doc):
Brad Bishop977dc1a2019-02-06 16:01:43 -050092 cls.test_file = os.path.join(rpmdir, f)
93 cls.dst = '/tmp/base-passwd-doc.rpm'
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094
Brad Bishop977dc1a2019-02-06 16:01:43 -050095 @OETestDepends(['rpm.RpmBasicTest.test_rpm_query'])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050096 def test_rpm_install(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050097 self.tc.target.copyTo(self.test_file, self.dst)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080098 status, output = self.target.run('rpm -ivh /tmp/base-passwd-doc.rpm')
99 msg = 'Failed to install base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 self.assertEqual(status, 0, msg=msg)
Brad Bishop977dc1a2019-02-06 16:01:43 -0500101 self.tc.target.run('rm -f %s' % self.dst)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 @OETestDepends(['rpm.RpmInstallRemoveTest.test_rpm_install'])
104 def test_rpm_remove(self):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800105 status,output = self.target.run('rpm -e base-passwd-doc')
106 msg = 'Failed to remove base-passwd-doc package: %s' % output
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 self.assertEqual(status, 0, msg=msg)
108
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 @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 Bishop1a4b7ee2018-12-16 17:11:34 -0800116 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 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 Bishop977dc1a2019-02-06 16:01:43 -0500127 self.tc.target.copyTo(self.test_file, self.dst)
128
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500129 # Remove the package just in case
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 self.target.run('rpm -e base-passwd-doc')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131
132 # Install/Remove a package 10 times
133 for i in range(10):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 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 Bishop6e60e8b2018-02-01 10:27:11 -0500136 self.assertEqual(0, status, msg=msg)
137
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138 status, output = self.target.run('rpm -e base-passwd-doc')
139 msg = 'Failed to remove base-passwd-doc package. Reason: {}'.format(output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 self.assertEqual(0, status, msg=msg)
141
Brad Bishop977dc1a2019-02-06 16:01:43 -0500142 self.tc.target.run('rm -f %s' % self.dst)
143
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144