Skip to content

Commit a8ab9ee

Browse files
committed
Simplify test to actually test step
1 parent e5e21f8 commit a8ab9ee

File tree

1 file changed

+17
-61
lines changed

1 file changed

+17
-61
lines changed

src/test/java/org/jenkinsci/plugins/docker/workflow/FromFingerprintStepTest.java

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import hudson.EnvVars;
2727
import hudson.Launcher.LocalLauncher;
28+
import hudson.model.Fingerprint;
2829
import hudson.util.StreamTaskListener;
30+
import org.jenkinsci.plugins.docker.commons.fingerprint.DockerDescendantFingerprintFacet;
31+
import org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints;
2932
import org.jenkinsci.plugins.docker.workflow.client.DockerClient;
3033
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
3134
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
@@ -36,77 +39,27 @@
3639
import org.jvnet.hudson.test.RestartableJenkinsRule;
3740

3841
import static org.jenkinsci.plugins.docker.workflow.DockerTestUtil.assumeDocker;
42+
import static org.junit.Assert.assertNotNull;
3943

4044
public class FromFingerprintStepTest {
4145
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
4246

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";
4548

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";
10153

10254
String script = "node {\n" +
10355
" 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" +
10659
" echo \"built ${built.id}\"\n" +
10760
"}";
10861

109-
return script;
62+
assertBuild("build", script, BUSYBOX_IMAGE);
11063
}
11164

11265
private void assertBuild(final String projectName, final String pipelineCode, final String fromImage) throws Exception {
@@ -120,8 +73,11 @@ public void evaluate() throws Throwable {
12073
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
12174
DockerClient client = new DockerClient(new LocalLauncher(StreamTaskListener.NULL), null, null);
12275
String ancestorImageId = client.inspect(new EnvVars(), fromImage, ".Id");
123-
story.j.assertLogContains("built from-with-arg", b);
12476
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);
12581
}
12682
});
12783
}

0 commit comments

Comments
 (0)