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