Skip to content

Commit 328cd67

Browse files
author
Marcus Linke
committed
Add test for netty impl
1 parent e80c386 commit 328cd67

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/test/java/com/github/dockerjava/netty/exec/CopyArchiveToContainerCmdExecTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.dockerjava.netty.exec;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
45
import static org.hamcrest.Matchers.isEmptyOrNullString;
56
import static org.hamcrest.Matchers.not;
67

@@ -21,6 +22,7 @@
2122

2223
import com.github.dockerjava.api.command.CreateContainerResponse;
2324
import com.github.dockerjava.api.exception.NotFoundException;
25+
import com.github.dockerjava.core.command.WaitContainerResultCallback;
2426
import com.github.dockerjava.core.util.CompressArchiveUtil;
2527
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
2628

@@ -119,4 +121,38 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{
119121
FileUtils.deleteDirectory(localDir.toFile());
120122
}
121123

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+
122158
}

0 commit comments

Comments
 (0)