Skip to content

Commit cc93a1f

Browse files
committed
Added Test Case for CopyArchiveToContainerCmdImpl when copying files with execute permission.
Test Case that simulates a scenario where a script file with permission to execute is copied to the containers with CopyArchiveToContainerCmdImpl and then it is executed.
1 parent e229005 commit cc93a1f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.isEmptyOrNullString;
55
import static org.hamcrest.Matchers.not;
6+
import static org.hamcrest.Matchers.equalTo;
67

78
import java.io.IOException;
89
import java.io.InputStream;
@@ -117,5 +118,39 @@ public void copyDirWithLastAddedTarEnryEmptyDir() throws Exception{
117118
.withHostResource(localDir.toString())
118119
.exec();
119120
}
121+
122+
@Test
123+
public void copyFileWithExecutePermission() throws Exception {
124+
// create script file, add permission to execute
125+
scriptPath = Files.createTempFile("run", ".sh");
126+
boolean executable = scriptPath.toFile().setExecutable(true, false);
127+
if (!executable){
128+
throw new Exception("Execute permission on file not set!");
129+
}
130+
String snippet = "Running script with execute permission.";
131+
String scriptTextStr = "#!/bin/sh\necho \"" + snippet + "\"";
132+
// write content for created script
133+
Files.write(scriptPath, scriptTextStr.getBytes());
134+
// create a test container which starts and waits 3 seconds for the
135+
// script to be copied to the container's home dir and then executes it
136+
String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString();
137+
CreateContainerResponse container = docker.createContainerCmd("busybox")
138+
.withName("test")
139+
.withCmd("/bin/sh", "-c", containerCmd)
140+
.exec();
141+
// start the container
142+
docker.startContainerCmd(container.getId()).exec();
143+
// copy script to container home dir
144+
docker.copyArchiveToContainerCmd(container.getId())
145+
.withRemotePath("/home")
146+
.withHostResource(scriptPath.toString())
147+
.exec();
148+
// await exid code
149+
int exitCode = docker.waitContainerCmd(container.getId())
150+
.exec(new WaitContainerResultCallback())
151+
.awaitStatusCode();
152+
// check result
153+
assertThat(exitCode, equalTo(0));
154+
}
120155

121156
}

0 commit comments

Comments
 (0)