|
1 | 1 | package com.github.dockerjava.netty.exec; |
2 | 2 |
|
3 | 3 | import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
4 | 5 | import static org.hamcrest.Matchers.isEmptyOrNullString; |
5 | 6 | import static org.hamcrest.Matchers.not; |
6 | 7 |
|
|
21 | 22 |
|
22 | 23 | import com.github.dockerjava.api.command.CreateContainerResponse; |
23 | 24 | import com.github.dockerjava.api.exception.NotFoundException; |
| 25 | +import com.github.dockerjava.core.command.WaitContainerResultCallback; |
24 | 26 | import com.github.dockerjava.core.util.CompressArchiveUtil; |
25 | 27 | import com.github.dockerjava.netty.AbstractNettyDockerClientTest; |
26 | 28 |
|
@@ -119,4 +121,38 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{ |
119 | 121 | FileUtils.deleteDirectory(localDir.toFile()); |
120 | 122 | } |
121 | 123 |
|
| 124 | + @Test |
| 125 | + public void copyFileWithExecutePermission() throws Exception { |
| 126 | + // create script file, add permission to execute |
| 127 | + Path scriptPath = Files.createTempFile("run", ".sh"); |
| 128 | + boolean executable = scriptPath.toFile().setExecutable(true, false); |
| 129 | + if (!executable){ |
| 130 | + throw new Exception("Execute permission on file not set!"); |
| 131 | + } |
| 132 | + String snippet = "Running script with execute permission."; |
| 133 | + String scriptTextStr = "#!/bin/sh\necho \"" + snippet + "\""; |
| 134 | + // write content for created script |
| 135 | + Files.write(scriptPath, scriptTextStr.getBytes()); |
| 136 | + // create a test container which starts and waits 3 seconds for the |
| 137 | + // script to be copied to the container's home dir and then executes it |
| 138 | + String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString(); |
| 139 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") |
| 140 | + .withName("test") |
| 141 | + .withCmd("/bin/sh", "-c", containerCmd) |
| 142 | + .exec(); |
| 143 | + // start the container |
| 144 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 145 | + // copy script to container home dir |
| 146 | + dockerClient.copyArchiveToContainerCmd(container.getId()) |
| 147 | + .withRemotePath("/home") |
| 148 | + .withHostResource(scriptPath.toString()) |
| 149 | + .exec(); |
| 150 | + // await exid code |
| 151 | + int exitCode = dockerClient.waitContainerCmd(container.getId()) |
| 152 | + .exec(new WaitContainerResultCallback()) |
| 153 | + .awaitStatusCode(); |
| 154 | + // check result |
| 155 | + assertThat(exitCode, equalTo(0)); |
| 156 | + } |
| 157 | + |
122 | 158 | } |
0 commit comments