Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests for #1646 (permission issues on local files) #1648

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions awscli/customizations/s3/s3handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ def _enqueue_range_download_tasks(self, filename, remove_remote_file=False):
chunksize = find_chunksize(filename.size, self.chunksize)
num_downloads = int(filename.size / chunksize)
context = tasks.MultipartDownloadContext(num_downloads)
create_file_task = tasks.CreateLocalFileTask(context=context,
filename=filename)
create_file_task = tasks.CreateLocalFileTask(
context=context, filename=filename,
result_queue=self.result_queue)
self.executor.submit(create_file_task)
self._do_enqueue_range_download_tasks(
filename=filename, chunksize=chunksize,
Expand Down
8 changes: 7 additions & 1 deletion awscli/customizations/s3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ def __call__(self):


class CreateLocalFileTask(OrderableTask):
def __init__(self, context, filename):
def __init__(self, context, filename, result_queue):
self._context = context
self._filename = filename
self._result_queue = result_queue

def __call__(self):
dirname = os.path.dirname(self._filename.dest)
Expand All @@ -284,6 +285,11 @@ def __call__(self):
with open(self._filename.dest, 'wb'):
pass
except Exception as e:
message = print_operation(self._filename, failed=True,
dryrun=False)
message += '\n' + str(e)
result = {'message': message, 'error': True}
self._result_queue.put(PrintTask(**result))
self._context.cancel()
else:
self._context.announce_file_created()
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/customizations/s3/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,38 @@ def test_is_special_file(self):
"socket." % file_path), p.stderr)


class TestUnableToWriteToFile(BaseS3CLICommand):

def extra_setup(self):
self.bucket_name = self.create_bucket()

@skip_if_windows('Write permissions tests only supported on mac/linux')
def test_no_write_access_small_file(self):
os.chmod(self.files.rootdir, 0o444)
self.put_object(self.bucket_name, 'foo.txt',
contents='Hello world')
p = aws('s3 cp s3://%s/foo.txt %s' % (
self.bucket_name, os.path.join(self.files.rootdir, 'foo.txt')))
self.assertEqual(p.rc, 1)
self.assertIn('download failed', p.stderr)

@skip_if_windows('Write permissions tests only supported on mac/linux')
def test_no_write_access_large_file(self):
# We have to use a file like object because using a string
# would result in the header + body sent as a single packet
# which effectively disables the expect 100 continue logic.
# This will result in a test error because we won't follow
# the temporary redirect for the newly created bucket.
contents = six.StringIO('a' * 10 * 1024 * 1024)
self.put_object(self.bucket_name, 'foo.txt',
contents=contents)
os.chmod(self.files.rootdir, 0o444)
p = aws('s3 cp s3://%s/foo.txt %s' % (
self.bucket_name, os.path.join(self.files.rootdir, 'foo.txt')))
self.assertEqual(p.rc, 1)
self.assertIn('download failed', p.stderr)


@skip_if_windows('Symlink tests only supported on mac/linux')
class TestSymlinks(BaseS3CLICommand):
"""
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/customizations/s3/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import threading
import mock
import socket
import os
import tempfile
import shutil
from six.moves import queue

from botocore.exceptions import IncompleteReadError
from botocore.vendored.requests.packages.urllib3.exceptions import \
Expand All @@ -31,6 +35,7 @@
from awscli.customizations.s3.tasks import RetriesExeededError
from awscli.customizations.s3.executor import ShutdownThreadRequest
from awscli.customizations.s3.utils import StablePriorityQueue
from awscli.testutils import skip_if_windows


class TestMultipartUploadContext(unittest.TestCase):
Expand Down Expand Up @@ -326,6 +331,40 @@ def test_print_operation(self):
self.assertIn(r'e:\foo', message)


class TestCreateLocalFileTask(unittest.TestCase):
def setUp(self):
self.result_queue = queue.Queue()
self.tempdir = tempfile.mkdtemp()
self.filename = mock.Mock()
self.filename.src = 'bucket/key'
self.filename.dest = os.path.join(self.tempdir, 'local', 'file')
self.filename.operation_name = 'download'
self.context = mock.Mock()
self.task = CreateLocalFileTask(self.context,
self.filename,
self.result_queue)

def tearDown(self):
shutil.rmtree(self.tempdir)

def test_creates_file_and_announces(self):
self.task()
self.assertTrue(os.path.isfile(self.filename.dest))
self.context.announce_file_created.assert_called_with()
self.assertTrue(self.result_queue.empty())

@skip_if_windows('Write permissions tests only supported on mac/linux')
def test_cancel_command_on_exception(self):
# Set destination directory to read-only
os.chmod(self.tempdir, 0o444)
self.task()
self.assertFalse(os.path.isfile(self.filename.dest))
self.context.cancel.assert_called_with()
self.assertFalse(self.result_queue.empty())
error_message = self.result_queue.get()
self.assertIn("download failed", error_message.message)


class TestDownloadPartTask(unittest.TestCase):
def setUp(self):
self.result_queue = mock.Mock()
Expand Down Expand Up @@ -495,7 +534,7 @@ def setUp(self):
def create_task(self):
# We don't actually care about the arguments, we just want to test
# the ordering of the tasks.
return CreateLocalFileTask(None, None)
return CreateLocalFileTask(None, None, None)

def complete_task(self):
return CompleteDownloadTask(None, None, None, None, None)
Expand Down