-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: component download timeout retry (#1389)
- Loading branch information
1 parent
39add0e
commit 850cddc
Showing
5 changed files
with
98 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package lwcomponent_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lacework/go-sdk/lwcomponent" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDownloadFile(t *testing.T) { | ||
var ( | ||
urlPath string = "/lw-cdk-store/catalog/component-example/1.0.0/component-example-linux-amd64.tar.gz" | ||
content string = "CDK component" | ||
) | ||
|
||
file, err := os.CreateTemp("", "lwcomponent-downloadFile") | ||
assert.Nil(t, err) | ||
defer file.Close() | ||
|
||
mux := http.NewServeMux() | ||
|
||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
mux.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) { | ||
if assert.Equal(t, "GET", r.Method, "Get() should be a GET method") { | ||
fmt.Fprint(w, content) | ||
} | ||
}) | ||
|
||
t.Run("happy path", func(t *testing.T) { | ||
err = lwcomponent.DownloadFile(file.Name(), fmt.Sprintf("%s%s", server.URL, urlPath), 0) | ||
assert.Nil(t, err) | ||
|
||
buf, err := os.ReadFile(file.Name()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, content, string(buf)) | ||
}) | ||
|
||
t.Run("timeout error", func(t *testing.T) { | ||
err = lwcomponent.DownloadFile(file.Name(), fmt.Sprintf("%s%s", server.URL, urlPath), 1*time.Microsecond) | ||
assert.NotNil(t, err) | ||
assert.True(t, os.IsTimeout(err)) | ||
}) | ||
|
||
t.Run("non-timeout error", func(t *testing.T) { | ||
err = lwcomponent.DownloadFile(file.Name(), "", 0) | ||
assert.NotNil(t, err) | ||
assert.False(t, os.IsTimeout(err)) | ||
}) | ||
} |
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