Skip to content

Commit

Permalink
Feat: Automatically create parent directories of portPropertyFile path
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Villena <[email protected]>
  • Loading branch information
Willena committed Mar 2, 2024
1 parent 110a8f8 commit b67c433
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/fabric8/maven/docker/access/PortMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ public void write() throws IOException {

private void writeProperties(Properties props, String file) throws IOException {
File propFile = new File(file);
File parent = propFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (OutputStream os = new FileOutputStream(propFile)) {
props.store(os, "Docker ports");
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -84,6 +85,37 @@ void testWriteImageAndGlobal() throws IOException {
thenPropsContains("other.ip1", "1.2.3.4");
}

@Test
void testWriteCreatePaths() throws IOException {
File dir = Files.createTempDirectory("dmp-").toFile();
Assertions.assertTrue(dir.delete());
String globalFile = Paths.get(dir.getAbsolutePath(), "dmp-tmp.properties").toFile().getAbsolutePath();
PortMapping mapping = createPortMapping("jolokia.port:8080", "18181:8181", "127.0.0.1:9090:9090", "127.0.0.1:other.port:5678");

// check that we can write on non-existant path with create set to "true"
givenAPortMappingWriter(globalFile);
whenUpdateDynamicMapping(mapping, "0.0.0.0", 8080, 49900);
whenUpdateDynamicMapping(mapping, "127.0.0.1", 5678, 49901);
whenWritePortMappings(null, mapping);
thenPropsFileExists(globalFile);
thenPropsSizeIs(2);
thenPropsContains("jolokia.port", 49900);
thenPropsContains("other.port", 49901);

// Check that we can still write in an existing path
String globalFile2 = Paths.get(dir.getAbsolutePath(), "dmp-tmp2.properties").toFile().getAbsolutePath();
givenAPortMappingWriter(globalFile2);
whenUpdateDynamicMapping(mapping, "0.0.0.0", 8080, 49900);
whenUpdateDynamicMapping(mapping, "127.0.0.1", 5678, 49901);
whenWritePortMappings(null, mapping);
thenPropsFileExists(globalFile2);
thenPropsSizeIs(2);
thenPropsContains("jolokia.port", 49900);
thenPropsContains("other.port", 49901);


}

private void givenADockerHostAddress(String host) {
projProperties.setProperty("docker.host.address", host);
}
Expand Down

0 comments on commit b67c433

Please sign in to comment.