Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-workhorse
Commits
65c1d58c
Commit
65c1d58c
authored
Apr 18, 2017
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Put Gitaly integration tests in gitaly_test.go
parent
c03b113f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
208 additions
and
191 deletions
+208
-191
gitaly_test.go
gitaly_test.go
+208
-0
main_test.go
main_test.go
+0
-191
No files found.
gitaly_test.go
0 → 100644
View file @
65c1d58c
package
main
import
(
"fmt"
"math/rand"
"net"
"os"
"os/exec"
"path"
"strings"
"testing"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/testhelper"
pb
"gitlab.com/gitlab-org/gitaly-proto/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
func
TestFailedCloneNoGitaly
(
t
*
testing
.
T
)
{
// Prepare clone directory
require
.
NoError
(
t
,
os
.
RemoveAll
(
scratchDir
))
authBody
:=
&
api
.
Response
{
GL_ID
:
"user-123"
,
RepoPath
:
repoPath
(
t
),
// This will create a failure to connect to Gitaly
GitalyAddress
:
"unix:/nonexistent"
,
}
// Prepare test server and backend
ts
:=
testAuthServer
(
nil
,
200
,
authBody
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
// Do the git clone
cloneCmd
:=
exec
.
Command
(
"git"
,
"clone"
,
fmt
.
Sprintf
(
"%s/%s"
,
ws
.
URL
,
testRepo
),
checkoutDir
)
out
,
err
:=
cloneCmd
.
CombinedOutput
()
t
.
Log
(
string
(
out
))
assert
.
Error
(
t
,
err
,
"git clone should have failed"
)
}
func
TestGetInfoRefsProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
gitalyAddress
:=
"unix://"
+
socketPath
apiResponse
.
GitalyAddress
=
gitalyAddress
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/info/refs?service=git-upload-pack"
_
,
body
:=
httpGet
(
t
,
ws
.
URL
+
resource
)
expectedContent
:=
string
(
testhelper
.
GitalyInfoRefsResponseMock
)
assert
.
Equal
(
t
,
expectedContent
,
body
,
"GET %q: response body"
)
}
func
TestPostReceivePackProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
.
GitalyAddress
=
"unix://"
+
socketPath
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-receive-pack"
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-receive-pack-request"
,
testhelper
.
GitalyReceivePackResponseMock
,
)
expectedBody
:=
strings
.
Join
([]
string
{
apiResponse
.
RepoPath
,
apiResponse
.
Repository
.
StorageName
,
apiResponse
.
Repository
.
RelativePath
,
apiResponse
.
GL_ID
,
string
(
testhelper
.
GitalyReceivePackResponseMock
),
},
"
\0
00"
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q"
,
resource
)
assert
.
Equal
(
t
,
expectedBody
,
body
,
"POST %q: response body"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-receive-pack-result"
)
}
func
TestPostUploadPackProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
.
GitalyAddress
=
"unix://"
+
socketPath
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-upload-pack"
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-upload-pack-request"
,
testhelper
.
GitalyUploadPackResponseMock
,
)
expectedBody
:=
strings
.
Join
([]
string
{
apiResponse
.
RepoPath
,
apiResponse
.
Repository
.
StorageName
,
apiResponse
.
Repository
.
RelativePath
,
string
(
testhelper
.
GitalyUploadPackResponseMock
),
},
"
\0
00"
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q"
,
resource
)
assert
.
Equal
(
t
,
expectedBody
,
body
,
"POST %q: response body"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-result"
)
}
func
TestGetInfoRefsHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/info/refs?service=git-upload-pack"
resp
,
body
:=
httpGet
(
t
,
ws
.
URL
+
resource
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"GET %q"
,
resource
)
assert
.
NotContains
(
t
,
string
(
testhelper
.
GitalyInfoRefsResponseMock
),
body
,
"GET %q: should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-advertisement"
)
}
func
TestPostReceivePackHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-receive-pack"
payload
:=
[]
byte
(
"This payload should not reach Gitaly"
)
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-receive-pack-request"
,
payload
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q: status code"
,
resource
)
assert
.
NotContains
(
t
,
payload
,
body
,
"POST %q: request should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-receive-pack-result"
)
}
func
TestPostUploadPackHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-upload-pack"
payload
:=
[]
byte
(
"This payload should not reach Gitaly"
)
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-upload-pack-request"
,
payload
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q: status code"
,
resource
)
assert
.
NotContains
(
t
,
payload
,
body
,
"POST %q: request should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-result"
)
}
func
startGitalyServer
(
t
*
testing
.
T
)
(
*
grpc
.
Server
,
string
)
{
socketPath
:=
path
.
Join
(
scratchDir
,
fmt
.
Sprintf
(
"gitaly-%d.sock"
,
rand
.
Int
()))
server
:=
grpc
.
NewServer
()
listener
,
err
:=
net
.
Listen
(
"unix"
,
socketPath
)
require
.
NoError
(
t
,
err
)
pb
.
RegisterSmartHTTPServer
(
server
,
testhelper
.
NewGitalyServer
())
go
server
.
Serve
(
listener
)
return
server
,
socketPath
}
main_test.go
View file @
65c1d58c
...
...
@@ -7,8 +7,6 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"net/http/httptest"
"os"
...
...
@@ -16,7 +14,6 @@ import (
"path"
"regexp"
"strconv"
"strings"
"testing"
"time"
...
...
@@ -31,7 +28,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
const
scratchDir
=
"testdata/scratch"
...
...
@@ -128,30 +124,6 @@ func TestDeniedClone(t *testing.T) {
assert
.
Error
(
t
,
err
,
"git clone should have failed"
)
}
func
TestFailedCloneNoGitaly
(
t
*
testing
.
T
)
{
// Prepare clone directory
require
.
NoError
(
t
,
os
.
RemoveAll
(
scratchDir
))
authBody
:=
&
api
.
Response
{
GL_ID
:
"user-123"
,
RepoPath
:
repoPath
(
t
),
// This will create a failure to connect to Gitaly
GitalyAddress
:
"unix:/nonexistent"
,
}
// Prepare test server and backend
ts
:=
testAuthServer
(
nil
,
200
,
authBody
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
// Do the git clone
cloneCmd
:=
exec
.
Command
(
"git"
,
"clone"
,
fmt
.
Sprintf
(
"%s/%s"
,
ws
.
URL
,
testRepo
),
checkoutDir
)
out
,
err
:=
cloneCmd
.
CombinedOutput
()
t
.
Log
(
string
(
out
))
assert
.
Error
(
t
,
err
,
"git clone should have failed"
)
}
func
TestAllowedPush
(
t
*
testing
.
T
)
{
preparePushRepo
(
t
)
...
...
@@ -448,156 +420,6 @@ func TestApiContentTypeBlock(t *testing.T) {
assert
.
NotContains
(
t
,
wrongResponse
,
body
,
"GET %q: response body"
,
resourcePath
)
}
func
TestGetInfoRefsProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
gitalyAddress
:=
"unix://"
+
socketPath
apiResponse
.
GitalyAddress
=
gitalyAddress
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/info/refs?service=git-upload-pack"
_
,
body
:=
httpGet
(
t
,
ws
.
URL
+
resource
)
expectedContent
:=
string
(
testhelper
.
GitalyInfoRefsResponseMock
)
assert
.
Equal
(
t
,
expectedContent
,
body
,
"GET %q: response body"
)
}
func
TestPostReceivePackProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
.
GitalyAddress
=
"unix://"
+
socketPath
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-receive-pack"
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-receive-pack-request"
,
testhelper
.
GitalyReceivePackResponseMock
,
)
expectedBody
:=
strings
.
Join
([]
string
{
apiResponse
.
RepoPath
,
apiResponse
.
Repository
.
StorageName
,
apiResponse
.
Repository
.
RelativePath
,
apiResponse
.
GL_ID
,
string
(
testhelper
.
GitalyReceivePackResponseMock
),
},
"
\0
00"
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q"
,
resource
)
assert
.
Equal
(
t
,
expectedBody
,
body
,
"POST %q: response body"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-receive-pack-result"
)
}
func
TestPostUploadPackProxiedToGitalySuccessfully
(
t
*
testing
.
T
)
{
apiResponse
:=
gitOkBody
(
t
)
gitalyServer
,
socketPath
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
.
GitalyAddress
=
"unix://"
+
socketPath
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-upload-pack"
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-upload-pack-request"
,
testhelper
.
GitalyUploadPackResponseMock
,
)
expectedBody
:=
strings
.
Join
([]
string
{
apiResponse
.
RepoPath
,
apiResponse
.
Repository
.
StorageName
,
apiResponse
.
Repository
.
RelativePath
,
string
(
testhelper
.
GitalyUploadPackResponseMock
),
},
"
\0
00"
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q"
,
resource
)
assert
.
Equal
(
t
,
expectedBody
,
body
,
"POST %q: response body"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-result"
)
}
func
TestGetInfoRefsHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/info/refs?service=git-upload-pack"
resp
,
body
:=
httpGet
(
t
,
ws
.
URL
+
resource
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"GET %q"
,
resource
)
assert
.
NotContains
(
t
,
string
(
testhelper
.
GitalyInfoRefsResponseMock
),
body
,
"GET %q: should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-advertisement"
)
}
func
TestPostReceivePackHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-receive-pack"
payload
:=
[]
byte
(
"This payload should not reach Gitaly"
)
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-receive-pack-request"
,
payload
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q: status code"
,
resource
)
assert
.
NotContains
(
t
,
payload
,
body
,
"POST %q: request should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-receive-pack-result"
)
}
func
TestPostUploadPackHandledLocallyDueToEmptyGitalySocketPath
(
t
*
testing
.
T
)
{
gitalyServer
,
_
:=
startGitalyServer
(
t
)
defer
gitalyServer
.
Stop
()
apiResponse
:=
gitOkBody
(
t
)
apiResponse
.
GitalyAddress
=
""
ts
:=
testAuthServer
(
nil
,
200
,
apiResponse
)
defer
ts
.
Close
()
ws
:=
startWorkhorseServer
(
ts
.
URL
)
defer
ws
.
Close
()
resource
:=
"/gitlab-org/gitlab-test.git/git-upload-pack"
payload
:=
[]
byte
(
"This payload should not reach Gitaly"
)
resp
,
body
:=
httpPost
(
t
,
ws
.
URL
+
resource
,
"application/x-git-upload-pack-request"
,
payload
)
assert
.
Equal
(
t
,
200
,
resp
.
StatusCode
,
"POST %q: status code"
,
resource
)
assert
.
NotContains
(
t
,
payload
,
body
,
"POST %q: request should not have been proxied to Gitaly"
,
resource
)
testhelper
.
AssertResponseHeader
(
t
,
resp
,
"Content-Type"
,
"application/x-git-upload-pack-result"
)
}
func
TestAPIFalsePositivesAreProxied
(
t
*
testing
.
T
)
{
goodResponse
:=
[]
byte
(
`<html></html>`
)
ts
:=
testhelper
.
TestServerWithHandler
(
regexp
.
MustCompile
(
`.`
),
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -744,19 +566,6 @@ func startWorkhorseServerWithConfig(cfg *config.Config) *httptest.Server {
return
httptest
.
NewServer
(
u
)
}
func
startGitalyServer
(
t
*
testing
.
T
)
(
*
grpc
.
Server
,
string
)
{
socketPath
:=
path
.
Join
(
scratchDir
,
fmt
.
Sprintf
(
"gitaly-%d.sock"
,
rand
.
Int
()))
server
:=
grpc
.
NewServer
()
listener
,
err
:=
net
.
Listen
(
"unix"
,
socketPath
)
require
.
NoError
(
t
,
err
)
pb
.
RegisterSmartHTTPServer
(
server
,
testhelper
.
NewGitalyServer
())
go
server
.
Serve
(
listener
)
return
server
,
socketPath
}
func
runOrFail
(
t
*
testing
.
T
,
cmd
*
exec
.
Cmd
)
{
out
,
err
:=
cmd
.
CombinedOutput
()
t
.
Logf
(
"%s"
,
out
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment