From d15def74e31c2ce01ee3eeec08d578c6eca1fd38 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Sat, 14 Nov 2020 16:25:00 +0100 Subject: [PATCH] Add some tests for pkg/git/git.com Add some more tests for pkg/git/git.go We are creating a fake local git repo where to clone from to avoid network flakiness. Signed-off-by: Chmouel Boudjnah --- pkg/git/git.go | 6 +- pkg/git/git_test.go | 152 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 4 deletions(-) diff --git a/pkg/git/git.go b/pkg/git/git.go index 540df6b53e9..24f8a4c74e5 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -76,7 +76,7 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error { if err := ensureHomeEnv(logger); err != nil { return err } - validateGitAuth(logger, spec.URL) + validateGitAuth(logger, pipeline.CredsDir, spec.URL) if spec.Path != "" { if _, err := run(logger, "", "init", spec.Path); err != nil { @@ -261,9 +261,9 @@ func userHasKnownHostsFile(logger *zap.SugaredLogger) (bool, error) { return true, nil } -func validateGitAuth(logger *zap.SugaredLogger, url string) { +func validateGitAuth(logger *zap.SugaredLogger, credsDir, url string) { sshCred := true - if _, err := os.Stat(pipeline.CredsDir + "/.ssh"); os.IsNotExist(err) { + if _, err := os.Stat(credsDir + "/.ssh"); os.IsNotExist(err) { sshCred = false } urlSSHFormat := ValidateGitSSHURLFormat(url) diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index f685eb79add..9ef5bb372e8 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -15,7 +15,15 @@ limitations under the License. */ package git -import "testing" +import ( + "io/ioutil" + "os" + "strings" + "testing" + + "go.uber.org/zap" + "go.uber.org/zap/zaptest/observer" +) func TestValidateGitSSHURLFormat(t *testing.T) { tests := []struct { @@ -103,3 +111,145 @@ func TestValidateGitSSHURLFormat(t *testing.T) { } } } + +func Test_validateGitAuth(t *testing.T) { + tests := []struct { + name string + url string + logMessage string + wantSSHdir bool + }{ + { + name: "Validate HTTP Auth", + url: "http://google.com", + logMessage: "", + wantSSHdir: false, + }, + { + name: "SSH URL but no SSH credentials", + url: "ssh://git@github.com:chmouel/tekton", + logMessage: "URL(\"ssh://git@github.com:chmouel/tekton\") appears to need SSH authentication but no SSH credentials have been provided", + wantSSHdir: false, + }, + { + name: "Validate SSH Auth", + url: "http://github.com/chmouel/tekton", + logMessage: "SSH credentials have been provided but the URL(\"http://github.com/chmouel/tekton\") is not a valid SSH URL. This warning can be safely ignored if the URL is for a public repo or you are using basic auth", + wantSSHdir: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + observer, log := observer.New(zap.InfoLevel) + logger := zap.New(observer).Sugar() + credsDir, cleanup := createTempDir(t) + defer cleanup() + if tt.wantSSHdir { + os.MkdirAll(credsDir+"/.ssh", 0755) + } + + validateGitAuth(logger, credsDir, tt.url) + if tt.logMessage != "" { + takeAll := log.TakeAll() + if len(takeAll) == 0 { + t.Fatal("We didn't receive any logging") + } + gotmsg := takeAll[0].Message + if tt.logMessage != gotmsg { + t.Errorf("log message: '%s'\n should be '%s'", tt.logMessage, gotmsg) + } + } + }) + } +} + +func TestFetch(t *testing.T) { + tests := []struct { + name string + logMessage string + spec FetchSpec + wantErr bool + }{ + { + name: "test-good", + logMessage: "Successfully cloned", + wantErr: false, + spec: FetchSpec{ + URL: "", + Revision: "", + Refspec: "", + Path: "", + Depth: 0, + Submodules: false, + SSLVerify: false, + HTTPProxy: "", + HTTPSProxy: "", + NOProxy: "", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + observer, log := observer.New(zap.InfoLevel) + logger := zap.New(observer).Sugar() + + gitDir, cleanup := createTempDir(t) + defer cleanup() + createTempGit(t, logger, gitDir) + tt.spec.URL = gitDir + + targetPath, cleanup2 := createTempDir(t) + defer cleanup2() + tt.spec.Path = targetPath + + if err := Fetch(logger, tt.spec); (err != nil) != tt.wantErr { + t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr) + } + + if tt.logMessage != "" { + takeAll := log.TakeAll() + if len(takeAll) == 0 { + t.Fatal("We didn't receive any logging") + } + gotmsg := takeAll[0].Message + if !strings.Contains(gotmsg, tt.logMessage) { + t.Errorf("log message: '%s'\n should contains: '%s'", tt.logMessage, gotmsg) + } + } + }) + } +} + +func createTempDir(t *testing.T) (string, func()) { + dir, err := ioutil.TempDir("", "git-init-") + if err != nil { + t.Fatalf("unexpected error creating temp directory: %v", err) + } + return dir, func() { + if err := os.RemoveAll(dir); err != nil { + t.Errorf("unexpected error cleaning up temp directory: %v", err) + } + } +} + +// Create a temporary Git dir locally for testing against instead of using a potentially flaky remote URL. +func createTempGit(t *testing.T, logger *zap.SugaredLogger, gitDir string) { + if _, err := run(logger, "", "init", gitDir); err != nil { + t.Fatal(err) + } + if err := os.Chdir(gitDir); err != nil { + t.Fatalf("failed to change directory with path %s; err: %v", gitDir, err) + } + if _, err := run(logger, "", "checkout", "-b", "main"); err != nil { + t.Fatal(err) + } + + // Not defining globally so we don't mess with the global gitconfig + if _, err := run(logger, "", "config", "user.email", "tester@tekton.dev"); err != nil { + t.Fatal(err) + } + + if _, err := run(logger, "", "commit", "--allow-empty", "-m", "Hello Moto"); err != nil { + t.Fatal(err.Error()) + } +}