@@ -6,8 +6,9 @@ package integrations
6
6
7
7
import (
8
8
"context"
9
+ "crypto/rand"
10
+ "fmt"
9
11
"io/ioutil"
10
- "math/rand"
11
12
"net"
12
13
"net/http"
13
14
"net/url"
@@ -17,14 +18,21 @@ import (
17
18
"time"
18
19
19
20
"code.gitea.io/git"
21
+ "code.gitea.io/gitea/models"
20
22
"code.gitea.io/gitea/modules/setting"
21
23
api "code.gitea.io/sdk/gitea"
22
24
23
25
"github.com/Unknwon/com"
24
26
"github.com/stretchr/testify/assert"
25
27
)
26
28
27
- func onGiteaWebRun (t * testing.T , callback func (* testing.T , * url.URL )) {
29
+ const (
30
+ littleSize = 1024 //1ko
31
+ bigSize = 128 * 1024 * 1024 //128Mo
32
+ )
33
+
34
+ func onGiteaRun (t * testing.T , callback func (* testing.T , * url.URL )) {
35
+ prepareTestEnv (t )
28
36
s := http.Server {
29
37
Handler : mac ,
30
38
}
@@ -35,151 +43,241 @@ func onGiteaWebRun(t *testing.T, callback func(*testing.T, *url.URL)) {
35
43
assert .NoError (t , err )
36
44
37
45
defer func () {
38
- ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
46
+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Minute )
39
47
s .Shutdown (ctx )
40
48
cancel ()
41
49
}()
42
50
43
51
go s .Serve (listener )
52
+ //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
44
53
45
54
callback (t , u )
46
55
}
47
56
48
57
func TestGit (t * testing.T ) {
49
- prepareTestEnv (t )
50
-
51
- onGiteaWebRun (t , func (t * testing.T , u * url.URL ) {
52
- dstPath , err := ioutil .TempDir ("" , "repo-tmp-17" )
53
- assert .NoError (t , err )
54
- defer os .RemoveAll (dstPath )
58
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
55
59
u .Path = "user2/repo1.git"
56
60
57
- t .Run ("Standard" , func (t * testing.T ) {
61
+ t .Run ("HTTP" , func (t * testing.T ) {
62
+ dstPath , err := ioutil .TempDir ("" , "repo-tmp-17" )
63
+ assert .NoError (t , err )
64
+ defer os .RemoveAll (dstPath )
65
+ t .Run ("Standard" , func (t * testing.T ) {
66
+ t .Run ("CloneNoLogin" , func (t * testing.T ) {
67
+ dstLocalPath , err := ioutil .TempDir ("" , "repo1" )
68
+ assert .NoError (t , err )
69
+ defer os .RemoveAll (dstLocalPath )
70
+ err = git .Clone (u .String (), dstLocalPath , git.CloneRepoOptions {})
71
+ assert .NoError (t , err )
72
+ assert .True (t , com .IsExist (filepath .Join (dstLocalPath , "README.md" )))
73
+ })
58
74
59
- t .Run ("CloneNoLogin" , func (t * testing.T ) {
60
- dstLocalPath , err := ioutil .TempDir ("" , "repo1" )
61
- assert .NoError (t , err )
62
- defer os .RemoveAll (dstLocalPath )
63
- err = git .Clone (u .String (), dstLocalPath , git.CloneRepoOptions {})
64
- assert .NoError (t , err )
65
- assert .True (t , com .IsExist (filepath .Join (dstLocalPath , "README.md" )))
66
- })
75
+ t .Run ("CreateRepo" , func (t * testing.T ) {
76
+ session := loginUser (t , "user2" )
77
+ req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
78
+ AutoInit : true ,
79
+ Description : "Temporary repo" ,
80
+ Name : "repo-tmp-17" ,
81
+ Private : false ,
82
+ Gitignores : "" ,
83
+ License : "WTFPL" ,
84
+ Readme : "Default" ,
85
+ })
86
+ session .MakeRequest (t , req , http .StatusCreated )
87
+ })
67
88
68
- t .Run ("CreateRepo" , func (t * testing.T ) {
69
- session := loginUser (t , "user2" )
70
- req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
71
- AutoInit : true ,
72
- Description : "Temporary repo" ,
73
- Name : "repo-tmp-17" ,
74
- Private : false ,
75
- Gitignores : "" ,
76
- License : "WTFPL" ,
77
- Readme : "Default" ,
89
+ u .Path = "user2/repo-tmp-17.git"
90
+ u .User = url .UserPassword ("user2" , userPassword )
91
+ t .Run ("Clone" , func (t * testing.T ) {
92
+ err = git .Clone (u .String (), dstPath , git.CloneRepoOptions {})
93
+ assert .NoError (t , err )
94
+ assert .True (t , com .IsExist (filepath .Join (dstPath , "README.md" )))
78
95
})
79
- session .MakeRequest (t , req , http .StatusCreated )
80
- })
81
96
82
- u .Path = "user2/repo-tmp-17.git"
83
- u .User = url .UserPassword ("user2" , userPassword )
84
- t .Run ("Clone" , func (t * testing.T ) {
85
- err = git .Clone (u .String (), dstPath , git.CloneRepoOptions {})
86
- assert .NoError (t , err )
87
- assert .True (t , com .IsExist (filepath .Join (dstPath , "README.md" )))
97
+ t .Run ("PushCommit" , func (t * testing.T ) {
98
+ t .Run ("Little" , func (t * testing.T ) {
99
+ commitAndPush (t , littleSize , dstPath )
100
+ })
101
+ t .Run ("Big" , func (t * testing.T ) {
102
+ commitAndPush (t , bigSize , dstPath )
103
+ })
104
+ })
88
105
})
106
+ t .Run ("LFS" , func (t * testing.T ) {
107
+ t .Run ("PushCommit" , func (t * testing.T ) {
108
+ //Setup git LFS
109
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("install" ).RunInDir (dstPath )
110
+ assert .NoError (t , err )
111
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("track" , "data-file-*" ).RunInDir (dstPath )
112
+ assert .NoError (t , err )
113
+ err = git .AddChanges (dstPath , false , ".gitattributes" )
114
+ assert .NoError (t , err )
89
115
90
- t .Run ("PushCommit" , func (t * testing.T ) {
91
- data := make ([]byte , 1024 )
92
- _ , err := rand .Read (data )
93
- assert .NoError (t , err )
94
- tmpFile , err := ioutil .TempFile (dstPath , "data-file-" )
95
- defer tmpFile .Close ()
96
- _ , err = tmpFile .Write (data )
97
- assert .NoError (t , err )
98
-
99
- //Commit
100
- err = git .AddChanges (dstPath , false , filepath .Base (tmpFile .Name ()))
101
- assert .NoError (t , err )
102
- err = git .CommitChanges (dstPath , git.CommitChangesOptions {
103
- Committer : & git.Signature {
104
-
105
- Name : "User Two" ,
106
- When : time .Now (),
107
- },
108
- Author : & git.Signature {
109
-
110
- Name : "User Two" ,
111
- When : time .Now (),
112
- },
113
- Message : "Testing commit" ,
116
+ t .Run ("Little" , func (t * testing.T ) {
117
+ commitAndPush (t , littleSize , dstPath )
118
+ })
119
+ t .Run ("Big" , func (t * testing.T ) {
120
+ commitAndPush (t , bigSize , dstPath )
121
+ })
114
122
})
115
- assert .NoError (t , err )
116
-
117
- //Push
118
- err = git .Push (dstPath , git.PushOptions {
119
- Branch : "master" ,
120
- Remote : u .String (),
121
- Force : false ,
123
+ t .Run ("Locks" , func (t * testing.T ) {
124
+ lockTest (t , u .String (), dstPath )
122
125
})
123
- assert .NoError (t , err )
124
126
})
125
127
})
126
- t .Run ("LFS" , func (t * testing.T ) {
127
- t .Run ("PushCommit" , func (t * testing.T ) {
128
- /* Generate random file */
129
- data := make ([]byte , 1024 )
130
- _ , err := rand .Read (data )
131
- assert .NoError (t , err )
132
- tmpFile , err := ioutil .TempFile (dstPath , "data-file-" )
133
- defer tmpFile .Close ()
134
- _ , err = tmpFile .Write (data )
135
- assert .NoError (t , err )
136
-
137
- //Setup git LFS
138
- _ , err = git .NewCommand ("lfs" ).AddArguments ("install" ).RunInDir (dstPath )
139
- assert .NoError (t , err )
140
- _ , err = git .NewCommand ("lfs" ).AddArguments ("track" , "data-file-*" ).RunInDir (dstPath )
141
- assert .NoError (t , err )
142
-
143
- //Commit
144
- err = git .AddChanges (dstPath , false , ".gitattributes" , filepath .Base (tmpFile .Name ()))
145
- assert .NoError (t , err )
146
- err = git .CommitChanges (dstPath , git.CommitChangesOptions {
147
- Committer : & git.Signature {
148
-
149
- Name : "User Two" ,
150
- When : time .Now (),
151
- },
152
- Author : & git.Signature {
153
-
154
- Name : "User Two" ,
155
- When : time .Now (),
156
- },
157
- Message : "Testing LFS " ,
158
- })
159
- assert .NoError (t , err )
128
+ t .Run ("SSH" , func (t * testing.T ) {
129
+ //Setup remote link
130
+ u .Scheme = "ssh"
131
+ u .User = url .User ("git" )
132
+ u .Host = fmt .Sprintf ("%s:%d" , setting .SSH .ListenHost , setting .SSH .ListenPort )
133
+ u .Path = "user2/repo-tmp-18.git"
160
134
161
- //Push
162
- u .User = url .UserPassword ("user2" , userPassword )
163
- err = git .Push (dstPath , git.PushOptions {
164
- Branch : "master" ,
165
- Remote : u .String (),
166
- Force : false ,
167
- })
168
- assert .NoError (t , err )
135
+ //Setup key
136
+ keyFile := filepath .Join (setting .AppDataPath , "my-testing-key" )
137
+ _ , _ , err := com .ExecCmd ("ssh-keygen" , "-f" , keyFile , "-t" , "rsa" , "-N" , "" )
138
+ assert .NoError (t , err )
139
+ defer os .RemoveAll (keyFile )
140
+ defer os .RemoveAll (keyFile + ".pub" )
141
+
142
+ session := loginUser (t , "user1" )
143
+ keyOwner := models .AssertExistsAndLoadBean (t , & models.User {Name : "user2" }).(* models.User )
144
+ urlStr := fmt .Sprintf ("/api/v1/admin/users/%s/keys" , keyOwner .Name )
145
+
146
+ dataPubKey , err := ioutil .ReadFile (keyFile + ".pub" )
147
+ assert .NoError (t , err )
148
+ req := NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
149
+ "key" : string (dataPubKey ),
150
+ "title" : "test-key" ,
169
151
})
170
- t .Run ("Locks" , func (t * testing.T ) {
171
- _ , err = git .NewCommand ("remote" ).AddArguments ("set-url" , "origin" , u .String ()).RunInDir (dstPath ) //TODO add test ssh git-lfs-creds
172
- assert .NoError (t , err )
173
- _ , err = git .NewCommand ("lfs" ).AddArguments ("locks" ).RunInDir (dstPath )
174
- assert .NoError (t , err )
175
- _ , err = git .NewCommand ("lfs" ).AddArguments ("lock" , "README.md" ).RunInDir (dstPath )
176
- assert .NoError (t , err )
177
- _ , err = git .NewCommand ("lfs" ).AddArguments ("locks" ).RunInDir (dstPath )
178
- assert .NoError (t , err )
179
- _ , err = git .NewCommand ("lfs" ).AddArguments ("unlock" , "README.md" ).RunInDir (dstPath )
180
- assert .NoError (t , err )
152
+ session .MakeRequest (t , req , http .StatusCreated )
153
+
154
+ //Setup ssh wrapper
155
+ sshWrapper , err := ioutil .TempFile (setting .AppDataPath , "tmp-ssh-wrapper" )
156
+ sshWrapper .WriteString ("#!/bin/sh\n \n " )
157
+ sshWrapper .WriteString ("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i \" " + filepath .Join (setting .AppWorkPath , keyFile ) + "\" $* \n \n " )
158
+ err = sshWrapper .Chmod (os .ModePerm )
159
+ assert .NoError (t , err )
160
+ sshWrapper .Close ()
161
+ defer os .RemoveAll (sshWrapper .Name ())
162
+
163
+ //Setup clone folder
164
+ dstPath , err := ioutil .TempDir ("" , "repo-tmp-18" )
165
+ assert .NoError (t , err )
166
+ defer os .RemoveAll (dstPath )
167
+
168
+ t .Run ("Standard" , func (t * testing.T ) {
169
+ t .Run ("CreateRepo" , func (t * testing.T ) {
170
+ session := loginUser (t , "user2" )
171
+ req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
172
+ AutoInit : true ,
173
+ Description : "Temporary repo" ,
174
+ Name : "repo-tmp-18" ,
175
+ Private : false ,
176
+ Gitignores : "" ,
177
+ License : "WTFPL" ,
178
+ Readme : "Default" ,
179
+ })
180
+ session .MakeRequest (t , req , http .StatusCreated )
181
+ })
182
+ //TODO get url from api
183
+ t .Run ("Clone" , func (t * testing.T ) {
184
+ _ , err = git .NewCommand ("clone" ).AddArguments ("--config" , "core.sshCommand=" + filepath .Join (setting .AppWorkPath , sshWrapper .Name ()), u .String (), dstPath ).Run ()
185
+ assert .NoError (t , err )
186
+ assert .True (t , com .IsExist (filepath .Join (dstPath , "README.md" )))
187
+ })
188
+ //time.Sleep(5 * time.Minute)
189
+ t .Run ("PushCommit" , func (t * testing.T ) {
190
+ t .Run ("Little" , func (t * testing.T ) {
191
+ commitAndPush (t , littleSize , dstPath )
192
+ })
193
+ t .Run ("Big" , func (t * testing.T ) {
194
+ commitAndPush (t , bigSize , dstPath )
195
+ })
196
+ })
181
197
})
198
+ t .Run ("LFS" , func (t * testing.T ) {
199
+ os .Setenv ("GIT_SSH_COMMAND" , filepath .Join (setting .AppWorkPath , sshWrapper .Name ())) //TODO remove when fixed https://github.com/git-lfs/git-lfs/issues/2215
200
+ defer os .Unsetenv ("GIT_SSH_COMMAND" )
201
+ t .Run ("PushCommit" , func (t * testing.T ) {
202
+ //Setup git LFS
203
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("install" ).RunInDir (dstPath )
204
+ assert .NoError (t , err )
205
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("track" , "data-file-*" ).RunInDir (dstPath )
206
+ assert .NoError (t , err )
207
+ err = git .AddChanges (dstPath , false , ".gitattributes" )
208
+ assert .NoError (t , err )
182
209
210
+ t .Run ("Little" , func (t * testing.T ) {
211
+ commitAndPush (t , littleSize , dstPath )
212
+ })
213
+ t .Run ("Big" , func (t * testing.T ) {
214
+ commitAndPush (t , bigSize , dstPath )
215
+ })
216
+ })
217
+ /* Failed without #3152. TODO activate with fix.
218
+ t.Run("Locks", func(t *testing.T) {
219
+ lockTest(t, u.String(), dstPath)
220
+ })
221
+ */
222
+ })
183
223
})
184
224
})
185
225
}
226
+
227
+ func lockTest (t * testing.T , remote , repoPath string ) {
228
+ _ , err := git .NewCommand ("remote" ).AddArguments ("set-url" , "origin" , remote ).RunInDir (repoPath ) //TODO add test ssh git-lfs-creds
229
+ assert .NoError (t , err )
230
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("locks" ).RunInDir (repoPath )
231
+ assert .NoError (t , err )
232
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("lock" , "README.md" ).RunInDir (repoPath )
233
+ assert .NoError (t , err )
234
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("locks" ).RunInDir (repoPath )
235
+ assert .NoError (t , err )
236
+ _ , err = git .NewCommand ("lfs" ).AddArguments ("unlock" , "README.md" ).RunInDir (repoPath )
237
+ assert .NoError (t , err )
238
+ }
239
+
240
+ func commitAndPush (t * testing.T , size int , repoPath string ) {
241
+ err := generateCommitWithNewData (
size ,
repoPath ,
"[email protected] " ,
"User Two" )
242
+ assert .NoError (t , err )
243
+ _ , err = git .NewCommand ("push" ).RunInDir (repoPath ) //Push
244
+ assert .NoError (t , err )
245
+ }
246
+
247
+ func generateCommitWithNewData (size int , repoPath , email , fullName string ) error {
248
+ //Generate random file
249
+ data := make ([]byte , size )
250
+ _ , err := rand .Read (data )
251
+ if err != nil {
252
+ return err
253
+ }
254
+ tmpFile , err := ioutil .TempFile (repoPath , "data-file-" )
255
+ if err != nil {
256
+ return err
257
+ }
258
+ defer tmpFile .Close ()
259
+ _ , err = tmpFile .Write (data )
260
+ if err != nil {
261
+ return err
262
+ }
263
+
264
+ //Commit
265
+ err = git .AddChanges (repoPath , false , filepath .Base (tmpFile .Name ()))
266
+ if err != nil {
267
+ return err
268
+ }
269
+ err = git .CommitChanges (repoPath , git.CommitChangesOptions {
270
+ Committer : & git.Signature {
271
+ Email : email ,
272
+ Name : fullName ,
273
+ When : time .Now (),
274
+ },
275
+ Author : & git.Signature {
276
+ Email : email ,
277
+ Name : fullName ,
278
+ When : time .Now (),
279
+ },
280
+ Message : fmt .Sprintf ("Testing commit @ %v" , time .Now ()),
281
+ })
282
+ return err
283
+ }
0 commit comments