@ -6,8 +6,9 @@ package integrations
import (
import (
"context"
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"io/ioutil"
"math/rand"
"net"
"net"
"net/http"
"net/http"
"net/url"
"net/url"
@ -17,6 +18,7 @@ import (
"time"
"time"
"code.gitea.io/git"
"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/sdk/gitea"
api "code.gitea.io/sdk/gitea"
@ -24,7 +26,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
)
func onGiteaWebRun ( t * testing . T , callback func ( * testing . T , * url . URL ) ) {
const (
littleSize = 1024 //1ko
bigSize = 128 * 1024 * 1024 //128Mo
)
func onGiteaRun ( t * testing . T , callback func ( * testing . T , * url . URL ) ) {
prepareTestEnv ( t )
s := http . Server {
s := http . Server {
Handler : mac ,
Handler : mac ,
}
}
@ -35,151 +43,241 @@ func onGiteaWebRun(t *testing.T, callback func(*testing.T, *url.URL)) {
assert . NoError ( t , err )
assert . NoError ( t , err )
defer func ( ) {
defer func ( ) {
ctx , cancel := context . WithTimeout ( context . Background ( ) , time . Second * 10 )
ctx , cancel := context . WithTimeout ( context . Background ( ) , 2 * time . Minute )
s . Shutdown ( ctx )
s . Shutdown ( ctx )
cancel ( )
cancel ( )
} ( )
} ( )
go s . Serve ( listener )
go s . Serve ( listener )
//Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
callback ( t , u )
callback ( t , u )
}
}
func TestGit ( t * testing . T ) {
func TestGit ( t * testing . T ) {
prepareTestEnv ( t )
onGiteaRun ( t , func ( t * testing . T , u * url . URL ) {
onGiteaWebRun ( t , func ( t * testing . T , u * url . URL ) {
dstPath , err := ioutil . TempDir ( "" , "repo-tmp-17" )
assert . NoError ( t , err )
defer os . RemoveAll ( dstPath )
u . Path = "user2/repo1.git"
u . Path = "user2/repo1.git"
t . Run ( "Standard" , func ( t * testing . T ) {
t . Run ( "HTTP" , func ( t * testing . T ) {
dstPath , err := ioutil . TempDir ( "" , "repo-tmp-17" )
assert . NoError ( t , err )
defer os . RemoveAll ( dstPath )
t . Run ( "Standard" , func ( t * testing . T ) {
t . Run ( "CloneNoLogin" , func ( t * testing . T ) {
dstLocalPath , err := ioutil . TempDir ( "" , "repo1" )
assert . NoError ( t , err )
defer os . RemoveAll ( dstLocalPath )
err = git . Clone ( u . String ( ) , dstLocalPath , git . CloneRepoOptions { } )
assert . NoError ( t , err )
assert . True ( t , com . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) ) )
} )
t . Run ( "CloneNoLogin" , func ( t * testing . T ) {
t . Run ( "CreateRepo" , func ( t * testing . T ) {
dstLocalPath , err := ioutil . TempDir ( "" , "repo1" )
session := loginUser ( t , "user2" )
assert . NoError ( t , err )
req := NewRequestWithJSON ( t , "POST" , "/api/v1/user/repos" , & api . CreateRepoOption {
defer os . RemoveAll ( dstLocalPath )
AutoInit : true ,
err = git . Clone ( u . String ( ) , dstLocalPath , git . CloneRepoOptions { } )
Description : "Temporary repo" ,
assert . NoError ( t , err )
Name : "repo-tmp-17" ,
assert . True ( t , com . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) ) )
Private : false ,
} )
Gitignores : "" ,
License : "WTFPL" ,
Readme : "Default" ,
} )
session . MakeRequest ( t , req , http . StatusCreated )
} )
t . Run ( "CreateRepo" , func ( t * testing . T ) {
u . Path = "user2/repo-tmp-17.git"
session := loginUser ( t , "user2" )
u . User = url . UserPassword ( "user2" , userPassword )
req := NewRequestWithJSON ( t , "POST" , "/api/v1/user/repos" , & api . CreateRepoOption {
t . Run ( "Clone" , func ( t * testing . T ) {
AutoInit : true ,
err = git . Clone ( u . String ( ) , dstPath , git . CloneRepoOptions { } )
Description : "Temporary repo" ,
assert . NoError ( t , err )
Name : "repo-tmp-17" ,
assert . True ( t , com . IsExist ( filepath . Join ( dstPath , "README.md" ) ) )
Private : false ,
Gitignores : "" ,
License : "WTFPL" ,
Readme : "Default" ,
} )
} )
session . MakeRequest ( t , req , http . StatusCreated )
} )
u . Path = "user2/repo-tmp-17.git"
t . Run ( "PushCommit" , func ( t * testing . T ) {
u . User = url . UserPassword ( "user2" , userPassword )
t . Run ( "Little" , func ( t * testing . T ) {
t . Run ( "Clone" , func ( t * testing . T ) {
commitAndPush ( t , littleSize , dstPath )
err = git . Clone ( u . String ( ) , dstPath , git . CloneRepoOptions { } )
} )
assert . NoError ( t , err )
t . Run ( "Big" , func ( t * testing . T ) {
assert . True ( t , com . IsExist ( filepath . Join ( dstPath , "README.md" ) ) )
commitAndPush ( t , bigSize , dstPath )
} )
} )
} )
} )
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 ( "PushCommit" , func ( t * testing . T ) {
t . Run ( "Little" , func ( t * testing . T ) {
data := make ( [ ] byte , 1024 )
commitAndPush ( t , littleSize , dstPath )
_ , err := rand . Read ( data )
} )
assert . NoError ( t , err )
t . Run ( "Big" , func ( t * testing . T ) {
tmpFile , err := ioutil . TempFile ( dstPath , "data-file-" )
commitAndPush ( t , bigSize , dstPath )
defer tmpFile . Close ( )
} )
_ , err = tmpFile . Write ( data )
assert . NoError ( t , err )
//Commit
err = git . AddChanges ( dstPath , false , filepath . Base ( tmpFile . Name ( ) ) )
assert . NoError ( t , err )
err = git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Message : "Testing commit" ,
} )
} )
assert . NoError ( t , err )
t . Run ( "Locks" , func ( t * testing . T ) {
lockTest ( t , u . String ( ) , dstPath )
//Push
err = git . Push ( dstPath , git . PushOptions {
Branch : "master" ,
Remote : u . String ( ) ,
Force : false ,
} )
} )
assert . NoError ( t , err )
} )
} )
} )
} )
t . Run ( "LFS" , func ( t * testing . T ) {
t . Run ( "SSH" , func ( t * testing . T ) {
t . Run ( "PushCommit" , func ( t * testing . T ) {
//Setup remote link
/* Generate random file */
u . Scheme = "ssh"
data := make ( [ ] byte , 1024 )
u . User = url . User ( "git" )
_ , err := rand . Read ( data )
u . Host = fmt . Sprintf ( "%s:%d" , setting . SSH . ListenHost , setting . SSH . ListenPort )
assert . NoError ( t , err )
u . Path = "user2/repo-tmp-18.git"
tmpFile , err := ioutil . TempFile ( dstPath , "data-file-" )
defer tmpFile . Close ( )
_ , err = tmpFile . Write ( data )
assert . NoError ( t , err )
//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 )
//Commit
err = git . AddChanges ( dstPath , false , ".gitattributes" , filepath . Base ( tmpFile . Name ( ) ) )
assert . NoError ( t , err )
err = git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Author : & git . Signature {
Email : "user2@example.com" ,
Name : "User Two" ,
When : time . Now ( ) ,
} ,
Message : "Testing LFS " ,
} )
assert . NoError ( t , err )
//Push
//Setup key
u . User = url . UserPassword ( "user2" , userPassword )
keyFile := filepath . Join ( setting . AppDataPath , "my-testing-key" )
err = git . Push ( dstPath , git . PushOptions {
_ , _ , err := com . ExecCmd ( "ssh-keygen" , "-f" , keyFile , "-t" , "rsa" , "-N" , "" )
Branch : "master" ,
assert . NoError ( t , err )
Remote : u . String ( ) ,
defer os . RemoveAll ( keyFile )
Force : false ,
defer os . RemoveAll ( keyFile + ".pub" )
} )
assert . NoError ( t , err )
session := loginUser ( t , "user1" )
keyOwner := models . AssertExistsAndLoadBean ( t , & models . User { Name : "user2" } ) . ( * models . User )
urlStr := fmt . Sprintf ( "/api/v1/admin/users/%s/keys" , keyOwner . Name )
dataPubKey , err := ioutil . ReadFile ( keyFile + ".pub" )
assert . NoError ( t , err )
req := NewRequestWithValues ( t , "POST" , urlStr , map [ string ] string {
"key" : string ( dataPubKey ) ,
"title" : "test-key" ,
} )
} )
t . Run ( "Locks" , func ( t * testing . T ) {
session . MakeRequest ( t , req , http . StatusCreated )
_ , err = git . NewCommand ( "remote" ) . AddArguments ( "set-url" , "origin" , u . String ( ) ) . RunInDir ( dstPath ) //TODO add test ssh git-lfs-creds
assert . NoError ( t , err )
//Setup ssh wrapper
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "locks" ) . RunInDir ( dstPath )
sshWrapper , err := ioutil . TempFile ( setting . AppDataPath , "tmp-ssh-wrapper" )
assert . NoError ( t , err )
sshWrapper . WriteString ( "#!/bin/sh\n\n" )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "lock" , "README.md" ) . RunInDir ( dstPath )
sshWrapper . WriteString ( "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i \"" + filepath . Join ( setting . AppWorkPath , keyFile ) + "\" $* \n\n" )
assert . NoError ( t , err )
err = sshWrapper . Chmod ( os . ModePerm )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "locks" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
assert . NoError ( t , err )
sshWrapper . Close ( )
_ , err = git . NewCommand ( "lfs" ) . AddArguments ( "unlock" , "README.md" ) . RunInDir ( dstPath )
defer os . RemoveAll ( sshWrapper . Name ( ) )
assert . NoError ( t , err )
//Setup clone folder
dstPath , err := ioutil . TempDir ( "" , "repo-tmp-18" )
assert . NoError ( t , err )
defer os . RemoveAll ( dstPath )
t . Run ( "Standard" , func ( t * testing . T ) {
t . Run ( "CreateRepo" , func ( t * testing . T ) {
session := loginUser ( t , "user2" )
req := NewRequestWithJSON ( t , "POST" , "/api/v1/user/repos" , & api . CreateRepoOption {
AutoInit : true ,
Description : "Temporary repo" ,
Name : "repo-tmp-18" ,
Private : false ,
Gitignores : "" ,
License : "WTFPL" ,
Readme : "Default" ,
} )
session . MakeRequest ( t , req , http . StatusCreated )
} )
//TODO get url from api
t . Run ( "Clone" , func ( t * testing . T ) {
_ , err = git . NewCommand ( "clone" ) . AddArguments ( "--config" , "core.sshCommand=" + filepath . Join ( setting . AppWorkPath , sshWrapper . Name ( ) ) , u . String ( ) , dstPath ) . Run ( )
assert . NoError ( t , err )
assert . True ( t , com . IsExist ( filepath . Join ( dstPath , "README.md" ) ) )
} )
//time.Sleep(5 * time.Minute)
t . Run ( "PushCommit" , func ( t * testing . T ) {
t . Run ( "Little" , func ( t * testing . T ) {
commitAndPush ( t , littleSize , dstPath )
} )
t . Run ( "Big" , func ( t * testing . T ) {
commitAndPush ( t , bigSize , dstPath )
} )
} )
} )
} )
t . Run ( "LFS" , func ( t * testing . T ) {
os . Setenv ( "GIT_SSH_COMMAND" , filepath . Join ( setting . AppWorkPath , sshWrapper . Name ( ) ) ) //TODO remove when fixed https://github.com/git-lfs/git-lfs/issues/2215
defer os . Unsetenv ( "GIT_SSH_COMMAND" )
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 ) {
commitAndPush ( t , littleSize , dstPath )
} )
t . Run ( "Big" , func ( t * testing . T ) {
commitAndPush ( t , bigSize , dstPath )
} )
} )
/ * Failed without # 3152. TODO activate with fix .
t . Run ( "Locks" , func ( t * testing . T ) {
lockTest ( t , u . String ( ) , dstPath )
} )
* /
} )
} )
} )
} )
} )
}
}
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 )
}
func commitAndPush ( t * testing . T , size int , repoPath string ) {
err := generateCommitWithNewData ( size , repoPath , "user2@example.com" , "User Two" )
assert . NoError ( t , err )
_ , err = git . NewCommand ( "push" ) . RunInDir ( repoPath ) //Push
assert . NoError ( t , err )
}
func generateCommitWithNewData ( size int , repoPath , email , fullName string ) error {
//Generate random file
data := make ( [ ] byte , size )
_ , err := rand . Read ( data )
if err != nil {
return err
}
tmpFile , err := ioutil . TempFile ( repoPath , "data-file-" )
if err != nil {
return err
}
defer tmpFile . Close ( )
_ , err = tmpFile . Write ( data )
if err != nil {
return err
}
//Commit
err = git . AddChanges ( repoPath , false , filepath . Base ( tmpFile . Name ( ) ) )
if err != nil {
return err
}
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 ( ) ) ,
} )
return err
}