[FIXED JENKINS-26196] More robust handling of server errors in FilePath.installIfNecessaryFrom.

This commit is contained in:
Jesse Glick committed 2014-12-29 14:49:27 -05:00
1 parent babfb832a3
commit 1fcee97d2f
3 files changed
+37 -6

No files matched your search

+3
View File
@@ -63,6 +63,9 @@ Upcoming changes</a>
<li class=bug>
Do not throw exception on <code>/signup</code> when not possible.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-11172">issue 11172</a>)
<li class=bug>
Tool installer which downloads and unpacks archives should not fail the build if the tool already exists and the server returns an error code.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-26196">issue 26196</a>)
<li class=bug>
Fingerprint compaction aggravated lazy-loading performance issues.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-19392">issue 19392</a>)
+13 -6
View File
@@ -756,11 +756,12 @@ public final class FilePath implements Serializable {
public boolean installIfNecessaryFrom(@Nonnull URL archive, @CheckForNull TaskListener listener, @Nonnull String message) throws IOException, InterruptedException {
try {
FilePath timestamp = this.child(".timestamp");
long lastModified = timestamp.lastModified();
URLConnection con;
try {
con = ProxyConfiguration.open(archive);
if (timestamp.exists()) {
con.setIfModifiedSince(timestamp.lastModified());
if (lastModified != 0) {
con.setIfModifiedSince(lastModified);
}
con.connect();
} catch (IOException x) {
@@ -775,15 +776,21 @@ public final class FilePath implements Serializable {
}
}
if (con instanceof HttpURLConnection
&& ((HttpURLConnection)con).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
return false;
if (lastModified != 0 && con instanceof HttpURLConnection) {
HttpURLConnection httpCon = (HttpURLConnection) con;
int responseCode = httpCon.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
return false;
} else if (responseCode != HttpURLConnection.HTTP_OK) {
listener.getLogger().println("Skipping installation of " + archive + " to " + remote + " due to server error: " + responseCode + " " + httpCon.getResponseMessage());
return false;
}
}
long sourceTimestamp = con.getLastModified();
if(this.exists()) {
if(timestamp.exists() && sourceTimestamp ==timestamp.lastModified())
if (lastModified != 0 && sourceTimestamp == lastModified)
return false; // already up to date
this.deleteContents();
} else {
@@ -27,6 +27,7 @@ import hudson.FilePath.TarCompression;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.NullStream;
import hudson.util.StreamTaskListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -34,6 +35,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
@@ -545,6 +547,25 @@ public class FilePathTest {
assertTrue(d.installIfNecessaryFrom(url, null, null));
}
@Issue("JENKINS-26196")
@Test public void installIfNecessarySkipsDownloadWhenErroneous() throws Exception {
File tmp = temp.getRoot();
final FilePath d = new FilePath(tmp);
d.child(".timestamp").touch(123000);
final HttpURLConnection con = mock(HttpURLConnection.class);
final URL url = someUrlToZipFile(con);
when(con.getResponseCode()).thenReturn(HttpURLConnection.HTTP_GATEWAY_TIMEOUT);
when(con.getResponseMessage()).thenReturn("Gateway Timeout");
when(con.getInputStream()).thenThrow(new ConnectException());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String message = "going ahead";
assertFalse(d.installIfNecessaryFrom(url, new StreamTaskListener(baos), message));
verify(con).setIfModifiedSince(123000);
String log = baos.toString();
assertFalse(log, log.contains(message));
assertTrue(log, log.contains("504 Gateway Timeout"));
}
private URL someUrlToZipFile(final URLConnection con) throws IOException {
final URLStreamHandler urlHandler = new URLStreamHandler() {