Skip to content

Commit

Permalink
#2871: Change longFileMode to LONGFILE_POSIX for creating tar in PodU…
Browse files Browse the repository at this point in the history
…pload, improve exception handling in PodUpload.
  • Loading branch information
msinger-dev authored and manusa committed Mar 9, 2021
1 parent f478183 commit e9526e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix #2857: Fix the log of an unexpected error from an Informer's EventHandler
* Fix #2853: Cannot change the type of the Service from ClusterIP to ExternalName with PATCH
* Fix #2783: OpenIDConnectionUtils#persistKubeConfigWithUpdatedToken persists access token instead of refresh token
* Fix #2871: Change longFileMode to LONGFILE_POSIX for creating tar in PodUpload, improve exception handling in PodUpload.

#### Improvements
* Fix #2781: RawCustomResourceOperationsImpl#delete now returns a boolean value for deletion status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.TimeoutException;
import java.util.function.ObjIntConsumer;
import java.util.zip.GZIPOutputStream;

Expand Down Expand Up @@ -104,31 +106,34 @@ private static boolean uploadDirectory(OkHttpClient client, PodOperationContext
final GZIPOutputStream gzip = new GZIPOutputStream(b64Out)

) {
final CountDownLatch done = new CountDownLatch(1);
final AtomicReference<IOException> readFileException = new AtomicReference<>(null);
podUploadWebSocketListener.waitUntilReady(DEFAULT_CONNECTION_TIMEOUT_SECONDS);
final Runnable readFiles = () -> {
final Callable<?> readFiles = () -> {
try (final TarArchiveOutputStream tar = new TarArchiveOutputStream(gzip)) {
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
for (File file : pathToUpload.toFile().listFiles()) {
addFileToTar(null, file, tar);
}
tar.flush();
} catch (IOException ex) {
readFileException.set(ex);
} finally {
done.countDown();
}
return null;
};
final ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(readFiles);
Future<?> readFilesFuture = es.submit(readFiles);
copy(pis, podUploadWebSocketListener::send);
podUploadWebSocketListener.waitUntilComplete(DEFAULT_COMPLETE_REQUEST_TIMEOUT_SECONDS);
final boolean doneGracefully = done.await(100, TimeUnit.SECONDS);
es.shutdown();
if (readFileException.get() != null) {
throw readFileException.get();
try {
readFilesFuture.get(100, TimeUnit.SECONDS);
return true;
} catch (ExecutionException ex) {
if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
}
throw new IOException(ex.getMessage(), ex.getCause());
} catch (TimeoutException e) {
return false;
} finally {
es.shutdown();
}
return doneGracefully;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,18 @@ void testUploadFileHappyScenarioShouldUploadFile() throws Exception {

@Test
void testUploadDirectoryHappyScenarioShouldUploadDirectory() throws Exception {
uploadDirectoryAndVerify("/upload");
}

@Test
void testUploadDirectoryLongFileNameShouldUploadDirectory() throws Exception {
uploadDirectoryAndVerify("/upload_long");
}

private void uploadDirectoryAndVerify(String resourcePath) throws IOException, InterruptedException {
when(mockContext.getDir()).thenReturn("/mock/dir");
when(mockPathToUpload.toFile())
.thenReturn(new File(PodUpload.class.getResource("/upload").getFile()));
.thenReturn(new File(PodUpload.class.getResource(resourcePath).getFile()));
when(mockClient.newWebSocket(any(), any())).thenAnswer(newWebSocket -> {
final PodUploadWebSocketListener wsl = newWebSocket.getArgument(1, PodUploadWebSocketListener.class);
// Set ready status
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
====
Copyright (C) 2015 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====

This is a file to test uploads

0 comments on commit e9526e0

Please sign in to comment.