commit:     b01cd4208a17a141311d490788aff11537312575
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Mar 12 10:20:56 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Apr 28 00:04:09 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=b01cd420

sync/zipfile: Add testcase for etag

Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 lib/portage/tests/sync/meson.build          |  1 +
 lib/portage/tests/sync/test_sync_zipfile.py | 99 +++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/lib/portage/tests/sync/meson.build 
b/lib/portage/tests/sync/meson.build
index b78583021f..8c566080e3 100644
--- a/lib/portage/tests/sync/meson.build
+++ b/lib/portage/tests/sync/meson.build
@@ -1,6 +1,7 @@
 py.install_sources(
     [
         'test_sync_local.py',
+        'test_sync_zipfile.py',
         '__init__.py',
         '__test__.py',
     ],

diff --git a/lib/portage/tests/sync/test_sync_zipfile.py 
b/lib/portage/tests/sync/test_sync_zipfile.py
new file mode 100644
index 0000000000..4fbde8a351
--- /dev/null
+++ b/lib/portage/tests/sync/test_sync_zipfile.py
@@ -0,0 +1,99 @@
+# Copyright 2024 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+import http.server
+import os
+import shutil
+import socketserver
+import subprocess
+import tempfile
+import textwrap
+import threading
+from functools import partial
+
+import portage
+from portage.tests import TestCase
+from portage.tests.resolver.ResolverPlayground import ResolverPlayground
+
+
+class test_sync_zipfile_case(TestCase):
+    def test_sync_zipfile(self):
+        cpv = "dev-libs/A-0"
+        ebuilds = {
+            cpv: {"EAPI": "8"},
+        }
+        etag = "foo"
+
+        server = None
+        playground = None
+        tmpdir = tempfile.mkdtemp()
+        try:
+
+            class Handler(http.server.SimpleHTTPRequestHandler):
+                def end_headers(self):
+                    self.send_header("etag", etag)
+                    super().end_headers()
+
+            server = socketserver.TCPServer(
+                ("127.0.0.1", 0),
+                partial(Handler, directory=tmpdir),
+            )
+            threading.Thread(target=server.serve_forever, daemon=True).start()
+
+            playground = ResolverPlayground(
+                ebuilds=ebuilds,
+            )
+            settings = playground.settings
+
+            env = settings.environ()
+
+            repos_conf = textwrap.dedent(
+                """
+                [test_repo]
+                location = %(location)s
+                sync-type = zipfile
+                sync-uri = %(sync-uri)s
+                auto-sync = true
+            """
+            )
+
+            repo_location = f"{playground.eprefix}/var/repositories/test_repo"
+
+            env["PORTAGE_REPOSITORIES"] = repos_conf % {
+                "location": repo_location,
+                "sync-uri": 
"http://{}:{}/test_repo.zip".format(*server.server_address),
+            }
+
+            shutil.make_archive(os.path.join(tmpdir, "test_repo"), "zip", 
repo_location)
+
+            ebuild = 
playground.trees[playground.eroot]["porttree"].dbapi.findname(cpv)
+            self.assertTrue(os.path.exists(ebuild))
+            shutil.rmtree(repo_location)
+            self.assertFalse(os.path.exists(ebuild))
+
+            result = subprocess.run(
+                [
+                    "emerge",
+                    "--sync",
+                ],
+                env=env,
+                stdout=subprocess.PIPE,
+                stderr=subprocess.STDOUT,
+            )
+            output = result.stdout.decode(errors="replace")
+            try:
+                self.assertEqual(result.returncode, os.EX_OK)
+            except Exception:
+                print(output)
+                raise
+
+            repo = settings.repositories["test_repo"]
+            sync_mod = portage.sync.module_controller.get_class("zipfile")
+            status, repo_revision = sync_mod().retrieve_head(options={"repo": 
repo})
+            self.assertEqual(status, os.EX_OK)
+            self.assertEqual(repo_revision, etag)
+        finally:
+            if server is not None:
+                server.shutdown()
+            shutil.rmtree(tmpdir)
+            playground.cleanup()

Reply via email to