1from oeqa.core.decorator.depends import OETestDepends
2from oeqa.runtime.cases.dnf import DnfTest
3from oeqa.utils.httpserver import HTTPService
4from oeqa.core.decorator.data import skipIfDataVar
5
6class DnfSelftest(DnfTest):
7
8    @classmethod
9    def setUpClass(cls):
10        import tempfile
11        cls.temp_dir = tempfile.TemporaryDirectory(prefix="oeqa-remotefeeds-")
12        cls.repo_server = HTTPService(os.path.join(cls.tc.td['WORKDIR'], 'oe-rootfs-repo'),
13                                      '0.0.0.0', port=cls.tc.target.server_port,
14                                      logger=cls.tc.logger)
15        cls.repo_server.start()
16
17    @classmethod
18    def tearDownClass(cls):
19        cls.repo_server.stop()
20        cls.temp_dir.cleanup()
21
22    @OETestDepends(['dnf.DnfBasicTest.test_dnf_help'])
23    @skipIfDataVar('PACKAGE_FEED_URIS', None,
24                   'Not suitable as PACKAGE_FEED_URIS is not set')
25    def test_verify_package_feeds(self):
26        """
27        Summary: Check correct setting of PACKAGE_FEED_URIS var
28        Expected: 1. Feeds were correctly set for dnf
29                  2. Update recovers packages from host's repo
30        Author: Humberto Ibarra <humberto.ibarra.lopez@intel.com>
31        Author: Alexander Kanavin <alex.kanavin@gmail.com>
32        """
33        # When we created an image, we had to supply fake ip and port
34        # for the feeds. Now we can patch the real ones into the config file.
35        temp_file = os.path.join(self.temp_dir.name, 'tmp.repo')
36        self.tc.target.copyFrom("/etc/yum.repos.d/oe-remote-repo.repo", temp_file)
37        fixed_config = open(temp_file, "r").read().replace("bogus_ip", self.tc.target.server_ip).replace("bogus_port", str(self.repo_server.port))
38        with open(temp_file, "w") as f:
39            f.write(fixed_config)
40        self.tc.target.copyTo(temp_file, "/etc/yum.repos.d/oe-remote-repo.repo")
41
42        import re
43        # Use '-y' for non-interactive mode: automatically import the feed signing key
44        output_makecache = self.dnf('-vy makecache')
45        self.assertTrue(re.match(r".*Failed to synchronize cache", output_makecache, re.DOTALL) is None, msg = "dnf makecache failed to synchronize repo: %s" %(output_makecache))
46        self.assertTrue(re.match(r".*Metadata cache created", output_makecache, re.DOTALL) is not None, msg = "dnf makecache failed: %s" %(output_makecache))
47
48        output_repoinfo = self.dnf('-v repoinfo')
49        matchobj = re.match(r".*Repo-pkgs\s*:\s*(?P<n_pkgs>[0-9]+)", output_repoinfo, re.DOTALL)
50        self.assertTrue(matchobj is not None, msg = "Could not find the amount of packages in dnf repoinfo output: %s" %(output_repoinfo))
51        self.assertTrue(int(matchobj.group('n_pkgs')) > 0, msg = "Amount of remote packages is not more than zero: %s\n" %(output_repoinfo))
52