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
|
#!/usr/bin/env python
# sugar-lint: disable
import os
import socket
import shutil
from os.path import exists
from __init__ import tests
import active_document as ad
from active_toolkit import coroutine, sockets
from sugar_network.local import activities_crawler
from sugar_network.local.mountset import Mountset
from sugar_network.local.bus import IPCServer
from sugar_network.resources.user import User
from sugar_network.resources.context import Context
from sugar_network.toolkit import http
from sugar_network import local, Client, ServerError, sugar, node
from sugar_network.resources.volume import Volume
from sugar_network.local.mounts import HomeMount, RemoteMount
class MountsetTest(tests.Test):
def setUp(self):
tests.Test.setUp(self)
def tearDown(self):
tests.Test.tearDown(self)
def mountset(self):
local.mounts_root.value = tests.tmpdir
volume = Volume('local', [User, Context])
mounts = Mountset(volume)
Client._connection = mounts
self.mounted = coroutine.Event()
def events_cb(event):
if event['event'] in ('mount', 'unmount'):
self.mount_events.append((event['event'], event['mountpoint']))
self.mounted.set()
self.mount_events = []
Client.connect(events_cb)
mounts.open()
mounts.opened.wait()
# Let `open()` start processing spawned jobs
coroutine.dispatch()
return mounts
def test_Populate(self):
os.makedirs('1/.sugar-network')
os.makedirs('2/.sugar-network')
self.mountset()
self.mounted.wait()
self.mounted.clear()
self.assertEqual([
('mount', tests.tmpdir + '/1'),
('mount', tests.tmpdir + '/2'),
],
self.mount_events)
Client(tests.tmpdir + '/1').Context(
type='activity',
title='remote',
summary='summary',
description='description').post()
Client(tests.tmpdir + '/2').Context(
type='activity',
title='remote',
summary='summary',
description='description').post()
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/1', 'name': '1', 'private': True},
{'mountpoint': tests.tmpdir + '/2', 'name': '2', 'private': True},
]),
sorted(Client.mounts()))
def test_Mount(self):
self.mountset()
os.makedirs('tmp/1/.sugar-network')
shutil.move('tmp/1', '.')
self.mounted.wait()
self.mounted.clear()
self.assertEqual(
[('mount', tests.tmpdir + '/1')],
self.mount_events)
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/1', 'name': '1', 'private': True},
]),
sorted(Client.mounts()))
Client(tests.tmpdir + '/1').Context(
type='activity',
title='remote',
summary='summary',
description='description').post()
os.makedirs('tmp/2/.sugar-network')
shutil.move('tmp/2', '.')
self.mounted.wait()
self.mounted.clear()
self.assertEqual(
[('mount', tests.tmpdir + '/2')],
self.mount_events[1:])
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/1', 'name': '1', 'private': True},
{'mountpoint': tests.tmpdir + '/2', 'name': '2', 'private': True},
]),
sorted(Client.mounts()))
Client(tests.tmpdir + '/2').Context(
type='activity',
title='remote',
summary='summary',
description='description').post()
def test_Unmount(self):
os.makedirs('1/.sugar-network')
os.makedirs('2/.sugar-network')
self.mountset()
self.mounted.wait()
self.mounted.clear()
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/1', 'name': '1', 'private': True},
{'mountpoint': tests.tmpdir + '/2', 'name': '2', 'private': True},
]),
sorted(Client.mounts()))
del self.mount_events[:]
shutil.rmtree('1')
self.mounted.wait()
self.mounted.clear()
self.assertEqual(
[('unmount', tests.tmpdir + '/1')],
self.mount_events)
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/2', 'name': '2', 'private': True},
]),
sorted(Client.mounts()))
def test_MountNode(self):
local.server_mode.value = True
self.mountset()
self.touch('tmp/mnt/.sugar-network')
self.touch(('tmp/mnt/node', 'node'))
shutil.move('tmp/mnt', '.')
self.mounted.wait()
self.mounted.clear()
client = Client(tests.tmpdir + '/mnt')
self.assertEqual(
sorted([
{'mountpoint': tests.tmpdir + '/mnt', 'name': 'mnt', 'private': False},
]),
sorted(Client.mounts()))
guid = client.Context(
type='activity',
title='title',
summary='summary',
description='description').post()
local.api_url.value = 'http://localhost:%s' % node.port.value
self.assertEqual(
{'guid': guid, 'title': {'en-US': 'title'}},
http.request('GET', ['context', guid], params={'reply': 'guid,title'}))
def test_RootRemount(self):
mountset = self.mountset()
client = Client('/')
mountset['/'] = RemoteMount(mountset.home_volume)
self.assertEqual(['/'], [i['mountpoint'] for i in mountset.mounts()])
assert not mountset['/'].mounted
self.assertEqual(0, client.Context.cursor().total)
self.assertRaises(RuntimeError, client.Context(type='activity', title='', summary='', description='').post)
pid = self.fork(self.restful_server)
coroutine.sleep(1)
self.assertEqual([], self.mount_events)
self.assertRaises(RuntimeError, client.Context(type='activity', title='', summary='', description='').post)
self.mounted.wait()
self.mounted.clear()
assert mountset['/'].mounted
self.assertEqual([('mount', '/')], self.mount_events)
del self.mount_events[:]
guid = client.Context(
type='activity',
title='title',
summary='summary',
description='description',
).post()
self.assertEqual(
[guid],
[i['guid'] for i in client.Context.cursor()])
self.waitpid(pid)
self.mounted.wait()
self.mounted.clear()
assert not mountset['/'].mounted
self.assertEqual([('unmount', '/')], self.mount_events)
del self.mount_events[:]
self.assertEqual(0, client.Context.cursor().total)
self.assertRaises(RuntimeError, client.Context(type='activity', title='', summary='', description='').post)
pid = self.fork(self.restful_server)
coroutine.sleep(1)
self.assertEqual([], self.mount_events)
self.assertRaises(RuntimeError, client.Context(type='activity', title='', summary='', description='').post)
self.mounted.wait()
self.mounted.clear()
assert mountset['/'].mounted
self.assertEqual([('mount', '/')], self.mount_events)
del self.mount_events[:]
if __name__ == '__main__':
tests.main()
|