Skip to content

Commit c60a21f

Browse files
cleanup the DockerServerCredentialsBinding test
Cleanup the DockerServerCredentialsBinding test. Moved some multi-lines strings into separate files, because it was difficult to read.
1 parent 6e992a4 commit c60a21f

File tree

7 files changed

+93
-77
lines changed

7 files changed

+93
-77
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.bat eol=crlf

src/test/java/org/jenkinsci/plugins/docker/commons/credentials/DockerServerCredentialsBindingTest.java

Lines changed: 26 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
package org.jenkinsci.plugins.docker.commons.credentials;
2626

27+
import java.io.IOException;
28+
import java.io.InputStream;
2729
import java.util.Collections;
2830

31+
import org.apache.commons.io.IOUtils;
2932
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
3033
import org.jenkinsci.plugins.credentialsbinding.impl.BindingStep;
31-
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
32-
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerDomainSpecification;
3334
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
3435
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
3536
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -42,15 +43,15 @@
4243
import org.jvnet.hudson.test.BuildWatcher;
4344
import org.jvnet.hudson.test.RestartableJenkinsRule;
4445

45-
import hudson.Functions;
46-
4746
import com.cloudbees.plugins.credentials.CredentialsProvider;
4847
import com.cloudbees.plugins.credentials.CredentialsScope;
4948
import com.cloudbees.plugins.credentials.CredentialsStore;
5049
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
5150
import com.cloudbees.plugins.credentials.domains.Domain;
5251
import com.cloudbees.plugins.credentials.domains.DomainSpecification;
5352

53+
import hudson.FilePath;
54+
5455
import static org.hamcrest.Matchers.instanceOf;
5556
import static org.junit.Assert.*;
5657

@@ -92,81 +93,16 @@ public void evaluate() throws Throwable {
9293
"docker-client-cert", "desc", "clientKey", "clientCertificate", "serverCaCertificate");
9394
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
9495
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
95-
final String shellStep1, shellStep2;
96-
if (Functions.isWindows()) {
97-
shellStep1 = ""
98-
+ " bat '''\n"
99-
+ " REM check existence of the credentials dir\n"
100-
+ " if not exist %DOCKER_CERT_PATH% exit /B 1\n"
101-
+ "\n"
102-
+ " REM check existence of the certificate files\n"
103-
+ " if not exist %DOCKER_CERT_PATH%\\\\key.pem exit /B 1\n"
104-
+ " if not exist %DOCKER_CERT_PATH%\\\\cert.pem exit /B 1\n"
105-
+ " if not exist %DOCKER_CERT_PATH%\\\\ca.pem exit /B 1\n"
106-
+ "\n"
107-
+ " REM keep location of the certificate dir for the next step\n"
108-
+ " echo %DOCKER_CERT_PATH% > cert-path\n"
109-
+ " '''\n";
110-
shellStep2 = ""
111-
+ " bat '''\n"
112-
+ " REM get path of the certificate directory\n"
113-
+ " if not exist cert-path exit /B 1\n"
114-
+ " set /p cert_path=<cert-path\n"
115-
+ "\n"
116-
+ " REM check it has been deleted\n"
117-
+ " if exist %cert_path% exit /B 1\n"
118-
+ " '''\n";
119-
} else {
120-
shellStep1 = ""
121-
+ " sh '''\n"
122-
+ " set -e -x\n"
123-
+ "\n"
124-
+ " # check permissions on the credentials dir and its parent\n"
125-
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH\") = 700 ]\n"
126-
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH\"/..) = 700 ]\n"
127-
+ "\n"
128-
+ " # check permissions and content of the certificate files\n"
129-
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/key.pem\") = 600 ]\n"
130-
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/cert.pem\") = 600 ]\n"
131-
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/ca.pem\") = 600 ]\n"
132-
+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/key.pem\") = 9 ]\n"
133-
+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/cert.pem\") = 17 ]\n"
134-
+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/ca.pem\") = 19 ]\n"
135-
+ "\n"
136-
+ " # keep location of the certificate dir for the next step\n"
137-
+ " echo \"$DOCKER_CERT_PATH\" > cert-path\n"
138-
+ " '''\n";
139-
shellStep2 = ""
140-
+ " sh '''\n"
141-
+ " set -e -x\n"
142-
+ "\n"
143-
+ " # get path of the certificate directory\n"
144-
+ " cert_path=$(cat cert-path)\n"
145-
+ "\n"
146-
+ " # check it was where we would expect it to be\n"
147-
+ " echo \"$cert_path\" | grep -q \"/workspace/p@tmp/secretFiles/[-0-9a-f]\\\\{36\\\\}$\"\n"
148-
+ "\n"
149-
+ " # check it has been deleted\n"
150-
+ " if [ -e \"$cert_path\" ] ; then\n"
151-
+ " echo \"$cert_path still exists!!!\" >&2\n"
152-
+ " exit 1\n"
153-
+ " fi\n"
154-
+ " '''\n";
155-
}
156-
p.setDefinition(new CpsFlowDefinition(""
157-
+ "node {\n"
158-
+ " withCredentials([dockerCert(\n"
159-
+ " variable: 'DOCKER_CERT_PATH',\n"
160-
+ " credentialsId: 'docker-client-cert')]) {\n"
161-
+ " semaphore 'basics'\n"
162-
+ "\n"
163-
+ shellStep1
164-
+ " }\n"
165-
+ "\n"
166-
+ shellStep2
167-
+ "}", true));
96+
String pipelineScript = IOUtils.toString(getTestResourceInputStream("basics-Jenkinsfile"));
97+
p.setDefinition(new CpsFlowDefinition(pipelineScript, true));
16898
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
16999
SemaphoreStep.waitForStart("basics/1", b);
100+
// copy some test scripts into the workspace
101+
FilePath workspace = story.j.jenkins.getWorkspaceFor(p);
102+
copyTestResourceIntoWorkspace(workspace, "basics-step1.bat", 0755);
103+
copyTestResourceIntoWorkspace(workspace, "basics-step2.bat", 0755);
104+
copyTestResourceIntoWorkspace(workspace, "basics-step1.sh", 0755);
105+
copyTestResourceIntoWorkspace(workspace, "basics-step2.sh", 0755);
170106
}
171107
});
172108
story.addStep(new Statement() {
@@ -183,4 +119,17 @@ public void evaluate() throws Throwable {
183119
});
184120
}
185121

122+
private InputStream getTestResourceInputStream(String fileName) {
123+
return getClass().getResourceAsStream(getClass().getSimpleName() + "/" + fileName);
124+
}
125+
126+
private FilePath copyTestResourceIntoWorkspace(FilePath workspace, String fileName, int mask)
127+
throws IOException, InterruptedException {
128+
InputStream in = getTestResourceInputStream(fileName);
129+
FilePath f = workspace.child(fileName);
130+
f.copyFrom(in);
131+
f.chmod(mask);
132+
return f;
133+
}
134+
186135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env groovy
2+
3+
node {
4+
5+
withCredentials([
6+
dockerCert(variable: 'DOCKER_CERT_PATH',
7+
credentialsId: 'docker-client-cert')])
8+
{
9+
semaphore 'basics'
10+
// check that the credentials are there
11+
if (isUnix()) sh './basics-step1.sh'
12+
else bat 'basics-step1.bat'
13+
}
14+
15+
// check that the credentials are gone
16+
if (isUnix()) sh './basics-step2.sh'
17+
else bat 'basics-step2.bat'
18+
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
REM check existence of the credentials dir
2+
if not exist %DOCKER_CERT_PATH% exit /B 1
3+
4+
REM check existence of the certificate files
5+
if not exist %DOCKER_CERT_PATH%\key.pem exit /B 1
6+
if not exist %DOCKER_CERT_PATH%\cert.pem exit /B 1
7+
if not exist %DOCKER_CERT_PATH%\ca.pem exit /B 1
8+
9+
REM keep location of the certificate dir for the next step
10+
echo %DOCKER_CERT_PATH% > cert-path
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
set -e -x
3+
4+
# check permissions on the credentials dir and its parent
5+
[ $(stat -c %a "$DOCKER_CERT_PATH") = 700 ]
6+
[ $(stat -c %a "$DOCKER_CERT_PATH"/..) = 700 ]
7+
8+
# check permissions and content of the certificate files
9+
[ $(stat -c %a "$DOCKER_CERT_PATH/key.pem") = 600 ]
10+
[ $(stat -c %a "$DOCKER_CERT_PATH/cert.pem") = 600 ]
11+
[ $(stat -c %a "$DOCKER_CERT_PATH/ca.pem") = 600 ]
12+
[ $(stat -c %s "$DOCKER_CERT_PATH/key.pem") = 9 ]
13+
[ $(stat -c %s "$DOCKER_CERT_PATH/cert.pem") = 17 ]
14+
[ $(stat -c %s "$DOCKER_CERT_PATH/ca.pem") = 19 ]
15+
16+
# keep location of the certificate dir for the next step
17+
echo "$DOCKER_CERT_PATH" > cert-path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
REM get path of the certificate directory
2+
if not exist cert-path exit /B 1
3+
set /p cert_path=<cert-path
4+
5+
REM check it has been deleted
6+
if exist %cert_path% exit /B 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
set -e -x
3+
4+
# get path of the certificate directory
5+
cert_path=$(cat cert-path)
6+
7+
# check it was where we would expect it to be
8+
echo "$cert_path" | grep -q '/workspace/p@tmp/secretFiles/[-0-9a-f]\{36\}$'
9+
10+
# check it has been deleted
11+
if [ -e "$cert_path" ] ; then
12+
echo "$cert_path still exists!!!" >&2
13+
exit 1
14+
fi

0 commit comments

Comments
 (0)