1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# SPDX-License-Identifier: MIT 3*4882a593Smuzhiyun# 4*4882a593Smuzhiyun 5*4882a593Smuzhiyunimport os 6*4882a593Smuzhiyunimport re 7*4882a593Smuzhiyunimport subprocess 8*4882a593Smuzhiyunfrom oeqa.utils.httpserver import HTTPService 9*4882a593Smuzhiyun 10*4882a593Smuzhiyunfrom oeqa.runtime.case import OERuntimeTestCase 11*4882a593Smuzhiyunfrom oeqa.core.decorator.depends import OETestDepends 12*4882a593Smuzhiyunfrom oeqa.core.decorator.data import skipIfNotDataVar, skipIfNotFeature, skipIfInDataVar, skipIfNotInDataVar 13*4882a593Smuzhiyunfrom oeqa.runtime.decorator.package import OEHasPackage 14*4882a593Smuzhiyun 15*4882a593Smuzhiyunclass DnfTest(OERuntimeTestCase): 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun def dnf(self, command, expected = 0): 18*4882a593Smuzhiyun command = 'dnf %s' % command 19*4882a593Smuzhiyun status, output = self.target.run(command, 1500) 20*4882a593Smuzhiyun message = os.linesep.join([command, output]) 21*4882a593Smuzhiyun self.assertEqual(status, expected, message) 22*4882a593Smuzhiyun return output 23*4882a593Smuzhiyun 24*4882a593Smuzhiyunclass DnfBasicTest(DnfTest): 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun @skipIfNotFeature('package-management', 27*4882a593Smuzhiyun 'Test requires package-management to be in IMAGE_FEATURES') 28*4882a593Smuzhiyun @skipIfNotDataVar('IMAGE_PKGTYPE', 'rpm', 29*4882a593Smuzhiyun 'RPM is not the primary package manager') 30*4882a593Smuzhiyun @OEHasPackage(['dnf']) 31*4882a593Smuzhiyun @OETestDepends(['ssh.SSHTest.test_ssh']) 32*4882a593Smuzhiyun def test_dnf_help(self): 33*4882a593Smuzhiyun self.dnf('--help') 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun @OETestDepends(['dnf.DnfBasicTest.test_dnf_help']) 36*4882a593Smuzhiyun def test_dnf_version(self): 37*4882a593Smuzhiyun self.dnf('--version') 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun @OETestDepends(['dnf.DnfBasicTest.test_dnf_help']) 40*4882a593Smuzhiyun def test_dnf_info(self): 41*4882a593Smuzhiyun self.dnf('info dnf') 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun @OETestDepends(['dnf.DnfBasicTest.test_dnf_help']) 44*4882a593Smuzhiyun def test_dnf_search(self): 45*4882a593Smuzhiyun self.dnf('search dnf') 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun @OETestDepends(['dnf.DnfBasicTest.test_dnf_help']) 48*4882a593Smuzhiyun def test_dnf_history(self): 49*4882a593Smuzhiyun self.dnf('history') 50*4882a593Smuzhiyun 51*4882a593Smuzhiyunclass DnfRepoTest(DnfTest): 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun @classmethod 54*4882a593Smuzhiyun def setUpClass(cls): 55*4882a593Smuzhiyun cls.repo_server = HTTPService(os.path.join(cls.tc.td['WORKDIR'], 'oe-testimage-repo'), 56*4882a593Smuzhiyun '0.0.0.0', port=cls.tc.target.server_port, 57*4882a593Smuzhiyun logger=cls.tc.logger) 58*4882a593Smuzhiyun cls.repo_server.start() 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun @classmethod 61*4882a593Smuzhiyun def tearDownClass(cls): 62*4882a593Smuzhiyun cls.repo_server.stop() 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun def dnf_with_repo(self, command): 65*4882a593Smuzhiyun pkgarchs = os.listdir(os.path.join(self.tc.td['WORKDIR'], 'oe-testimage-repo')) 66*4882a593Smuzhiyun deploy_url = 'http://%s:%s/' %(self.target.server_ip, self.repo_server.port) 67*4882a593Smuzhiyun cmdlinerepoopts = ["--repofrompath=oe-testimage-repo-%s,%s%s" %(arch, deploy_url, arch) for arch in pkgarchs] 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun output = self.dnf(" ".join(cmdlinerepoopts) + " --nogpgcheck " + command) 70*4882a593Smuzhiyun return output 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun @OETestDepends(['dnf.DnfBasicTest.test_dnf_help']) 73*4882a593Smuzhiyun def test_dnf_makecache(self): 74*4882a593Smuzhiyun self.dnf_with_repo('makecache') 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun# Does not work when repo is specified on the command line 78*4882a593Smuzhiyun# @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 79*4882a593Smuzhiyun# def test_dnf_repolist(self): 80*4882a593Smuzhiyun# self.dnf_with_repo('repolist') 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 83*4882a593Smuzhiyun def test_dnf_repoinfo(self): 84*4882a593Smuzhiyun self.dnf_with_repo('repoinfo') 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 87*4882a593Smuzhiyun def test_dnf_install(self): 88*4882a593Smuzhiyun output = self.dnf_with_repo('list run-postinsts-dev') 89*4882a593Smuzhiyun if 'Installed Packages' in output: 90*4882a593Smuzhiyun self.dnf_with_repo('remove -y run-postinsts-dev') 91*4882a593Smuzhiyun self.dnf_with_repo('install -y run-postinsts-dev') 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_install']) 94*4882a593Smuzhiyun def test_dnf_install_dependency(self): 95*4882a593Smuzhiyun self.dnf_with_repo('remove -y run-postinsts') 96*4882a593Smuzhiyun self.dnf_with_repo('install -y run-postinsts-dev') 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_install_dependency']) 99*4882a593Smuzhiyun def test_dnf_install_from_disk(self): 100*4882a593Smuzhiyun self.dnf_with_repo('remove -y run-postinsts-dev') 101*4882a593Smuzhiyun self.dnf_with_repo('install -y --downloadonly run-postinsts-dev') 102*4882a593Smuzhiyun status, output = self.target.run('find /var/cache/dnf -name run-postinsts-dev*rpm', 1500) 103*4882a593Smuzhiyun self.assertEqual(status, 0, output) 104*4882a593Smuzhiyun self.dnf_with_repo('install -y %s' % output) 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_install_from_disk']) 107*4882a593Smuzhiyun def test_dnf_install_from_http(self): 108*4882a593Smuzhiyun output = subprocess.check_output('%s %s -name run-postinsts-dev*' % (bb.utils.which(os.getenv('PATH'), "find"), 109*4882a593Smuzhiyun os.path.join(self.tc.td['WORKDIR'], 'oe-testimage-repo')), shell=True).decode("utf-8") 110*4882a593Smuzhiyun rpm_path = output.split("/")[-2] + "/" + output.split("/")[-1] 111*4882a593Smuzhiyun url = 'http://%s:%s/%s' %(self.target.server_ip, self.repo_server.port, rpm_path) 112*4882a593Smuzhiyun self.dnf_with_repo('remove -y run-postinsts-dev') 113*4882a593Smuzhiyun self.dnf_with_repo('install -y %s' % url) 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_install']) 116*4882a593Smuzhiyun def test_dnf_reinstall(self): 117*4882a593Smuzhiyun self.dnf_with_repo('reinstall -y run-postinsts-dev') 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 120*4882a593Smuzhiyun @skipIfInDataVar('DISTRO_FEATURES', 'usrmerge', 'Test run when not enable usrmerge') 121*4882a593Smuzhiyun @OEHasPackage('busybox') 122*4882a593Smuzhiyun def test_dnf_installroot(self): 123*4882a593Smuzhiyun rootpath = '/home/root/chroot/test' 124*4882a593Smuzhiyun #Copy necessary files to avoid errors with not yet installed tools on 125*4882a593Smuzhiyun #installroot directory. 126*4882a593Smuzhiyun self.target.run('mkdir -p %s/etc' % rootpath, 1500) 127*4882a593Smuzhiyun self.target.run('mkdir -p %s/bin %s/sbin %s/usr/bin %s/usr/sbin' % (rootpath, rootpath, rootpath, rootpath), 1500) 128*4882a593Smuzhiyun self.target.run('mkdir -p %s/dev' % rootpath, 1500) 129*4882a593Smuzhiyun #Handle different architectures lib dirs 130*4882a593Smuzhiyun self.target.run('mkdir -p %s/lib' % rootpath, 1500) 131*4882a593Smuzhiyun self.target.run('mkdir -p %s/libx32' % rootpath, 1500) 132*4882a593Smuzhiyun self.target.run('mkdir -p %s/lib64' % rootpath, 1500) 133*4882a593Smuzhiyun self.target.run('cp /lib/libtinfo.so.5 %s/lib' % rootpath, 1500) 134*4882a593Smuzhiyun self.target.run('cp /libx32/libtinfo.so.5 %s/libx32' % rootpath, 1500) 135*4882a593Smuzhiyun self.target.run('cp /lib64/libtinfo.so.5 %s/lib64' % rootpath, 1500) 136*4882a593Smuzhiyun self.target.run('cp -r /etc/rpm %s/etc' % rootpath, 1500) 137*4882a593Smuzhiyun self.target.run('cp -r /etc/dnf %s/etc' % rootpath, 1500) 138*4882a593Smuzhiyun self.target.run('cp /bin/sh %s/bin' % rootpath, 1500) 139*4882a593Smuzhiyun self.target.run('mount -o bind /dev %s/dev/' % rootpath, 1500) 140*4882a593Smuzhiyun self.dnf_with_repo('install --installroot=%s -v -y --rpmverbosity=debug busybox run-postinsts' % rootpath) 141*4882a593Smuzhiyun status, output = self.target.run('test -e %s/var/cache/dnf' % rootpath, 1500) 142*4882a593Smuzhiyun self.assertEqual(0, status, output) 143*4882a593Smuzhiyun status, output = self.target.run('test -e %s/bin/busybox' % rootpath, 1500) 144*4882a593Smuzhiyun self.assertEqual(0, status, output) 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 147*4882a593Smuzhiyun @skipIfNotInDataVar('DISTRO_FEATURES', 'usrmerge', 'Test run when enable usrmerge') 148*4882a593Smuzhiyun @OEHasPackage('busybox') 149*4882a593Smuzhiyun def test_dnf_installroot_usrmerge(self): 150*4882a593Smuzhiyun rootpath = '/home/root/chroot/test' 151*4882a593Smuzhiyun #Copy necessary files to avoid errors with not yet installed tools on 152*4882a593Smuzhiyun #installroot directory. 153*4882a593Smuzhiyun self.target.run('mkdir -p %s/etc' % rootpath, 1500) 154*4882a593Smuzhiyun self.target.run('mkdir -p %s/usr/bin %s/usr/sbin' % (rootpath, rootpath), 1500) 155*4882a593Smuzhiyun self.target.run('ln -sf -r %s/usr/bin %s/bin' % (rootpath, rootpath), 1500) 156*4882a593Smuzhiyun self.target.run('ln -sf -r %s/usr/sbin %s/sbin' % (rootpath, rootpath), 1500) 157*4882a593Smuzhiyun self.target.run('mkdir -p %s/dev' % rootpath, 1500) 158*4882a593Smuzhiyun #Handle different architectures lib dirs 159*4882a593Smuzhiyun self.target.run('mkdir -p %s/usr/lib' % rootpath, 1500) 160*4882a593Smuzhiyun self.target.run('mkdir -p %s/usr/libx32' % rootpath, 1500) 161*4882a593Smuzhiyun self.target.run('mkdir -p %s/usr/lib64' % rootpath, 1500) 162*4882a593Smuzhiyun self.target.run('cp /lib/libtinfo.so.5 %s/usr/lib' % rootpath, 1500) 163*4882a593Smuzhiyun self.target.run('cp /libx32/libtinfo.so.5 %s/usr/libx32' % rootpath, 1500) 164*4882a593Smuzhiyun self.target.run('cp /lib64/libtinfo.so.5 %s/usr/lib64' % rootpath, 1500) 165*4882a593Smuzhiyun self.target.run('ln -sf -r %s/lib %s/usr/lib' % (rootpath,rootpath), 1500) 166*4882a593Smuzhiyun self.target.run('ln -sf -r %s/libx32 %s/usr/libx32' % (rootpath,rootpath), 1500) 167*4882a593Smuzhiyun self.target.run('ln -sf -r %s/lib64 %s/usr/lib64' % (rootpath,rootpath), 1500) 168*4882a593Smuzhiyun self.target.run('cp -r /etc/rpm %s/etc' % rootpath, 1500) 169*4882a593Smuzhiyun self.target.run('cp -r /etc/dnf %s/etc' % rootpath, 1500) 170*4882a593Smuzhiyun self.target.run('cp /bin/sh %s/bin' % rootpath, 1500) 171*4882a593Smuzhiyun self.target.run('mount -o bind /dev %s/dev/' % rootpath, 1500) 172*4882a593Smuzhiyun self.dnf_with_repo('install --installroot=%s -v -y --rpmverbosity=debug busybox run-postinsts' % rootpath) 173*4882a593Smuzhiyun status, output = self.target.run('test -e %s/var/cache/dnf' % rootpath, 1500) 174*4882a593Smuzhiyun self.assertEqual(0, status, output) 175*4882a593Smuzhiyun status, output = self.target.run('test -e %s/bin/busybox' % rootpath, 1500) 176*4882a593Smuzhiyun self.assertEqual(0, status, output) 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun @OETestDepends(['dnf.DnfRepoTest.test_dnf_makecache']) 179*4882a593Smuzhiyun def test_dnf_exclude(self): 180*4882a593Smuzhiyun excludepkg = 'curl-dev' 181*4882a593Smuzhiyun self.dnf_with_repo('install -y curl*') 182*4882a593Smuzhiyun self.dnf('list %s' % excludepkg, 0) 183*4882a593Smuzhiyun #Avoid remove dependencies to skip some errors on different archs and images 184*4882a593Smuzhiyun self.dnf_with_repo('remove --setopt=clean_requirements_on_remove=0 -y curl*') 185*4882a593Smuzhiyun #check curl-dev is not installed adter removing all curl occurrences 186*4882a593Smuzhiyun status, output = self.target.run('dnf list --installed | grep %s'% excludepkg, 1500) 187*4882a593Smuzhiyun self.assertEqual(1, status, "%s was not removed, is listed as installed"%excludepkg) 188*4882a593Smuzhiyun self.dnf_with_repo('install -y --exclude=%s --exclude=curl-staticdev curl*' % excludepkg) 189*4882a593Smuzhiyun #check curl-dev is not installed after being excluded 190*4882a593Smuzhiyun status, output = self.target.run('dnf list --installed | grep %s'% excludepkg , 1500) 191*4882a593Smuzhiyun self.assertEqual(1, status, "%s was not excluded, is listed as installed"%excludepkg) 192