2017-11-02 20:51:03 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
2018-01-16 14:07:47 +03:00
"crypto/rand"
"fmt"
2017-11-02 20:51:03 +03:00
"io/ioutil"
2019-02-12 18:09:43 +03:00
"net/http"
2017-12-08 15:21:37 +03:00
"net/url"
2017-11-02 20:51:03 +03:00
"os"
2019-02-12 18:09:43 +03:00
"path"
2017-11-02 20:51:03 +03:00
"path/filepath"
"testing"
"time"
"code.gitea.io/git"
2019-02-12 18:09:43 +03:00
"code.gitea.io/gitea/models"
2017-11-02 20:51:03 +03:00
"github.com/stretchr/testify/assert"
)
2018-01-16 14:07:47 +03:00
const (
littleSize = 1024 //1ko
bigSize = 128 * 1024 * 1024 //128Mo
)
2019-02-04 02:56:53 +03:00
func TestGit ( t * testing . T ) {
onGiteaRun ( t , testGit )
}
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
func testGit ( t * testing . T , u * url . URL ) {
username := "user2"
baseAPITestContext := NewAPITestContext ( t , username , "repo1" )
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
u . Path = baseAPITestContext . GitPath ( )
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
t . Run ( "HTTP" , func ( t * testing . T ) {
httpContext := baseAPITestContext
httpContext . Reponame = "repo-tmp-17"
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
dstPath , err := ioutil . TempDir ( "" , httpContext . Reponame )
2019-02-12 18:09:43 +03:00
var little , big , littleLFS , bigLFS string
2019-02-04 02:56:53 +03:00
assert . NoError ( t , err )
defer os . RemoveAll ( dstPath )
t . Run ( "Standard" , func ( t * testing . T ) {
ensureAnonymousClone ( t , u )
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
t . Run ( "CreateRepo" , doAPICreateRepository ( httpContext , false ) )
2017-11-02 20:51:03 +03:00
2019-02-04 02:56:53 +03:00
u . Path = httpContext . GitPath ( )
u . User = url . UserPassword ( username , userPassword )
2017-12-08 15:21:37 +03:00
2019-02-04 02:56:53 +03:00
t . Run ( "Clone" , doGitClone ( dstPath , u ) )
2017-12-08 15:21:37 +03:00
2019-02-04 02:56:53 +03:00
t . Run ( "PushCommit" , func ( t * testing . T ) {
t . Run ( "Little" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
little = commitAndPush ( t , littleSize , dstPath )
2017-12-08 15:21:37 +03:00
} )
2019-02-04 02:56:53 +03:00
t . Run ( "Big" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
big = commitAndPush ( t , bigSize , dstPath )
2018-01-16 14:07:47 +03:00
} )
2017-12-08 15:21:37 +03:00
} )
2019-02-04 02:56:53 +03:00
} )
t . Run ( "LFS" , func ( t * testing . T ) {
t . Run ( "PushCommit" , func ( t * testing . T ) {
//Setup git LFS
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "install" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "track" , "data-file-*" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
err = git . AddChanges ( dstPath , false , ".gitattributes" )
assert . NoError ( t , err )
t . Run ( "Little" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
littleLFS = commitAndPush ( t , littleSize , dstPath )
2017-12-08 15:21:37 +03:00
} )
2019-02-04 02:56:53 +03:00
t . Run ( "Big" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
bigLFS = commitAndPush ( t , bigSize , dstPath )
2017-12-08 15:21:37 +03:00
} )
} )
2019-02-04 02:56:53 +03:00
t . Run ( "Locks" , func ( t * testing . T ) {
lockTest ( t , u . String ( ) , dstPath )
2017-12-08 15:21:37 +03:00
} )
2019-02-04 02:56:53 +03:00
} )
2019-02-12 18:09:43 +03:00
t . Run ( "Raw" , func ( t * testing . T ) {
session := loginUser ( t , "user2" )
// Request raw paths
req := NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/raw/branch/master/" , little ) )
resp := session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/raw/branch/master/" , big ) )
nilResp := session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , nilResp . Length )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/raw/branch/master/" , littleLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . NotEqual ( t , littleSize , resp . Body . Len ( ) )
assert . Contains ( t , resp . Body . String ( ) , models . LFSMetaFileIdentifier )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/raw/branch/master/" , bigLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . NotEqual ( t , bigSize , resp . Body . Len ( ) )
assert . Contains ( t , resp . Body . String ( ) , models . LFSMetaFileIdentifier )
} )
t . Run ( "Media" , func ( t * testing . T ) {
session := loginUser ( t , "user2" )
// Request media paths
req := NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/media/branch/master/" , little ) )
resp := session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Length )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/media/branch/master/" , big ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Length )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/media/branch/master/" , littleLFS ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Length )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-17/media/branch/master/" , bigLFS ) )
resp = session . MakeRequestNilResponseRecorder ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Length )
} )
2019-02-04 02:56:53 +03:00
} )
t . Run ( "SSH" , func ( t * testing . T ) {
sshContext := baseAPITestContext
sshContext . Reponame = "repo-tmp-18"
keyname := "my-testing-key"
//Setup key the user ssh key
withKeyFile ( t , keyname , func ( keyFile string ) {
t . Run ( "CreateUserKey" , doAPICreateUserKey ( sshContext , "test-key" , keyFile ) )
2018-01-16 14:07:47 +03:00
2019-02-04 02:56:53 +03:00
//Setup remote link
sshURL := createSSHUrl ( sshContext . GitPath ( ) , u )
2018-01-16 14:07:47 +03:00
//Setup clone folder
2019-02-04 02:56:53 +03:00
dstPath , err := ioutil . TempDir ( "" , sshContext . Reponame )
2018-01-16 14:07:47 +03:00
assert . NoError ( t , err )
defer os . RemoveAll ( dstPath )
2019-02-12 18:09:43 +03:00
var little , big , littleLFS , bigLFS string
2018-01-16 14:07:47 +03:00
t . Run ( "Standard" , func ( t * testing . T ) {
2019-02-04 02:56:53 +03:00
t . Run ( "CreateRepo" , doAPICreateRepository ( sshContext , false ) )
2018-01-16 14:07:47 +03:00
//TODO get url from api
2019-02-04 02:56:53 +03:00
t . Run ( "Clone" , doGitClone ( dstPath , sshURL ) )
2018-01-16 14:07:47 +03:00
//time.Sleep(5 * time.Minute)
t . Run ( "PushCommit" , func ( t * testing . T ) {
t . Run ( "Little" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
little = commitAndPush ( t , littleSize , dstPath )
2018-01-16 14:07:47 +03:00
} )
t . Run ( "Big" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
big = commitAndPush ( t , bigSize , dstPath )
2018-01-16 14:07:47 +03:00
} )
} )
2017-12-08 15:21:37 +03:00
} )
2018-01-16 14:07:47 +03:00
t . Run ( "LFS" , func ( t * testing . T ) {
t . Run ( "PushCommit" , func ( t * testing . T ) {
//Setup git LFS
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "install" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "track" , "data-file-*" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
err = git . AddChanges ( dstPath , false , ".gitattributes" )
assert . NoError ( t , err )
2017-11-02 20:51:03 +03:00
2018-01-16 14:07:47 +03:00
t . Run ( "Little" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
littleLFS = commitAndPush ( t , littleSize , dstPath )
2018-01-16 14:07:47 +03:00
} )
t . Run ( "Big" , func ( t * testing . T ) {
2019-02-12 18:09:43 +03:00
bigLFS = commitAndPush ( t , bigSize , dstPath )
2018-01-16 14:07:47 +03:00
} )
} )
t . Run ( "Locks" , func ( t * testing . T ) {
2018-01-27 19:48:15 +03:00
lockTest ( t , u . String ( ) , dstPath )
2018-01-16 14:07:47 +03:00
} )
} )
2019-02-12 18:09:43 +03:00
t . Run ( "Raw" , func ( t * testing . T ) {
session := loginUser ( t , "user2" )
// Request raw paths
req := NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/raw/branch/master/" , little ) )
resp := session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/raw/branch/master/" , big ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/raw/branch/master/" , littleLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . NotEqual ( t , littleSize , resp . Body . Len ( ) )
assert . Contains ( t , resp . Body . String ( ) , models . LFSMetaFileIdentifier )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/raw/branch/master/" , bigLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . NotEqual ( t , bigSize , resp . Body . Len ( ) )
assert . Contains ( t , resp . Body . String ( ) , models . LFSMetaFileIdentifier )
} )
t . Run ( "Media" , func ( t * testing . T ) {
session := loginUser ( t , "user2" )
// Request media paths
req := NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/media/branch/master/" , little ) )
resp := session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/media/branch/master/" , big ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/media/branch/master/" , littleLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , littleSize , resp . Body . Len ( ) )
req = NewRequest ( t , "GET" , path . Join ( "/user2/repo-tmp-18/media/branch/master/" , bigLFS ) )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , bigSize , resp . Body . Len ( ) )
} )
2019-02-04 02:56:53 +03:00
2017-12-08 15:21:37 +03:00
} )
2019-02-04 02:56:53 +03:00
2017-11-02 20:51:03 +03:00
} )
}
2018-01-16 14:07:47 +03:00
2019-02-04 02:56:53 +03:00
func ensureAnonymousClone ( t * testing . T , u * url . URL ) {
dstLocalPath , err := ioutil . TempDir ( "" , "repo1" )
assert . NoError ( t , err )
defer os . RemoveAll ( dstLocalPath )
t . Run ( "CloneAnonymous" , doGitClone ( dstLocalPath , u ) )
}
2018-01-16 14:07:47 +03:00
func lockTest ( t * testing . T , remote , repoPath string ) {
_ , err := git . NewCommand ( "remote" ) . AddArguments ( "set-url" , "origin" , remote ) . RunInDir ( repoPath ) //TODO add test ssh git-lfs-creds
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "locks" ) . RunInDir ( repoPath )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "lock" , "README.md" ) . RunInDir ( repoPath )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "locks" ) . RunInDir ( repoPath )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "unlock" , "README.md" ) . RunInDir ( repoPath )
assert . NoError ( t , err )
}
2019-02-12 18:09:43 +03:00
func commitAndPush ( t * testing . T , size int , repoPath string ) string {
name , err := generateCommitWithNewData ( size , repoPath , "user2@example.com" , "User Two" )
2018-01-16 14:07:47 +03:00
assert . NoError ( t , err )
_ , err = git . NewCommand ( "push" ) . RunInDir ( repoPath ) //Push
assert . NoError ( t , err )
2019-02-12 18:09:43 +03:00
return name
2018-01-16 14:07:47 +03:00
}
2019-02-12 18:09:43 +03:00
func generateCommitWithNewData ( size int , repoPath , email , fullName string ) ( string , error ) {
2018-01-16 14:07:47 +03:00
//Generate random file
data := make ( [ ] byte , size )
_ , err := rand . Read ( data )
if err != nil {
2019-02-12 18:09:43 +03:00
return "" , err
2018-01-16 14:07:47 +03:00
}
tmpFile , err := ioutil . TempFile ( repoPath , "data-file-" )
if err != nil {
2019-02-12 18:09:43 +03:00
return "" , err
2018-01-16 14:07:47 +03:00
}
defer tmpFile . Close ( )
_ , err = tmpFile . Write ( data )
if err != nil {
2019-02-12 18:09:43 +03:00
return "" , err
2018-01-16 14:07:47 +03:00
}
//Commit
err = git . AddChanges ( repoPath , false , filepath . Base ( tmpFile . Name ( ) ) )
if err != nil {
2019-02-12 18:09:43 +03:00
return "" , err
2018-01-16 14:07:47 +03:00
}
err = git . CommitChanges ( repoPath , git . CommitChangesOptions {
Committer : & git . Signature {
Email : email ,
Name : fullName ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : email ,
Name : fullName ,
When : time . Now ( ) ,
} ,
Message : fmt . Sprintf ( "Testing commit @ %v" , time . Now ( ) ) ,
} )
2019-02-12 18:09:43 +03:00
return filepath . Base ( tmpFile . Name ( ) ) , err
2018-01-16 14:07:47 +03:00
}