-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This branch's aim is to improve the unit tests to get to > 80% test coverage. The only non-test / mechanical change is in the download logic: this was changed to print all the errors encountered when failing to download multiple assets as opposed to just the first one. This is in `internal/lock.go`.
- Loading branch information
Showing
17 changed files
with
331 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/cisco-open/grabit/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRunAdd(t *testing.T) { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`abcdef`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
port, server := test.HttpHandler(handler) | ||
defer server.Close() | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", test.TmpFile(t, ""), "add", fmt.Sprintf("http://localhost:%d/test.html", port)}) | ||
err := cmd.Execute() | ||
assert.Nil(t, 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
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,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cisco-open/grabit/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRunDelete(t *testing.T) { | ||
testfilepath := test.TmpFile(t, ` | ||
[[Resource]] | ||
Urls = ['http://localhost:123456/test.html'] | ||
Integrity = 'sha256-asdasdasd' | ||
Tags = ['tag1', 'tag2'] | ||
`) | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", testfilepath, "delete", "http://localhost:123456/test.html"}) | ||
err := cmd.Execute() | ||
assert.Nil(t, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/cisco-open/grabit/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRunDownload(t *testing.T) { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`abcdef`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
port, server := test.HttpHandler(handler) | ||
defer server.Close() | ||
testfilepath := test.TmpFile(t, fmt.Sprintf(` | ||
[[Resource]] | ||
Urls = ['http://localhost:%d/test.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
`, port)) | ||
outputDir := test.TmpDir(t) | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", testfilepath, "download", "--dir", outputDir}) | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestRunDownloadWithTags(t *testing.T) { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`abcdef`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
port, server := test.HttpHandler(handler) | ||
defer server.Close() | ||
testfilepath := test.TmpFile(t, fmt.Sprintf(` | ||
[[Resource]] | ||
Urls = ['http://localhost:%d/test.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
Tags = ['tag'] | ||
[[Resource]] | ||
Urls = ['http://localhost:%d/test2.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
Tags = ['tag1', 'tag2'] | ||
`, port, port)) | ||
outputDir := test.TmpDir(t) | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", testfilepath, "download", "--tag", "tag", "--dir", outputDir}) | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
files, err := os.ReadDir(outputDir) | ||
assert.Nil(t, err) | ||
actualFiles := []string{} | ||
for _, file := range files { | ||
actualFiles = append(actualFiles, file.Name()) | ||
} | ||
assert.ElementsMatch(t, []string{"test.html"}, actualFiles) | ||
} | ||
|
||
func TestRunDownloadWithoutTags(t *testing.T) { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`abcdef`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
port, server := test.HttpHandler(handler) | ||
defer server.Close() | ||
testfilepath := test.TmpFile(t, fmt.Sprintf(` | ||
[[Resource]] | ||
Urls = ['http://localhost:%d/test.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
Tags = ['tag'] | ||
[[Resource]] | ||
Urls = ['http://localhost:%d/test2.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
Tags = ['tag1', 'tag2'] | ||
`, port, port)) | ||
outputDir := test.TmpDir(t) | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", testfilepath, "download", "--notag", "tag", "--dir", outputDir}) | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
files, err := os.ReadDir(outputDir) | ||
assert.Nil(t, err) | ||
actualFiles := []string{} | ||
for _, file := range files { | ||
actualFiles = append(actualFiles, file.Name()) | ||
} | ||
assert.ElementsMatch(t, []string{"test2.html"}, actualFiles) | ||
} | ||
|
||
func TestRunDownloadMultipleErrors(t *testing.T) { | ||
testfilepath := test.TmpFile(t, ` | ||
[[Resource]] | ||
Urls = ['http://localhost:1234/test.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
[[Resource]] | ||
Urls = ['http://cannot-be-resolved.no:12/test.html'] | ||
Integrity = 'sha256-vvV+x/U6bUC+tkCngKY5yDvCmsipgW8fxsXG3Nk8RyE=' | ||
`) | ||
cmd := NewRootCmd() | ||
cmd.SetArgs([]string{"-f", testfilepath, "download"}) | ||
err := cmd.Execute() | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), "failed to download") | ||
assert.Contains(t, err.Error(), "lookup cannot-be-resolved.no: no such host") | ||
} |
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
Oops, something went wrong.