Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / contrib / test_swift_smoke.py @ 959

History | View | Annotate | Download (12.8 KB)

1
# test_smoke.py -- Functional tests for the Swift backend.
2
# Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
3
#
4
# Author: Fabien Boucher <fabien.boucher@enovance.com>
5
#
6
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
7
# General Public License as public by the Free Software Foundation; version 2.0
8
# or (at your option) any later version. You can redistribute it and/or
9
# modify it under the terms of either of these two licenses.
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
# You should have received a copy of the licenses; if not, see
18
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
19
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
20
# License, Version 2.0.
21
#
22

    
23
"""Start functional tests
24

25
A Swift installation must be available before
26
starting those tests. The account and authentication method used
27
during this functional tests must be changed in the configuration file
28
passed as environment variable.
29
The container used to create a fake repository is defined
30
in cls.fakerepo and will be deleted after the tests.
31

32
DULWICH_SWIFT_CFG=/tmp/conf.cfg PYTHONPATH=. python -m unittest \
33
    dulwich.tests_swift.test_smoke
34
"""
35

    
36
import os
37
import unittest
38
import tempfile
39
import shutil
40

    
41
import gevent
42
from gevent import monkey
43
monkey.patch_all()
44

    
45
from dulwich import server
46
from dulwich import repo
47
from dulwich import index
48
from dulwich import client
49
from dulwich import objects
50
from dulwich.contrib import swift
51

    
52

    
53
class DulwichServer():
54
    """Start the TCPGitServer with Swift backend
55
    """
56
    def __init__(self, backend, port):
57
        self.port = port
58
        self.backend = backend
59

    
60
    def run(self):
61
        self.server = server.TCPGitServer(self.backend,
62
                                          'localhost',
63
                                          port=self.port)
64
        self.job = gevent.spawn(self.server.serve_forever)
65

    
66
    def stop(self):
67
        self.server.shutdown()
68
        gevent.joinall((self.job,))
69

    
70

    
71
class SwiftSystemBackend(server.Backend):
72

    
73
    def open_repository(self, path):
74
        return swift.SwiftRepo(path, conf=swift.load_conf())
75

    
76

    
77
class SwiftRepoSmokeTest(unittest.TestCase):
78

    
79
    @classmethod
80
    def setUpClass(cls):
81
        cls.backend = SwiftSystemBackend()
82
        cls.port = 9148
83
        cls.server_address = 'localhost'
84
        cls.fakerepo = 'fakerepo'
85
        cls.th_server = DulwichServer(cls.backend, cls.port)
86
        cls.th_server.run()
87
        cls.conf = swift.load_conf()
88

    
89
    @classmethod
90
    def tearDownClass(cls):
91
        cls.th_server.stop()
92

    
93
    def setUp(self):
94
        self.scon = swift.SwiftConnector(self.fakerepo, self.conf)
95
        if self.scon.test_root_exists():
96
            try:
97
                self.scon.del_root()
98
            except swift.SwiftException:
99
                pass
100
        self.temp_d = tempfile.mkdtemp()
101
        if os.path.isdir(self.temp_d):
102
            shutil.rmtree(self.temp_d)
103

    
104
    def tearDown(self):
105
        if self.scon.test_root_exists():
106
            try:
107
                self.scon.del_root()
108
            except swift.SwiftException:
109
                pass
110
        if os.path.isdir(self.temp_d):
111
            shutil.rmtree(self.temp_d)
112

    
113
    def test_init_bare(self):
114
        swift.SwiftRepo.init_bare(self.scon, self.conf)
115
        self.assertTrue(self.scon.test_root_exists())
116
        obj = self.scon.get_container_objects()
117
        filtered = [o for o in obj if o['name'] == 'info/refs'
118
                    or o['name'] == 'objects/pack']
119
        self.assertEqual(len(filtered), 2)
120

    
121
    def test_clone_bare(self):
122
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
123
        swift.SwiftRepo.init_bare(self.scon, self.conf)
124
        tcp_client = client.TCPGitClient(self.server_address,
125
                                         port=self.port)
126
        remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
127
        # The remote repo is empty (no refs retreived)
128
        self.assertEqual(remote_refs, None)
129

    
130
    def test_push_commit(self):
131
        def determine_wants(*args):
132
            return {"refs/heads/master": local_repo.refs["HEAD"]}
133

    
134
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
135
        # Nothing in the staging area
136
        local_repo.do_commit('Test commit', 'fbo@localhost')
137
        sha = local_repo.refs.read_loose_ref('refs/heads/master')
138
        swift.SwiftRepo.init_bare(self.scon, self.conf)
139
        tcp_client = client.TCPGitClient(self.server_address,
140
                                         port=self.port)
141
        tcp_client.send_pack(self.fakerepo,
142
                             determine_wants,
143
                             local_repo.object_store.generate_pack_contents)
144
        swift_repo = swift.SwiftRepo("fakerepo", self.conf)
145
        remote_sha = swift_repo.refs.read_loose_ref('refs/heads/master')
146
        self.assertEqual(sha, remote_sha)
147

    
148
    def test_push_branch(self):
149
        def determine_wants(*args):
150
            return {"refs/heads/mybranch":
151
                    local_repo.refs["refs/heads/mybranch"]}
152

    
153
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
154
        # Nothing in the staging area
155
        local_repo.do_commit('Test commit', 'fbo@localhost',
156
                             ref='refs/heads/mybranch')
157
        sha = local_repo.refs.read_loose_ref('refs/heads/mybranch')
158
        swift.SwiftRepo.init_bare(self.scon, self.conf)
159
        tcp_client = client.TCPGitClient(self.server_address,
160
                                         port=self.port)
161
        tcp_client.send_pack("/fakerepo",
162
                             determine_wants,
163
                             local_repo.object_store.generate_pack_contents)
164
        swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
165
        remote_sha = swift_repo.refs.read_loose_ref('refs/heads/mybranch')
166
        self.assertEqual(sha, remote_sha)
167

    
168
    def test_push_multiple_branch(self):
169
        def determine_wants(*args):
170
            return {"refs/heads/mybranch":
171
                    local_repo.refs["refs/heads/mybranch"],
172
                    "refs/heads/master":
173
                    local_repo.refs["refs/heads/master"],
174
                    "refs/heads/pullr-108":
175
                    local_repo.refs["refs/heads/pullr-108"]}
176

    
177
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
178
        # Nothing in the staging area
179
        local_shas = {}
180
        remote_shas = {}
181
        for branch in ('master', 'mybranch', 'pullr-108'):
182
            local_shas[branch] = local_repo.do_commit(
183
                'Test commit %s' % branch, 'fbo@localhost',
184
                ref='refs/heads/%s' % branch)
185
        swift.SwiftRepo.init_bare(self.scon, self.conf)
186
        tcp_client = client.TCPGitClient(self.server_address,
187
                                         port=self.port)
188
        tcp_client.send_pack(self.fakerepo,
189
                             determine_wants,
190
                             local_repo.object_store.generate_pack_contents)
191
        swift_repo = swift.SwiftRepo("fakerepo", self.conf)
192
        for branch in ('master', 'mybranch', 'pullr-108'):
193
            remote_shas[branch] = swift_repo.refs.read_loose_ref(
194
                'refs/heads/%s' % branch)
195
        self.assertDictEqual(local_shas, remote_shas)
196

    
197
    def test_push_data_branch(self):
198
        def determine_wants(*args):
199
            return {"refs/heads/master": local_repo.refs["HEAD"]}
200
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
201
        os.mkdir(os.path.join(self.temp_d, "dir"))
202
        files = ('testfile', 'testfile2', 'dir/testfile3')
203
        i = 0
204
        for f in files:
205
            file(os.path.join(self.temp_d, f), 'w').write("DATA %s" % i)
206
            i += 1
207
        local_repo.stage(files)
208
        local_repo.do_commit('Test commit', 'fbo@localhost',
209
                             ref='refs/heads/master')
210
        swift.SwiftRepo.init_bare(self.scon, self.conf)
211
        tcp_client = client.TCPGitClient(self.server_address,
212
                                         port=self.port)
213
        tcp_client.send_pack(self.fakerepo,
214
                             determine_wants,
215
                             local_repo.object_store.generate_pack_contents)
216
        swift_repo = swift.SwiftRepo("fakerepo", self.conf)
217
        commit_sha = swift_repo.refs.read_loose_ref('refs/heads/master')
218
        otype, data = swift_repo.object_store.get_raw(commit_sha)
219
        commit = objects.ShaFile.from_raw_string(otype, data)
220
        otype, data = swift_repo.object_store.get_raw(commit._tree)
221
        tree = objects.ShaFile.from_raw_string(otype, data)
222
        objs = tree.items()
223
        objs_ = []
224
        for tree_entry in objs:
225
            objs_.append(swift_repo.object_store.get_raw(tree_entry.sha))
226
        # Blob
227
        self.assertEqual(objs_[1][1], 'DATA 0')
228
        self.assertEqual(objs_[2][1], 'DATA 1')
229
        # Tree
230
        self.assertEqual(objs_[0][0], 2)
231

    
232
    def test_clone_then_push_data(self):
233
        self.test_push_data_branch()
234
        shutil.rmtree(self.temp_d)
235
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
236
        tcp_client = client.TCPGitClient(self.server_address,
237
                                         port=self.port)
238
        remote_refs = tcp_client.fetch(self.fakerepo, local_repo)
239
        files = (os.path.join(self.temp_d, 'testfile'),
240
                 os.path.join(self.temp_d, 'testfile2'))
241
        local_repo["HEAD"] = remote_refs["refs/heads/master"]
242
        indexfile = local_repo.index_path()
243
        tree = local_repo["HEAD"].tree
244
        index.build_index_from_tree(local_repo.path, indexfile,
245
                                    local_repo.object_store, tree)
246
        for f in files:
247
            self.assertEqual(os.path.isfile(f), True)
248

    
249
        def determine_wants(*args):
250
            return {"refs/heads/master": local_repo.refs["HEAD"]}
251
        os.mkdir(os.path.join(self.temp_d, "test"))
252
        files = ('testfile11', 'testfile22', 'test/testfile33')
253
        i = 0
254
        for f in files:
255
            file(os.path.join(self.temp_d, f), 'w').write("DATA %s" % i)
256
            i += 1
257
        local_repo.stage(files)
258
        local_repo.do_commit('Test commit', 'fbo@localhost',
259
                             ref='refs/heads/master')
260
        tcp_client.send_pack("/fakerepo",
261
                             determine_wants,
262
                             local_repo.object_store.generate_pack_contents)
263

    
264
    def test_push_remove_branch(self):
265
        def determine_wants(*args):
266
            return {"refs/heads/pullr-108": objects.ZERO_SHA,
267
                    "refs/heads/master":
268
                    local_repo.refs['refs/heads/master'],
269
                    "refs/heads/mybranch":
270
                    local_repo.refs['refs/heads/mybranch'],
271
                    }
272
        self.test_push_multiple_branch()
273
        local_repo = repo.Repo(self.temp_d)
274
        tcp_client = client.TCPGitClient(self.server_address,
275
                                         port=self.port)
276
        tcp_client.send_pack(self.fakerepo,
277
                             determine_wants,
278
                             local_repo.object_store.generate_pack_contents)
279
        swift_repo = swift.SwiftRepo("fakerepo", self.conf)
280
        self.assertNotIn('refs/heads/pullr-108', swift_repo.refs.allkeys())
281

    
282
    def test_push_annotated_tag(self):
283
        def determine_wants(*args):
284
            return {"refs/heads/master": local_repo.refs["HEAD"],
285
                    "refs/tags/v1.0": local_repo.refs["refs/tags/v1.0"]}
286
        local_repo = repo.Repo.init(self.temp_d, mkdir=True)
287
        # Nothing in the staging area
288
        sha = local_repo.do_commit('Test commit', 'fbo@localhost')
289
        otype, data = local_repo.object_store.get_raw(sha)
290
        commit = objects.ShaFile.from_raw_string(otype, data)
291
        tag = objects.Tag()
292
        tag.tagger = "fbo@localhost"
293
        tag.message = "Annotated tag"
294
        tag.tag_timezone = objects.parse_timezone('-0200')[0]
295
        tag.tag_time = commit.author_time
296
        tag.object = (objects.Commit, commit.id)
297
        tag.name = "v0.1"
298
        local_repo.object_store.add_object(tag)
299
        local_repo.refs['refs/tags/v1.0'] = tag.id
300
        swift.SwiftRepo.init_bare(self.scon, self.conf)
301
        tcp_client = client.TCPGitClient(self.server_address,
302
                                         port=self.port)
303
        tcp_client.send_pack(self.fakerepo,
304
                             determine_wants,
305
                             local_repo.object_store.generate_pack_contents)
306
        swift_repo = swift.SwiftRepo(self.fakerepo, self.conf)
307
        tag_sha = swift_repo.refs.read_loose_ref('refs/tags/v1.0')
308
        otype, data = swift_repo.object_store.get_raw(tag_sha)
309
        rtag = objects.ShaFile.from_raw_string(otype, data)
310
        self.assertEqual(rtag.object[1], commit.id)
311
        self.assertEqual(rtag.id, tag.id)
312

    
313

    
314
if __name__ == '__main__':
315
    unittest.main()