Skip to content

Commit

Permalink
fix: remote helmfile from git::ssh source
Browse files Browse the repository at this point in the history
The remote helmfile feature introduced by #648 was unable to be sourced from private git repositories due to URL parsing issue in helmfile. This fixes that.

Ref #469 (comment)
  • Loading branch information
mumoshu committed Jun 25, 2019
1 parent cfe309e commit e51de37
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
15 changes: 10 additions & 5 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (e InvalidURLError) Error() string {
}

type Source struct {
Getter, Scheme, Host, Dir, File, RawQuery string
Getter, Scheme, User, Host, Dir, File, RawQuery string
}

func IsRemote(goGetterSrc string) bool {
Expand Down Expand Up @@ -135,6 +135,7 @@ func Parse(goGetterSrc string) (*Source, error) {

return &Source{
Getter: getter,
User: u.User.String(),
Scheme: u.Scheme,
Host: u.Host,
Dir: pathComponents[0],
Expand All @@ -154,6 +155,7 @@ func (r *Remote) Fetch(goGetterSrc string) (string, error) {

r.Logger.Debugf("getter: %s", u.Getter)
r.Logger.Debugf("scheme: %s", u.Scheme)
r.Logger.Debugf("user: %s", u.User)
r.Logger.Debugf("host: %s", u.Host)
r.Logger.Debugf("dir: %s", u.Dir)
r.Logger.Debugf("file: %s", u.File)
Expand Down Expand Up @@ -195,11 +197,14 @@ func (r *Remote) Fetch(goGetterSrc string) (string, error) {

if !cached {
var getterSrc string

if len(query) == 0 {
getterSrc = srcDir
if u.User != "" {
getterSrc = fmt.Sprintf("%s://%s@%s%s", u.Scheme, u.User, u.Host, u.Dir)
} else {
getterSrc = strings.Join([]string{srcDir, query}, "?")
getterSrc = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, u.Dir)
}

if len(query) > 0 {
getterSrc = strings.Join([]string{getterSrc, query}, "?")
}

if u.Getter != "" {
Expand Down
73 changes: 72 additions & 1 deletion pkg/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestRemote(t *testing.T) {
func TestRemote_HttpsGitHub(t *testing.T) {
cleanfs := map[string]string{
"path/to/home": "",
}
Expand Down Expand Up @@ -85,6 +85,77 @@ func TestRemote(t *testing.T) {
}
}

func TestRemote_SShGitHub(t *testing.T) {
cleanfs := map[string]string{
"path/to/home": "",
}
cachefs := map[string]string{
"path/to/home/.helmfile/cache/ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml": "foo: bar",
}

type testcase struct {
files map[string]string
expectCacheHit bool
}

testcases := []testcase{
{files: cleanfs, expectCacheHit: false},
{files: cachefs, expectCacheHit: true},
}

for i := range testcases {
testcase := testcases[i]

t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
testfs := testhelper.NewTestFs(testcase.files)

hit := true

get := func(wd, src, dst string) error {
if wd != "path/to/home" {
return fmt.Errorf("unexpected wd: %s", wd)
}
if src != "git::ssh://[email protected]/cloudposse/helmfiles.git?ref=0.40.0" {
return fmt.Errorf("unexpected src: %s", src)
}

hit = false

return nil
}

getter := &testGetter{
get: get,
}
remote := &Remote{
Logger: helmexec.NewLogger(os.Stderr, "debug"),
Home: "path/to/home",
Getter: getter,
ReadFile: testfs.ReadFile,
FileExists: testfs.FileExistsAt,
DirExists: testfs.DirectoryExistsAt,
}

url := "git::ssh://[email protected]/cloudposse/helmfiles.git@releases/kiam.yaml?ref=0.40.0"
file, err := remote.Locate(url)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if file != "path/to/home/.helmfile/cache/ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml" {
t.Errorf("unexpected file located: %s", file)
}

if testcase.expectCacheHit && !hit {
t.Errorf("unexpected result: unexpected cache miss")
}
if !testcase.expectCacheHit && hit {
t.Errorf("unexpected result: unexpected cache hit")
}
})
}
}

type testGetter struct {
get func(wd, src, dst string) error
}
Expand Down

1 comment on commit e51de37

@Dustin77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestRemote

Please sign in to comment.