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