File: test_dumb.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (335 lines) | stat: -rw-r--r-- 11,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# test_dumb.py -- Compatibility tests for dumb HTTP git repositories
# Copyright (C) 2025 Dulwich contributors
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Compatibility tests for dumb HTTP git repositories."""

import io
import os
import sys
import tempfile
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler
from unittest import skipUnless

from dulwich.client import HttpGitClient
from dulwich.porcelain import clone
from dulwich.repo import Repo
from tests.compat.utils import (
    CompatTestCase,
    rmtree_ro,
    run_git_or_fail,
)


def no_op_progress(msg):
    """Progress callback that does nothing."""


class DumbHTTPRequestHandler(SimpleHTTPRequestHandler):
    """HTTP request handler for dumb git protocol."""

    def __init__(self, *args, directory=None, **kwargs):
        self.directory = directory
        super().__init__(*args, directory=directory, **kwargs)

    def log_message(self, format, *args):
        # Suppress logging during tests
        pass


class DumbHTTPGitServer:
    """Simple HTTP server for serving git repositories."""

    def __init__(self, root_path, port=0):
        self.root_path = root_path

        def handler(*args, **kwargs):
            return DumbHTTPRequestHandler(*args, directory=root_path, **kwargs)

        self.server = HTTPServer(("127.0.0.1", port), handler)
        self.server.allow_reuse_address = True
        self.port = self.server.server_port
        self.thread = None

    def start(self):
        """Start the HTTP server in a background thread."""
        self.thread = threading.Thread(target=self.server.serve_forever)
        self.thread.daemon = True
        self.thread.start()

        # Give the server a moment to start and verify it's listening
        import socket
        import time

        for i in range(50):  # Try for up to 5 seconds
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(0.1)
                result = sock.connect_ex(("127.0.0.1", self.port))
                sock.close()
                if result == 0:
                    return  # Server is ready
            except OSError:
                pass
            time.sleep(0.1)

        # If we get here, server failed to start
        raise RuntimeError(f"HTTP server failed to start on port {self.port}")

    def stop(self):
        """Stop the HTTP server."""
        self.server.shutdown()
        self.server.server_close()
        if self.thread:
            self.thread.join()

    @property
    def url(self):
        """Get the base URL for this server."""
        return f"http://127.0.0.1:{self.port}"


class DumbHTTPClientNoPackTests(CompatTestCase):
    """Tests for dumb HTTP client against real git repositories."""

    with_pack = False
    with_missing_remote_head = False

    def setUp(self):
        super().setUp()
        # Create a temporary directory for test repos
        self.temp_dir = tempfile.mkdtemp()
        self.addCleanup(rmtree_ro, self.temp_dir)

        # Create origin repository
        self.origin_path = os.path.join(self.temp_dir, "origin.git")
        os.mkdir(self.origin_path)
        run_git_or_fail(["init", "--bare"], cwd=self.origin_path)

        # Create a working repository to push from
        self.work_path = os.path.join(self.temp_dir, "work")
        os.mkdir(self.work_path)
        run_git_or_fail(["init"], cwd=self.work_path)
        run_git_or_fail(
            ["config", "user.email", "test@example.com"], cwd=self.work_path
        )
        run_git_or_fail(["config", "user.name", "Test User"], cwd=self.work_path)

        nb_files = 10
        if self.with_pack:
            # adding more files will create a pack file in the repository
            nb_files = 50

        for i in range(nb_files):
            test_file = os.path.join(self.work_path, f"test{i}.txt")
            with open(test_file, "w") as f:
                f.write(f"Hello, world {i}!\n")
            run_git_or_fail(["add", f"test{i}.txt"], cwd=self.work_path)
            run_git_or_fail(["commit", "-m", f"Commit {i}"], cwd=self.work_path)

        # Push to origin
        run_git_or_fail(
            ["remote", "add", "origin", self.origin_path], cwd=self.work_path
        )
        run_git_or_fail(["push", "origin", "master"], cwd=self.work_path)

        # Update server info for dumb HTTP
        run_git_or_fail(["update-server-info"], cwd=self.origin_path)

        if self.with_missing_remote_head:
            os.remove(os.path.join(self.origin_path, "HEAD"))

        # Start HTTP server
        self.server = DumbHTTPGitServer(self.origin_path)
        self.server.start()
        self.addCleanup(self.server.stop)

        pack_dir = os.path.join(self.origin_path, "objects", "pack")
        if self.with_pack:
            assert os.listdir(pack_dir)
        else:
            assert not os.listdir(pack_dir)

    @skipUnless(
        sys.platform != "win32", "git clone from Python HTTPServer fails on Windows"
    )
    def test_clone_dumb(self):
        dest_path = os.path.join(self.temp_dir, "cloned")
        # Use a dummy errstream to suppress progress output
        repo = clone(self.server.url, dest_path, errstream=io.BytesIO())
        self.addCleanup(repo.close)
        assert b"HEAD" in repo

    def test_clone_from_dumb_http(self):
        """Test cloning from a dumb HTTP server."""
        dest_path = os.path.join(self.temp_dir, "cloned")

        # Use dulwich to clone via dumb HTTP
        client = HttpGitClient(self.server.url)

        # Create destination repo
        dest_repo = Repo.init(dest_path, mkdir=True)

        try:
            # Fetch from dumb HTTP
            def determine_wants(refs, depth=None):
                return [
                    sha for ref, sha in refs.items() if ref.startswith(b"refs/heads/")
                ]

            result = client.fetch(
                "/", dest_repo, determine_wants=determine_wants, progress=no_op_progress
            )

            # Update refs
            for ref, sha in result.refs.items():
                if ref.startswith(b"refs/heads/"):
                    dest_repo.refs[ref] = sha

            # Checkout files
            dest_repo.get_worktree().reset_index()

            # Verify the clone
            test_file = os.path.join(dest_path, "test0.txt")
            self.assertTrue(os.path.exists(test_file))
            with open(test_file) as f:
                self.assertEqual("Hello, world 0!\n", f.read())
        finally:
            # Ensure repo is closed before cleanup
            dest_repo.close()

    @skipUnless(
        sys.platform != "win32", "git clone from Python HTTPServer fails on Windows"
    )
    def test_fetch_new_commit_from_dumb_http(self):
        """Test fetching new commits from a dumb HTTP server."""
        # First clone the repository
        dest_path = os.path.join(self.temp_dir, "cloned")

        run_git_or_fail(["clone", self.server.url, dest_path])

        # Make a new commit in the origin
        test_file2 = os.path.join(self.work_path, "test2.txt")
        with open(test_file2, "w") as f:
            f.write("Second file\n")
        run_git_or_fail(["add", "test2.txt"], cwd=self.work_path)
        run_git_or_fail(["commit", "-m", "Second commit"], cwd=self.work_path)
        run_git_or_fail(["push", "origin", "master"], cwd=self.work_path)

        # Update server info again
        run_git_or_fail(["update-server-info"], cwd=self.origin_path)

        # Fetch with dulwich client
        client = HttpGitClient(self.server.url)
        dest_repo = Repo(dest_path)

        try:
            old_refs = dest_repo.get_refs()

            def determine_wants(refs, depth=None):
                wants = []
                for ref, sha in refs.items():
                    if ref.startswith(b"refs/heads/") and sha != old_refs.get(ref):
                        wants.append(sha)
                return wants

            result = client.fetch(
                "/", dest_repo, determine_wants=determine_wants, progress=no_op_progress
            )

            # Update refs
            for ref, sha in result.refs.items():
                if ref.startswith(b"refs/heads/"):
                    dest_repo.refs[ref] = sha

            # Reset to new commit
            dest_repo.get_worktree().reset_index()

            # Verify the new file exists
            test_file2_dest = os.path.join(dest_path, "test2.txt")
            self.assertTrue(os.path.exists(test_file2_dest))
            with open(test_file2_dest) as f:
                self.assertEqual("Second file\n", f.read())
        finally:
            # Ensure repo is closed before cleanup
            dest_repo.close()

    @skipUnless(
        os.name == "posix", "Skipping on non-POSIX systems due to permission handling"
    )
    def test_fetch_from_dumb_http_with_tags(self):
        """Test fetching tags from a dumb HTTP server."""
        # Create a tag in origin
        run_git_or_fail(["tag", "-a", "v1.0", "-m", "Version 1.0"], cwd=self.work_path)
        run_git_or_fail(["push", "origin", "v1.0"], cwd=self.work_path)

        # Update server info
        run_git_or_fail(["update-server-info"], cwd=self.origin_path)

        # Clone with dulwich
        dest_path = os.path.join(self.temp_dir, "cloned_with_tags")
        dest_repo = Repo.init(dest_path, mkdir=True)

        try:
            client = HttpGitClient(self.server.url)

            def determine_wants(refs, depth=None):
                return [
                    sha
                    for ref, sha in refs.items()
                    if ref.startswith((b"refs/heads/", b"refs/tags/"))
                ]

            result = client.fetch(
                "/", dest_repo, determine_wants=determine_wants, progress=no_op_progress
            )

            # Update refs
            for ref, sha in result.refs.items():
                dest_repo.refs[ref] = sha

            # Check that the tag exists
            self.assertIn(b"refs/tags/v1.0", dest_repo.refs)

            # Verify tag points to the right commit
            tag_sha = dest_repo.refs[b"refs/tags/v1.0"]
            tag_obj = dest_repo[tag_sha]
            self.assertEqual(b"tag", tag_obj.type_name)
        finally:
            # Ensure repo is closed before cleanup
            dest_repo.close()


class DumbHTTPClientWithPackTests(DumbHTTPClientNoPackTests):
    with_pack = True


class DumbHTTPClientWithMissingRemoteHEAD(DumbHTTPClientNoPackTests):
    with_missing_remote_head = True

    # we only want to test clone operation as removing the HEAD file
    # prevents any push operation used in tests below

    def test_fetch_from_dumb_http_with_tags(self):
        pass

    def test_fetch_new_commit_from_dumb_http(self):
        pass