Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-57589 Fix parsing of container creation date #175

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
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.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -75,9 +74,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;
Expand Down Expand Up @@ -232,13 +228,8 @@ public void rm(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws I
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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Date;

/**
* @author <a href="mailto:[email protected]">[email protected]</a>
Expand All @@ -58,15 +59,18 @@ public void setup() throws Exception {
@Test
public void test_run() throws IOException, InterruptedException {
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
Date createdEarliest = new Date();
String containerId =
dockerClient.run(launchEnv, "learn/tutorial", null, null, Collections.<String, String>emptyMap(), Collections.<String>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.assertTrue(containerRecord.getCreated() >= createdEarliest.getTime());
Assert.assertTrue(containerRecord.getCreated() <= createdLatest.getTime());
Assert.assertEquals(Collections.<String>emptyList(), dockerClient.getVolumes(launchEnv, containerId));

// Also test that the stop works and cleans up after itself
Expand Down