Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lifangmoler committed Feb 28, 2019
1 parent a134506 commit 9d45bf4
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 32 deletions.
20 changes: 14 additions & 6 deletions get_gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package getter
import (
"context"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -33,24 +34,31 @@ func (g *GCSGetter) ClientMode(u *url.URL) (ClientMode, error) {
return 0, err
}
iter := client.Bucket(bucket).Objects(ctx, &storage.Query{Prefix: object})

count := 0
for {
_, err := iter.Next()
objAtrr, err := iter.Next()

if err == iterator.Done {
log.Printf("done")
break
}
if err != nil && err != iterator.Done {
return 0, err
}
log.Printf("iter: %+v", *objAtrr)
log.Println()
count++
if err == iterator.Done {
break
}
log.Printf("count: %d", count)

}
log.Println()
if count <= 1 {
// Return file if there are no matches as well.
// GetFile will fail in this case.
return ClientModeFile, nil
} else {
return ClientModeDir, nil
}
return ClientModeDir, nil
}

func (g *GCSGetter) Get(dst string, u *url.URL) error {
Expand Down
153 changes: 153 additions & 0 deletions get_gcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package getter

import (
"net/url"
"os"
"path/filepath"
"testing"
)

func init() {
}

func TestGCSGetter_impl(t *testing.T) {
var _ Getter = new(GCSGetter)
}
func TestGCSGetter(t *testing.T) {
g := new(GCSGetter)
dst := tempDir(t)

// With a dir that doesn't exist
err := g.Get(
dst, testURL("https://www.googleapis.com/storage/v1/hc-oss-test/go-getter/folder"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGCSGetter_subdir(t *testing.T) {
g := new(GCSGetter)
dst := tempDir(t)

// With a dir that doesn't exist
err := g.Get(
dst, testURL("https://www.googleapis.com/storage/v1/hc-oss-test/go-getter/folder/subfolder"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the sub file exists
subPath := filepath.Join(dst, "sub.tf")
if _, err := os.Stat(subPath); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestGCSGetter_GetFile(t *testing.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/hc-oss-test/go-getter/folder/main.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
assertContents(t, dst, "# Main\n")
}

func TestGCSGetter_GetFile_notfound(t *testing.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/hc-oss-test/go-getter/folder/404.tf"))
if err == nil {
t.Fatalf("expected error, got none")
}
}

func TestGCSGetter_ClientMode_dir(t *testing.T) {
g := new(GCSGetter)

// Check client mode on a key prefix with only a single key.
mode, err := g.ClientMode(
testURL("https://www.googleapis.com/storage/v1/eddie-test-devmvp-1/tf-environments"))
if err != nil {
t.Fatalf("err: %s", err)
}
if mode != ClientModeDir {
t.Fatal("expect ClientModeDir")
}
}

// func TestGCSGetter_ClientMode_file(t *testing.T) {
// g := new(GCSGetter)

// // Check client mode on a key prefix which contains sub-keys.
// mode, err := g.ClientMode(
// testURL("https://www.googleapis.com/storage/v1/hc-oss-test/go-getter/folder/main.tf"))
// if err != nil {
// t.Fatalf("err: %s", err)
// }
// if mode != ClientModeFile {
// t.Fatal("expect ClientModeFile")
// }
// }

func TestGCSGetter_Url(t *testing.T) {
var gcstests = []struct {
name string
url string
bucket string
path string
}{
{
name: "test1",
url: "gcs::https://www.googleapis.com/storage/v1/hc-oss-test/go-getter/foo/null.zip",
bucket: "hc-oss-test",
path: "go-getter/foo/null.zip",
},
}

for i, pt := range gcstests {
t.Run(pt.name, func(t *testing.T) {
g := new(GCSGetter)
forced, src := getForcedGetter(pt.url)
u, err := url.Parse(src)

if err != nil {
t.Errorf("test %d: unexpected error: %s", i, err)
}
if forced != "gcs" {
t.Fatalf("expected forced protocol to be gcs")
}

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

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

if bucket != pt.bucket {
t.Fatalf("expected %s, got %s", pt.bucket, bucket)
}
if path != pt.path {
t.Fatalf("expected %s, got %s", pt.path, path)
}
})
}
}
43 changes: 21 additions & 22 deletions get_git_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package getter

import (
"encoding/base64"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -228,32 +227,32 @@ func TestGitGetter_gitVersion(t *testing.T) {
}
}

func TestGitGetter_sshKey(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
// func TestGitGetter_sshKey(t *testing.T) {
// if !testHasGit {
// t.Log("git not found, skipping")
// t.Skip()
// }

g := new(GitGetter)
dst := tempDir(t)
// g := new(GitGetter)
// dst := tempDir(t)

encodedKey := base64.StdEncoding.EncodeToString([]byte(testGitToken))
// encodedKey := base64.StdEncoding.EncodeToString([]byte(testGitToken))

u, err := url.Parse("ssh://[email protected]/hashicorp/test-private-repo" +
"?sshkey=" + encodedKey)
if err != nil {
t.Fatal(err)
}
// u, err := url.Parse("ssh://[email protected]/hashicorp/test-private-repo" +
// "?sshkey=" + encodedKey)
// if err != nil {
// t.Fatal(err)
// }

if err := g.Get(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
// if err := g.Get(dst, u); err != nil {
// t.Fatalf("err: %s", err)
// }

readmePath := filepath.Join(dst, "README.md")
if _, err := os.Stat(readmePath); err != nil {
t.Fatalf("err: %s", err)
}
}
// readmePath := filepath.Join(dst, "README.md")
// if _, err := os.Stat(readmePath); err != nil {
// t.Fatalf("err: %s", err)
// }
// }

func TestGitGetter_submodule(t *testing.T) {
if !testHasGit {
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/hashicorp/go-getter

require (
cloud.google.com/go v0.36.0
github.com/aws/aws-sdk-go v1.15.78
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
github.com/cheggaaa/pb v1.0.27
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.0
github.com/hashicorp/go-safetemp v1.0.0
Expand All @@ -14,11 +14,9 @@ require (
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.0.0
github.com/mitchellh/go-testing-interface v1.0.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/ulikunitz/xz v0.5.5
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/api v0.1.0
gopkg.in/cheggaaa/pb.v1 v1.0.27 // indirect
)
Loading

0 comments on commit 9d45bf4

Please sign in to comment.