From cb029f0ace2c08543bfa9aaf072fb88d52aa2a0b Mon Sep 17 00:00:00 2001 From: rufo Date: Tue, 21 May 2019 23:43:19 +0100 Subject: [PATCH 1/5] Fix parsing of container creation date --- .../docker/workflow/client/DockerClient.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java index 37c4a303b..11b86e521 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java @@ -43,6 +43,7 @@ import java.nio.charset.Charset; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -75,9 +76,6 @@ public class DockerClient { @SuppressFBWarnings(value="MS_SHOULD_BE_FINAL", justification="mutable for scripts") @Restricted(NoExternalUse.class) public static int CLIENT_TIMEOUT = Integer.getInteger(DockerClient.class.getName() + ".CLIENT_TIMEOUT", 180); // TODO 2.4+ SystemProperties - - // e.g. 2015-04-09T13:40:21.981801679Z - public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; private final Launcher launcher; private final @CheckForNull Node node; @@ -222,18 +220,13 @@ public void rm(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws I return fieldValue; } - private @CheckForNull Date getCreatedDate(@Nonnull EnvVars launchEnv, @Nonnull String objectId) throws IOException, InterruptedException { + private @CheckForNull Date getCreatedDate(@Nonnull EnvVars launchEnv, @Nonnull String objectId) throws InterruptedException { String createdString = inspect(launchEnv, objectId, "json .Created"); if (createdString == null) { return null; } - // TODO Currently truncating. Find out how to specify last part for parsing (TZ etc) - String s = createdString.substring(1, DOCKER_DATE_TIME_FORMAT.length() - 1); - try { - return new SimpleDateFormat(DOCKER_DATE_TIME_FORMAT).parse(s); - } catch (ParseException e) { - throw new IOException(String.format("Error parsing created date '%s' for object '%s'.", s, objectId), e); - } + String s = createdString.substring(1, createdString.length() - 1); // remove enclosing quotes + return Date.from(OffsetDateTime.parse(s).toInstant()); } /** From 04b6148a492f780267f6040fd650e66ba821f40a Mon Sep 17 00:00:00 2001 From: rufo Date: Tue, 21 May 2019 23:52:22 +0100 Subject: [PATCH 2/5] fixup --- .../jenkinsci/plugins/docker/workflow/client/DockerClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java index 11b86e521..261cf3acb 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java @@ -220,7 +220,7 @@ public void rm(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws I return fieldValue; } - private @CheckForNull Date getCreatedDate(@Nonnull EnvVars launchEnv, @Nonnull String objectId) throws InterruptedException { + private @CheckForNull Date getCreatedDate(@Nonnull EnvVars launchEnv, @Nonnull String objectId) throws IOException, InterruptedException { String createdString = inspect(launchEnv, objectId, "json .Created"); if (createdString == null) { return null; From e4585e49c339349de93778350e7c201704694f18 Mon Sep 17 00:00:00 2001 From: rufo Date: Mon, 26 Aug 2019 21:34:23 +0100 Subject: [PATCH 3/5] Remove unused imports --- .../jenkinsci/plugins/docker/workflow/client/DockerClient.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java index 261cf3acb..01f73b600 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java @@ -41,8 +41,6 @@ import java.io.Reader; import java.io.StringReader; import java.nio.charset.Charset; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; From 5104544314f70011f8d12e454fff8c9190f83ca9 Mon Sep 17 00:00:00 2001 From: rufo Date: Mon, 26 Aug 2019 21:36:18 +0100 Subject: [PATCH 4/5] Improve tests for creation date parsing --- .../plugins/docker/workflow/client/DockerClientTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java index 3052aef0c..4a7cb2f2c 100644 --- a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java +++ b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.Collections; +import java.util.Date; /** * @author tom.fennelly@gmail.com @@ -58,15 +59,18 @@ public void setup() throws Exception { @Test public void test_run() throws IOException, InterruptedException { EnvVars launchEnv = newLaunchEnv(); + Date createdEarliest = new Date(); String containerId = dockerClient.run(launchEnv, "learn/tutorial", null, null, Collections.emptyMap(), Collections.emptyList(), new EnvVars(), dockerClient.whoAmI(), "cat"); + Date createdLatest = new Date(); Assert.assertEquals(64, containerId.length()); ContainerRecord containerRecord = dockerClient.getContainerRecord(launchEnv, containerId); Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId()); Assert.assertTrue(containerRecord.getContainerName().length() > 0); Assert.assertTrue(containerRecord.getHost().length() > 0); - Assert.assertTrue(containerRecord.getCreated() > 1000000000000L); + Assert.assertFalse(containerRecord.getCreated().before(createdEarliest)); + Assert.assertFalse(containerRecord.getCreated().after(createdLatest)); Assert.assertEquals(Collections.emptyList(), dockerClient.getVolumes(launchEnv, containerId)); // Also test that the stop works and cleans up after itself From 6e7cbb24b616bd670f14da58663a7b7acf9182ee Mon Sep 17 00:00:00 2001 From: rufo Date: Mon, 26 Aug 2019 22:16:38 +0100 Subject: [PATCH 5/5] fixup --- .../plugins/docker/workflow/client/DockerClientTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java index 4a7cb2f2c..065f24dfa 100644 --- a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java +++ b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java @@ -69,8 +69,8 @@ public void test_run() throws IOException, InterruptedException { Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId()); Assert.assertTrue(containerRecord.getContainerName().length() > 0); Assert.assertTrue(containerRecord.getHost().length() > 0); - Assert.assertFalse(containerRecord.getCreated().before(createdEarliest)); - Assert.assertFalse(containerRecord.getCreated().after(createdLatest)); + Assert.assertTrue(containerRecord.getCreated() >= createdEarliest.getTime()); + Assert.assertTrue(containerRecord.getCreated() <= createdLatest.getTime()); Assert.assertEquals(Collections.emptyList(), dockerClient.getVolumes(launchEnv, containerId)); // Also test that the stop works and cleans up after itself