xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/runtime/cases/apt.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# SPDX-License-Identifier: MIT
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun
5*4882a593Smuzhiyunimport os
6*4882a593Smuzhiyunfrom oeqa.utils.httpserver import HTTPService
7*4882a593Smuzhiyunfrom oeqa.runtime.case import OERuntimeTestCase
8*4882a593Smuzhiyunfrom oeqa.core.decorator.data import skipIfNotDataVar, skipIfNotFeature
9*4882a593Smuzhiyunfrom oeqa.runtime.decorator.package import OEHasPackage
10*4882a593Smuzhiyun
11*4882a593Smuzhiyunclass AptTest(OERuntimeTestCase):
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun    def pkg(self, command, expected = 0):
14*4882a593Smuzhiyun        command = 'apt-get %s' % command
15*4882a593Smuzhiyun        status, output = self.target.run(command, 1500)
16*4882a593Smuzhiyun        message = os.linesep.join([command, output])
17*4882a593Smuzhiyun        self.assertEqual(status, expected, message)
18*4882a593Smuzhiyun        return output
19*4882a593Smuzhiyun
20*4882a593Smuzhiyunclass AptRepoTest(AptTest):
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun    @classmethod
23*4882a593Smuzhiyun    def setUpClass(cls):
24*4882a593Smuzhiyun        service_repo = os.path.join(cls.tc.td['DEPLOY_DIR_DEB'], '')
25*4882a593Smuzhiyun        cls.repo_server = HTTPService(service_repo,
26*4882a593Smuzhiyun                                      '0.0.0.0', port=cls.tc.target.server_port,
27*4882a593Smuzhiyun                                      logger=cls.tc.logger)
28*4882a593Smuzhiyun        cls.repo_server.start()
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun    @classmethod
31*4882a593Smuzhiyun    def tearDownClass(cls):
32*4882a593Smuzhiyun        cls.repo_server.stop()
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    def setup_source_config_for_package_install(self):
35*4882a593Smuzhiyun        apt_get_source_server = 'http://%s:%s/' % (self.tc.target.server_ip, self.repo_server.port)
36*4882a593Smuzhiyun        apt_get_sourceslist_dir = '/etc/apt/'
37*4882a593Smuzhiyun        self.target.run('cd %s; echo deb [ allow-insecure=yes ] %s/all ./ > sources.list' % (apt_get_sourceslist_dir, apt_get_source_server))
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun    def setup_source_config_for_package_install_signed(self):
40*4882a593Smuzhiyun        apt_get_source_server = 'http:\/\/%s:%s' % (self.tc.target.server_ip, self.repo_server.port)
41*4882a593Smuzhiyun        apt_get_sourceslist_dir = '/etc/apt/'
42*4882a593Smuzhiyun        self.target.run("cd %s; cp sources.list sources.list.bak; sed -i 's/\[trusted=yes\] http:\/\/bogus_ip:bogus_port/%s/g' sources.list" % (apt_get_sourceslist_dir, apt_get_source_server))
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun    def cleanup_source_config_for_package_install(self):
45*4882a593Smuzhiyun        apt_get_sourceslist_dir = '/etc/apt/'
46*4882a593Smuzhiyun        self.target.run('cd %s; rm sources.list' % (apt_get_sourceslist_dir))
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun    def cleanup_source_config_for_package_install_signed(self):
49*4882a593Smuzhiyun        apt_get_sourceslist_dir = '/etc/apt/'
50*4882a593Smuzhiyun        self.target.run('cd %s; mv sources.list.bak sources.list' % (apt_get_sourceslist_dir))
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun    def setup_key(self):
53*4882a593Smuzhiyun        # the key is found on the target /etc/pki/packagefeed-gpg/
54*4882a593Smuzhiyun        # named PACKAGEFEED-GPG-KEY-poky-branch
55*4882a593Smuzhiyun        self.target.run('cd %s; apt-key add P*' % ('/etc/pki/packagefeed-gpg'))
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun    @skipIfNotFeature('package-management',
58*4882a593Smuzhiyun                      'Test requires package-management to be in IMAGE_FEATURES')
59*4882a593Smuzhiyun    @skipIfNotDataVar('IMAGE_PKGTYPE', 'deb',
60*4882a593Smuzhiyun                      'DEB is not the primary package manager')
61*4882a593Smuzhiyun    @OEHasPackage(['apt'])
62*4882a593Smuzhiyun    def test_apt_install_from_repo(self):
63*4882a593Smuzhiyun        if not self.tc.td.get('PACKAGE_FEED_GPG_NAME'):
64*4882a593Smuzhiyun            self.setup_source_config_for_package_install()
65*4882a593Smuzhiyun            self.pkg('update')
66*4882a593Smuzhiyun            self.pkg('remove --yes run-postinsts-dev')
67*4882a593Smuzhiyun            self.pkg('install --yes --allow-unauthenticated run-postinsts-dev')
68*4882a593Smuzhiyun            self.cleanup_source_config_for_package_install()
69*4882a593Smuzhiyun        else:
70*4882a593Smuzhiyun            # when we are here a key has been set to sign the package feed and
71*4882a593Smuzhiyun            # public key and gnupg installed on the image by test_testimage_apt
72*4882a593Smuzhiyun            self.setup_source_config_for_package_install_signed()
73*4882a593Smuzhiyun            self.setup_key()
74*4882a593Smuzhiyun            self.pkg('update')
75*4882a593Smuzhiyun            self.pkg('install --yes run-postinsts-dev')
76*4882a593Smuzhiyun            self.pkg('remove --yes run-postinsts-dev')
77*4882a593Smuzhiyun            self.cleanup_source_config_for_package_install_signed()
78