-
Notifications
You must be signed in to change notification settings - Fork 314
/
test_file_blobstore.py
executable file
·164 lines (127 loc) · 5.16 KB
/
test_file_blobstore.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
import pytest
import os
import errno
from subprocess import call
from wal_e.storage import StorageLayout
from wal_e import exception
from wal_e.operator.file_operator import FileBackup
from wal_e.blobstore.file import uri_put_file
from wal_e.blobstore.file import uri_get_file
from wal_e.blobstore.file import do_lzop_get
from wal_e.blobstore.file import write_and_return_error
def create_files(files):
"""Helper function to create a test directory structure.
File path is used as file contents"""
for f in files:
dir_path = os.path.dirname(f)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
fp = open(f, "wb")
fp.write(f.encode("utf-8"))
fp.close()
def test_valid_prefix():
store = StorageLayout("file://localhost/tmp")
assert store.is_file is True
def test_invalid_prefix():
with pytest.raises(exception.UserException):
StorageLayout("notfile://localhost/tmp")
def test_uri_put_file_writes_key_file(tmpdir):
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
with open(base + "/src.txt", "rb") as f:
uri_put_file("", "file://localhost/" + base + "/dst.txt", f)
with open(base + "/dst.txt", "rb") as dst_file:
assert dst_file.read() == file_list[0].encode('utf-8')
def test_uri_put_file_creates_key_dir(tmpdir):
"""Verify file upload"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
with open(file_list[0], "rb") as f:
uri_put_file("", "file://localhost/" + base + "/subdir/dst.txt", f)
with open(base + "/subdir//dst.txt", "rb") as dst_file:
assert dst_file.read() == file_list[0].encode('utf-8')
def test_uri_get_file(tmpdir):
"""Verify file download"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
file_contents = uri_get_file("", "file://localhost/" + base + "/src.txt")
assert file_contents == file_list[0].encode('utf-8')
def test_bucket_list(tmpdir):
"""Verify bucket keys can be listed"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/subdirfile.txt",
base + "/subdir/file.txt"]
create_files(file_list)
store = StorageLayout("file://localhost/" + base)
backup = FileBackup(store, "", "")
conn = backup.cinfo.connect("")
bucket = conn.get_bucket("")
result = bucket.list(base)
assert len(result) == len(file_list)
for f in file_list:
matches = [x for x in result if x.path == f]
assert len(matches) == 1
assert hasattr(matches[0], 'size') is True
assert hasattr(matches[0], 'last_modified') is True
def test_delete_keys(tmpdir):
"""Verify keys are deleted and bucket is trimmed"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/subdir1/file.txt",
base + "/subdir2/file.txt"]
create_files(file_list)
store = StorageLayout("file://localhost/" + base)
backup = FileBackup(store, "", "")
conn = backup.cinfo.connect("")
bucket = conn.get_bucket("")
bucket.delete_keys(file_list)
assert len(os.listdir(base)) == 0
def test_do_lzop_get(tmpdir):
"""Create a dummy lzo file and confirm it is download/decompression"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
call(["lzop", base + "/src.txt"])
do_lzop_get("", "file://localhost/" + base + "/src.txt.lzo",
base + "/dst.txt", False, do_retry=True)
with open(base + "/dst.txt", "rb") as dst_file:
assert dst_file.read() == file_list[0].encode('utf-8')
def test_do_lzop_get_missing_key(tmpdir):
"""Verify exception is raised for missing key"""
base = str(tmpdir.mkdir("base"))
with pytest.raises(IOError) as e:
do_lzop_get("", "file://localhost/" + base + "/src.txt.lzo",
base + "/dst.txt", False, do_retry=True)
assert e.value.errno == errno.ENOENT
def test_write_and_return_error(tmpdir):
"""Verify None as result in normal operation"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
store = StorageLayout("file://localhost/" + base)
backup = FileBackup(store, "", "")
conn = backup.cinfo.connect("")
bucket = conn.get_bucket("")
f = open(base + "/dst.txt", "wb")
key = bucket.get_key(base + "/src.txt")
result = write_and_return_error(key, f)
assert result is None
with open(base + "/dst.txt", "rb") as dst_file:
assert dst_file.read() == file_list[0].encode('utf-8')
def test_write_and_return_error_with_error(tmpdir):
"""Verify exception as result in error operation"""
base = str(tmpdir.mkdir("base"))
file_list = [base + "/src.txt"]
create_files(file_list)
store = StorageLayout("file://localhost/" + base)
backup = FileBackup(store, "", "")
conn = backup.cinfo.connect("")
bucket = conn.get_bucket("")
f = open(base + "/dst.txt", "wb")
key = bucket.get_key(base + "/missing.txt")
with pytest.raises(IOError) as e:
result = write_and_return_error(key, f)
raise result
assert e.value.errno == errno.ENOENT