~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to src/server/integtests/test_aq_cancel.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008-2015 Canonical
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as
 
5
# published by the Free Software Foundation, either version 3 of the
 
6
# License, or (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# For further info, check  http://launchpad.net/filesync-server
 
17
 
 
18
"""Test cancellation of the action queue's commands."""
 
19
 
 
20
import os
 
21
 
 
22
from cStringIO import StringIO
 
23
 
 
24
from twisted.internet import defer, error
 
25
 
 
26
from ubuntuone.storageprotocol import request
 
27
from ubuntuone.storage.server.testing.aq_helpers import (
 
28
    FakeGetContent,
 
29
    NO_CONTENT_HASH,
 
30
    TestContentBase,
 
31
    aShareUUID,
 
32
    anEmptyShareList,
 
33
    anUUID,
 
34
)
 
35
from ubuntuone.storageprotocol.content_hash import content_hash_factory, crc32
 
36
from ubuntuone.syncdaemon.marker import MDMarker as Marker
 
37
 
 
38
 
 
39
class AQCancelTestBase(TestContentBase):
 
40
    """Things common to TestCancel and TestCancelMarker."""
 
41
 
 
42
    def setUp(self):
 
43
        """Set up."""
 
44
        self.connlost_deferred = defer.Deferred()
 
45
        return super(AQCancelTestBase, self).setUp()
 
46
 
 
47
    def tearDown(self):
 
48
        """Tear thins down."""
 
49
        self.eq.push('SYS_NET_DISCONNECTED')
 
50
        self.eq.push('SYS_USER_DISCONNECT')
 
51
        return super(AQCancelTestBase, self).tearDown()
 
52
 
 
53
    def hiccup(self):
 
54
        """Hiccup the network."""
 
55
        self.eq.push('SYS_NET_DISCONNECTED')
 
56
        self.eq.push('SYS_USER_DISCONNECT')
 
57
        self.connlost_deferred.errback(error.ConnectionLost())
 
58
        self.eq.push('SYS_CONNECTION_LOST')
 
59
        self.eq.push('SYS_USER_CONNECT',
 
60
                     access_token=self.access_tokens['jack'])
 
61
        self.eq.push('SYS_NET_CONNECTED')
 
62
        return self.wait_for_nirvana(.1)
 
63
 
 
64
 
 
65
class TestCancel(AQCancelTestBase):
 
66
    """Cancellation of non-marker-related things."""
 
67
 
 
68
    def test_list_shares(self):
 
69
        """Hiccup the network in the middle of a list_shares."""
 
70
        def worker():
 
71
            """Async worker."""
 
72
            self.aq.list_shares()
 
73
            return self.hiccup()
 
74
        d = self.nuke_client_method('query', worker,
 
75
                                    lambda: self.connlost_deferred)
 
76
        self.assertInQ(d, ('AQ_SHARES_LIST', {'shares_list':
 
77
                                              anEmptyShareList}))
 
78
        return d
 
79
 
 
80
    def test_create_share(self):
 
81
        """Hiccup the network in the middle of a create_share."""
 
82
        def worker():
 
83
            """Async worker."""
 
84
            self.aq.create_share(self.root, 'jack', '', 'View', 'marker:x', '')
 
85
            return self.hiccup()
 
86
        d = self.nuke_client_method('create_share', worker,
 
87
                                    lambda: self.connlost_deferred)
 
88
        self.assertInQ(d, ('AQ_CREATE_SHARE_OK',
 
89
                           {'marker': 'marker:x',
 
90
                            'share_id': aShareUUID}))
 
91
        return d
 
92
 
 
93
    def test_answer_share(self):
 
94
        """Hiccup the network in the middle of an answer_share."""
 
95
        def worker():
 
96
            """Asynch worker."""
 
97
            share_id = self.listener.get_id_for_marker('marker:x')
 
98
            self.aq.answer_share(share_id, 'Yes')
 
99
            return self.hiccup()
 
100
        d = self.wait_for_nirvana()
 
101
        d.addCallback(lambda _: self.nuke_client_method(
 
102
                      'accept_share', worker, lambda: self.connlost_deferred))
 
103
 
 
104
        self.aq.create_share(self.root, 'jack', '', 'View', 'marker:x', '')
 
105
        self.assertInQ(d, ('AQ_ANSWER_SHARE_OK',
 
106
                           {'answer': 'Yes', 'share_id': anUUID}))
 
107
        return d
 
108
 
 
109
    def test_unlink(self):
 
110
        """Hiccup the network in the middle of an unlink."""
 
111
        def worker():
 
112
            """Asynch worker."""
 
113
            new_id = self.listener.get_id_for_marker('marker:x')
 
114
            self.aq.unlink('', self.root, new_id, '', False)
 
115
            return self.hiccup()
 
116
        d = self.wait_for_nirvana()
 
117
        d.addCallback(lambda _: self.nuke_client_method(
 
118
                      'unlink', worker, lambda: self.connlost_deferred))
 
119
 
 
120
        parent_path = self.main.fs.get_by_node_id('', self.root).path
 
121
        mdid = self.main.fs.create(os.path.join(parent_path, u"test"), '')
 
122
        self.aq.make_file('', self.root, 'hola', 'marker:x', mdid)
 
123
        self.assertInQ(d, ('AQ_UNLINK_OK', {'share_id': '',
 
124
                                            'node_id': anUUID,
 
125
                                            'parent_id': self.root,
 
126
                                            'was_dir': False,
 
127
                                            'old_path': '',
 
128
                                            'new_generation': 2L}))
 
129
        return d
 
130
 
 
131
    def test_move(self):
 
132
        """Hiccup the network in the middle of a move."""
 
133
        def worker():
 
134
            """Async worker."""
 
135
            new_id = self.listener.get_id_for_marker('marker:x')
 
136
            self.aq.move('', new_id, self.root, self.root,
 
137
                         'chau', 'from', 'to')
 
138
            return self.hiccup()
 
139
        d = self.wait_for_nirvana()
 
140
        d.addCallback(lambda _: self.nuke_client_method(
 
141
                      'move', worker, lambda: self.connlost_deferred))
 
142
        parent_path = self.main.fs.get_by_node_id('', self.root).path
 
143
        mdid = self.main.fs.create(os.path.join(parent_path, u"test"), '')
 
144
        self.aq.make_file('', self.root, 'hola', 'marker:x', mdid)
 
145
 
 
146
        self.assertInQ(d, ('AQ_MOVE_OK', {'share_id': '',
 
147
                                          'node_id': anUUID,
 
148
                                          'new_generation': 2L}))
 
149
        return d
 
150
 
 
151
    def test_make_file(self):
 
152
        """Hiccup the network in the middle of a make_file."""
 
153
        def worker():
 
154
            """Async worker."""
 
155
            parent_path = self.main.fs.get_by_node_id('', self.root).path
 
156
            mdid = self.main.fs.create(os.path.join(parent_path, u"test"), '')
 
157
            self.aq.make_file('', self.root, 'hola', 'marker:x', mdid)
 
158
            return self.hiccup()
 
159
        d = self.nuke_client_method('make_file', worker,
 
160
                                    lambda: self.connlost_deferred)
 
161
        self.assertInQ(d, ('AQ_FILE_NEW_OK', {'new_id': anUUID,
 
162
                                              'marker': 'marker:x',
 
163
                                              'new_generation': 1L,
 
164
                                              'volume_id': request.ROOT}))
 
165
        return d
 
166
 
 
167
    @defer.inlineCallbacks
 
168
    def test_download(self):
 
169
        """Hiccup the network in the middle of a download."""
 
170
        self.patch(self.main.fs, 'get_partial_for_writing',
 
171
                   lambda s, n: StringIO())
 
172
        hash_value, _, _, d = self._mk_file_w_content()
 
173
        mdid, node_id = yield d
 
174
 
 
175
        def worker():
 
176
            """Async worker."""
 
177
            self.aq.download('', node_id, hash_value, mdid)
 
178
            return self.hiccup()
 
179
        fake_gc = FakeGetContent(self.connlost_deferred, '',
 
180
                                 self.root, hash_value)
 
181
        d = self.nuke_client_method('get_content_request', worker,
 
182
                                    lambda: fake_gc)
 
183
        self.assertInQ(d, ('AQ_DOWNLOAD_COMMIT',
 
184
                           {'share_id': '',
 
185
                            'node_id': node_id,
 
186
                            'server_hash': hash_value}))
 
187
        yield d
 
188
 
 
189
    @defer.inlineCallbacks
 
190
    def test_upload(self):
 
191
        """Hiccup the network in the middle of an upload."""
 
192
        data = os.urandom(1000)
 
193
        hash_object = content_hash_factory()
 
194
        hash_object.update(data)
 
195
        hash_value = hash_object.content_hash()
 
196
        crc32_value = crc32(data)
 
197
        size = len(data)
 
198
        self.patch(self.main.fs, 'open_file', lambda mdid: StringIO(data))
 
199
        mdid, node_id = yield self._mkfile('hola')
 
200
 
 
201
        def worker():
 
202
            """Async worker."""
 
203
            self.aq.upload('', node_id, NO_CONTENT_HASH, hash_value,
 
204
                           crc32_value, size, mdid)
 
205
            return self.hiccup()
 
206
        d = self.wait_for_nirvana()
 
207
        d.addCallback(lambda _: self.nuke_client_method(
 
208
            'put_content_request', worker, lambda: self.connlost_deferred))
 
209
 
 
210
        self.assertInQ(d, lambda: ('AQ_UPLOAD_FINISHED',
 
211
                                   {'share_id': '',
 
212
                                    'hash': hash_value,
 
213
                                    'node_id': anUUID,
 
214
                                    'new_generation': 2L}))
 
215
        yield d
 
216
 
 
217
 
 
218
class TestCancelMarker(AQCancelTestBase):
 
219
    """Cancellation of marker-related things."""
 
220
 
 
221
    @defer.inlineCallbacks
 
222
    def setUp(self):
 
223
        """Set things up."""
 
224
        yield super(TestCancelMarker, self).setUp()
 
225
        self.marker = Marker('marker')
 
226
 
 
227
    def test_create_share(self):
 
228
        """Hiccup the network in the middle of a create_share."""
 
229
        def worker():
 
230
            """Async worker."""
 
231
            dir_path = os.path.join(self.main.root_dir, 'testdir')
 
232
            mdid = self.main.fs.create(dir_path, request.ROOT)
 
233
            marker = Marker(mdid)
 
234
            self.aq.make_dir('', self.root, 'hola', marker, mdid)
 
235
            self.aq.create_share(marker, 'jack', '', 'View', 'marker:x', '')
 
236
            return self.hiccup()
 
237
        d = self.nuke_client_method('make_dir', worker)
 
238
        self.assertInQ(d, ('AQ_CREATE_SHARE_OK',
 
239
                           {'marker': 'marker:x',
 
240
                            'share_id': aShareUUID}))
 
241
        return d
 
242
 
 
243
    def test_unlink(self):
 
244
        """Hiccup the network in the middle of an unlink."""
 
245
        def worker():
 
246
            """Async worker."""
 
247
            dir_path = os.path.join(self.main.root_dir, 'testdir')
 
248
            mdid = self.main.fs.create(dir_path, request.ROOT)
 
249
            marker = Marker(mdid)
 
250
            self.aq.make_dir('', self.root, 'hola', marker, mdid)
 
251
            self.aq.unlink('', self.root, marker, 'dir', False)
 
252
            return self.hiccup()
 
253
        d = self.nuke_client_method('make_dir', worker)
 
254
 
 
255
        self.assertInQ(d, ('AQ_UNLINK_OK', {'share_id': '',
 
256
                                            'node_id': anUUID,
 
257
                                            'parent_id': self.root,
 
258
                                            'was_dir': False,
 
259
                                            'old_path': 'dir',
 
260
                                            'new_generation': 2L}))
 
261
        return d
 
262
 
 
263
    def test_move(self):
 
264
        """Hiccup the network in the middle of a move."""
 
265
        def worker():
 
266
            """Async worker."""
 
267
            dir_path = os.path.join(self.main.root_dir, 'testdir')
 
268
            mdid = self.main.fs.create(dir_path, request.ROOT)
 
269
            marker = Marker(mdid)
 
270
            self.aq.make_dir('', self.root, 'hola', marker, mdid)
 
271
            self.aq.move('', marker, self.root, self.root, 'chau', 'dir', 'to')
 
272
            return self.hiccup()
 
273
        d = self.nuke_client_method('make_dir', worker)
 
274
        self.assertInQ(d, ('AQ_MOVE_OK', {'share_id': '',
 
275
                                          'node_id': anUUID,
 
276
                                          'new_generation': 2L}))
 
277
        return d
 
278
 
 
279
    def test_make_file(self):
 
280
        """Hiccup the network in the middle of a make_file."""
 
281
        def worker():
 
282
            """Async worker."""
 
283
            dir_path = os.path.join(self.main.root_dir, 'testdir')
 
284
            mdid = self.main.fs.create(dir_path, request.ROOT)
 
285
            marker = Marker(mdid)
 
286
            self.aq.make_dir('', self.root, 'chau', marker, mdid)
 
287
            file_path = os.path.join(dir_path, 'testfile')
 
288
            mdid = self.main.fs.create(file_path, request.ROOT)
 
289
            self.aq.make_file('', marker, 'hola', 'marker:x', mdid)
 
290
            return self.hiccup()
 
291
 
 
292
        d = self.nuke_client_method('make_dir', worker)
 
293
        self.assertInQ(d, ('AQ_FILE_NEW_OK', {'new_id': anUUID,
 
294
                                              'marker': 'marker:x',
 
295
                                              'new_generation': 2L,
 
296
                                              'volume_id': request.ROOT}))
 
297
        return d