Skip to content

Commit

Permalink
Add some tests for pkg/git/git.com
Browse files Browse the repository at this point in the history
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 <chmouel@chmouel.com>
  • Loading branch information
chmouel authored and tekton-robot committed Nov 30, 2020
1 parent 4fdd018 commit d15def7
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
152 changes: 151 additions & 1 deletion pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}

0 comments on commit d15def7

Please sign in to comment.