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

Fix #3568: Pod upload file fails if file is directly in root path #3719

Merged
merged 1 commit into from Jan 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
* Fix #3588: `openshift-server-mock` is not listed in dependencyManagement in main pom
* Fix #3679: output additionalProperties field with correct value type for map-like fields (CRD Generator)
* Fix #3648: `Serialization.unmarshal` fails to deserialize YAML with single document in presence of document delimiter(`---`)
* Fix #3568: Pod file upload fails if the path is `/`

#### Improvements
* Fix #3674: allows the connect and websocket timeouts to apply to watches instead of a hardcoded timeout
Expand Down
Expand Up @@ -73,13 +73,8 @@ public static boolean upload(HttpClient client, PodOperationContext context,
private static boolean uploadFile(HttpClient client, PodOperationContext context,
OperationSupport operationSupport, Path pathToUpload)
throws IOException, InterruptedException {

final String file = context.getFile();
final String directory = file.substring(0, file.lastIndexOf('/'));
final String command = String.format(
"mkdir -p %s && base64 -d - > %s", shellQuote(directory), shellQuote(file));
final PodUploadWebSocketListener podUploadWebSocketListener = initWebSocket(
buildCommandUrl(command, context, operationSupport), client);
buildCommandUrl(createExecCommandForUpload(context), context, operationSupport), client);
try (
final FileInputStream fis = new FileInputStream(pathToUpload.toFile());
final Base64.InputStream b64In = new Base64.InputStream(fis, Base64.ENCODE)
Expand Down Expand Up @@ -193,4 +188,12 @@ private static URL buildCommandUrl(String command, PodOperationContext context,
return new URL(
URLUtils.join(operationSupport.getResourceUrl().toString(), commandBuilder.toString()));
}

static String createExecCommandForUpload(PodOperationContext context) {
final String file = context.getFile();
String directoryTrimmedFromFilePath = file.substring(0, file.lastIndexOf('/'));
final String directory = directoryTrimmedFromFilePath.isEmpty() ? "/" : directoryTrimmedFromFilePath;
return String.format(
"mkdir -p %s && base64 -d - > %s", shellQuote(directory), shellQuote(file));
}
}
Expand Up @@ -80,7 +80,7 @@ void tearDown() {
}

@Test
void testUploadInvalidParametersShouldThrowException() throws Exception {
void testUploadInvalidParametersShouldThrowException() {
final IllegalArgumentException result = assertThrows(IllegalArgumentException.class,
() -> PodUpload.upload(mockClient, mockContext, operationSupport, mockPathToUpload));

Expand Down Expand Up @@ -171,4 +171,28 @@ void testCopy() throws Exception {
PodUpload.copy(input, consumer);
}

@Test
void createExecCommandForUpload_withFileInRootPath_shouldCreateValidExecCommandForUpload() {
// Given
when(mockContext.getFile()).thenReturn("/cp.log");

// When
String result = PodUpload.createExecCommandForUpload(mockContext);

// Then
assertThat(result, equalTo("mkdir -p '/' && base64 -d - > '/cp.log'"));
}

@Test
void createExecCommandForUpload_withNormalFile_shouldCreateValidExecCommandForUpload() {
// Given
when(mockContext.getFile()).thenReturn("/tmp/foo/cp.log");

// When
String result = PodUpload.createExecCommandForUpload(mockContext);

// Then
assertThat(result, equalTo("mkdir -p '/tmp/foo' && base64 -d - > '/tmp/foo/cp.log'"));
}

}