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

[pull] master from GoogleContainerTools:master #101

Merged
merged 2 commits into from
Jan 23, 2020
Merged
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 @@ -54,7 +54,8 @@ static Future<Optional<String>> newUpdateChecker(
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
return UpdateChecker.checkForUpdate(executorService, projectProperties::log, VERSION_URL);
return UpdateChecker.checkForUpdate(
executorService, projectProperties::log, VERSION_URL, projectProperties.getToolName());
} finally {
executorService.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ static Future<Optional<String>> newUpdateChecker(
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
return UpdateChecker.checkForUpdate(executorService, projectProperties::log, VERSION_URL);
return UpdateChecker.checkForUpdate(
executorService, projectProperties::log, VERSION_URL, projectProperties.getToolName());
} finally {
executorService.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
import com.google.cloud.tools.jib.ProjectInfo;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.filesystem.XdgDirectories;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Request;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.json.JsonTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -68,19 +70,28 @@ private static class VersionJsonTemplate implements JsonTemplate {
* @param executorService the {@link ExecutorService}
* @param log {@link Consumer} used to log messages
* @param versionUrl the location to check for the latest version
* @param toolName the tool name
* @return a new {@link UpdateChecker}
*/
public static Future<Optional<String>> checkForUpdate(
ExecutorService executorService, Consumer<LogEvent> log, String versionUrl) {
ExecutorService executorService, Consumer<LogEvent> log, String versionUrl, String toolName) {
return executorService.submit(
() ->
performUpdateCheck(
log, Verify.verifyNotNull(ProjectInfo.VERSION), versionUrl, getConfigDir()));
log,
Verify.verifyNotNull(ProjectInfo.VERSION),
versionUrl,
getConfigDir(),
toolName));
}

@VisibleForTesting
static Optional<String> performUpdateCheck(
Consumer<LogEvent> log, String currentVersion, String versionUrl, Path configDir) {
Consumer<LogEvent> log,
String currentVersion,
String versionUrl,
Path configDir,
String toolName) {
// Abort if offline or update checks are disabled
if (Boolean.getBoolean(PropertyNames.DISABLE_UPDATE_CHECKS)) {
return Optional.empty();
Expand Down Expand Up @@ -139,11 +150,17 @@ static Optional<String> performUpdateCheck(
}

// Check for update
HttpURLConnection connection = (HttpURLConnection) new URL(versionUrl).openConnection();
FailoverHttpClient httpClient = new FailoverHttpClient(true, false, ignored -> {});
try {
connection.setConnectTimeout(3000);
Response response =
httpClient.get(
new URL(versionUrl),
Request.builder()
.setHttpTimeout(3000)
.setUserAgent("jib " + currentVersion + " " + toolName)
.build());
VersionJsonTemplate version =
JsonTemplateMapper.readJson(connection.getInputStream(), VersionJsonTemplate.class);
JsonTemplateMapper.readJson(response.getBody(), VersionJsonTemplate.class);
Files.write(lastUpdateCheck, Instant.now().toString().getBytes(StandardCharsets.UTF_8));
if (currentVersion.equals(version.latest)) {
return Optional.empty();
Expand All @@ -156,7 +173,7 @@ static Optional<String> performUpdateCheck(
+ "). Update your build configuration to use the latest features and fixes!");

} finally {
connection.disconnect();
httpClient.shutDown();
}

} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public void testPerformUpdateCheck_newVersionFound() throws IOException {
setupConfigAndLastUpdateCheck();
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertTrue(testWebServer.getInputRead().contains("User-Agent: jib 1.0.2 tool name"));
Assert.assertTrue(message.isPresent());
Assert.assertEquals(
"A new version of Jib (2.0.0) is available (currently using 1.0.2). Update your build "
Expand All @@ -94,12 +95,12 @@ public void testPerformUpdateCheck_newJsonField()
new TestWebServer(
false,
Collections.singletonList(
"HTTP/1.1 200 OK\nContent-Length:18\n\n{\"latest\":\"2.0.0\",\"unknownField\":\"unknown\"}"),
"HTTP/1.1 200 OK\nContent-Length:43\n\n{\"latest\":\"2.0.0\",\"unknownField\":\"unknown\"}"),
1);
setupConfigAndLastUpdateCheck();
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertTrue(message.isPresent());
Assert.assertEquals(
"A new version of Jib (2.0.0) is available (currently using 1.0.2). Update your build "
Expand All @@ -113,11 +114,12 @@ public void testPerformUpdateCheck_onLatest() throws IOException {
setupConfigAndLastUpdateCheck();
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "2.0.0", testWebServer.getEndpoint(), configDir);
ignored -> {}, "2.0.0", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertFalse(message.isPresent());
String modifiedTime =
new String(
Files.readAllBytes(configDir.resolve("lastUpdateCheck")), StandardCharsets.UTF_8);
Assert.assertTrue(testWebServer.getInputRead().contains("User-Agent: jib 2.0.0 tool name"));
Assert.assertTrue(Instant.parse(modifiedTime).isAfter(before));
}

Expand All @@ -126,7 +128,7 @@ public void testPerformUpdateCheck_noConfigOrLastUpdateCheck() throws IOExceptio
Instant before = Instant.now();
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertTrue(message.isPresent());
Assert.assertEquals(
"A new version of Jib (2.0.0) is available (currently using 1.0.2). Update your build "
Expand All @@ -147,7 +149,7 @@ public void testPerformUpdateCheck_lastUpdateCheckTooSoon() throws IOException {
modifiedTime.toString().getBytes(StandardCharsets.UTF_8));
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertFalse(message.isPresent());

// lastUpdateCheck should not have changed
Expand All @@ -162,7 +164,7 @@ public void testPerformUpdateCheck_systemProperty() {
System.setProperty(PropertyNames.DISABLE_UPDATE_CHECKS, "true");
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertFalse(message.isPresent());
}

Expand All @@ -173,7 +175,7 @@ public void testPerformUpdateCheck_configDisabled() throws IOException {
"{\"disableUpdateCheck\":true}".getBytes(StandardCharsets.UTF_8));
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertFalse(message.isPresent());
}

Expand All @@ -183,7 +185,7 @@ public void testPerformUpdateCheck_badConfig() throws IOException {
configDir.resolve("config.json"), "corrupt config".getBytes(StandardCharsets.UTF_8));
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
Assert.assertFalse(message.isPresent());
}

Expand All @@ -194,7 +196,7 @@ public void testPerformUpdateCheck_badLastUpdateTime() throws IOException {
configDir.resolve("lastUpdateCheck"), "bad timestamp".getBytes(StandardCharsets.UTF_8));
Optional<String> message =
UpdateChecker.performUpdateCheck(
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir);
ignored -> {}, "1.0.2", testWebServer.getEndpoint(), configDir, "tool name");
String modifiedTime =
new String(
Files.readAllBytes(configDir.resolve("lastUpdateCheck")), StandardCharsets.UTF_8);
Expand All @@ -219,7 +221,8 @@ public void testPerformUpdateCheck_failSilently()
},
"1.0.2",
badServer.getEndpoint(),
configDir);
configDir,
"tool name");
Assert.assertFalse(message.isPresent());
}
}
Expand Down