Skip to content

Commit

Permalink
add testcase and make generation check fail if invalid generation is …
Browse files Browse the repository at this point in the history
…provided
  • Loading branch information
ScottSuarez committed Feb 26, 2021
1 parent 53fded9 commit 15ae4df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
4 changes: 1 addition & 3 deletions get_gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -135,8 +134,7 @@ func (g *GCSGetter) GetFile(dst string, u *url.URL) error {
func (g *GCSGetter) getObject(ctx context.Context, client *storage.Client, dst, bucket, object, fragment string) error {
var rc *storage.Reader
var err error
fragmentHasGeneration := regexp.MustCompile("^\\d+$").MatchString(fragment)
if fragmentHasGeneration {
if fragment != "" {
generation, err := strconv.ParseInt(fragment, 10, 64)
if err != nil {
return err
Expand Down
44 changes: 43 additions & 1 deletion get_gcs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package getter

import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -88,6 +89,47 @@ func TestGCSGetter_GetFile(t *testing.T) {
assertContents(t, dst, "# Main\n")
}

func TestGCSGetter_GetGenerationFile(t *testing.T) {
defer initGCPCredentials(t)()

g := new(GCSGetter)
dst := tempTestFile(t)
defer os.RemoveAll(filepath.Dir(dst))

// Download
err := g.GetFile(
dst, testURL("https://www.googleapis.com/storage/v1/go-getter-testcase-data/DO_NOT_DELETE/generation_test.txt#1614317688843055"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
content, err := ioutil.ReadFile(dst)
if err != nil {
t.Fatalf("err: %s", err)
}
if string(content) != "a" {
t.Fatalf("expected file contents from generation to be `a` but got `%s`", content)
}

// Download
err = g.GetFile(
dst, testURL("https://www.googleapis.com/storage/v1/go-getter-testcase-data/DO_NOT_DELETE/generation_test.txt#1614317705239142"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
content, err = ioutil.ReadFile(dst)
if err != nil {
t.Fatalf("err: %s", err)
}
if string(content) != "b" {
t.Fatalf("expected file contents from generation to be `a` but got `%s`", content)
}

}

func TestGCSGetter_GetFile_notfound(t *testing.T) {
g := new(GCSGetter)
dst := tempTestFile(t)
Expand Down Expand Up @@ -180,7 +222,7 @@ func TestGCSGetter_Url(t *testing.T) {
t.Fatalf("expected forced protocol to be gcs")
}

bucket, path, err := g.parseURL(u)
bucket, path, _, err := g.parseURL(u)

if err != nil {
t.Fatalf("err: %s", err)
Expand Down

0 comments on commit 15ae4df

Please sign in to comment.