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