| 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 |
from __future__ import unicode_literals |
| 10 |
|
| 11 |
from contextlib import contextmanager |
| 12 |
import os |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from samples.cli import AzureDataLakeFSCommand |
| 17 |
from azure.datalake.store.exceptions import PermissionError |
| 18 |
from tests.testing import azure, my_vcr, working_dir |
| 19 |
|
| 20 |
@pytest.fixture() |
| 21 |
def client(azure): |
| 22 |
yield AzureDataLakeFSCommand(azure) |
| 23 |
|
| 24 |
|
| 25 |
@contextmanager |
| 26 |
def setup_dir(azure): |
| 27 |
d = str(working_dir() / 'foo') |
| 28 |
azure.mkdir(d) |
| 29 |
try: |
| 30 |
yield d |
| 31 |
finally: |
| 32 |
azure.rm(d, recursive=True) |
| 33 |
|
| 34 |
|
| 35 |
@contextmanager |
| 36 |
def setup_file(azure): |
| 37 |
tmp = str(working_dir() / 'foo' / 'bar') |
| 38 |
with azure.open(tmp, 'wb') as f: |
| 39 |
f.write('123456'.encode()) |
| 40 |
try: |
| 41 |
yield tmp |
| 42 |
finally: |
| 43 |
azure.rm(tmp) |
| 44 |
|
| 45 |
|
| 46 |
def read_stdout(captured): |
| 47 |
out, _ = captured.readouterr() |
| 48 |
return out |
| 49 |
|
| 50 |
|
| 51 |
@my_vcr.use_cassette |
| 52 |
def test_cat(capsys, azure, client): |
| 53 |
with setup_file(azure) as azurefile: |
| 54 |
client.onecmd('cat ' + azurefile) |
| 55 |
assert read_stdout(capsys) == '123456' |
| 56 |
|
| 57 |
|
| 58 |
@my_vcr.use_cassette |
| 59 |
def test_chgrp(capsys, azure, client): |
| 60 |
pass |
| 61 |
|
| 62 |
@my_vcr.use_cassette |
| 63 |
def test_chmod(capsys, azure, client): |
| 64 |
with setup_file(azure) as azurefile: |
| 65 |
client.onecmd('info ' + azurefile) |
| 66 |
assert 'permission = 770' in read_stdout(capsys) |
| 67 |
|
| 68 |
client.onecmd('chmod 0550 ' + azurefile) |
| 69 |
assert not read_stdout(capsys) |
| 70 |
|
| 71 |
client.onecmd('info ' + azurefile) |
| 72 |
assert 'permission = 550' in read_stdout(capsys) |
| 73 |
|
| 74 |
@my_vcr.use_cassette |
| 75 |
def test_df(capsys, azure, client): |
| 76 |
with setup_file(azure) as _: |
| 77 |
client.onecmd('df ' + str(working_dir())) |
| 78 |
out = read_stdout(capsys) |
| 79 |
assert len(out.strip().split('\n')) == 6 |
| 80 |
assert 'quota' in out |
| 81 |
|
| 82 |
|
| 83 |
@my_vcr.use_cassette |
| 84 |
def test_du(capsys, azure, client): |
| 85 |
with setup_file(azure) as azurefile: |
| 86 |
client.onecmd('du ' + azurefile) |
| 87 |
out = read_stdout(capsys) |
| 88 |
assert len(out.strip().split('\n')) == 1 |
| 89 |
|
| 90 |
|
| 91 |
@my_vcr.use_cassette |
| 92 |
def test_exists(capsys, azure, client): |
| 93 |
with setup_file(azure) as azurefile: |
| 94 |
client.onecmd('exists ' + azurefile) |
| 95 |
assert read_stdout(capsys) == 'True\n' |
| 96 |
|
| 97 |
|
| 98 |
@my_vcr.use_cassette |
| 99 |
def test_get(tmpdir, azure, client): |
| 100 |
with setup_file(azure) as azurefile: |
| 101 |
f = os.path.basename(azurefile) |
| 102 |
localfile = tmpdir.dirname + '/' + f |
| 103 |
|
| 104 |
client.onecmd(' '.join(['get', '-f', azurefile, tmpdir.dirname])) |
| 105 |
|
| 106 |
assert os.path.exists(localfile) |
| 107 |
|
| 108 |
with open(localfile, 'rb') as lf: |
| 109 |
content = lf.read() |
| 110 |
assert content == b'123456' |
| 111 |
|
| 112 |
|
| 113 |
@my_vcr.use_cassette |
| 114 |
def test_head(capsys, azure, client): |
| 115 |
with setup_file(azure) as azurefile: |
| 116 |
client.onecmd('head ' + azurefile) |
| 117 |
assert read_stdout(capsys) == '123456' |
| 118 |
|
| 119 |
|
| 120 |
@my_vcr.use_cassette |
| 121 |
def test_head_bytes(capsys, azure, client): |
| 122 |
with setup_file(azure) as azurefile: |
| 123 |
client.onecmd('head -c 3 ' + azurefile) |
| 124 |
assert read_stdout(capsys) == '123' |
| 125 |
|
| 126 |
|
| 127 |
@my_vcr.use_cassette |
| 128 |
def test_info(capsys, azure, client): |
| 129 |
with setup_file(azure) as azurefile: |
| 130 |
client.onecmd('info ' + azurefile) |
| 131 |
out = read_stdout(capsys) |
| 132 |
assert len(out.strip().split('\n')) == 13 |
| 133 |
assert 'modificationTime' in out |
| 134 |
|
| 135 |
|
| 136 |
@my_vcr.use_cassette |
| 137 |
def test_ls(capsys, azure, client): |
| 138 |
with setup_file(azure) as azurefile: |
| 139 |
d = os.path.dirname(azurefile) |
| 140 |
f = os.path.basename(azurefile) |
| 141 |
|
| 142 |
client.onecmd('ls ' + d) |
| 143 |
assert read_stdout(capsys) == f + '\n' |
| 144 |
|
| 145 |
|
| 146 |
@my_vcr.use_cassette |
| 147 |
def test_ls_detailed(capsys, azure, client): |
| 148 |
with setup_file(azure) as azurefile: |
| 149 |
d = os.path.dirname(azurefile) |
| 150 |
f = os.path.basename(azurefile) |
| 151 |
|
| 152 |
client.onecmd('ls -l ' + d) |
| 153 |
out = read_stdout(capsys) |
| 154 |
assert len(out.strip().split('\n')) == 1 |
| 155 |
assert f in out |
| 156 |
|
| 157 |
|
| 158 |
@my_vcr.use_cassette |
| 159 |
def test_mkdir_and_rmdir(capsys, azure, client): |
| 160 |
with setup_dir(azure) as azuredir: |
| 161 |
d = azuredir + '/foo' |
| 162 |
|
| 163 |
client.onecmd('mkdir ' + d) |
| 164 |
assert not read_stdout(capsys) |
| 165 |
|
| 166 |
client.onecmd('info ' + d) |
| 167 |
assert 'DIRECTORY' in read_stdout(capsys) |
| 168 |
|
| 169 |
client.onecmd('rmdir ' + d) |
| 170 |
assert not read_stdout(capsys) |
| 171 |
|
| 172 |
client.onecmd('exists ' + d) |
| 173 |
assert read_stdout(capsys) == 'False\n' |
| 174 |
|
| 175 |
|
| 176 |
@my_vcr.use_cassette |
| 177 |
def test_mv(capsys, azure, client): |
| 178 |
with setup_file(azure) as azurefile: |
| 179 |
f = os.path.dirname(azurefile) + '/foo' |
| 180 |
|
| 181 |
client.onecmd(' '.join(['mv', azurefile, f])) |
| 182 |
assert not read_stdout(capsys) |
| 183 |
|
| 184 |
client.onecmd('exists ' + azurefile) |
| 185 |
assert read_stdout(capsys) == 'False\n' |
| 186 |
|
| 187 |
client.onecmd(' '.join(['mv', f, azurefile])) |
| 188 |
assert not read_stdout(capsys) |
| 189 |
|
| 190 |
client.onecmd('exists ' + azurefile) |
| 191 |
assert read_stdout(capsys) == 'True\n' |
| 192 |
|
| 193 |
|
| 194 |
@my_vcr.use_cassette |
| 195 |
def test_put(capsys, tmpdir, azure, client): |
| 196 |
localfile = tmpdir.dirname + '/foo' |
| 197 |
|
| 198 |
with open(localfile, 'wb') as lf: |
| 199 |
lf.write(b'123456') |
| 200 |
|
| 201 |
with setup_dir(azure) as azuredir: |
| 202 |
client.onecmd(' '.join(['put', '-f', localfile, azuredir + '/foo'])) |
| 203 |
|
| 204 |
client.onecmd('head ' + azuredir + '/foo') |
| 205 |
assert read_stdout(capsys).endswith('123456') |
| 206 |
|
| 207 |
client.onecmd('rm ' + azuredir + '/foo') |
| 208 |
assert not read_stdout(capsys) |
| 209 |
|
| 210 |
|
| 211 |
@my_vcr.use_cassette |
| 212 |
def test_tail(capsys, azure, client): |
| 213 |
with setup_file(azure) as azurefile: |
| 214 |
client.onecmd('tail ' + azurefile) |
| 215 |
assert read_stdout(capsys) == '123456' |
| 216 |
|
| 217 |
|
| 218 |
@my_vcr.use_cassette |
| 219 |
def test_tail_bytes(capsys, azure, client): |
| 220 |
with setup_file(azure) as azurefile: |
| 221 |
client.onecmd('tail -c 3 ' + azurefile) |
| 222 |
assert read_stdout(capsys) == '456' |
| 223 |
|
| 224 |
|
| 225 |
@my_vcr.use_cassette |
| 226 |
def test_touch_and_rm(capsys, azure, client): |
| 227 |
with setup_dir(azure) as azuredir: |
| 228 |
f = azuredir + '/foo' |
| 229 |
|
| 230 |
client.onecmd('touch ' + f) |
| 231 |
assert not read_stdout(capsys) |
| 232 |
|
| 233 |
client.onecmd('exists ' + f) |
| 234 |
assert read_stdout(capsys) == 'True\n' |
| 235 |
|
| 236 |
client.onecmd('rm ' + f) |
| 237 |
assert not read_stdout(capsys) |
| 238 |
|
| 239 |
client.onecmd('exists ' + f) |
| 240 |
assert read_stdout(capsys) == 'False\n' |