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
|
from testfixtures import log_capture
from tests.base_test import BaseTest
from tests import config
from core.sessions import SessionURL
from core import modules
import utils
from core import messages
import subprocess
import os
def setUpModule():
subprocess.check_output("""
BASE_FOLDER="{config.base_folder}/test_file_zip/"
rm -rf "$BASE_FOLDER"
mkdir -p "$BASE_FOLDER/dir1/dir2/dir3/dir4"
echo -n 1 > "$BASE_FOLDER/dir1/f1"
echo -n 1 > "$BASE_FOLDER/dir1/dir2/f2"
echo -n 1 > "$BASE_FOLDER/dir1/dir2/dir3/f3"
echo -n 1 > "$BASE_FOLDER/dir1/dir2/dir3/dir4/f4"
cd "$BASE_FOLDER" && zip -r "test_0.zip" "dir1/"
echo -n 1 > "$BASE_FOLDER/f5"
rm -rf "$BASE_FOLDER/dir1"
chown www-data: -R "$BASE_FOLDER/"
""".format(
config = config
), shell=True)
class FileZip(BaseTest):
folders_rel = [
'test_file_zip/dir1',
'test_file_zip/dir1/dir2',
'test_file_zip/dir1/dir2/dir3',
'test_file_zip/dir1/dir2/dir3/dir4',
]
folders_abs = [
os.path.join(config.base_folder, f)
for f in folders_rel
]
files_rel = [
'test_file_zip/dir1/f1',
'test_file_zip/dir1/dir2/f2',
'test_file_zip/dir1/dir2/dir3/f3',
'test_file_zip/dir1/dir2/dir3/dir4/f4',
]
files_abs = [
os.path.join(config.base_folder, f)
for f in files_rel
]
zips_rel = [
'test_file_zip/test_0.zip'
]
zips_abs = [
os.path.join(config.base_folder, f)
for f in zips_rel
]
other_file_rel = 'test_file_zip/f5'
other_file_abs = os.path.join(config.base_folder, other_file_rel)
def setUp(self):
self.session = SessionURL(
self.url,
self.password,
volatile = True
)
modules.load_modules(self.session)
self.run_argv = modules.loaded['file_zip'].run_argv
def test_compress_decompress(self):
# Uncompress test.zip
self.assertTrue(self.run_argv(["--decompress", self.zips_rel[0], 'test_file_zip/' ]));
for file in self.files_abs:
self.assertEqual(subprocess.check_output("cat %s" % file, shell=True), b'1')
for folder in self.folders_abs:
subprocess.check_call('stat -c %%a "%s"' % folder, shell=True)
# Compress it again giving szipting folder
self.assertTrue(self.run_argv(['test_file_zip/test_1.zip', self.folders_rel[0]]));
self.zips_rel.append('test_file_zip/test_1.zip')
self.zips_abs.append(os.path.join(config.base_folder, self.zips_rel[-1]))
# Uncompress the new archive and recheck
self.assertTrue(self.run_argv(["--decompress", 'test_file_zip/test_1.zip', 'test_file_zip/']));
for file in self.files_abs:
self.assertEqual(subprocess.check_output("cat %s" % file, shell=True), b'1')
for folder in self.folders_abs:
subprocess.check_call('stat -c %%a "%s"' % folder, shell=True)
def test_compress_multiple(self):
# Uncompress test.zip
self.assertTrue(self.run_argv(["--decompress", self.zips_rel[0], 'test_file_zip/' ]));
for file in self.files_abs:
self.assertEqual(subprocess.check_output("cat %s" % file, shell=True), b'1')
for folder in self.folders_abs:
subprocess.check_call('stat -c %%a "%s"' % folder, shell=True)
# Create a new zip adding also other_file
self.assertTrue(self.run_argv(['test_file_zip/test_2.zip', self.folders_rel[0], self.other_file_rel]));
self.zips_rel.append('test_file_zip/test_2.zip')
self.zips_abs.append(os.path.join(config.base_folder, self.zips_rel[-1]))
# Remove all the files
subprocess.check_output("rm -rf %s" % self.folders_abs[0], shell=True)
subprocess.check_output("rm %s" % self.other_file_abs, shell=True)
# Uncompress the new archive and recheck
self.assertTrue(self.run_argv(["--decompress", 'test_file_zip/test_2.zip', 'test_file_zip/']));
for file in self.files_abs:
self.assertEqual(subprocess.check_output("cat %s" % file, shell=True), b'1')
for folder in self.folders_abs:
subprocess.check_call('stat -c %%a "%s"' % folder, shell=True)
# TODO: here skips the final check since f5 is misplaced
# on the archive
# Archive: /var/www/html/test_file_zip/test_2.zip
# testing: dir1/ OK
# testing: dir1/dir2/ OK
# testing: dir1/dir2/dir3/ OK
# testing: dir1/dir2/dir3/dir4/ OK
# testing: dir1/dir2/dir3/dir4/f4 OK
# testing: dir1/dir2/dir3/f3 OK
# testing: dir1/dir2/f2 OK
# testing: dir1/f1 OK
# testing: test_file_zip/f5 OK
#self.assertEqual(subprocess.check_output("cat %s" % self.other_file_abs, shell=True),'1')
@log_capture()
def test_already_exists(self, log_captured):
# Create a new zip with other_file, with the name test_0.zip
self.assertIsNone(self.run_argv(['test_file_zip/test_0.zip', self.other_file_rel]));
self.assertEqual(log_captured.records[-1].msg,
"File 'test_file_zip/test_0.zip' already exists, skipping compressing")
@log_capture()
def test_unexistant_decompress(self, log_captured):
self.assertIsNone(self.run_argv(["--decompress", 'bogus', '.']));
self.assertEqual(log_captured.records[-1].msg,
"Skipping file 'bogus', check existance and permission")
@log_capture()
def test_unexistant_compress(self, log_captured):
self.assertIsNone(self.run_argv(['bogus.zip', 'bogus']));
self.assertEqual(log_captured.records[-1].msg,
"File 'bogus.zip' not created, check existance and permission")
|