Skip to content

Commit

Permalink
Fix fabric8io#3568: Pod upload file fails if file is directly in root…
Browse files Browse the repository at this point in the history
… path

Pod file upload seems be be creating invalid exec command for upload if
the file is directly in root path e.g. `/cp.log`. We create parent
directory of file by creating a substring based on last index of `/`.
This returns an empty string for a string like `/cp.log`.

Assume directory path to be `/` when directory is returned as empty string.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 10, 2022
1 parent 173fa34 commit f08cad0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Fix #3712: properly return the full resource name for resources with empty group
* 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 #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
* Fix #3651: Introduce SchemaFrom annotation as escape hatch (CRD Generator)
Expand Down
Original file line number Diff line number Diff line change
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));
}
}
Original file line number Diff line number Diff line change
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'"));
}

}

0 comments on commit f08cad0

Please sign in to comment.