| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# coding=utf-8 |
| 3 |
# -------------------------------------------------------------------------- |
| 4 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 |
# Licensed under the MIT License. See License.txt in the project root for |
| 6 |
# license information. |
| 7 |
# -------------------------------------------------------------------------- |
| 8 |
|
| 9 |
import io |
| 10 |
import sys |
| 11 |
|
| 12 |
import pytest |
| 13 |
import datetime |
| 14 |
from azure.datalake.store import utils |
| 15 |
from azure.datalake.store.exceptions import PermissionError, FileNotFoundError |
| 16 |
from tests.testing import azure, second_azure, azure_teardown, my_vcr, posix, tmpfile, working_dir, create_files |
| 17 |
from tests.settings import AZURE_ACL_TEST_APPID |
| 18 |
|
| 19 |
test_dir = working_dir() |
| 20 |
symlinktestsdisabled = "symlink" not in str(test_dir).lower() |
| 21 |
|
| 22 |
a = posix(test_dir / 'a') |
| 23 |
b = posix(test_dir / 'b') |
| 24 |
c = posix(test_dir / 'c') |
| 25 |
d = posix(test_dir / 'd') |
| 26 |
specialCharFile = posix(test_dir / '12,+3#456?.txt') |
| 27 |
|
| 28 |
|
| 29 |
@my_vcr.use_cassette |
| 30 |
def test_simple(azure): |
| 31 |
with azure_teardown(azure): |
| 32 |
data = b'a' * (2**16) |
| 33 |
|
| 34 |
with azure.open(a, 'wb') as f: |
| 35 |
assert f.blocksize == 4*2**20 |
| 36 |
l = f.write(data) |
| 37 |
assert l == len(data) |
| 38 |
|
| 39 |
with azure.open(a, 'rb') as f: |
| 40 |
out = f.read(len(data)) |
| 41 |
assert len(data) == len(out) |
| 42 |
assert out == data |
| 43 |
|
| 44 |
|
| 45 |
@my_vcr.use_cassette |
| 46 |
def test_idempotent_connect(azure): |
| 47 |
azure.connect() |
| 48 |
azure.connect() |
| 49 |
|
| 50 |
|
| 51 |
@my_vcr.use_cassette |
| 52 |
def test_ls_touch(azure): |
| 53 |
with azure_teardown(azure): |
| 54 |
assert not azure.ls(test_dir, invalidate_cache=False) |
| 55 |
azure.touch(a) |
| 56 |
azure.touch(b) |
| 57 |
L = azure.ls(test_dir, True, invalidate_cache=False) |
| 58 |
assert set(d['name'] for d in L) == set([a, b]) |
| 59 |
L = azure.ls(test_dir, False, invalidate_cache=False) |
| 60 |
assert set(L) == set([a, b]) |
| 61 |
|
| 62 |
@my_vcr.use_cassette |
| 63 |
def test_ls_empty_with_details(azure): |
| 64 |
with azure_teardown(azure): |
| 65 |
assert not azure.ls(test_dir, invalidate_cache=False, detail=True) |
| 66 |
|
| 67 |
@my_vcr.use_cassette |
| 68 |
def test_ls_touch_invalidate_cache(azure, second_azure): |
| 69 |
with azure_teardown(azure): |
| 70 |
assert not azure.ls(test_dir, invalidate_cache=False) |
| 71 |
assert not second_azure.ls(test_dir, invalidate_cache=False) |
| 72 |
azure.touch(a) |
| 73 |
azure.touch(b) |
| 74 |
L = azure.ls(test_dir, True, invalidate_cache=False) |
| 75 |
assert not second_azure.ls(test_dir, invalidate_cache=False) |
| 76 |
L_second = second_azure.ls(test_dir, True, invalidate_cache=True) |
| 77 |
assert set(d['name'] for d in L) == set([a, b]) |
| 78 |
assert L == L_second |
| 79 |
|
| 80 |
@my_vcr.use_cassette |
| 81 |
def test_ls_batched(azure): |
| 82 |
|
| 83 |
test_dir = working_dir() / 'abc' |
| 84 |
azure.mkdir(test_dir) |
| 85 |
with azure_teardown(azure): |
| 86 |
test_size = 10 |
| 87 |
assert azure._ls(test_dir, batch_size=10) == [] |
| 88 |
create_files(azure, number_of_files = 10, prefix='123', root_path=test_dir) |
| 89 |
with pytest.raises(ValueError): |
| 90 |
assert len(azure._ls(test_dir, batch_size=1)) == test_size |
| 91 |
|
| 92 |
assert len(azure._ls(test_dir, batch_size=9)) == test_size |
| 93 |
assert len(azure._ls(test_dir, batch_size=10)) == test_size |
| 94 |
assert len(azure._ls(test_dir, batch_size=11)) == test_size |
| 95 |
assert len(azure._ls(test_dir, batch_size=2)) == test_size |
| 96 |
assert len(azure._ls(test_dir, batch_size=100)) == test_size |
| 97 |
assert len(azure._ls(test_dir)) == test_size |
| 98 |
|
| 99 |
|
| 100 |
@my_vcr.use_cassette |
| 101 |
def test_rm(azure): |
| 102 |
with azure_teardown(azure): |
| 103 |
assert not azure.exists(a, invalidate_cache=False) |
| 104 |
azure.touch(a) |
| 105 |
assert azure.exists(a, invalidate_cache=False) |
| 106 |
azure.rm(a) |
| 107 |
assert not azure.exists(a, invalidate_cache=False) |
| 108 |
|
| 109 |
@my_vcr.use_cassette |
| 110 |
def test_seek(azure): |
| 111 |
with azure_teardown(azure): |
| 112 |
with azure.open(a, 'wb') as f: |
| 113 |
f.write(b'123') |
| 114 |
|
| 115 |
with azure.open(a) as f: |
| 116 |
# assert False |
| 117 |
with pytest.raises(ValueError): |
| 118 |
f.seek(1000) |
| 119 |
with pytest.raises(ValueError): |
| 120 |
f.seek(-1) |
| 121 |
with pytest.raises(ValueError): |
| 122 |
f.seek(-5, 2) |
| 123 |
with pytest.raises(ValueError): |
| 124 |
f.seek(0, 10) |
| 125 |
f.seek(0) |
| 126 |
assert f.read(1) == b'1' |
| 127 |
f.seek(0) |
| 128 |
assert f.read(1) == b'1' |
| 129 |
f.seek(3) |
| 130 |
assert f.read(1) == b'' |
| 131 |
f.seek(-1, 2) |
| 132 |
assert f.read(1) == b'3' |
| 133 |
f.seek(-1, 1) |
| 134 |
f.seek(-1, 1) |
| 135 |
assert f.read(1) == b'2' |
| 136 |
for i in range(4): |
| 137 |
assert f.seek(i) == i |
| 138 |
|
| 139 |
@pytest.mark.skipif(not symlinktestsdisabled, reason="Concat is not supported over symlinks.") |
| 140 |
@my_vcr.use_cassette |
| 141 |
def test_concat(azure): |
| 142 |
aplus = a + "+file1" |
| 143 |
bplus = b + "+file2" |
| 144 |
cplus = c + "+res" |
| 145 |
with azure.open(aplus, 'wb') as f: |
| 146 |
f.write(b'hello ') |
| 147 |
with azure.open(bplus, 'wb') as f: |
| 148 |
f.write(b'world') |
| 149 |
try: |
| 150 |
azure.rm(cplus) |
| 151 |
except: |
| 152 |
pass |
| 153 |
|
| 154 |
azure.concat(cplus, [aplus, bplus]) |
| 155 |
out = azure.cat(cplus) |
| 156 |
azure.rm(cplus) |
| 157 |
|
| 158 |
assert out == b'hello world' |
| 159 |
|
| 160 |
|
| 161 |
@my_vcr.use_cassette |
| 162 |
def test_bad_open(azure): |
| 163 |
with pytest.raises(IOError): |
| 164 |
azure.open('') |
| 165 |
|
| 166 |
|
| 167 |
@my_vcr.use_cassette |
| 168 |
def test_errors(azure): |
| 169 |
with pytest.raises((IOError, OSError)): |
| 170 |
azure.open(test_dir / 'shfoshf', 'rb') |
| 171 |
|
| 172 |
# This is totally OK: directory is silently created |
| 173 |
# Will need extend invalidate_cache |
| 174 |
# with pytest.raises((IOError, OSError)): |
| 175 |
# azure.touch(test_dir / 'shfoshf' / 'x') |
| 176 |
|
| 177 |
with pytest.raises((IOError, OSError)): |
| 178 |
azure.rm(test_dir / 'shfoshf' / 'xxx') |
| 179 |
|
| 180 |
with pytest.raises((IOError, OSError)): |
| 181 |
azure.mv(test_dir / 'shfoshf' / 'x', test_dir / 'shfoshxbf' / 'y') |
| 182 |
|
| 183 |
# with pytest.raises(IOError): |
| 184 |
# azure.chown('unknown', 'someone', 'group') |
| 185 |
|
| 186 |
# with pytest.raises(IOError): |
| 187 |
# azure.chmod('unknonwn', 'rb') |
| 188 |
|
| 189 |
with pytest.raises(IOError): |
| 190 |
azure.rm(test_dir / 'unknown') |
| 191 |
|
| 192 |
|
| 193 |
@my_vcr.use_cassette |
| 194 |
def test_glob_walk(azure): |
| 195 |
with azure_teardown(azure): |
| 196 |
azure.mkdir(test_dir / 'c') |
| 197 |
azure.mkdir(test_dir / 'c' / 'd') |
| 198 |
filenames = ['a', 'a1', 'a2', 'a3', 'b1', 'c/x1', 'c/x2', 'c/d/x3'] |
| 199 |
filenames = [test_dir / s for s in filenames] |
| 200 |
for fn in filenames: |
| 201 |
azure.touch(fn) |
| 202 |
|
| 203 |
assert set(azure.glob(test_dir / 'a*')) == { |
| 204 |
posix(test_dir / 'a'), |
| 205 |
posix(test_dir / 'a1'), |
| 206 |
posix(test_dir / 'a2'), |
| 207 |
posix(test_dir / 'a3')} |
| 208 |
|
| 209 |
assert set(azure.glob(test_dir / 'c' / '*')) == { |
| 210 |
posix(test_dir / 'c' / 'x1'), |
| 211 |
posix(test_dir / 'c' / 'x2')} |
| 212 |
|
| 213 |
assert (set(azure.glob(test_dir / 'c')) == |
| 214 |
set(azure.glob(test_dir / 'c' / ''))) |
| 215 |
|
| 216 |
assert set(azure.glob(test_dir / 'a')) == {posix(test_dir / 'a')} |
| 217 |
assert set(azure.glob(test_dir / 'a1')) == {posix(test_dir / 'a1')} |
| 218 |
|
| 219 |
assert set(azure.glob(test_dir / '*')) == { |
| 220 |
posix(test_dir / 'a'), |
| 221 |
posix(test_dir / 'a1'), |
| 222 |
posix(test_dir / 'a2'), |
| 223 |
posix(test_dir / 'a3'), |
| 224 |
posix(test_dir / 'b1')} |
| 225 |
|
| 226 |
assert set(azure.walk(test_dir, invalidate_cache=False)) == { |
| 227 |
posix(test_dir / 'a'), |
| 228 |
posix(test_dir / 'a1'), |
| 229 |
posix(test_dir / 'a2'), |
| 230 |
posix(test_dir / 'a3'), |
| 231 |
posix(test_dir / 'b1'), |
| 232 |
posix(test_dir / 'c' / 'x1'), |
| 233 |
posix(test_dir / 'c' / 'x2'), |
| 234 |
posix(test_dir / 'c' / 'd' / 'x3')} |
| 235 |
|
| 236 |
assert set(azure.walk(test_dir / 'c', invalidate_cache=False)) == { |
| 237 |
posix(test_dir / 'c' / 'x1'), |
| 238 |
posix(test_dir / 'c' / 'x2'), |
| 239 |
posix(test_dir / 'c' / 'd' / 'x3')} |
| 240 |
|
| 241 |
assert set(azure.walk(test_dir / 'c', invalidate_cache=False)) == set(azure.walk(test_dir / 'c', invalidate_cache=False)) |
| 242 |
|
| 243 |
# test glob and walk with details=True |
| 244 |
glob_details = azure.glob(test_dir / '*', details=True, invalidate_cache=False) |
| 245 |
|
| 246 |
# validate that the objects are subscriptable |
| 247 |
assert glob_details[0]['name'] is not None |
| 248 |
assert glob_details[0]['type'] is not None |
| 249 |
|
| 250 |
walk_details = azure.walk(test_dir, details=True, invalidate_cache=False) |
| 251 |
assert walk_details[0]['name'] is not None |
| 252 |
assert walk_details[0]['type'] is not None |
| 253 |
|
| 254 |
@my_vcr.use_cassette |
| 255 |
def test_glob_walk_invalidate_cache(azure): |
| 256 |
with azure_teardown(azure): |
| 257 |
azure.mkdir(test_dir / 'c') |
| 258 |
azure.mkdir(test_dir / 'c' / 'd') |
| 259 |
filenames = ['a', 'a1', 'a2', 'a3', 'b1', 'c/x1', 'c/x2', 'c/d/x3'] |
| 260 |
filenames = [test_dir / s for s in filenames] |
| 261 |
for fn in filenames: |
| 262 |
azure.touch(fn) |
| 263 |
|
| 264 |
assert set(azure.glob(test_dir / 'a*')) == { |
| 265 |
posix(test_dir / 'a'), |
| 266 |
posix(test_dir / 'a1'), |
| 267 |
posix(test_dir / 'a2'), |
| 268 |
posix(test_dir / 'a3')} |
| 269 |
|
| 270 |
assert set(azure.glob(test_dir / 'c' / '*')) == { |
| 271 |
posix(test_dir / 'c' / 'x1'), |
| 272 |
posix(test_dir / 'c' / 'x2')} |
| 273 |
|
| 274 |
assert (set(azure.glob(test_dir / 'c')) == |
| 275 |
set(azure.glob(test_dir / 'c' / ''))) |
| 276 |
|
| 277 |
assert set(azure.glob(test_dir / 'a')) == {posix(test_dir / 'a')} |
| 278 |
assert set(azure.glob(test_dir / 'a1')) == {posix(test_dir / 'a1')} |
| 279 |
|
| 280 |
assert set(azure.glob(test_dir / '*')) == { |
| 281 |
posix(test_dir / 'a'), |
| 282 |
posix(test_dir / 'a1'), |
| 283 |
posix(test_dir / 'a2'), |
| 284 |
posix(test_dir / 'a3'), |
| 285 |
posix(test_dir / 'b1')} |
| 286 |
|
| 287 |
assert set(azure.walk(test_dir, invalidate_cache=True)) == { |
| 288 |
posix(test_dir / 'a'), |
| 289 |
posix(test_dir / 'a1'), |
| 290 |
posix(test_dir / 'a2'), |
| 291 |
posix(test_dir / 'a3'), |
| 292 |
posix(test_dir / 'b1'), |
| 293 |
posix(test_dir / 'c' / 'x1'), |
| 294 |
posix(test_dir / 'c' / 'x2'), |
| 295 |
posix(test_dir / 'c' / 'd' / 'x3')} |
| 296 |
|
| 297 |
assert set(azure.walk(test_dir / 'c', invalidate_cache=True)) == { |
| 298 |
posix(test_dir / 'c' / 'x1'), |
| 299 |
posix(test_dir / 'c' / 'x2'), |
| 300 |
posix(test_dir / 'c' / 'd' / 'x3')} |
| 301 |
|
| 302 |
assert set(azure.walk(test_dir / 'c', invalidate_cache=True)) == set(azure.walk(test_dir / 'c', invalidate_cache=True)) |
| 303 |
|
| 304 |
# test glob and walk with details=True |
| 305 |
glob_details = azure.glob(test_dir / '*', details=True, invalidate_cache=True) |
| 306 |
|
| 307 |
# validate that the objects are subscriptable |
| 308 |
assert glob_details[0]['name'] is not None |
| 309 |
assert glob_details[0]['type'] is not None |
| 310 |
|
| 311 |
walk_details = azure.walk(test_dir, details=True, invalidate_cache=True) |
| 312 |
assert walk_details[0]['name'] is not None |
| 313 |
assert walk_details[0]['type'] is not None |
| 314 |
|
| 315 |
@my_vcr.use_cassette |
| 316 |
def test_info(azure): |
| 317 |
with azure_teardown(azure): |
| 318 |
with azure.open(a, 'wb') as f: |
| 319 |
f.write(b'a' * 5) |
| 320 |
|
| 321 |
info = azure.info(a, invalidate_cache=False) |
| 322 |
assert info['length'] == 5 |
| 323 |
assert info['name'] == a |
| 324 |
assert info['type'] == 'FILE' |
| 325 |
|
| 326 |
assert azure.info(test_dir, invalidate_cache=True)['type'] == 'DIRECTORY' |
| 327 |
|
| 328 |
@my_vcr.use_cassette |
| 329 |
def test_info_invalidate_cache(azure, second_azure): |
| 330 |
with azure_teardown(azure): |
| 331 |
# construct initial cache and ensure the file doesn't already exist |
| 332 |
assert not azure.exists(a, invalidate_cache=False) |
| 333 |
assert not second_azure.exists(a, invalidate_cache=False) |
| 334 |
|
| 335 |
with azure.open(a, 'wb') as f: |
| 336 |
f.write(b'a' * 5) |
| 337 |
|
| 338 |
# verify that it works in the fs that did the write and not on the other |
| 339 |
info = azure.info(a, invalidate_cache=False) |
| 340 |
with pytest.raises(FileNotFoundError): |
| 341 |
second_azure.info(a, invalidate_cache=False) |
| 342 |
|
| 343 |
# then invalidate |
| 344 |
second_info = second_azure.info(a, invalidate_cache=True) |
| 345 |
assert info['length'] == 5 |
| 346 |
assert info['name'] == a |
| 347 |
assert info['type'] == 'FILE' |
| 348 |
|
| 349 |
assert info['length'] == second_info['length'] |
| 350 |
assert info['name'] == second_info['name'] |
| 351 |
assert info['type'] == second_info['type'] |
| 352 |
|
| 353 |
# assure that the cache was properly repopulated on the info call |
| 354 |
assert second_azure.info(test_dir, invalidate_cache=False)['type'] == 'DIRECTORY' |
| 355 |
|
| 356 |
@my_vcr.use_cassette |
| 357 |
def test_df(azure): |
| 358 |
with azure_teardown(azure): |
| 359 |
with azure.open(a, 'wb') as f: |
| 360 |
f.write(b'a' * 10) |
| 361 |
with azure.open(b, 'wb') as f: |
| 362 |
f.write(b'a' * 10) |
| 363 |
|
| 364 |
result = azure.df(test_dir) |
| 365 |
assert result['fileCount'] > 0 |
| 366 |
assert result['spaceConsumed'] > 0 |
| 367 |
|
| 368 |
|
| 369 |
@my_vcr.use_cassette |
| 370 |
def test_move(azure): |
| 371 |
with azure_teardown(azure): |
| 372 |
azure.touch(a) |
| 373 |
assert azure.exists(a, invalidate_cache=False) |
| 374 |
assert not azure.exists(b, invalidate_cache=False) |
| 375 |
azure.mv(a, b) |
| 376 |
assert not azure.exists(a, invalidate_cache=False) |
| 377 |
assert azure.exists(b, invalidate_cache=False) |
| 378 |
|
| 379 |
|
| 380 |
@my_vcr.use_cassette |
| 381 |
@pytest.mark.xfail(reason='copy not implemented on ADL') |
| 382 |
def test_copy(azure): |
| 383 |
with azure_teardown(azure): |
| 384 |
azure.touch(a) |
| 385 |
assert azure.exists(a, invalidate_cache=False) |
| 386 |
assert not azure.exists(b, invalidate_cache=False) |
| 387 |
azure.cp(a, b) |
| 388 |
assert azure.exists(a, invalidate_cache=False) |
| 389 |
assert azure.exists(b, invalidate_cache=False) |
| 390 |
|
| 391 |
|
| 392 |
@my_vcr.use_cassette |
| 393 |
def test_exists(azure): |
| 394 |
with azure_teardown(azure): |
| 395 |
assert not azure.exists(a, invalidate_cache=False) |
| 396 |
azure.touch(a) |
| 397 |
assert azure.exists(a, invalidate_cache=False) |
| 398 |
azure.rm(a) |
| 399 |
assert not azure.exists(a, invalidate_cache=False) |
| 400 |
|
| 401 |
@my_vcr.use_cassette |
| 402 |
def test_exists_remove_invalidate_cache(azure, second_azure): |
| 403 |
with azure_teardown(azure): |
| 404 |
# test to ensure file does not exist up front, cache doesn't matter |
| 405 |
assert not azure.exists(a, invalidate_cache=False) |
| 406 |
assert not second_azure.exists(a, invalidate_cache=False) |
| 407 |
azure.touch(a) |
| 408 |
# now ensure that it exists in the client that did the work, but not in the other |
| 409 |
assert azure.exists(a, invalidate_cache=False) |
| 410 |
assert not second_azure.exists(a, invalidate_cache=False) |
| 411 |
# now, with cache invalidation it should exist |
| 412 |
assert second_azure.exists(a, invalidate_cache=True) |
| 413 |
azure.rm(a) |
| 414 |
# same idea with remove. It should no longer exist (cache invalidated or not) in client 1, but still exist in client 2 |
| 415 |
assert not azure.exists(a, invalidate_cache=False) |
| 416 |
assert second_azure.exists(a, invalidate_cache=False) |
| 417 |
# now ensure it does not exist when we do invalidate the cache |
| 418 |
assert not second_azure.exists(a, invalidate_cache=True) |
| 419 |
|
| 420 |
|
| 421 |
@my_vcr.use_cassette |
| 422 |
def test_cat(azure): |
| 423 |
with azure_teardown(azure): |
| 424 |
with azure.open(a, 'wb') as f: |
| 425 |
f.write(b'0123456789') |
| 426 |
assert azure.cat(a) == b'0123456789' |
| 427 |
with pytest.raises(IOError): |
| 428 |
azure.cat(b) |
| 429 |
|
| 430 |
|
| 431 |
@my_vcr.use_cassette |
| 432 |
def test_full_read(azure): |
| 433 |
with azure_teardown(azure): |
| 434 |
with azure.open(a, 'wb') as f: |
| 435 |
f.write(b'0123456789') |
| 436 |
|
| 437 |
with azure.open(a, 'rb') as f: |
| 438 |
assert len(f.read(4)) == 4 |
| 439 |
assert len(f.read(4)) == 4 |
| 440 |
assert len(f.read(4)) == 2 |
| 441 |
|
| 442 |
with azure.open(a, 'rb') as f: |
| 443 |
assert len(f.read()) == 10 |
| 444 |
|
| 445 |
with azure.open(a, 'rb') as f: |
| 446 |
assert f.tell() == 0 |
| 447 |
f.seek(3) |
| 448 |
assert f.read(4) == b'3456' |
| 449 |
assert f.tell() == 7 |
| 450 |
assert f.read(4) == b'789' |
| 451 |
assert f.tell() == 10 |
| 452 |
|
| 453 |
@my_vcr.use_cassette |
| 454 |
def test_readinto(azure): |
| 455 |
with azure_teardown(azure): |
| 456 |
with azure.open(a, 'wb') as f: |
| 457 |
f.write(b'0123456789') |
| 458 |
|
| 459 |
with azure.open(a, 'rb') as f: |
| 460 |
buffer = bytearray(6) |
| 461 |
l = f.readinto(buffer) |
| 462 |
assert l == 6 |
| 463 |
assert buffer == b'012345' |
| 464 |
|
| 465 |
buffer = bytearray(6) |
| 466 |
l = f.readinto(buffer) |
| 467 |
assert l == 4 |
| 468 |
assert buffer == b'6789\x00\x00' |
| 469 |
|
| 470 |
buffer = bytearray(6) |
| 471 |
l = f.readinto(buffer) |
| 472 |
assert buffer == b'\x00\x00\x00\x00\x00\x00' |
| 473 |
assert l == 0 |
| 474 |
|
| 475 |
with azure.open(a, 'rb') as f: |
| 476 |
buffer = bytearray(6) |
| 477 |
l = f.readinto(buffer) |
| 478 |
assert l == 6 |
| 479 |
assert buffer == b'012345' |
| 480 |
|
| 481 |
l = f.readinto(buffer) |
| 482 |
assert l == 4 |
| 483 |
assert buffer == b'678945' # 45 from previous buffer fill should not be overwritten |
| 484 |
|
| 485 |
|
| 486 |
@my_vcr.use_cassette |
| 487 |
def test_filename_specialchar(azure): |
| 488 |
with azure_teardown(azure): |
| 489 |
with azure.open(specialCharFile, 'wb') as f: |
| 490 |
f.write(b'0123456789') |
| 491 |
|
| 492 |
with azure.open(specialCharFile, 'rb') as f: |
| 493 |
assert len(f.read(4)) == 4 |
| 494 |
assert len(f.read(4)) == 4 |
| 495 |
assert len(f.read(4)) == 2 |
| 496 |
|
| 497 |
with azure.open(specialCharFile, 'rb') as f: |
| 498 |
assert len(f.read()) == 10 |
| 499 |
|
| 500 |
with azure.open(specialCharFile, 'rb') as f: |
| 501 |
assert f.tell() == 0 |
| 502 |
f.seek(3) |
| 503 |
assert f.read(4) == b'3456' |
| 504 |
assert f.tell() == 7 |
| 505 |
assert f.read(4) == b'789' |
| 506 |
assert f.tell() == 10 |
| 507 |
|
| 508 |
|
| 509 |
def __ready_and_read_file_for_cache_test(azure, data=b'0123456789abcdef'): |
| 510 |
with azure.open(a, 'wb') as f: |
| 511 |
f.write(data) |
| 512 |
|
| 513 |
f = azure.open(a, 'rb', blocksize=4) |
| 514 |
# start cache @ 2 |
| 515 |
f.seek(2) |
| 516 |
# end cache @ 6 |
| 517 |
f.read(4) |
| 518 |
return f |
| 519 |
|
| 520 |
|
| 521 |
@my_vcr.use_cassette |
| 522 |
def test_cache_read_overlapping_end(azure): |
| 523 |
with azure_teardown(azure): |
| 524 |
|
| 525 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 526 |
assert f.start == 2 |
| 527 |
assert f.end == 6 |
| 528 |
assert f.cache == b'2345' |
| 529 |
|
| 530 |
# offset/loc @ 4 |
| 531 |
f.seek(4) |
| 532 |
data = f.read(4) |
| 533 |
|
| 534 |
assert data == b'4567' |
| 535 |
assert f.start == 6 |
| 536 |
assert f.end == 10 |
| 537 |
|
| 538 |
|
| 539 |
@my_vcr.use_cassette |
| 540 |
def test_cache_read_overlapping_start(azure): |
| 541 |
with azure_teardown(azure): |
| 542 |
|
| 543 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 544 |
assert f.start == 2 |
| 545 |
assert f.end == 6 |
| 546 |
assert f.cache == b'2345' |
| 547 |
|
| 548 |
# offset/loc @ 1 |
| 549 |
f.seek(1) |
| 550 |
data = f.read(3) |
| 551 |
|
| 552 |
assert data == b'123' |
| 553 |
assert f.start == 1 |
| 554 |
assert f.end == 5 |
| 555 |
|
| 556 |
|
| 557 |
@my_vcr.use_cassette |
| 558 |
def test_cache_read_subset(azure): |
| 559 |
with azure_teardown(azure): |
| 560 |
|
| 561 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 562 |
assert f.start == 2 |
| 563 |
assert f.end == 6 |
| 564 |
assert f.cache == b'2345' |
| 565 |
|
| 566 |
# offset/loc @ 1 |
| 567 |
f.seek(4) |
| 568 |
data = f.read(1) |
| 569 |
|
| 570 |
assert data == b'4' |
| 571 |
assert f.start == 2 |
| 572 |
assert f.end == 6 |
| 573 |
|
| 574 |
|
| 575 |
@my_vcr.use_cassette |
| 576 |
def test_cache_read_superset(azure): |
| 577 |
with azure_teardown(azure): |
| 578 |
|
| 579 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 580 |
assert f.start == 2 |
| 581 |
assert f.end == 6 |
| 582 |
assert f.cache == b'2345' |
| 583 |
|
| 584 |
# offset/loc @ 1 |
| 585 |
f.seek(1) |
| 586 |
data = f.read(7) |
| 587 |
|
| 588 |
assert data == b'1234567' |
| 589 |
assert f.start == 5 |
| 590 |
assert f.end == 9 |
| 591 |
|
| 592 |
|
| 593 |
@my_vcr.use_cassette |
| 594 |
def test_cache_read_continuous_end(azure): |
| 595 |
with azure_teardown(azure): |
| 596 |
|
| 597 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 598 |
assert f.start == 2 |
| 599 |
assert f.end == 6 |
| 600 |
assert f.cache == b'2345' |
| 601 |
|
| 602 |
# offset/loc @ 1 |
| 603 |
f.seek(6) |
| 604 |
data = f.read(2) |
| 605 |
|
| 606 |
assert data == b'67' |
| 607 |
assert f.start == 6 |
| 608 |
assert f.end == 10 |
| 609 |
|
| 610 |
|
| 611 |
@my_vcr.use_cassette |
| 612 |
def test_cache_read_continuous_start(azure): |
| 613 |
with azure_teardown(azure): |
| 614 |
|
| 615 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 616 |
assert f.start == 2 |
| 617 |
assert f.end == 6 |
| 618 |
assert f.cache == b'2345' |
| 619 |
|
| 620 |
# offset/loc @ 1 |
| 621 |
f.seek(0) |
| 622 |
data = f.read(2) |
| 623 |
|
| 624 |
assert data == b'01' |
| 625 |
assert f.start == 0 |
| 626 |
assert f.end == 4 |
| 627 |
|
| 628 |
|
| 629 |
@my_vcr.use_cassette |
| 630 |
def test_cache_read_within_start(azure): |
| 631 |
with azure_teardown(azure): |
| 632 |
|
| 633 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 634 |
assert f.start == 2 |
| 635 |
assert f.end == 6 |
| 636 |
assert f.cache == b'2345' |
| 637 |
|
| 638 |
# offset/loc @ 1 |
| 639 |
f.seek(2) |
| 640 |
data = f.read(2) |
| 641 |
|
| 642 |
assert data == b'23' |
| 643 |
assert f.start == 2 |
| 644 |
assert f.end == 6 |
| 645 |
|
| 646 |
|
| 647 |
@my_vcr.use_cassette |
| 648 |
def test_cache_read_within_end(azure): |
| 649 |
with azure_teardown(azure): |
| 650 |
|
| 651 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 652 |
assert f.start == 2 |
| 653 |
assert f.end == 6 |
| 654 |
assert f.cache == b'2345' |
| 655 |
|
| 656 |
# offset/loc @ 1 |
| 657 |
f.seek(4) |
| 658 |
data = f.read(2) |
| 659 |
|
| 660 |
assert data == b'45' |
| 661 |
assert f.start == 2 |
| 662 |
assert f.end == 6 |
| 663 |
|
| 664 |
|
| 665 |
@my_vcr.use_cassette |
| 666 |
def test_cache_read_zero_start(azure): |
| 667 |
with azure_teardown(azure): |
| 668 |
|
| 669 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 670 |
assert f.start == 2 |
| 671 |
assert f.end == 6 |
| 672 |
assert f.cache == b'2345' |
| 673 |
|
| 674 |
# offset/loc @ 1 |
| 675 |
f.seek(2) |
| 676 |
data = f.read(0) |
| 677 |
|
| 678 |
assert data == b'' |
| 679 |
assert f.start == 2 |
| 680 |
assert f.end == 6 |
| 681 |
|
| 682 |
|
| 683 |
@my_vcr.use_cassette |
| 684 |
def test_cache_read_zero_end(azure): |
| 685 |
with azure_teardown(azure): |
| 686 |
|
| 687 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 688 |
assert f.start == 2 |
| 689 |
assert f.end == 6 |
| 690 |
assert f.cache == b'2345' |
| 691 |
|
| 692 |
# offset/loc @ 1 |
| 693 |
f.seek(6) |
| 694 |
data = f.read(0) |
| 695 |
|
| 696 |
assert data == b'' |
| 697 |
assert f.start == 2 |
| 698 |
assert f.end == 6 |
| 699 |
|
| 700 |
|
| 701 |
@my_vcr.use_cassette |
| 702 |
def test_cache_read_outside_end(azure): |
| 703 |
with azure_teardown(azure): |
| 704 |
|
| 705 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 706 |
assert f.start == 2 |
| 707 |
assert f.end == 6 |
| 708 |
assert f.cache == b'2345' |
| 709 |
|
| 710 |
# offset/loc @ 1 |
| 711 |
f.seek(8) |
| 712 |
data = f.read(2) |
| 713 |
|
| 714 |
assert data == b'89' |
| 715 |
assert f.start == 8 |
| 716 |
assert f.end == 12 |
| 717 |
|
| 718 |
|
| 719 |
@my_vcr.use_cassette |
| 720 |
def test_cache_read_superset_big(azure): |
| 721 |
with azure_teardown(azure): |
| 722 |
|
| 723 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 724 |
assert f.start == 2 |
| 725 |
assert f.end == 6 |
| 726 |
assert f.cache == b'2345' |
| 727 |
|
| 728 |
# offset/loc @ 1 |
| 729 |
f.seek(2) |
| 730 |
data = f.read(20) |
| 731 |
|
| 732 |
assert data == b'23456789abcdef' |
| 733 |
assert f.start == 14 |
| 734 |
assert f.end == 16 |
| 735 |
|
| 736 |
|
| 737 |
@my_vcr.use_cassette |
| 738 |
def test_cache_read_superset_verybig(azure): |
| 739 |
with azure_teardown(azure): |
| 740 |
data = b'0123456789abcdefghijklmnopqrstuvwxyz' |
| 741 |
with __ready_and_read_file_for_cache_test(azure, data) as f: |
| 742 |
assert f.start == 2 |
| 743 |
assert f.end == 6 |
| 744 |
assert f.cache == b'2345' |
| 745 |
|
| 746 |
# offset/loc @ 1 |
| 747 |
f.seek(2) |
| 748 |
data = f.read(36) |
| 749 |
|
| 750 |
assert data == b'23456789abcdefghijklmnopqrstuvwxyz' |
| 751 |
assert f.start == 34 |
| 752 |
assert f.end == 36 |
| 753 |
|
| 754 |
|
| 755 |
@my_vcr.use_cassette |
| 756 |
def test_cache_read_multiple_reads(azure): |
| 757 |
with azure_teardown(azure): |
| 758 |
|
| 759 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 760 |
assert f.start == 2 |
| 761 |
assert f.end == 6 |
| 762 |
assert f.cache == b'2345' |
| 763 |
|
| 764 |
# offset/loc @ 1 |
| 765 |
f.seek(1) |
| 766 |
data = f.read(3) |
| 767 |
assert data == b'123' |
| 768 |
assert f.start == 1 |
| 769 |
assert f.end == 5 |
| 770 |
data = f.read(3) |
| 771 |
assert data == b'456' |
| 772 |
assert f.start == 5 |
| 773 |
assert f.end == 9 |
| 774 |
data = f.read(3) |
| 775 |
|
| 776 |
assert data == b'789' |
| 777 |
assert f.start == 9 |
| 778 |
assert f.end == 13 |
| 779 |
data = f.read(3) |
| 780 |
assert data == b'abc' |
| 781 |
assert f.start == 9 |
| 782 |
assert f.end == 13 |
| 783 |
|
| 784 |
|
| 785 |
@my_vcr.use_cassette |
| 786 |
def test_cache_read_multiple_reads_big(azure): |
| 787 |
with azure_teardown(azure): |
| 788 |
|
| 789 |
data = b'0123456789abcdefghijklmnopqrstuvwxyz' |
| 790 |
with __ready_and_read_file_for_cache_test(azure, data) as f: |
| 791 |
assert f.start == 2 |
| 792 |
assert f.end == 6 |
| 793 |
assert f.cache == b'2345' |
| 794 |
|
| 795 |
# offset/loc @ 1 |
| 796 |
f.seek(1) |
| 797 |
data = f.read(6) |
| 798 |
assert data == b'123456' |
| 799 |
assert f.start == 5 |
| 800 |
assert f.end == 9 |
| 801 |
data = f.read(9) |
| 802 |
|
| 803 |
assert data == b'789abcdef' |
| 804 |
assert f.start == 13 |
| 805 |
assert f.end == 17 |
| 806 |
data = f.read(6) |
| 807 |
assert data == b'ghijkl' |
| 808 |
assert f.start == 21 |
| 809 |
assert f.end == 25 |
| 810 |
data = f.read(9) |
| 811 |
assert data == b'mnopqrstu' |
| 812 |
assert f.start == 29 |
| 813 |
assert f.end == 33 |
| 814 |
data = f.read(9) |
| 815 |
assert data == b'vwxyz' |
| 816 |
assert f.start == 33 |
| 817 |
assert f.end == 36 |
| 818 |
|
| 819 |
|
| 820 |
@my_vcr.use_cassette |
| 821 |
def test_cache_read_full_read(azure): |
| 822 |
with azure_teardown(azure): |
| 823 |
|
| 824 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 825 |
assert f.start == 2 |
| 826 |
assert f.end == 6 |
| 827 |
assert f.cache == b'2345' |
| 828 |
|
| 829 |
# offset/loc @ 1 |
| 830 |
f.seek(3) |
| 831 |
data = f.read(40) |
| 832 |
|
| 833 |
assert data == b'3456789abcdef' |
| 834 |
assert f.start == 14 |
| 835 |
assert f.end == 16 |
| 836 |
|
| 837 |
|
| 838 |
@my_vcr.use_cassette |
| 839 |
def test_cache_read_outside_start(azure): |
| 840 |
with azure_teardown(azure): |
| 841 |
|
| 842 |
with __ready_and_read_file_for_cache_test(azure) as f: |
| 843 |
assert f.start == 2 |
| 844 |
assert f.end == 6 |
| 845 |
assert f.cache == b'2345' |
| 846 |
|
| 847 |
# offset/loc @ 1 |
| 848 |
f.seek(0) |
| 849 |
data = f.read(1) |
| 850 |
|
| 851 |
assert data == b'0' |
| 852 |
assert f.start == 0 |
| 853 |
assert f.end == 4 |
| 854 |
|
| 855 |
|
| 856 |
@my_vcr.use_cassette |
| 857 |
def test_tail_head(azure): |
| 858 |
with azure_teardown(azure): |
| 859 |
with azure.open(a, 'wb') as f: |
| 860 |
f.write(b'0123456789') |
| 861 |
|
| 862 |
assert azure.tail(a, 3) == b'789' |
| 863 |
assert azure.head(a, 3) == b'012' |
| 864 |
assert azure.tail(a, 100) == b'0123456789' |
| 865 |
|
| 866 |
@my_vcr.use_cassette |
| 867 |
def test_read_delimited_block(azure): |
| 868 |
fn = a |
| 869 |
delimiter = b'\n' |
| 870 |
data = delimiter.join([b'123', b'456', b'789']) |
| 871 |
with azure_teardown(azure): |
| 872 |
with azure.open(fn, 'wb') as f: |
| 873 |
f.write(data) |
| 874 |
|
| 875 |
#TODO: add E2E validation with the transfer client once delimiters are hooked up |
| 876 |
assert azure.read_block(fn, 1, 2) == b'23' |
| 877 |
assert azure.read_block(fn, 0, 1, delimiter=b'\n') == b'1' |
| 878 |
assert azure.read_block(fn, 0, 2, delimiter=b'\n') == b'12' |
| 879 |
assert azure.read_block(fn, 0, 3, delimiter=b'\n') == b'123' |
| 880 |
assert azure.read_block(fn, 0, 4, delimiter=b'\n') == b'123\n' |
| 881 |
assert azure.read_block(fn, 0, 5, delimiter=b'\n') == b'123\n' |
| 882 |
assert azure.read_block(fn, 0, 8, delimiter=b'\n') == b'123\n456\n' |
| 883 |
assert azure.read_block(fn, 0, 100, delimiter=b'\n') == b'123\n456\n' |
| 884 |
assert azure.read_block(fn, 1, 1, delimiter=b'\n') == b'2' |
| 885 |
assert azure.read_block(fn, 1, 5, delimiter=b'\n') == b'23\n' |
| 886 |
assert azure.read_block(fn, 1, 8, delimiter=b'\n') == b'23\n456\n' |
| 887 |
|
| 888 |
azure.rm(fn) |
| 889 |
# test the negative cases of just the util read block |
| 890 |
with io.BytesIO(bytearray([1] * 2**22)) as data: |
| 891 |
with pytest.raises(IndexError): |
| 892 |
utils.read_block(data, 0, 2**22, delimiter=b'\n') |
| 893 |
|
| 894 |
# ensure it throws if the new line is past 4MB |
| 895 |
data.seek(2**22) |
| 896 |
data.write(b'\n') |
| 897 |
data.seek(0) |
| 898 |
with pytest.raises(IndexError): |
| 899 |
utils.read_block(data, 0, 1 + 2**22, delimiter=b'\n') |
| 900 |
|
| 901 |
@my_vcr.use_cassette |
| 902 |
def test_readline(azure): |
| 903 |
with azure_teardown(azure): |
| 904 |
with azure.open(a, 'wb') as f: |
| 905 |
f.write(b'\n'.join([b'123', b'456', b'789'])) |
| 906 |
|
| 907 |
with azure.open(a) as f: |
| 908 |
assert f.readline() == b'123\n' |
| 909 |
assert f.readline() == b'456\n' |
| 910 |
assert f.readline() == b'789' |
| 911 |
assert f.readline() == b'' |
| 912 |
|
| 913 |
|
| 914 |
@my_vcr.use_cassette |
| 915 |
def test_touch_exists(azure): |
| 916 |
with azure_teardown(azure): |
| 917 |
azure.touch(a) |
| 918 |
assert azure.exists(a, invalidate_cache=False) |
| 919 |
|
| 920 |
|
| 921 |
@my_vcr.use_cassette |
| 922 |
def test_write_in_read_mode(azure): |
| 923 |
with azure_teardown(azure): |
| 924 |
azure.touch(a) |
| 925 |
|
| 926 |
with azure.open(a, 'rb') as f: |
| 927 |
with pytest.raises(ValueError): |
| 928 |
f.write(b'123') |
| 929 |
|
| 930 |
|
| 931 |
@my_vcr.use_cassette |
| 932 |
def test_readlines(azure): |
| 933 |
with azure_teardown(azure): |
| 934 |
with azure.open(a, 'wb') as f: |
| 935 |
f.write(b'123\n456') |
| 936 |
|
| 937 |
with azure.open(a, 'rb') as f: |
| 938 |
lines = f.readlines() |
| 939 |
assert lines == [b'123\n', b'456'] |
| 940 |
|
| 941 |
with azure.open(a, 'rb') as f: |
| 942 |
assert list(f) == lines |
| 943 |
|
| 944 |
with azure.open(a, 'wb') as f: |
| 945 |
with pytest.raises(ValueError): |
| 946 |
f.read() |
| 947 |
|
| 948 |
bigdata = [b'fe', b'fi', b'fo'] * 1000 |
| 949 |
with azure.open(a, 'wb') as f: |
| 950 |
f.write(b'\n'.join(bigdata)) |
| 951 |
with azure.open(a, 'rb') as f: |
| 952 |
lines = list(f) |
| 953 |
assert all(l in [b'fe\n', b'fi\n', b'fo', b'fo\n'] for l in lines) |
| 954 |
|
| 955 |
|
| 956 |
@my_vcr.use_cassette |
| 957 |
def test_put(azure): |
| 958 |
data = b'1234567890' * 100 |
| 959 |
with azure_teardown(azure): |
| 960 |
with tmpfile() as fn: |
| 961 |
with open(fn, 'wb') as f: |
| 962 |
f.write(data) |
| 963 |
|
| 964 |
azure.put(fn, a) |
| 965 |
|
| 966 |
assert azure.cat(a) == data |
| 967 |
|
| 968 |
|
| 969 |
@my_vcr.use_cassette |
| 970 |
def test_get(azure): |
| 971 |
data = b'1234567890' |
| 972 |
with azure_teardown(azure): |
| 973 |
with tmpfile() as fn: |
| 974 |
with azure.open(a, 'wb') as f: |
| 975 |
f.write(data) |
| 976 |
|
| 977 |
azure.get(a, fn) |
| 978 |
|
| 979 |
with open(fn, 'rb') as f: |
| 980 |
data2 = f.read() |
| 981 |
assert data2 == data |
| 982 |
|
| 983 |
with pytest.raises(IOError): |
| 984 |
azure.get(b, fn) |
| 985 |
|
| 986 |
|
| 987 |
@my_vcr.use_cassette |
| 988 |
def test_du(azure): |
| 989 |
with azure_teardown(azure): |
| 990 |
with azure.open(a, 'wb') as f: |
| 991 |
f.write(b'123') |
| 992 |
with azure.open(b, 'wb') as f: |
| 993 |
f.write(b'4567') |
| 994 |
|
| 995 |
assert azure.du(test_dir) == {a: 3, b: 4} |
| 996 |
assert azure.du(test_dir, total=True) == 3 + 4 |
| 997 |
|
| 998 |
|
| 999 |
@my_vcr.use_cassette |
| 1000 |
def test_text_bytes(azure): |
| 1001 |
with pytest.raises(NotImplementedError): |
| 1002 |
azure.open(a, 'wt') |
| 1003 |
|
| 1004 |
with pytest.raises(NotImplementedError): |
| 1005 |
azure.open(a, 'rt') |
| 1006 |
|
| 1007 |
|
| 1008 |
@my_vcr.use_cassette |
| 1009 |
def test_append(azure): |
| 1010 |
with azure_teardown(azure): |
| 1011 |
with azure.open(a, mode='ab') as f: |
| 1012 |
f.write(b'123') |
| 1013 |
with azure.open(a, mode='ab') as f: |
| 1014 |
f.write(b'456') |
| 1015 |
|
| 1016 |
with azure.open(a, mode='rb') as f: |
| 1017 |
assert f.read() == b'123456' |
| 1018 |
|
| 1019 |
with azure.open(a, mode='ab') as f: |
| 1020 |
f.write(b'789') |
| 1021 |
with azure.open(a, mode='rb') as f: |
| 1022 |
assert f.read() == b'123456789' |
| 1023 |
|
| 1024 |
|
| 1025 |
@my_vcr.use_cassette |
| 1026 |
def test_write_empty(azure): |
| 1027 |
with azure_teardown(azure): |
| 1028 |
with azure.open(a, mode='wb') as f: |
| 1029 |
f.write(b'') |
| 1030 |
|
| 1031 |
with azure.open(a, mode='rb') as f: |
| 1032 |
assert f.read() == b'' |
| 1033 |
|
| 1034 |
|
| 1035 |
@my_vcr.use_cassette |
| 1036 |
def test_write_blocks(azure): |
| 1037 |
with azure_teardown(azure): |
| 1038 |
with azure.open(a, mode='wb', blocksize=5) as f: |
| 1039 |
f.write(b'000') |
| 1040 |
assert f.buffer.tell() == 3 |
| 1041 |
f.write(b'000') # forces flush |
| 1042 |
assert f.buffer.tell() == 1 |
| 1043 |
f.write(b'000') |
| 1044 |
assert f.tell() == 9 |
| 1045 |
assert azure.du(a)[a] == 9 |
| 1046 |
|
| 1047 |
@pytest.mark.skipif(not symlinktestsdisabled, reason="Zero byte append succeeds in Xstore, so this test won't raise expected exception.") |
| 1048 |
@my_vcr.use_cassette |
| 1049 |
def test_skip_existing_block(azure): |
| 1050 |
with azure.open(a, mode='wb') as f: |
| 1051 |
f.write(b'0' * 15) |
| 1052 |
|
| 1053 |
with pytest.raises((IOError, RuntimeError)): |
| 1054 |
with azure.open(a, mode='ab') as f: |
| 1055 |
assert f.tell() == 15 |
| 1056 |
f.loc = 5 # not a user method |
| 1057 |
f.write(b'blah') |
| 1058 |
|
| 1059 |
|
| 1060 |
@my_vcr.use_cassette |
| 1061 |
def test_gzip(azure): |
| 1062 |
import gzip |
| 1063 |
data = b'name,amount\nAlice,100\nBob,200' |
| 1064 |
with azure_teardown(azure): |
| 1065 |
with azure.open(a, mode='wb') as f: |
| 1066 |
with gzip.GzipFile(fileobj=f) as g: |
| 1067 |
g.write(b'name,amount\nAlice,100\nBob,200') |
| 1068 |
|
| 1069 |
with azure.open(a) as f: |
| 1070 |
with gzip.GzipFile(fileobj=f) as g: |
| 1071 |
bytes = g.read() |
| 1072 |
|
| 1073 |
assert bytes == data |
| 1074 |
|
| 1075 |
|
| 1076 |
@my_vcr.use_cassette |
| 1077 |
def test_fooable(azure): |
| 1078 |
with azure_teardown(azure): |
| 1079 |
azure.touch(a) |
| 1080 |
|
| 1081 |
with azure.open(a, mode='rb') as f: |
| 1082 |
assert f.readable() |
| 1083 |
assert f.seekable() |
| 1084 |
assert not f.writable() |
| 1085 |
|
| 1086 |
with azure.open(a, mode='wb') as f: |
| 1087 |
assert not f.readable() |
| 1088 |
assert not f.seekable() |
| 1089 |
assert f.writable() |
| 1090 |
|
| 1091 |
|
| 1092 |
@my_vcr.use_cassette |
| 1093 |
def test_closed(azure): |
| 1094 |
with azure_teardown(azure): |
| 1095 |
azure.touch(a) |
| 1096 |
|
| 1097 |
f = azure.open(a, mode='rb') |
| 1098 |
assert not f.closed |
| 1099 |
f.close() |
| 1100 |
assert f.closed |
| 1101 |
|
| 1102 |
|
| 1103 |
@my_vcr.use_cassette |
| 1104 |
def test_TextIOWrapper(azure): |
| 1105 |
with azure_teardown(azure): |
| 1106 |
with azure.open(a, mode='wb') as f: |
| 1107 |
f.write(b'1,2\n3,4\n5,6') |
| 1108 |
|
| 1109 |
with azure.open(a, mode='rb') as f: |
| 1110 |
ff = io.TextIOWrapper(f) |
| 1111 |
data = list(ff) |
| 1112 |
|
| 1113 |
assert data == ['1,2\n', '3,4\n', '5,6'] |
| 1114 |
|
| 1115 |
|
| 1116 |
@my_vcr.use_cassette |
| 1117 |
def test_array(azure): |
| 1118 |
from array import array |
| 1119 |
data = array('B', [65] * 1000) |
| 1120 |
|
| 1121 |
with azure_teardown(azure): |
| 1122 |
with azure.open(a, 'wb') as f: |
| 1123 |
f.write(data) |
| 1124 |
|
| 1125 |
with azure.open(a, 'rb') as f: |
| 1126 |
out = f.read() |
| 1127 |
assert out == b'A' * 1000 |
| 1128 |
|
| 1129 |
|
| 1130 |
def write_delimited_data(azure, delimiter): |
| 1131 |
data = delimiter.join([b'123', b'456', b'789']) |
| 1132 |
with azure.open(a, 'wb', delimiter=delimiter, blocksize=6) as f: |
| 1133 |
f.write(b'123' + delimiter) |
| 1134 |
assert f.buffer.tell() == 3 + len(delimiter) |
| 1135 |
f.write(b'456' + delimiter) # causes flush, but will only write till 123 + delimiter |
| 1136 |
assert f.buffer.tell() == 3 + len(delimiter) # buffer will have b'456' + delimiter |
| 1137 |
f.buffer, temp_buffer = io.BytesIO(), f.buffer # Emptry buffer so flush doesn't write any more data |
| 1138 |
f.loc, temp_loc = 3 + len(delimiter), f.loc # Fix location. |
| 1139 |
f.flush(force=True) # To Sync metadata. Force is needed as there is no data in buffer |
| 1140 |
|
| 1141 |
assert azure.cat(a) == b'123' + delimiter |
| 1142 |
f.buffer = temp_buffer |
| 1143 |
f.loc = temp_loc |
| 1144 |
# close causes forced flush |
| 1145 |
f.write(b'789') |
| 1146 |
|
| 1147 |
assert azure.cat(a) == data |
| 1148 |
|
| 1149 |
|
| 1150 |
@my_vcr.use_cassette |
| 1151 |
def test_delimiters_newline(azure): |
| 1152 |
with azure_teardown(azure): |
| 1153 |
write_delimited_data(azure, b'\n') |
| 1154 |
|
| 1155 |
|
| 1156 |
@my_vcr.use_cassette |
| 1157 |
def test_delimiters_dash(azure): |
| 1158 |
with azure_teardown(azure): |
| 1159 |
write_delimited_data(azure, b'--') |
| 1160 |
|
| 1161 |
|
| 1162 |
@my_vcr.use_cassette |
| 1163 |
def test_chmod(azure): |
| 1164 |
pass |
| 1165 |
|
| 1166 |
|
| 1167 |
@my_vcr.use_cassette |
| 1168 |
def test_chown(azure): |
| 1169 |
pass |
| 1170 |
|
| 1171 |
@my_vcr.use_cassette |
| 1172 |
def test_acl_management(azure): |
| 1173 |
pass |
| 1174 |
|
| 1175 |
|
| 1176 |
@my_vcr.use_cassette |
| 1177 |
def test_modify_acl_entries(azure): |
| 1178 |
with azure_teardown(azure): |
| 1179 |
acluser = AZURE_ACL_TEST_APPID |
| 1180 |
azure.touch(a) |
| 1181 |
|
| 1182 |
permission = "---" |
| 1183 |
azure.modify_acl_entries(a, acl_spec="user:"+acluser+":"+permission) |
| 1184 |
current_acl = azure.get_acl_status(a) |
| 1185 |
aclspec = [s for s in current_acl['entries'] if acluser in s][0] |
| 1186 |
assert aclspec.split(':')[-1] == permission |
| 1187 |
|
| 1188 |
permission = "rwx" |
| 1189 |
azure.modify_acl_entries(a, acl_spec="user:" + acluser + ":" + permission) |
| 1190 |
current_acl = azure.get_acl_status(a) |
| 1191 |
aclspec = [s for s in current_acl['entries'] if acluser in s][0] |
| 1192 |
assert aclspec.split(':')[-1] == permission |
| 1193 |
|
| 1194 |
|
| 1195 |
@my_vcr.use_cassette |
| 1196 |
def test_remove_acl_entries(azure): |
| 1197 |
with azure_teardown(azure): |
| 1198 |
acluser = AZURE_ACL_TEST_APPID |
| 1199 |
azure.touch(a) |
| 1200 |
|
| 1201 |
permission = "rwx" |
| 1202 |
azure.modify_acl_entries(a, acl_spec="user:"+acluser+":"+permission) |
| 1203 |
current_acl = azure.get_acl_status(a) |
| 1204 |
aclspec = [s for s in current_acl['entries'] if acluser in s] |
| 1205 |
assert aclspec != [] |
| 1206 |
|
| 1207 |
azure.remove_acl_entries(a, acl_spec="user:" + acluser) |
| 1208 |
current_acl = azure.get_acl_status(a) |
| 1209 |
aclspec = [s for s in current_acl['entries'] if acluser in s] |
| 1210 |
assert aclspec == [] |
| 1211 |
|
| 1212 |
@my_vcr.use_cassette |
| 1213 |
def test_set_acl(azure): |
| 1214 |
with azure_teardown(azure): |
| 1215 |
acluser = AZURE_ACL_TEST_APPID |
| 1216 |
azure.touch(a) |
| 1217 |
set_acl_base ="user::rwx,group::rwx,other::---," |
| 1218 |
|
| 1219 |
permission = "rwx" |
| 1220 |
azure.set_acl(a, acl_spec=set_acl_base + "user:"+acluser+":"+permission) |
| 1221 |
current_acl = azure.get_acl_status(a) |
| 1222 |
aclspec = [s for s in current_acl['entries'] if acluser in s][0] |
| 1223 |
assert len(current_acl['entries']) == 5 |
| 1224 |
assert aclspec.split(':')[-1] == permission |
| 1225 |
|
| 1226 |
@my_vcr.use_cassette |
| 1227 |
def test_set_expiry(azure): |
| 1228 |
with azure_teardown(azure): |
| 1229 |
# this future time gives the milliseconds since the epoch that have occured as of 01/31/2030 at noon |
| 1230 |
epoch_time = datetime.datetime.utcfromtimestamp(0) |
| 1231 |
final_time = datetime.datetime(2030, 1, 31, 12) |
| 1232 |
time_in_milliseconds = (final_time - epoch_time).total_seconds() * 1000 |
| 1233 |
|
| 1234 |
# create the file |
| 1235 |
azure.touch(a) |
| 1236 |
|
| 1237 |
# first get the existing expiry, which should be never |
| 1238 |
initial_expiry = azure.info(a, invalidate_cache=True)['msExpirationTime'] |
| 1239 |
azure.set_expiry(a, 'Absolute', time_in_milliseconds) |
| 1240 |
cur_expiry = azure.info(a, invalidate_cache=True)['msExpirationTime'] |
| 1241 |
# this is a range of +- 100ms because the service does a best effort to set it precisely, but there is |
| 1242 |
# no guarantee that the expiry will be to the exact millisecond |
| 1243 |
assert time_in_milliseconds - 100 <= cur_expiry <= time_in_milliseconds + 100 |
| 1244 |
assert initial_expiry != cur_expiry |
| 1245 |
|
| 1246 |
# now set it back to never expire and validate it is the same |
| 1247 |
azure.set_expiry(a, 'NeverExpire') |
| 1248 |
cur_expiry = azure.info(a)['msExpirationTime'] |
| 1249 |
assert initial_expiry == cur_expiry |
| 1250 |
|
| 1251 |
# now validate the fail cases |
| 1252 |
# bad enum |
| 1253 |
with pytest.raises(ValueError): |
| 1254 |
azure.set_expiry(a, 'BadEnumValue') |
| 1255 |
|
| 1256 |
# missing time |
| 1257 |
with pytest.raises(ValueError): |
| 1258 |
azure.set_expiry(a, 'Absolute') |
| 1259 |
|
| 1260 |
@pytest.mark.skipif(sys.platform != 'win32', reason="requires windows") |
| 1261 |
def test_backslash(): |
| 1262 |
from azure.datalake.store.core import AzureDLPath |
| 1263 |
|
| 1264 |
posix_abspath = '/foo/bar' |
| 1265 |
posix_relpath = 'foo/bar' |
| 1266 |
|
| 1267 |
win_abspath = AzureDLPath('\\foo\\bar') |
| 1268 |
win_relpath = AzureDLPath('foo\\bar') |
| 1269 |
|
| 1270 |
assert posix(win_abspath) == posix_abspath |
| 1271 |
assert posix(win_abspath.trim()) == posix_relpath |
| 1272 |
|
| 1273 |
assert 'foo' in win_abspath |
| 1274 |
assert 'foo' in win_relpath |
| 1275 |
|
| 1276 |
assert posix(AzureDLPath('\\*').globless_prefix) == '/' |
| 1277 |
assert posix(AzureDLPath('\\foo\\*').globless_prefix) == '/foo' |
| 1278 |
assert posix(AzureDLPath('\\foo\\b*').globless_prefix) == '/foo' |
| 1279 |
|
| 1280 |
|
| 1281 |
def test_forward_slash(): |
| 1282 |
from azure.datalake.store.core import AzureDLPath |
| 1283 |
|
| 1284 |
posix_abspath = '/foo/bar' |
| 1285 |
posix_relpath = 'foo/bar' |
| 1286 |
|
| 1287 |
abspath = AzureDLPath('/foo/bar') |
| 1288 |
relpath = AzureDLPath('foo/bar') |
| 1289 |
|
| 1290 |
assert posix(abspath) == posix_abspath |
| 1291 |
assert posix(abspath.trim()) == posix_relpath |
| 1292 |
|
| 1293 |
assert 'foo' in abspath |
| 1294 |
assert 'foo' in relpath |
| 1295 |
|
| 1296 |
assert posix(AzureDLPath('/*').globless_prefix) == '/' |
| 1297 |
assert posix(AzureDLPath('/foo/*').globless_prefix) == '/foo' |
| 1298 |
assert posix(AzureDLPath('/foo/b*').globless_prefix) == '/foo' |
| 1299 |
|
| 1300 |
|
| 1301 |
def test_DatalakeBadOffsetExceptionRecovery(azure): |
| 1302 |
from azure.datalake.store.core import _put_data_with_retry |
| 1303 |
data = b'abc' |
| 1304 |
_put_data_with_retry(azure.azure, 'CREATE', a, data=data) |
| 1305 |
_put_data_with_retry(azure.azure, 'APPEND', a, data=data, offset=len(data)) |
| 1306 |
_put_data_with_retry(azure.azure, 'APPEND', a, data=data, offset=len(data)) |
| 1307 |
assert azure.cat(a) == data*2 |
| 1308 |
_put_data_with_retry(azure.azure, 'APPEND', a, data=data) |
| 1309 |
assert azure.cat(a) == data*3 |
| 1310 |
|
| 1311 |
|
| 1312 |
def test_file_creation_open(azure): |
| 1313 |
with azure_teardown(azure): |
| 1314 |
if azure.exists(a): |
| 1315 |
azure.rm(a) |
| 1316 |
assert not azure.exists(a) |
| 1317 |
f = azure.open(a, "wb") |
| 1318 |
assert azure.exists(a) |
| 1319 |
f.close() |
| 1320 |
assert azure.info(a)['length'] == 0 |
| 1321 |
|