def test_upload_file__failure(capfd, config_file): """Ensure the multipart upload is cancelled on error.""" expected_name = os.path.basename(config_file) if "-" in expected_name: expected_name = expected_name[:expected_name.index("-")] config_file = "{}-0.0.1.tar.gz".format(config_file) expected_name = "/".join([ expected_name.replace("_", "-"), os.path.basename(config_file), ]) bucket = mock.Mock() bucket_key = mock.Mock() bucket.get_key = mock.Mock(return_value=bucket_key) mock_multipart = mock.Mock() mock_multipart.get_all_parts = mock.Mock(return_value=[]) bucket.initiate_multipart_upload = mock.Mock(return_value=mock_multipart) s3_config = mock.Mock() file_contents = "some content inside this file..." with open(config_file, "w") as openfile: openfile.write(file_contents) with mock.patch.object(upload, "_upload_chunk") as patched_chunk_uploader: upload.upload_file(config_file, bucket, s3_config) out, err = capfd.readouterr() assert "Uploading {} ...".format(expected_name) in out assert "done!" not in out assert "failed! :(" in out mock_multipart.cancel_upload.assert_called_once_with() patched_chunk_uploader.assert_called_once_with( bucket, mock_multipart.id, # multipart upload id 1, # 5MB chunk number this is config_file, # file to send 0, # start of file len(file_contents), # to the end of file ) bucket.initiate_multipart_upload.assert_called_once_with( expected_name, headers={"Content-Type": "application/octet-stream"} )
def test_upload_file(capfd, config_file): """Verify the calls made to upload a file to S3.""" bucket = mock.Mock() bucket_key = mock.Mock() bucket.get_key = mock.Mock(return_value=bucket_key) mock_multipart = mock.Mock() mock_multipart.get_all_parts = mock.Mock(return_value=["one"]) bucket.initiate_multipart_upload = mock.Mock(return_value=mock_multipart) s3_config = mock.Mock() file_contents = "some content inside this file..." with open(config_file, "w") as openfile: openfile.write(file_contents) with mock.patch.object(upload, "_upload_chunk") as patched_chunk_uploader: upload.upload_file(config_file, bucket, s3_config) out, err = capfd.readouterr() f_name = os.path.basename(config_file) expected_name = "{}/{}".format( f_name[:f_name.index("-")].replace("_", "-"), f_name, ) assert "Uploading {} ...".format(expected_name) in out assert "done!" in out assert "failed!" not in out mock_multipart.complete_upload.assert_called_once_with() bucket.get_key.assert_called_once_with(expected_name) bucket_key.set_acl.assert_called_once_with(s3_config.acl) patched_chunk_uploader.assert_called_once_with( bucket, mock_multipart.id, # multipart upload id 1, # 50MB chunk number this is config_file, # file to send 0, # start of file len(file_contents), # to the end of file ) bucket.initiate_multipart_upload.assert_called_once_with( expected_name, headers={"Content-Type": "application/octet-stream"} )