2424
2525package org .jenkinsci .plugins .docker .commons .credentials ;
2626
27+ import java .io .IOException ;
28+ import java .io .InputStream ;
2729import java .util .Collections ;
2830
31+ import org .apache .commons .io .IOUtils ;
2932import org .jenkinsci .plugins .credentialsbinding .MultiBinding ;
3033import org .jenkinsci .plugins .credentialsbinding .impl .BindingStep ;
31- import org .jenkinsci .plugins .docker .commons .credentials .DockerServerCredentials ;
32- import org .jenkinsci .plugins .docker .commons .credentials .DockerServerDomainSpecification ;
3334import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
3435import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
3536import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
4243import org .jvnet .hudson .test .BuildWatcher ;
4344import org .jvnet .hudson .test .RestartableJenkinsRule ;
4445
45- import hudson .Functions ;
46-
4746import com .cloudbees .plugins .credentials .CredentialsProvider ;
4847import com .cloudbees .plugins .credentials .CredentialsScope ;
4948import com .cloudbees .plugins .credentials .CredentialsStore ;
5049import com .cloudbees .plugins .credentials .SystemCredentialsProvider ;
5150import com .cloudbees .plugins .credentials .domains .Domain ;
5251import com .cloudbees .plugins .credentials .domains .DomainSpecification ;
5352
53+ import hudson .FilePath ;
54+
5455import static org .hamcrest .Matchers .instanceOf ;
5556import 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}
0 commit comments