forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
elastic-agent: don't swallow download errors (elastic#23235)
* elastic-agent: don't swallow download errors Stop swallowing the error from io.Copy when reading from response bodies in the http downloader. This prevents storing a partial download, which leads to a permanent error state.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
x-pack/elastic-agent/pkg/artifact/download/http/downloader_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" | ||
) | ||
|
||
func TestDownloadBodyError(t *testing.T) { | ||
// This tests the scenario where the download encounters a network error | ||
// part way through the download, while copying the response body. | ||
|
||
type connKey struct{} | ||
var srv *httptest.Server | ||
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.(http.Flusher).Flush() | ||
conn := r.Context().Value(connKey{}).(net.Conn) | ||
conn.Close() | ||
})) | ||
defer srv.Close() | ||
client := srv.Client() | ||
srv.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context { | ||
return context.WithValue(ctx, connKey{}, c) | ||
} | ||
|
||
targetDir, err := ioutil.TempDir(os.TempDir(), "") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
config := &artifact.Config{ | ||
SourceURI: srv.URL, | ||
TargetDirectory: targetDir, | ||
OperatingSystem: "linux", | ||
Architecture: "64", | ||
} | ||
|
||
testClient := NewDownloaderWithClient(config, *client) | ||
artifactPath, err := testClient.Download(context.Background(), beatSpec, version) | ||
if err == nil { | ||
os.Remove(artifactPath) | ||
t.Fatal("expected Download to return an error") | ||
} | ||
} |