2019-02-04 05:26:53 +05:30
// 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.
2022-09-03 00:48:23 +05:30
package integration
2019-02-04 05:26:53 +05:30
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
2019-10-12 05:43:27 +05:30
"path"
2019-02-04 05:26:53 +05:30
"path/filepath"
2021-11-06 11:53:32 +05:30
"strconv"
2019-02-04 05:26:53 +05:30
"testing"
"time"
2019-03-27 15:03:00 +05:30
"code.gitea.io/gitea/modules/git"
2019-02-04 05:26:53 +05:30
"code.gitea.io/gitea/modules/setting"
2019-02-07 12:43:12 +05:30
"code.gitea.io/gitea/modules/ssh"
2020-08-12 01:35:34 +05:30
"code.gitea.io/gitea/modules/util"
2022-09-03 00:48:23 +05:30
"code.gitea.io/gitea/tests"
2019-08-23 22:10:30 +05:30
2019-02-04 05:26:53 +05:30
"github.com/stretchr/testify/assert"
)
func withKeyFile ( t * testing . T , keyname string , callback func ( string ) ) {
2022-09-04 20:44:53 +05:30
tmpDir := t . TempDir ( )
2019-08-14 16:49:13 +05:30
2022-09-04 20:44:53 +05:30
err := os . Chmod ( tmpDir , 0 o700 )
2019-08-14 16:49:13 +05:30
assert . NoError ( t , err )
keyFile := filepath . Join ( tmpDir , keyname )
err = ssh . GenKeyPair ( keyFile )
2019-02-04 05:26:53 +05:30
assert . NoError ( t , err )
2021-09-22 11:08:34 +05:30
err = os . WriteFile ( path . Join ( tmpDir , "ssh" ) , [ ] byte ( "#!/bin/bash\n" +
2022-01-20 23:16:10 +05:30
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \"" + keyFile + "\" \"$@\"" ) , 0 o700 )
2019-10-12 05:43:27 +05:30
assert . NoError ( t , err )
2022-01-20 23:16:10 +05:30
// Setup ssh wrapper
2019-10-12 05:43:27 +05:30
os . Setenv ( "GIT_SSH" , path . Join ( tmpDir , "ssh" ) )
2019-02-04 05:26:53 +05:30
os . Setenv ( "GIT_SSH_COMMAND" ,
2019-08-17 03:58:55 +05:30
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \"" + keyFile + "\"" )
2019-02-04 05:26:53 +05:30
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" )
2021-11-06 11:53:32 +05:30
u2 . Host = net . JoinHostPort ( setting . SSH . ListenHost , strconv . Itoa ( setting . SSH . ListenPort ) )
2019-02-04 05:26:53 +05:30
u2 . Path = gitPath
return & u2
}
2021-04-17 00:00:16 +05:30
func onGiteaRunTB ( t testing . TB , callback func ( testing . TB , * url . URL ) , prepare ... bool ) {
2019-11-14 02:36:35 +05:30
if len ( prepare ) == 0 || prepare [ 0 ] {
2022-09-03 00:48:23 +05:30
defer tests . PrepareTestEnv ( t , 1 ) ( )
2019-11-14 02:36:35 +05:30
}
2019-02-04 05:26:53 +05:30
s := http . Server {
2020-11-13 18:21:07 +05:30
Handler : c ,
2019-02-04 05:26:53 +05:30
}
u , err := url . Parse ( setting . AppURL )
assert . NoError ( t , err )
listener , err := net . Listen ( "tcp" , u . Host )
2019-12-15 21:51:16 +05:30
i := 0
for err != nil && i <= 10 {
time . Sleep ( 100 * time . Millisecond )
listener , err = net . Listen ( "tcp" , u . Host )
i ++
}
2019-02-04 05:26:53 +05:30
assert . NoError ( t , err )
2019-11-26 21:05:41 +05:30
u . Host = listener . Addr ( ) . String ( )
2019-02-04 05:26:53 +05:30
defer func ( ) {
ctx , cancel := context . WithTimeout ( context . Background ( ) , 2 * time . Minute )
s . Shutdown ( ctx )
cancel ( )
} ( )
go s . Serve ( listener )
2022-01-20 23:16:10 +05:30
// Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
2019-02-04 05:26:53 +05:30
callback ( t , u )
}
2021-04-17 00:00:16 +05:30
func onGiteaRun ( t * testing . T , callback func ( * testing . T , * url . URL ) , prepare ... bool ) {
onGiteaRunTB ( t , func ( t testing . TB , u * url . URL ) {
callback ( t . ( * testing . T ) , u )
} , prepare ... )
}
2019-02-04 05:26:53 +05:30
func doGitClone ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
assert . NoError ( t , git . CloneWithArgs ( context . Background ( ) , git . AllowLFSFiltersArgs ( ) , u . String ( ) , dstLocalPath , git . CloneRepoOptions { } ) )
2020-12-25 15:29:32 +05:30
exist , err := util . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) )
assert . NoError ( t , err )
assert . True ( t , exist )
2019-02-04 05:26:53 +05:30
}
}
2022-01-24 02:49:32 +05:30
func doPartialGitClone ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
assert . NoError ( t , git . CloneWithArgs ( context . Background ( ) , git . AllowLFSFiltersArgs ( ) , u . String ( ) , dstLocalPath , git . CloneRepoOptions {
2022-01-24 02:49:32 +05:30
Filter : "blob:none" ,
} ) )
exist , err := util . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) )
assert . NoError ( t , err )
assert . True ( t , exist )
}
}
2020-04-28 14:02:23 +05:30
func doGitCloneFail ( u * url . URL ) func ( * testing . T ) {
2019-02-04 05:26:53 +05:30
return func ( t * testing . T ) {
2022-09-04 20:44:53 +05:30
tmpDir := t . TempDir ( )
2022-01-20 04:56:57 +05:30
assert . Error ( t , git . Clone ( git . DefaultContext , u . String ( ) , tmpDir , git . CloneRepoOptions { } ) )
2020-12-25 15:29:32 +05:30
exist , err := util . IsExist ( filepath . Join ( tmpDir , "README.md" ) )
assert . NoError ( t , err )
assert . False ( t , exist )
2019-02-04 05:26:53 +05:30
}
}
func doGitInitTestRepository ( dstPath string ) func ( * testing . T ) {
return func ( t * testing . T ) {
// Init repository in dstPath
2022-01-20 04:56:57 +05:30
assert . NoError ( t , git . InitRepository ( git . DefaultContext , dstPath , false ) )
2021-12-21 02:25:05 +05:30
// forcibly set default branch to master
2022-04-01 08:25:30 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , "symbolic-ref" , "HEAD" , git . BranchPrefix + "master" ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2021-12-21 02:25:05 +05:30
assert . NoError ( t , err )
2022-01-20 23:16:10 +05:30
assert . NoError ( t , os . WriteFile ( filepath . Join ( dstPath , "README.md" ) , [ ] byte ( fmt . Sprintf ( "# Testing Repository\n\nOriginally created in: %s" , dstPath ) ) , 0 o644 ) )
2019-02-04 05:26:53 +05:30
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 ) {
2022-10-23 20:14:45 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , "remote" , "add" ) . AddDynamicArguments ( remoteName , u . String ( ) ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-02-04 05:26:53 +05:30
assert . NoError ( t , err )
}
}
2022-10-23 20:14:45 +05:30
func doGitPushTestRepository ( dstPath string , args ... git . CmdArg ) func ( * testing . T ) {
2019-02-04 05:26:53 +05:30
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , append ( [ ] git . CmdArg { "push" , "-u" } , args ... ) ... ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-02-04 05:26:53 +05:30
assert . NoError ( t , err )
}
}
2022-10-23 20:14:45 +05:30
func doGitPushTestRepositoryFail ( dstPath string , args ... git . CmdArg ) func ( * testing . T ) {
2019-02-04 05:26:53 +05:30
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , append ( [ ] git . CmdArg { "push" } , args ... ) ... ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-02-04 05:26:53 +05:30
assert . Error ( t , err )
}
}
2019-05-31 15:42:15 +05:30
func doGitCreateBranch ( dstPath , branch string ) func ( * testing . T ) {
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , "checkout" , "-b" ) . AddDynamicArguments ( branch ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
}
}
2022-10-23 20:14:45 +05:30
func doGitCheckoutBranch ( dstPath string , args ... git . CmdArg ) func ( * testing . T ) {
2019-05-31 15:42:15 +05:30
return func ( t * testing . T ) {
2022-04-01 08:25:30 +05:30
_ , _ , err := git . NewCommandNoGlobals ( append ( append ( git . AllowLFSFiltersArgs ( ) , "checkout" ) , args ... ) ... ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
}
}
2022-10-23 20:14:45 +05:30
func doGitMerge ( dstPath string , args ... git . CmdArg ) func ( * testing . T ) {
2019-05-31 15:42:15 +05:30
return func ( t * testing . T ) {
2022-10-23 20:14:45 +05:30
_ , _ , err := git . NewCommand ( git . DefaultContext , append ( [ ] git . CmdArg { "merge" } , args ... ) ... ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
}
}
2022-10-23 20:14:45 +05:30
func doGitPull ( dstPath string , args ... git . CmdArg ) func ( * testing . T ) {
2019-05-31 15:42:15 +05:30
return func ( t * testing . T ) {
2022-04-01 08:25:30 +05:30
_ , _ , err := git . NewCommandNoGlobals ( append ( append ( git . AllowLFSFiltersArgs ( ) , "pull" ) , args ... ) ... ) . RunStdString ( & git . RunOpts { Dir : dstPath } )
2019-05-31 15:42:15 +05:30
assert . NoError ( t , err )
}
}