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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
"""
Base class for testing syncing algorithm
(c) 2013-2014 by Mega Limited, Wellsford, New Zealand
This file is part of the MEGA SDK - Client Access Engine.
Applications using the MEGA API must present a valid application key
and comply with the the rules set forth in the Terms of Service.
The MEGA SDK 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.
@copyright Simplified (2-clause) BSD License.
You should have received a copy of the license along with this
program.
"""
# TODO tests:
# * "pause" sync
# * lock directory
# * large (> 4Gb) files
# * > 10000 folders to synchronize
from sync_test_base import SyncTestBase
from sync_test_base import get_random_str
from sync_test_base import generate_unicode_name
import random
import os
import logging
import time
import math
class SyncTest(SyncTestBase):
"""
Class with MEGA SDK test methods
"""
# tests
def test_create_delete_files(self):
"""
create files with different size,
compare files on both folders,
remove files, check that files removed from the second folder
"""
logging.info("Launching test_create_delete_files test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create files
l_files = self.files_create()
self.assertIsNotNone(l_files, "Creating files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.files_remove(l_files), "Removing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_create_rename_delete_files(self):
"""
create files with different size,
compare files on both folders,
rename files
"""
logging.info("Launching test_create_rename_delete_files test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create files
l_files = self.files_create()
self.assertIsNotNone(l_files, "Creating files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# renaming
self.assertTrue(self.files_rename(l_files), "Renaming files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.files_remove(l_files), "Removing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_create_move_delete_files(self):
"""
create files with different size,
move and delete files
"""
logging.info("Launching test_create_move_delete_files test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create files
l_files = self.files_create()
self.assertIsNotNone(l_files, "Creating files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# move & delete
self.assertTrue(self.files_moveanddelete(l_files,"subfolder"), "Move&Delete files")
#~ self.assertTrue(self.files_moveanddelete(l_files,"."), "Move&Delete files") #this is expected to fail as of 20171019
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_mimic_update_with_backup_files(self):
"""
create files with different size,
mimic_update_with_backup
"""
logging.info("Launching test_mimic_update_with_backup_files test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create files
l_files = self.files_create()
self.assertIsNotNone(l_files, "Creating files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# mimic update with backup
self.assertTrue(self.files_mimic_update_with_backup(l_files), "Mimic update with backup files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# remove files
self.assertTrue(self.files_remove(l_files), "Removing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_create_delete_dirs(self):
"""
create directories with different amount of files,
compare directories on both sync folders,
remove directories, check that directories removed from the second folder
"""
logging.info("Launching test_create_delete_dirs test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create dirs
l_dirs = self.dirs_create()
self.assertIsNotNone(l_dirs, "Creating directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.dirs_check(l_dirs), "Comparing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.dirs_remove(l_dirs), "Removing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_create_rename_delete_dirs(self):
"""
create directories with different amount of files,
compare directories on both sync folders,
rename directories
compare directories on both sync folders,
remove directories, check that directories removed from the second folder
"""
logging.info("Launching test_create_rename_delete_dirs test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create dirs
l_dirs = self.dirs_create()
self.assertIsNotNone(l_dirs, "Creating directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.dirs_check(l_dirs), "Comparing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# rename dirs
self.assertTrue(self.dirs_rename(l_dirs), "Rename directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.dirs_check(l_dirs), "Comparing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.dirs_remove(l_dirs), "Removing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
def test_sync_files_write(self):
"""
write data to a file located in both sync folders
check for the result, expected result: both files contains the same content
"""
logging.info("Launching test_sync_files_write test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
for _ in range(0, self.nr_files):
self.assertTrue(self.app.is_alive(), "Test application is not running")
strlen = random.randint(10, 20)
fname = get_random_str(size=strlen)
fname_in = os.path.join(self.app.local_folder_in, fname)
fname_out = os.path.join(self.app.local_folder_out, fname)
logging.debug("Writing to both files: %s and %s" % (fname_in, fname_out))
with open(fname_in, 'a'):
os.utime(fname_in, None)
with open(fname_out, 'a'):
os.utime(fname_out, None)
#self.app.sync()
for _ in range(self.nr_changes):
with open(fname_in, 'a') as f_in:
f_in.write(get_random_str(100))
with open(fname_out, 'a') as f_out:
f_out.write(get_random_str(100))
for r in range(self.app.nr_retries):
self.app.attempt=r
md5_in = "INPUT FILE NOT READABLE";
md5_out = "OUTPUT FILE NOT READABLE";
try:
md5_in = self.md5_for_file(fname_in)
md5_out = self.md5_for_file(fname_out)
except IOError:
pass;
if md5_in == md5_out:
break
self.app.sync()
logging.debug("File %s md5: %s" % (fname_in, md5_in))
logging.debug("File %s md5: %s" % (fname_out, md5_out))
self.assertEqual(md5_in, md5_out, "Files do not match")
def test_local_operations(self):
"""
write data to a file located in both sync folders
check for the result, expected result: both files contains the same content
"""
logging.info("Launching test_local_operations test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
l_tree = self.local_tree_create("", self.nr_dirs)
self.assertIsNotNone(l_tree, "Failed to create directory tree!")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
self.assertTrue(self.local_tree_compare(l_tree), "Failed to compare directory trees!")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.assertTrue(self.local_tree_create_and_move(l_tree), "Failed to create a new sub folder and move an existing directory into it!")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.assertTrue(self.local_tree_multiple_renames(l_tree), "Failed to rename folder multiple times and then rename back to the original name!")
self.assertTrue(self.app.is_alive(), "Test application is not running")
def test_update_mtime(self):
"""
update mtime of a file in both local folders
"""
logging.info("Launching test_update_mtime test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
in_file = os.path.join(self.app.local_folder_in, "mtime_test")
out_file = os.path.join(self.app.local_folder_out, "mtime_test")
for _ in range(self.nr_time_changes):
logging.debug("Touching: %s" % in_file)
now = math.floor(time.time()) #floor to get seconds
with open(in_file, 'a'):
os.utime(in_file, (now, now))
# with open(out_file, 'a'):
# os.utime(in_file, (now, now))
atime=0
mtime=0
for r in range(self.app.nr_retries):
self.app.attempt=r
try:
mtime = os.path.getmtime(out_file)
except OSError:
pass
try:
atime = os.path.getatime(out_file)
except OSError:
pass
logging.debug("Comparing time: %s. atime: %d = %d, mtime: %d = %d" % (out_file, now, atime, now, mtime))
if (mtime==now): #all good
break;
self.app.sync()
logging.debug("Comparing time for %s failed! Retrying [%d/%d] .." % (out_file, r + 1, self.nr_retries))
#self.assertEqual(atime, now, "atime values are different")
self.assertEqual(mtime, now, "mtime values are different")
self.assertTrue(self.app.is_alive(), "Test application is not running")
def test_create_rename_delete_unicode_files_dirs(self):
"""
create directories with different amount of files,
using Unicode encoding for files / directories names,
compare directories on both sync folders,
rename directories
compare directories on both sync folders,
remove directories, check that directories removed from the second folder
"""
logging.info("Launching test_create_rename_delete_unicode_files_dirs test")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.change_folders();
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create files
l_files = self.files_create(generate_unicode_name)
self.assertIsNotNone(l_files, "Creating files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# renaming
self.assertTrue(self.files_rename(l_files, generate_unicode_name), "Renaming files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.files_check(l_files), "Comparing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.files_remove(l_files), "Removing files")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty #TODO: why is this twice?
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# create dirs
l_dirs = self.dirs_create(generate_unicode_name)
self.assertIsNotNone(l_dirs, "Creating directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.dirs_check(l_dirs), "Comparing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# rename dirs
self.assertTrue(self.dirs_rename(l_dirs, generate_unicode_name), "Rename directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
self.app.sync()
# comparing
self.assertTrue(self.dirs_check(l_dirs), "Comparing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# remove files
self.assertTrue(self.dirs_remove(l_dirs), "Removing directories")
self.assertTrue(self.app.is_alive(), "Test application is not running")
# make sure remote folders are empty
self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty")
self.assertTrue(self.app.is_alive(), "Test application is not running")
return True
|