2019-02-04 00:56:53 +01: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"
"net"
"net/http"
"net/url"
"os"
2019-10-12 02:13:27 +02:00
"path"
2019-02-04 00:56:53 +01:00
"path/filepath"
2021-11-06 07:23:32 +01:00
"strconv"
2019-10-12 02:13:27 +02:00
"strings"
2019-02-04 00:56:53 +01:00
"testing"
"time"
2019-03-27 10:33:00 +01:00
"code.gitea.io/gitea/modules/git"
2019-02-04 00:56:53 +01:00
"code.gitea.io/gitea/modules/setting"
2019-02-07 08:13:12 +01:00
"code.gitea.io/gitea/modules/ssh"
2020-08-11 22:05:34 +02:00
"code.gitea.io/gitea/modules/util"
2019-08-23 18:40:30 +02:00
2019-02-04 00:56:53 +01:00
"github.com/stretchr/testify/assert"
)
func withKeyFile ( t * testing . T , keyname string , callback func ( string ) ) {
2019-08-14 13:19:13 +02:00
2021-09-22 07:38:34 +02:00
tmpDir , err := os . MkdirTemp ( "" , "key-file" )
2019-08-14 13:19:13 +02:00
assert . NoError ( t , err )
2020-08-11 22:05:34 +02:00
defer util . RemoveAll ( tmpDir )
2019-08-14 13:19:13 +02:00
err = os . Chmod ( tmpDir , 0700 )
assert . NoError ( t , err )
keyFile := filepath . Join ( tmpDir , keyname )
err = ssh . GenKeyPair ( keyFile )
2019-02-04 00:56:53 +01:00
assert . NoError ( t , err )
2021-09-22 07:38:34 +02:00
err = os . WriteFile ( path . Join ( tmpDir , "ssh" ) , [ ] byte ( "#!/bin/bash\n" +
2019-10-12 02:13:27 +02:00
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \"" + keyFile + "\" \"$@\"" ) , 0700 )
assert . NoError ( t , err )
2019-02-04 00:56:53 +01:00
//Setup ssh wrapper
2019-10-12 02:13:27 +02:00
os . Setenv ( "GIT_SSH" , path . Join ( tmpDir , "ssh" ) )
2019-02-04 00:56:53 +01:00
os . Setenv ( "GIT_SSH_COMMAND" ,
2019-08-17 00:28:55 +02:00
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \"" + keyFile + "\"" )
2019-02-04 00:56:53 +01: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" )
2021-11-06 07:23:32 +01:00
u2 . Host = net . JoinHostPort ( setting . SSH . ListenHost , strconv . Itoa ( setting . SSH . ListenPort ) )
2019-02-04 00:56:53 +01:00
u2 . Path = gitPath
return & u2
}
2019-10-12 02:13:27 +02:00
func allowLFSFilters ( ) [ ] string {
// Now here we should explicitly allow lfs filters to run
filteredLFSGlobalArgs := make ( [ ] string , len ( git . GlobalCommandArgs ) )
j := 0
for _ , arg := range git . GlobalCommandArgs {
if strings . Contains ( arg , "lfs" ) {
j --
} else {
filteredLFSGlobalArgs [ j ] = arg
j ++
}
}
2019-11-27 01:35:52 +01:00
return filteredLFSGlobalArgs [ : j ]
2019-10-12 02:13:27 +02:00
}
2021-04-16 20:30:16 +02:00
func onGiteaRunTB ( t testing . TB , callback func ( testing . TB , * url . URL ) , prepare ... bool ) {
2019-11-13 22:06:35 +01:00
if len ( prepare ) == 0 || prepare [ 0 ] {
2019-12-08 02:14:50 +01:00
defer prepareTestEnv ( t , 1 ) ( )
2019-11-13 22:06:35 +01:00
}
2019-02-04 00:56:53 +01:00
s := http . Server {
2020-11-13 13:51:07 +01:00
Handler : c ,
2019-02-04 00:56:53 +01:00
}
u , err := url . Parse ( setting . AppURL )
assert . NoError ( t , err )
listener , err := net . Listen ( "tcp" , u . Host )
2019-12-15 17:21:16 +01:00
i := 0
for err != nil && i <= 10 {
time . Sleep ( 100 * time . Millisecond )
listener , err = net . Listen ( "tcp" , u . Host )
i ++
}
2019-02-04 00:56:53 +01:00
assert . NoError ( t , err )
2019-11-26 16:35:41 +01:00
u . Host = listener . Addr ( ) . String ( )
2019-02-04 00:56:53 +01:00
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 )
}
2021-04-16 20:30:16 +02:00
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 00:56:53 +01:00
func doGitClone ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
2020-12-02 19:36:06 +01:00
assert . NoError ( t , git . CloneWithArgs ( context . Background ( ) , u . String ( ) , dstLocalPath , allowLFSFilters ( ) , git . CloneRepoOptions { } ) )
2020-12-25 10:59:32 +01:00
exist , err := util . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) )
assert . NoError ( t , err )
assert . True ( t , exist )
2019-02-04 00:56:53 +01:00
}
}
2022-01-23 22:46:09 +01:00
func doPartialGitClone ( dstLocalPath string , u * url . URL ) func ( * testing . T ) {
return func ( t * testing . T ) {
assert . NoError ( t , git . CloneWithArgs ( context . Background ( ) , u . String ( ) , dstLocalPath , allowLFSFilters ( ) , git . CloneRepoOptions {
Filter : "blob:none" ,
} ) )
exist , err := util . IsExist ( filepath . Join ( dstLocalPath , "README.md" ) )
assert . NoError ( t , err )
assert . True ( t , exist )
}
}
2020-04-28 10:32:23 +02:00
func doGitCloneFail ( u * url . URL ) func ( * testing . T ) {
2019-02-04 00:56:53 +01:00
return func ( t * testing . T ) {
2021-09-22 07:38:34 +02:00
tmpDir , err := os . MkdirTemp ( "" , "doGitCloneFail" )
2020-04-28 10:32:23 +02:00
assert . NoError ( t , err )
2020-08-11 22:05:34 +02:00
defer util . RemoveAll ( tmpDir )
2020-04-28 10:32:23 +02:00
assert . Error ( t , git . Clone ( u . String ( ) , tmpDir , git . CloneRepoOptions { } ) )
2020-12-25 10:59:32 +01:00
exist , err := util . IsExist ( filepath . Join ( tmpDir , "README.md" ) )
assert . NoError ( t , err )
assert . False ( t , exist )
2019-02-04 00:56:53 +01:00
}
}
func doGitInitTestRepository ( dstPath string ) func ( * testing . T ) {
return func ( t * testing . T ) {
// Init repository in dstPath
assert . NoError ( t , git . InitRepository ( dstPath , false ) )
2021-12-20 21:55:05 +01:00
// forcibly set default branch to master
_ , err := git . NewCommand ( "symbolic-ref" , "HEAD" , git . BranchPrefix + "master" ) . RunInDir ( dstPath )
assert . NoError ( t , err )
2021-09-22 07:38:34 +02:00
assert . NoError ( t , os . WriteFile ( filepath . Join ( dstPath , "README.md" ) , [ ] byte ( fmt . Sprintf ( "# Testing Repository\n\nOriginally created in: %s" , dstPath ) ) , 0644 ) )
2019-02-04 00:56:53 +01:00
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 12:12:15 +02:00
func doGitPushTestRepository ( dstPath string , args ... string ) func ( * testing . T ) {
2019-02-04 00:56:53 +01:00
return func ( t * testing . T ) {
2019-05-31 12:12:15 +02:00
_ , err := git . NewCommand ( append ( [ ] string { "push" , "-u" } , args ... ) ... ) . RunInDir ( dstPath )
2019-02-04 00:56:53 +01:00
assert . NoError ( t , err )
}
}
2019-05-31 12:12:15 +02:00
func doGitPushTestRepositoryFail ( dstPath string , args ... string ) func ( * testing . T ) {
2019-02-04 00:56:53 +01:00
return func ( t * testing . T ) {
2019-05-31 12:12:15 +02:00
_ , err := git . NewCommand ( append ( [ ] string { "push" } , args ... ) ... ) . RunInDir ( dstPath )
2019-02-04 00:56:53 +01:00
assert . Error ( t , err )
}
}
2019-05-31 12:12:15 +02: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-11-27 01:35:52 +01:00
_ , err := git . NewCommandNoGlobals ( append ( append ( allowLFSFilters ( ) , "checkout" ) , args ... ) ... ) . RunInDir ( dstPath )
2019-05-31 12:12:15 +02: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-11-27 01:35:52 +01:00
_ , err := git . NewCommandNoGlobals ( append ( append ( allowLFSFilters ( ) , "pull" ) , args ... ) ... ) . RunInDir ( dstPath )
2019-05-31 12:12:15 +02:00
assert . NoError ( t , err )
}
}