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
|
# -*- coding: utf-8 -*-
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from __future__ import unicode_literals
from contextlib import contextmanager
import os
import pytest
from samples.cli import AzureDataLakeFSCommand
from azure.datalake.store.exceptions import PermissionError
from tests.testing import azure, my_vcr, working_dir
@pytest.fixture()
def client(azure):
yield AzureDataLakeFSCommand(azure)
@contextmanager
def setup_dir(azure):
d = str(working_dir() / 'foo')
azure.mkdir(d)
try:
yield d
finally:
azure.rm(d, recursive=True)
@contextmanager
def setup_file(azure):
tmp = str(working_dir() / 'foo' / 'bar')
with azure.open(tmp, 'wb') as f:
f.write('123456'.encode())
try:
yield tmp
finally:
azure.rm(tmp)
def read_stdout(captured):
out, _ = captured.readouterr()
return out
@my_vcr.use_cassette
def test_cat(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('cat ' + azurefile)
assert read_stdout(capsys) == '123456'
@my_vcr.use_cassette
def test_chgrp(capsys, azure, client):
pass
@my_vcr.use_cassette
def test_chmod(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('info ' + azurefile)
assert 'permission = 770' in read_stdout(capsys)
client.onecmd('chmod 0550 ' + azurefile)
assert not read_stdout(capsys)
client.onecmd('info ' + azurefile)
assert 'permission = 550' in read_stdout(capsys)
@my_vcr.use_cassette
def test_df(capsys, azure, client):
with setup_file(azure) as _:
client.onecmd('df ' + str(working_dir()))
out = read_stdout(capsys)
assert len(out.strip().split('\n')) == 6
assert 'quota' in out
@my_vcr.use_cassette
def test_du(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('du ' + azurefile)
out = read_stdout(capsys)
assert len(out.strip().split('\n')) == 1
@my_vcr.use_cassette
def test_exists(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('exists ' + azurefile)
assert read_stdout(capsys) == 'True\n'
@my_vcr.use_cassette
def test_get(tmpdir, azure, client):
with setup_file(azure) as azurefile:
f = os.path.basename(azurefile)
localfile = tmpdir.dirname + '/' + f
client.onecmd(' '.join(['get', '-f', azurefile, tmpdir.dirname]))
assert os.path.exists(localfile)
with open(localfile, 'rb') as lf:
content = lf.read()
assert content == b'123456'
@my_vcr.use_cassette
def test_head(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('head ' + azurefile)
assert read_stdout(capsys) == '123456'
@my_vcr.use_cassette
def test_head_bytes(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('head -c 3 ' + azurefile)
assert read_stdout(capsys) == '123'
@my_vcr.use_cassette
def test_info(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('info ' + azurefile)
out = read_stdout(capsys)
assert len(out.strip().split('\n')) == 13
assert 'modificationTime' in out
@my_vcr.use_cassette
def test_ls(capsys, azure, client):
with setup_file(azure) as azurefile:
d = os.path.dirname(azurefile)
f = os.path.basename(azurefile)
client.onecmd('ls ' + d)
assert read_stdout(capsys) == f + '\n'
@my_vcr.use_cassette
def test_ls_detailed(capsys, azure, client):
with setup_file(azure) as azurefile:
d = os.path.dirname(azurefile)
f = os.path.basename(azurefile)
client.onecmd('ls -l ' + d)
out = read_stdout(capsys)
assert len(out.strip().split('\n')) == 1
assert f in out
@my_vcr.use_cassette
def test_mkdir_and_rmdir(capsys, azure, client):
with setup_dir(azure) as azuredir:
d = azuredir + '/foo'
client.onecmd('mkdir ' + d)
assert not read_stdout(capsys)
client.onecmd('info ' + d)
assert 'DIRECTORY' in read_stdout(capsys)
client.onecmd('rmdir ' + d)
assert not read_stdout(capsys)
client.onecmd('exists ' + d)
assert read_stdout(capsys) == 'False\n'
@my_vcr.use_cassette
def test_mv(capsys, azure, client):
with setup_file(azure) as azurefile:
f = os.path.dirname(azurefile) + '/foo'
client.onecmd(' '.join(['mv', azurefile, f]))
assert not read_stdout(capsys)
client.onecmd('exists ' + azurefile)
assert read_stdout(capsys) == 'False\n'
client.onecmd(' '.join(['mv', f, azurefile]))
assert not read_stdout(capsys)
client.onecmd('exists ' + azurefile)
assert read_stdout(capsys) == 'True\n'
@my_vcr.use_cassette
def test_put(capsys, tmpdir, azure, client):
localfile = tmpdir.dirname + '/foo'
with open(localfile, 'wb') as lf:
lf.write(b'123456')
with setup_dir(azure) as azuredir:
client.onecmd(' '.join(['put', '-f', localfile, azuredir + '/foo']))
client.onecmd('head ' + azuredir + '/foo')
assert read_stdout(capsys).endswith('123456')
client.onecmd('rm ' + azuredir + '/foo')
assert not read_stdout(capsys)
@my_vcr.use_cassette
def test_tail(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('tail ' + azurefile)
assert read_stdout(capsys) == '123456'
@my_vcr.use_cassette
def test_tail_bytes(capsys, azure, client):
with setup_file(azure) as azurefile:
client.onecmd('tail -c 3 ' + azurefile)
assert read_stdout(capsys) == '456'
@my_vcr.use_cassette
def test_touch_and_rm(capsys, azure, client):
with setup_dir(azure) as azuredir:
f = azuredir + '/foo'
client.onecmd('touch ' + f)
assert not read_stdout(capsys)
client.onecmd('exists ' + f)
assert read_stdout(capsys) == 'True\n'
client.onecmd('rm ' + f)
assert not read_stdout(capsys)
client.onecmd('exists ' + f)
assert read_stdout(capsys) == 'False\n'
|