2019-02-03 18:56:53 -05:00
// Copyright 2019 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 (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
2019-10-11 19:13:27 -05:00
"path"
2019-02-03 18:56:53 -05:00
"path/filepath"
2019-10-11 19:13:27 -05:00
"strings"
2019-02-03 18:56:53 -05:00
"testing"
"time"
2019-03-27 04:33:00 -05:00
"code.gitea.io/gitea/modules/git"
2019-02-03 18:56:53 -05:00
"code.gitea.io/gitea/modules/setting"
2019-02-07 02:13:12 -05:00
"code.gitea.io/gitea/modules/ssh"
2019-08-23 11:40:30 -05:00
2019-02-03 18:56:53 -05:00
"github.com/stretchr/testify/assert"
2019-08-23 11:40:30 -05:00
"github.com/unknwon/com"
2019-02-03 18:56:53 -05:00
)
func withKeyFile ( t * testing . T , keyname string , callback func ( string ) ) {
2019-08-14 06:19:13 -05:00
tmpDir , err := ioutil . TempDir ( "" , "key-file" )
assert . NoError ( t , err )
defer os . RemoveAll ( tmpDir )
err = os . Chmod ( tmpDir , 0700 )
assert . NoError ( t , err )
keyFile := filepath . Join ( tmpDir , keyname )
err = ssh . GenKeyPair ( keyFile )
2019-02-03 18:56:53 -05:00
assert . NoError ( t , err )
2019-10-11 19:13:27 -05:00
err = ioutil . WriteFile ( path . Join ( tmpDir , "ssh" ) , [ ] byte ( "#!/bin/bash\n" +
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \"" + keyFile + "\" \"$@\"" ) , 0700 )
assert . NoError ( t , err )
2019-02-03 18:56:53 -05:00
//Setup ssh wrapper
2019-10-11 19:13:27 -05:00
os . Setenv ( "GIT_SSH" , path . Join ( tmpDir , "ssh" ) )
2019-02-03 18:56:53 -05:00
os . Setenv ( "GIT_SSH_COMMAND" ,
2019-08-16 17:28:55 -05:00
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \"" + keyFile + "\"" )
2019-02-03 18:56:53 -05:00
os . Setenv ( "GIT_SSH_VARIANT" , "ssh" )
callback ( keyFile )
}
func createSSHUrl ( gitPath string , u * url . URL ) * url . URL {
u2 := * u
u2 . Scheme = "ssh"
u2 . User = url . User ( "git" )
u2 . Host = fmt . Sprintf ( "%s:%d" , setting . SSH . ListenHost , setting . SSH . ListenPort )
u2 . Path = gitPath
return & u2
}
2019-10-11 19:13:27 -05:00
func allowLFSFilters ( ) [ ] string {
// Now here we should explicitly allow lfs filters to run
globalArgs := git . GlobalCommandArgs
filteredLFSGlobalArgs := make ( [ ] string , len ( git . GlobalCommandArgs ) )
j := 0
for _ , arg := range git . GlobalCommandArgs {
if strings . Contains ( arg , "lfs" ) {
j --
} else {
filteredLFSGlobalArgs [ j ] = arg
j ++
}
}
filteredLFSGlobalArgs = filteredLFSGlobalArgs [ : j ]
git . GlobalCommandArgs = filteredLFSGlobalArgs
return globalArgs
}
2019-11-13 16:06:35 -05:00
func onGiteaRun ( t * testing . T , callback func ( * testing . T , * url . URL ) , prepare ... bool ) {
if len ( prepare ) == 0 || prepare [ 0 ] {
prepareTestEnv ( t , 1 )
}
2019-02-03 18:56:53 -05:00
s := http . Server {
Handler : mac ,
}
u , err := url . Parse ( setting . AppURL )
assert . NoError ( t , err )
listener , err := net . Listen ( "tcp" , u . Host )
assert . NoError ( t , err )
defer func ( ) {
ctx , cancel := context . WithTimeout ( context . Background ( ) , 2 * time . Minute )
s . Shutdown ( ctx )
cancel ( )
} ( )
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 )
}
func doGitClone ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
2019-10-11 19:13:27 -05:00
oldGlobals := allowLFSFilters ( )
2019-02-03 18:56:53 -05:00
assert . NoError ( t , git . Clone ( u . String ( ) , dstLocalPath , git . CloneRepoOptions { } ) )
2019-10-11 19:13:27 -05:00
git . GlobalCommandArgs = oldGlobals
2019-02-03 18:56:53 -05:00
assert . True ( t , com . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) ) )
}
}
func doGitCloneFail ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
assert . Error ( t , git . Clone ( u . String ( ) , dstLocalPath , git . CloneRepoOptions { } ) )
assert . False ( t , com . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) ) )
}
}
func doGitInitTestRepository ( dstPath string ) func ( * testing . T ) {
return func ( t * testing . T ) {
// Init repository in dstPath
assert . NoError ( t , git . InitRepository ( dstPath , false ) )
assert . NoError ( t , ioutil . WriteFile ( filepath . Join ( dstPath , "README.md" ) , [ ] byte ( fmt . Sprintf ( "# Testing Repository\n\nOriginally created in: %s" , dstPath ) ) , 0644 ) )
assert . NoError ( t , git . AddChanges ( dstPath , true ) )
signature := git . Signature {
Email : "test@example.com" ,
Name : "test" ,
When : time . Now ( ) ,
}
assert . NoError ( t , git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & signature ,
Author : & signature ,
Message : "Initial Commit" ,
} ) )
}
}
func doGitAddRemote ( dstPath , remoteName string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
_ , err := git . NewCommand ( "remote" , "add" , remoteName , u . String ( ) ) . RunInDir ( dstPath )
assert . NoError ( t , err )
}
}
2019-05-31 05:12:15 -05:00
func doGitPushTestRepository ( dstPath string , args ... string ) func ( * testing . T ) {
2019-02-03 18:56:53 -05:00
return func ( t * testing . T ) {
2019-05-31 05:12:15 -05:00
_ , err := git . NewCommand ( append ( [ ] string { "push" , "-u" } , args ... ) ... ) . RunInDir ( dstPath )
2019-02-03 18:56:53 -05:00
assert . NoError ( t , err )
}
}
2019-05-31 05:12:15 -05:00
func doGitPushTestRepositoryFail ( dstPath string , args ... string ) func ( * testing . T ) {
2019-02-03 18:56:53 -05:00
return func ( t * testing . T ) {
2019-05-31 05:12:15 -05:00
_ , err := git . NewCommand ( append ( [ ] string { "push" } , args ... ) ... ) . RunInDir ( dstPath )
2019-02-03 18:56:53 -05:00
assert . Error ( t , err )
}
}
2019-05-31 05:12:15 -05:00
func doGitCreateBranch ( dstPath , branch string ) func ( * testing . T ) {
return func ( t * testing . T ) {
_ , err := git . NewCommand ( "checkout" , "-b" , branch ) . RunInDir ( dstPath )
assert . NoError ( t , err )
}
}
func doGitCheckoutBranch ( dstPath string , args ... string ) func ( * testing . T ) {
return func ( t * testing . T ) {
2019-10-11 19:13:27 -05:00
oldGlobals := allowLFSFilters ( )
2019-05-31 05:12:15 -05:00
_ , err := git . NewCommand ( append ( [ ] string { "checkout" } , args ... ) ... ) . RunInDir ( dstPath )
2019-10-11 19:13:27 -05:00
git . GlobalCommandArgs = oldGlobals
2019-05-31 05:12:15 -05:00
assert . NoError ( t , err )
}
}
func doGitMerge ( dstPath string , args ... string ) func ( * testing . T ) {
return func ( t * testing . T ) {
_ , err := git . NewCommand ( append ( [ ] string { "merge" } , args ... ) ... ) . RunInDir ( dstPath )
assert . NoError ( t , err )
}
}
func doGitPull ( dstPath string , args ... string ) func ( * testing . T ) {
return func ( t * testing . T ) {
2019-10-11 19:13:27 -05:00
oldGlobals := allowLFSFilters ( )
2019-05-31 05:12:15 -05:00
_ , err := git . NewCommand ( append ( [ ] string { "pull" } , args ... ) ... ) . RunInDir ( dstPath )
2019-10-11 19:13:27 -05:00
git . GlobalCommandArgs = oldGlobals
2019-05-31 05:12:15 -05:00
assert . NoError ( t , err )
}
}