Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/integration/master_slave.py
blob: 9a870e3b9f91f827aa8e993252fa59fa64e85663 (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
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
#!/usr/bin/env python
# sugar-lint: disable

import os
import time
import shutil
import signal
from cStringIO import StringIO
from contextlib import contextmanager
from os.path import exists, join, dirname, abspath

import rrdtool

from __init__ import tests, src_root

from sugar_network.client import Connection, keyfile
from sugar_network.toolkit.rrd import Rrd
from sugar_network.toolkit import coroutine, http


# /tmp might be on tmpfs wich returns 0 bytes for free mem all time
local_tmproot = join(abspath(dirname(__file__)), '.tmp')


class MasterSlaveTest(tests.Test):

    def setUp(self):
        tests.Test.setUp(self, tmp_root=local_tmproot)

        self.touch(('master/db/master', '127.0.0.1:8100'))

        self.master_pid = self.popen([join(src_root, 'sugar-network-node'), '-F', 'start',
            '--port=8100', '--data-root=master/db', '--cachedir=master/tmp',
            '-DDD', '--rundir=master/run', '--files-root=master/files',
            '--stats-root=master/stats', '--stats-user', '--stats-user-step=1',
            '--stats-user-rras=RRA:AVERAGE:0.5:1:100',
            '--index-flush-threshold=1', '--pull-timeout=1',
            '--obs-url=',
            ])
        self.slave_pid = self.popen([join(src_root, 'sugar-network-node'), '-F', 'start',
            '--api-url=http://127.0.0.1:8100',
            '--port=8101', '--data-root=slave/db', '--cachedir=slave/tmp',
            '-DDD', '--rundir=slave/run', '--files-root=slave/files',
            '--stats-root=slave/stats', '--stats-user', '--stats-user-step=1',
            '--stats-user-rras=RRA:AVERAGE:0.5:1:100',
            '--index-flush-threshold=1', '--sync-layers=pilot',
            ])

        coroutine.sleep(3)

    def tearDown(self):
        self.waitpid(self.master_pid, signal.SIGINT)
        self.waitpid(self.slave_pid, signal.SIGINT)
        tests.Test.tearDown(self)

    def test_OnlineSync(self):
        ts = int(time.time())
        master = Connection('http://127.0.0.1:8100', auth=http.SugarAuth(keyfile.value))
        slave = Connection('http://127.0.0.1:8101', auth=http.SugarAuth(keyfile.value))

        # Initial data

        context1 = master.post(['/context'], {
            'type': 'activity',
            'title': 'title1',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview1',
            'layer': 'pilot',
            })
        self.touch(('master/files/file1', 'file1'))

        context2 = slave.post(['/context'], {
            'type': 'activity',
            'title': 'title2',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview2',
            'layer': 'pilot',
            })
        slave.post(['user', tests.UID], {
            'name': 'db',
            'values': [(ts, {'field': 1})],
            }, cmd='stats-upload')

        # 1st sync
        slave.post(cmd='online-sync')

        self.assertEqual('title1', master.get(['context', context1, 'title']))
        self.assertEqual('preview1', master.request('GET', ['context', context1, 'preview']).content)
        self.assertEqual('title2', master.get(['context', context2, 'title']))
        self.assertEqual('preview2', master.request('GET', ['context', context2, 'preview']).content)

        stats = Rrd('master/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', ts, {'field': 1.0})]
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in stats])
        self.assertEqual('file1', file('master/files/file1').read())

        self.assertEqual('title1', slave.get(['context', context1, 'title']))
        self.assertEqual('title2', slave.get(['context', context2, 'title']))
        stats = Rrd('slave/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', ts, {'field': 1.0})]
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in stats])
        self.assertEqual('file1', file('slave/files/file1').read())

        # More data
        coroutine.sleep(1)

        context3 = master.post(['/context'], {
            'type': 'activity',
            'title': 'title3',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview3',
            'layer': 'pilot',
            })
        master.put(['context', context1, 'title'], 'title1_')
        master.put(['context', context2, 'preview'], 'preview2_')
        self.touch(('master/files/file1', 'file1_'))
        self.touch(('master/files/file2', 'file2'))

        context4 = slave.post(['/context'], {
            'type': 'activity',
            'title': 'title4',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview4',
            'layer': 'pilot',
            })
        slave.put(['context', context2, 'title'], 'title2_')
        slave.put(['context', context1, 'preview'], 'preview1_')
        slave.post(['user', tests.UID], {
            'name': 'db',
            'values': [(ts + 1, {'field': 2})],
            }, cmd='stats-upload')

        # 2nd sync
        slave.post(cmd='online-sync')

        self.assertEqual('title1_', master.get(['context', context1, 'title']))
        self.assertEqual('preview1_', master.request('GET', ['context', context1, 'preview']).content)
        self.assertEqual('title2_', master.get(['context', context2, 'title']))
        self.assertEqual('preview2_', master.request('GET', ['context', context2, 'preview']).content)
        self.assertEqual('title3', master.get(['context', context3, 'title']))
        self.assertEqual('preview3', master.request('GET', ['context', context3, 'preview']).content)
        self.assertEqual('title4', master.get(['context', context4, 'title']))
        self.assertEqual('preview3', master.request('GET', ['context', context3, 'preview']).content)
        stats = Rrd('master/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', ts, {'field': 1.0}), ('db', ts + 1, {'field': 2.0})]
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in stats])
        self.assertEqual('file1_', file('master/files/file1').read())
        self.assertEqual('file2', file('master/files/file2').read())

        self.assertEqual('title1_', slave.get(['context', context1, 'title']))
        self.assertEqual('preview1_', slave.request('GET', ['context', context1, 'preview']).content)
        self.assertEqual('title2_', slave.get(['context', context2, 'title']))
        self.assertEqual('preview2_', slave.request('GET', ['context', context2, 'preview']).content)
        self.assertEqual('title3', slave.get(['context', context3, 'title']))
        self.assertEqual('preview3', slave.request('GET', ['context', context3, 'preview']).content)
        self.assertEqual('title4', slave.get(['context', context4, 'title']))
        self.assertEqual('preview4', slave.request('GET', ['context', context4, 'preview']).content)
        stats = Rrd('slave/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', ts, {'field': 1.0}), ('db', ts + 1, {'field': 2.0})]
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in stats])
        self.assertEqual('file1_', file('slave/files/file1').read())
        self.assertEqual('file2', file('slave/files/file2').read())

    def test_OfflineSync(self):
        master = Connection('http://127.0.0.1:8100', auth=http.SugarAuth(keyfile.value))
        slave = Connection('http://127.0.0.1:8101', auth=http.SugarAuth(keyfile.value))

        # Create shared files on master
        self.touch(('master/files/1/1', '1'))
        self.touch(('master/files/2/2', '2'))

        # Create initial data on master
        guid_1 = master.post(['context'], {
            'type': 'activity',
            'title': 'title_1',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview1',
            'layer': 'pilot',
            })
        guid_2 = master.post(['context'], {
            'type': 'activity',
            'title': 'title_2',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview2',
            'layer': 'pilot',
            })

        # Create initial data on slave
        guid_3 = slave.post(['context'], {
            'type': 'activity',
            'title': 'title_3',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview3',
            'layer': 'pilot',
            })
        guid_4 = slave.post(['context'], {
            'type': 'activity',
            'title': 'title_4',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview4',
            'layer': 'pilot',
            })
        stats_timestamp = int(time.time())
        slave.post(['user', tests.UID], {
            'name': 'db',
            'values': [(stats_timestamp, {'f': 1}), (stats_timestamp + 1, {'f': 2})],
            }, cmd='stats-upload')

        # Clone initial dump from master
        pid = self.popen('V=1 %s sync1/sugar-network-sync http://127.0.0.1:8100' % join(src_root, 'sugar-network-sync'), shell=True)
        self.waitpid(pid, 0)
        # Import cloned data on slave
        slave.post(cmd='offline-sync', path=tests.tmpdir + '/sync1/sugar-network-sync')
        # Upload slave initial data to master
        pid = self.popen('V=1 sync1/sugar-network-sync/sugar-network-sync', shell=True)
        self.waitpid(pid, 0)

        # Update data on master
        guid_5 = master.post(['context'], {
            'type': 'activity',
            'title': 'title_5',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview5',
            'layer': 'pilot',
            })
        master.put(['context', guid_1, 'title'], 'title_1_')
        master.put(['context', guid_3, 'preview'], 'preview3_')

        # Update data on slave
        guid_6 = slave.post(['context'], {
            'type': 'activity',
            'title': 'title_6',
            'summary': 'summary',
            'description': 'description',
            'artifact_icon': 'artifact_icon',
            'preview': 'preview6',
            'layer': 'pilot',
            })
        slave.put(['context', guid_3, 'title'], 'title_3_')
        slave.put(['context', guid_1, 'preview'], 'preview1_')

        # Export slave changes
        slave.post(cmd='offline-sync', path=tests.tmpdir + '/sync2/sugar-network-sync')
        # Sync them with master
        pid = self.popen('V=1 sync2/sugar-network-sync/sugar-network-sync', shell=True)
        self.waitpid(pid, 0)
        # Process master's reply
        slave.post(cmd='offline-sync', path=tests.tmpdir + '/sync2/sugar-network-sync')

        self.assertEqual(
                {'total': 6, 'result': [
                    {'guid': guid_1, 'title': 'title_1_'},
                    {'guid': guid_2, 'title': 'title_2'},
                    {'guid': guid_3, 'title': 'title_3_'},
                    {'guid': guid_4, 'title': 'title_4'},
                    {'guid': guid_5, 'title': 'title_5'},
                    {'guid': guid_6, 'title': 'title_6'},
                    ]},
                master.get(['context'], reply=['guid', 'title'], layer='pilot'))
        self.assertEqual(
                {'total': 6, 'result': [
                    {'guid': guid_1, 'title': 'title_1_'},
                    {'guid': guid_2, 'title': 'title_2'},
                    {'guid': guid_3, 'title': 'title_3_'},
                    {'guid': guid_4, 'title': 'title_4'},
                    {'guid': guid_5, 'title': 'title_5'},
                    {'guid': guid_6, 'title': 'title_6'},
                    ]},
                slave.get(['context'], reply=['guid', 'title'], layer='pilot'))

        self.assertEqual('preview1_', master.request('GET', ['context', guid_1, 'preview']).content)
        self.assertEqual('preview2', master.request('GET', ['context', guid_2, 'preview']).content)
        self.assertEqual('preview3_', master.request('GET', ['context', guid_3, 'preview']).content)
        self.assertEqual('preview4', master.request('GET', ['context', guid_4, 'preview']).content)
        self.assertEqual('preview5', master.request('GET', ['context', guid_5, 'preview']).content)
        self.assertEqual('preview6', master.request('GET', ['context', guid_6, 'preview']).content)
        self.assertEqual('preview1_', slave.request('GET', ['context', guid_1, 'preview']).content)
        self.assertEqual('preview2', slave.request('GET', ['context', guid_2, 'preview']).content)
        self.assertEqual('preview3_', slave.request('GET', ['context', guid_3, 'preview']).content)
        self.assertEqual('preview4', slave.request('GET', ['context', guid_4, 'preview']).content)
        self.assertEqual('preview5', slave.request('GET', ['context', guid_5, 'preview']).content)
        self.assertEqual('preview6', slave.request('GET', ['context', guid_6, 'preview']).content)

        self.assertEqual('1', file('master/files/1/1').read())
        self.assertEqual('2', file('master/files/2/2').read())
        self.assertEqual('1', file('slave/files/1/1').read())
        self.assertEqual('2', file('slave/files/2/2').read())

        rrd = Rrd('master/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', stats_timestamp, {'f': 1.0}), ('db', stats_timestamp + 1, {'f': 2.0})],
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in rrd])
        rrd = Rrd('slave/stats/user/%s/%s' % (tests.UID[:2], tests.UID), 1)
        self.assertEqual([
            [('db', stats_timestamp, {'f': 1.0}), ('db', stats_timestamp + 1, {'f': 2.0})],
            ],
            [[(db.name,) + i for i in db.get(db.first, db.last)] for db in rrd])


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