Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/units/node/sync_offline.py
blob: 0353e587b4bf2d16dcf933a469c2502e20cfdfa7 (plain)
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
#!/usr/bin/env python
# sugar-lint: disable

import os
import time
import json
import uuid
from os.path import exists, join

import rrdtool

from __init__ import tests

from sugar_network import db, node, toolkit
from sugar_network.toolkit.rrd import Rrd
from sugar_network.client import api_url
from sugar_network.node import sync, stats_user, files_root
from sugar_network.node.slave import SlaveRoutes
from sugar_network.db import Volume
from sugar_network.toolkit import coroutine


class statvfs(object):

    f_bfree = None
    f_frsize = 1


class SyncOfflineTest(tests.Test):

    def setUp(self):
        tests.Test.setUp(self)
        self.uuid = 0
        self.override(toolkit, 'uuid', self.next_uuid)
        self.override(os, 'statvfs', lambda *args: statvfs())
        statvfs.f_bfree = 999999999
        stats_user.stats_user_step.value = 1
        stats_user.stats_user_rras.value = ['RRA:AVERAGE:0.5:1:100']
        node.sync_layers.value = 'pilot'

    def next_uuid(self):
        self.uuid += 1
        return str(self.uuid)

    def test_FailOnFullDump(self):

        class Document(db.Resource):
            pass

        volume = Volume('node', [Document])
        toolkit.ensure_key('node/key')
        cp = SlaveRoutes('node/key', volume)

        node.sync_layers.value = None
        self.assertRaises(RuntimeError, cp.offline_sync, tests.tmpdir + '/mnt')
        node.sync_layers.value = 'public'
        cp.offline_sync(tests.tmpdir + '/mnt')

    def test_Export(self):

        class Document(db.Resource):
            pass

        volume = Volume('node', [Document])
        toolkit.ensure_key('node/key')
        cp = SlaveRoutes('node/key', volume)
        stats_user.stats_user.value = True

        volume['document'].create({'guid': '1', 'prop': 'value1', 'ctime': 1, 'mtime': 1})
        volume['document'].create({'guid': '2', 'prop': 'value2', 'ctime': 2, 'mtime': 2})
        self.utime('node', 0)

        ts = int(time.time())
        rrd = Rrd('stats/user/dir/user', stats_user.stats_user_step.value, stats_user.stats_user_rras.value)
        rrd['db'].put({'field': 1}, ts)

        cp.offline_sync(tests.tmpdir + '/mnt')
        assert cp._offline_session is None

        self.assertEqual([
            ({'packet': 'diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'filename': '2.sneakernet'}, [
                {'resource': 'document'},
                {'guid': '1', 'diff': {
                    'guid': {'value': '1', 'mtime': 0},
                    'ctime': {'value': 1, 'mtime': 0},
                    'mtime': {'value': 1, 'mtime': 0},
                    }},
                {'guid': '2', 'diff': {
                    'guid': {'value': '2', 'mtime': 0},
                    'ctime': {'value': 2, 'mtime': 0},
                    'mtime': {'value': 2, 'mtime': 0},
                    }},
                {'commit': [[1, 2]]},
                ]),
            ({'packet': 'stats_diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'filename': '2.sneakernet'}, [
                {'db': 'db', 'user': 'user'},
                {'timestamp': ts, 'values': {'field': 1.0}},
                {'commit': {'user': {'db': [[1, ts]]}}},
                ]),
            ({'packet': 'files_pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'sequence': [[1, None]], 'filename': '2.sneakernet'}, []),
            ({'packet': 'pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'sequence': [[1, None]], 'filename': '2.sneakernet', 'layer':  ['pilot']}, []),
            ],
            sorted([(packet.props, [i for i in packet]) for packet in sync.sneakernet_decode('mnt')]))
        assert not exists('node/pull.sequence')
        assert not exists('node/push.sequence')

    def test_ContinuesExport(self):
        payload = ''.join([str(uuid.uuid4()) for i in xrange(5000)])

        class Document(db.Resource):

            @db.indexed_property(slot=1)
            def prop(self, value):
                return value

        volume = Volume('node', [Document])
        toolkit.ensure_key('node/key')
        cp = SlaveRoutes('node/key', volume)
        stats_user.stats_user.value = True

        volume['document'].create({'guid': '1', 'prop': payload, 'ctime': 1, 'mtime': 1})
        volume['document'].create({'guid': '2', 'prop': payload, 'ctime': 2, 'mtime': 2})
        self.utime('node', 0)

        ts = int(time.time())
        rrd = Rrd('stats/user/dir/user', stats_user.stats_user_step.value, stats_user.stats_user_rras.value)
        rrd['db'].put({'field': 1}, ts)

        statvfs.f_bfree = len(payload) * 1.5 + sync._SNEAKERNET_RESERVED_SIZE
        cp.offline_sync(tests.tmpdir + '/1')
        assert cp._offline_session is not None

        self.assertEqual([
            ({'packet': 'diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'filename': '2.sneakernet'}, [
                {'resource': 'document'},
                {'guid': '1', 'diff': {
                    'guid': {'value': '1', 'mtime': 0},
                    'ctime': {'value': 1, 'mtime': 0},
                    'mtime': {'value': 1, 'mtime': 0},
                    'prop': {'value': payload, 'mtime': 0},
                    }},
                {'commit': [[1, 1]]},
                ]),
            ({'packet': 'files_pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'sequence': [[1, None]], 'filename': '2.sneakernet'}, []),
            ({'packet': 'pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'sequence': [[1, None]], 'filename': '2.sneakernet', 'layer': ['pilot']}, []),
            ],
            sorted([(packet.props, [i for i in packet]) for packet in sync.sneakernet_decode('1')]))

        statvfs.f_bfree = 999999999
        cp.offline_sync(tests.tmpdir + '/2')
        assert cp._offline_session is None

        self.assertEqual([
            ({'packet': 'diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'filename': '3.sneakernet'}, [
                {'resource': 'document'},
                {'guid': '2', 'diff': {
                    'guid': {'value': '2', 'mtime': 0},
                    'ctime': {'value': 2, 'mtime': 0},
                    'mtime': {'value': 2, 'mtime': 0},
                    'prop': {'value': payload, 'mtime': 0},
                    }},
                {'commit': [[2, 2]]},
                ]),
            ({'packet': 'stats_diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '1', 'filename': '3.sneakernet'}, [
                {'db': 'db', 'user': 'user'},
                {'timestamp': ts, 'values': {'field': 1.0}},
                {'commit': {'user': {'db': [[1, ts]]}}},
                ]),
            ],
            sorted([(packet.props, [i for i in packet]) for packet in sync.sneakernet_decode('2')]))

        statvfs.f_bfree = 999999999
        cp.offline_sync(tests.tmpdir + '/3')
        assert cp._offline_session is None

        self.assertEqual([
            ({'packet': 'diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '4', 'filename': '5.sneakernet'}, [
                {'resource': 'document'},
                {'guid': '1', 'diff': {
                    'guid': {'value': '1', 'mtime': 0},
                    'ctime': {'value': 1, 'mtime': 0},
                    'mtime': {'value': 1, 'mtime': 0},
                    'prop': {'value': payload, 'mtime': 0},
                    }},
                {'guid': '2', 'diff': {
                    'guid': {'value': '2', 'mtime': 0},
                    'ctime': {'value': 2, 'mtime': 0},
                    'mtime': {'value': 2, 'mtime': 0},
                    'prop': {'value': payload, 'mtime': 0},
                    }},
                {'commit': [[1, 2]]},
                ]),
            ({'packet': 'stats_diff', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '4', 'filename': '5.sneakernet'}, [
                {'db': 'db', 'user': 'user'},
                {'timestamp': ts, 'values': {'field': 1.0}},
                {'commit': {'user': {'db': [[1, ts]]}}},
                ]),
            ({'packet': 'files_pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '4', 'sequence': [[1, None]], 'filename': '5.sneakernet'}, []),
            ({'packet': 'pull', 'src': cp.guid, 'dst': '127.0.0.1:8888', 'api_url': 'http://127.0.0.1:8888', 'session': '4', 'sequence': [[1, None]], 'filename': '5.sneakernet', 'layer': ['pilot']}, []),
            ],
            sorted([(packet.props, [i for i in packet]) for packet in sync.sneakernet_decode('3')]))

    def test_Import(self):
        class Document(db.Resource):
            pass

        volume = Volume('node', [Document])
        toolkit.ensure_key('node/key')
        cp = SlaveRoutes('node/key', volume)
        stats_user.stats_user.value = True
        files_root.value = 'files'

        ts = int(time.time())
        sync.sneakernet_encode([
            ('diff', {'src': '127.0.0.1:8888'}, [
                {'resource': 'document'},
                {'guid': '1', 'diff': {
                    'guid': {'value': '1', 'mtime': 0},
                    'ctime': {'value': 1, 'mtime': 0},
                    'mtime': {'value': 1, 'mtime': 0},
                    }},
                {'guid': '2', 'diff': {
                    'guid': {'value': '2', 'mtime': 0},
                    'ctime': {'value': 2, 'mtime': 0},
                    'mtime': {'value': 2, 'mtime': 0},
                    }},
                {'commit': [[1, 2]]},
                ]),
            ('files_diff', {'src': '127.0.0.1:8888'}, [
                {'op': 'update', 'blob_size': 1, 'blob': ['a'], 'path': '1'},
                {'op': 'update', 'blob_size': 2, 'blob': ['bb'], 'path': '2'},
                {'op': 'commit', 'sequence': [[1, 2]]},
                ]),
            ('ack', {'ack': [[101, 103]], 'sequence': [[1, 3]], 'src': '127.0.0.1:8888', 'dst': cp.guid}, []),
            ('stats_ack', {'sequence': {'user': {'db': [[1, ts]]}}, 'src': '127.0.0.1:8888', 'dst': cp.guid}, []),
            ],
            root='mnt')

        cp.offline_sync(tests.tmpdir + '/mnt')
        assert cp._offline_session is None

        self.assertEqual(
                ['1', '2'],
                [i.guid for i in volume['document'].find()[0]])
        self.assertEqual('a', file('files/1').read())
        self.assertEqual('bb', file('files/2').read())
        self.assertEqual([[4, None]], json.load(file('node/push.sequence')))
        self.assertEqual([[3, 100], [104, None]], json.load(file('node/pull.sequence')))
        self.assertEqual([[3, None]], json.load(file('node/files.sequence')))


if __name__ == '__main__':
    tests.main()