25
25
26
26
import hudson .EnvVars ;
27
27
import hudson .Launcher .LocalLauncher ;
28
+ import hudson .model .Fingerprint ;
28
29
import hudson .util .StreamTaskListener ;
30
+ import org .jenkinsci .plugins .docker .commons .fingerprint .DockerDescendantFingerprintFacet ;
31
+ import org .jenkinsci .plugins .docker .commons .fingerprint .DockerFingerprints ;
29
32
import org .jenkinsci .plugins .docker .workflow .client .DockerClient ;
30
33
import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
31
34
import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
36
39
import org .jvnet .hudson .test .RestartableJenkinsRule ;
37
40
38
41
import static org .jenkinsci .plugins .docker .workflow .DockerTestUtil .assumeDocker ;
42
+ import static org .junit .Assert .assertNotNull ;
39
43
40
44
public class FromFingerprintStepTest {
41
45
@ Rule public RestartableJenkinsRule story = new RestartableJenkinsRule ();
42
46
43
- private static final String HELLO_WORLD_IMAGE = "hello-world" ;
44
- private static final String BUSYBOX_IMAGE = "busybox" ;
47
+ private static final String BUSYBOX_IMAGE = "quay.io/prometheus/busybox:latest" ;
45
48
46
- /**
47
- * Test quotation marks in --build-arg parameters
48
- */
49
- @ Test public void buildWithFROMArgs () throws Exception {
50
- String dockerfile = "" +
51
- "ARG IMAGE_TO_UPDATE\\ n" +
52
- "FROM ${IMAGE_TO_UPDATE}\\ n" ;
53
-
54
- assertBuild ("prj-simple" ,
55
- script (dockerfile , "--build-arg IMAGE_TO_UPDATE=hello-world:latest" ), HELLO_WORLD_IMAGE );
56
-
57
- assertBuild ("prj-singlequotes-in-build-arg---aroundValue" ,
58
- script (dockerfile , "--build-arg IMAGE_TO_UPDATE=\\ 'hello-world:latest\\ '" ), HELLO_WORLD_IMAGE );
59
-
60
- assertBuild ("prj-dobulequotes-in-build-arg---aroundValue" ,
61
- script (dockerfile , "--build-arg IMAGE_TO_UPDATE=\" hello-world:latest\" " ), HELLO_WORLD_IMAGE );
62
-
63
- assertBuild ("prj-singlequotes-in-build-arg---aroundAll" ,
64
- script (dockerfile , "--build-arg \\ 'IMAGE_TO_UPDATE=hello-world:latest\\ '" ), HELLO_WORLD_IMAGE );
65
-
66
- assertBuild ("prj-doublequotes-in-build-arg---aroundAll" ,
67
- script (dockerfile , "--build-arg \" IMAGE_TO_UPDATE=hello-world:latest\" " ), HELLO_WORLD_IMAGE );
68
- }
69
-
70
- @ Test public void buildWithDefaultArgsFromDockerfile () throws Exception {
71
- String dockerfile = "" +
72
- "ARG REGISTRY_URL\\ n" +
73
- "ARG TAG=latest\\ n" +
74
- "FROM ${REGISTRY_URL}busybox:${TAG}\\ n" ;
75
-
76
- assertBuild ("prj-with-default-arg-from-dockerfile" ,
77
- script (dockerfile , "" ), BUSYBOX_IMAGE );
78
-
79
- assertBuild ("prj-override-empty-value" ,
80
- script (dockerfile , "--build-arg REGISTRY_URL=" ), BUSYBOX_IMAGE );
81
-
82
- assertBuild ("prj-with-override-value" ,
83
- script (dockerfile , "--build-arg TAG=1.27.1" ), BUSYBOX_IMAGE +":1.27.1" );
84
-
85
- assertBuild ("prj-with-override-value2" ,
86
- script (dockerfile , "--build-arg TAG=1.26" ), BUSYBOX_IMAGE +":1.26" );
87
- }
88
-
89
- @ Test public void buildOverridingRegistry () throws Exception {
90
- String quayDockerfile = "" +
91
- "ARG REGISTRY_URL\\ n" +
92
- "ARG TAG=latest\\ n" +
93
- "FROM ${REGISTRY_URL}prometheus/busybox:${TAG}\\ n" ;
94
-
95
- assertBuild ("prj-override-registry" ,
96
- script (quayDockerfile , "--build-arg REGISTRY_URL=\\ 'quay.io/\\ '" ), "quay.io/prometheus/busybox" );
97
- }
98
-
99
- private static String script (String dockerfile , String buildArg ) {
100
- String fullBuildArgs = buildArg + " buildWithFROMArgs" ;
49
+ @ Test public void build () throws Exception {
50
+ String dockerFile = "" +
51
+ "FROM " + BUSYBOX_IMAGE +"\\ n" +
52
+ "RUN echo 1" ;
101
53
102
54
String script = "node {\n " +
103
55
" sh 'mkdir buildWithFROMArgs'\n " +
104
- " writeFile file: 'buildWithFROMArgs/Dockerfile', text: '" + dockerfile + "'\n " +
105
- " def built = docker.build 'from-with-arg', '" + fullBuildArgs + "'\n " +
56
+ " writeFile file: 'Dockerfile', text: '" + dockerFile + "'\n " +
57
+ " def built = docker.build('my-tag') \n " +
58
+ " dockerFingerprintFrom dockerfile: 'Dockerfile', image: 'my-tag' \n " +
106
59
" echo \" built ${built.id}\" \n " +
107
60
"}" ;
108
61
109
- return script ;
62
+ assertBuild ( "build" , script , BUSYBOX_IMAGE ) ;
110
63
}
111
64
112
65
private void assertBuild (final String projectName , final String pipelineCode , final String fromImage ) throws Exception {
@@ -120,8 +73,11 @@ public void evaluate() throws Throwable {
120
73
WorkflowRun b = story .j .assertBuildStatusSuccess (p .scheduleBuild2 (0 ));
121
74
DockerClient client = new DockerClient (new LocalLauncher (StreamTaskListener .NULL ), null , null );
122
75
String ancestorImageId = client .inspect (new EnvVars (), fromImage , ".Id" );
123
- story .j .assertLogContains ("built from-with-arg" , b );
124
76
story .j .assertLogContains (ancestorImageId .replaceFirst ("^sha256:" , "" ).substring (0 , 12 ), b );
77
+ Fingerprint f = DockerFingerprints .of (ancestorImageId );
78
+ assertNotNull (f );
79
+ DockerDescendantFingerprintFacet descendantFacet = f .getFacet (DockerDescendantFingerprintFacet .class );
80
+ assertNotNull (descendantFacet );
125
81
}
126
82
});
127
83
}
0 commit comments