This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
/
test-Disk.py
199 lines (176 loc) · 6.04 KB
/
test-Disk.py
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
#!/usr/bin/env python3
#
# test-Disk.py
#
# Copyright 2017 Sly_tom_cat <stc@stc-nb>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import unittest
from subprocess import call
from re import findall
from Disk import Disk
from time import sleep as _sleep
from os import getenv, chdir, getcwd, remove, makedirs
from os.path import join as path_join, exists as pathExists
from shutil import rmtree
from tempfile import NamedTemporaryFile as tempFile
from Cloud import Cloud
sleep = _sleep
'''
def sleep(timeout):
if getenv('CIRCLE_ENV') == 'test':
_sleep(timeout)
else:
input('>>>>')
'''
class test_Disk(unittest.TestCase):
token = (getenv('API_TOKEN') if getenv('CIRCLE_ENV') == 'test' else
findall(r'API_TOKEN: (.*)', open('OAuth.info', 'rt').read())[0].strip())
disk = None
def setUp(self):
pass
def test_Disk_00_notStarted(self):
test_Disk.disk = Disk({'login': 'stc.yd', 'auth': self.token, 'path': '~/yd', 'start': False,
'ro': False, 'ow': False, 'exclude': ['excluded_folder']})
sleep(2)
self.assertTrue(self.disk.status == 'none')
self.assertEqual(self.disk.exit(), 0)
def test_Disk_01_noAccess(self):
test_Disk.disk = Disk({'login': 'stc.yd', 'auth': self.token, 'path': '/root', 'start': True,
'ro': False, 'ow': False, 'exclude': ['excluded_folder']})
sleep(2)
self.assertTrue(self.disk.status == 'fault')
self.assertEqual(self.disk.exit(), 0)
def test_Disk_10_InitialSync(self):
test_Disk.disk = Disk({'login': 'stc.yd', 'auth': self.token, 'path': '~/yd', 'start': True,
'ro': False, 'ow': False, 'exclude': ['excluded_folder']})
sleep(50)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_20_TestSequence(self):
chdir(self.disk.path)
call(['bash', 'test.sh'])
sleep(30)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_25_FullSync(self):
self.disk.fullSync()
sleep(30)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_30_Trush(self):
self.disk.trash()
sleep(30)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_40_GetStatus(self):
res = self.disk.getStatus()
sleep(1)
self.assertTrue(type(res) == dict)
def test_Disk_45_list(self):
l = 0
for i in self.disk.task('list', 5):
l += 1
self.assertTrue(l > 0)
def test_Disk_50_DownloadNew(self):
self.disk.disconnect()
remove(self.disk.h_data._filePath) # remove history
self.disk.h_data.clear() # reset it in memory
path = path_join(self.disk.path, 'word.docx')
remove(path)
self.disk.connect()
sleep(30)
self.assertTrue(pathExists(path))
self.assertTrue(self.disk.status == 'idle')
def test_Disk_60_Remove(self):
self.disk.disconnect()
rmtree(path_join(self.disk.path, 'd1'))
self.disk.connect()
sleep(30)
stat = self.disk.task('res', 'd1')[0]
self.assertFalse(stat)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_70_UplodNew_offLine(self):
self.disk.disconnect()
path = path_join(self.disk.path, 'd1', 'd2')
makedirs(path)
path = path_join(path, 'file')
with open(path, 'wt') as f:
f.write('test test file file')
self.disk.connect()
sleep(30)
stat = self.disk.task('res', 'd1/d2/file')[0]
self.assertTrue(stat)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_72_UplodUpd_onLine(self):
with open('d1/d2/file', 'wt') as f:
f.write('test file')
sleep(30)
stat = self.disk.task('res', 'd1/d2/file')[0]
self.assertTrue(stat)
self.assertTrue(self.disk.status == 'idle')
def test_Disk_75_UplodChanged_offLine(self):
self.disk.disconnect()
r_path = 'd1/d2/file'
path = path_join(self.disk.path, r_path)
mt = self.disk.h_data.get(path)
self.assertFalse(mt is None)
with open(r_path, 'wt') as f:
f.write('test test')
self.disk.connect()
sleep(30)
self.assertTrue(self.disk.status == 'idle')
stat, res = self.disk.task('res', 'd1/d2/file')
self.assertTrue(stat)
self.assertTrue(res['modified']> mt)
def test_Disk_77_DownloadUpd_offLine(self):
self.disk.disconnect()
r_path = 'd1/d2/file'
path = path_join(self.disk.path, r_path)
mt = self.disk.h_data.get(path)
with tempFile(delete=False, mode='wt') as f:
temp = f.name
f.write('file file')
Cloud.task(self.disk, 'up', r_path, temp)
self.disk.connect()
sleep(30)
self.assertTrue(self.disk.status == 'idle')
self.assertGreater(self.disk.h_data.get(path), mt)
'''
def test_Disk_80_Conflict(self):
self.disk.disconnect()
r_path = 'd1/d2/file'
path = path_join(self.disk.path, r_path)
mt = self.disk.cloud.h_data.get(path)
self.assertFalse(mt is None)
with open(r_path, 'wt') as f:
f.write('TEST TEST')
with tempFile(delete=False) as f:
temp = f.name
f.write('FILE FILE')
self.disk.Cloud.upload(self.disk.cloud, temp, r_path)
self.disk.connect()
sleep(30)
self.assertTrue(self.disk.status == 'idle')
### need more checks
stat, res = self.disk.cloud.getResource('d1/d2/file')
self.assertTrue(stat)
self.assertTrue(res['modified']> mt)
'''
def test_Disk_98_Exit(self):
self.assertEqual(self.disk.exit(), 0)
def test_Disk_99_Сlean(self):
rmtree(self.disk.path, ignore_errors=True)
if __name__ == '__main__':
unittest.main()