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
c0cde5db
Commit
c0cde5db
authored
Mar 01, 2017
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor register tests
parent
a5d889dc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
129 deletions
+43
-129
internal/builds/register_test.go
internal/builds/register_test.go
+43
-129
No files found.
internal/builds/register_test.go
View file @
c0cde5db
...
@@ -2,13 +2,14 @@ package builds
...
@@ -2,13 +2,14 @@ package builds
import
(
import
(
"bytes"
"bytes"
"errors"
"io"
"io"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"strings"
"testing"
"testing"
"time"
"time"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/redis"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/redis"
)
)
...
@@ -19,171 +20,84 @@ func echoRequest(rw http.ResponseWriter, req *http.Request) {
...
@@ -19,171 +20,84 @@ func echoRequest(rw http.ResponseWriter, req *http.Request) {
var
echoRequestFunc
=
http
.
HandlerFunc
(
echoRequest
)
var
echoRequestFunc
=
http
.
HandlerFunc
(
echoRequest
)
func
TestRegisterHandlerLargeBody
(
t
*
testing
.
T
)
{
const
applicationJson
=
"application/json"
h
:=
RegisterHandler
(
echoRequestFunc
,
nil
,
time
.
Second
)
data
:=
make
([]
byte
,
maxRegisterBodySize
+
5
)
func
expectHandlerWithWatcher
(
t
*
testing
.
T
,
watchHandler
WatchKeyHandler
,
data
string
,
contentType
string
,
expectedHttpStatus
int
,
msgAndArgs
...
interface
{})
{
h
:=
RegisterHandler
(
echoRequestFunc
,
watchHandler
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBuffer
(
data
))
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
contentType
)
h
.
ServeHTTP
(
rw
,
req
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusInternalServerError
,
rw
.
Code
)
assert
.
Equal
(
t
,
expectedHttpStatus
,
rw
.
Code
,
msgAndArgs
...
)
}
}
func
TestRegisterHandlerInvalidRunnerRequest
(
t
*
testing
.
T
)
{
func
expectHandler
(
t
*
testing
.
T
,
data
string
,
contentType
string
,
expectedHttpStatus
int
,
msgAndArgs
...
interface
{})
{
h
:=
RegisterHandler
(
echoRequestFunc
,
nil
,
time
.
Second
)
expectHandlerWithWatcher
(
t
,
nil
,
data
,
contentType
,
expectedHttpStatus
,
msgAndArgs
...
)
}
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
"invalid"
))
h
.
ServeHTTP
(
rw
,
req
)
func
TestRegisterHandlerLargeBody
(
t
*
testing
.
T
)
{
data
:=
strings
.
Repeat
(
"."
,
maxRegisterBodySize
+
5
)
expectHandler
(
t
,
data
,
applicationJson
,
http
.
StatusRequestEntityTooLarge
,
"rejects body with entity too large"
)
}
assert
.
Equal
(
t
,
http
.
StatusOK
,
rw
.
Code
)
func
TestRegisterHandlerInvalidRunnerRequest
(
t
*
testing
.
T
)
{
assert
.
Equal
(
t
,
"invalid"
,
rw
.
Body
.
String
())
expectHandler
(
t
,
"invalid content"
,
"text/plain"
,
http
.
StatusOK
,
"proxies request to upstream"
)
}
}
func
TestRegisterHandlerInvalidJsonPayload
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerInvalidJsonPayload
(
t
*
testing
.
T
)
{
h
:=
RegisterHandler
(
echoRequestFunc
,
nil
,
time
.
Second
)
expectHandler
(
t
,
"{["
,
applicationJson
,
http
.
StatusOK
,
"fails on parsing body and proxies request to upstream"
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
"{["
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusOK
,
rw
.
Code
)
assert
.
Equal
(
t
,
"{["
,
rw
.
Body
.
String
())
}
}
func
TestRegisterHandlerMissingData
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerMissingData
(
t
*
testing
.
T
)
{
datas
:=
[]
string
{
"{
\"
token
\"
:
\"
token
\"
}"
,
"{
\"
last_update
\"
:
\"
data
\"
}"
}
dataList
:=
[]
string
{
"{
\"
token
\"
:
\"
token
\"
}"
,
"{
\"
last_update
\"
:
\"
data
\"
}"
}
for
_
,
data
:=
range
datas
{
h
:=
RegisterHandler
(
echoRequestFunc
,
nil
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
for
_
,
data
:=
range
dataList
{
expectHandler
(
t
,
data
,
applicationJson
,
http
.
StatusOK
,
assert
.
Equal
(
t
,
http
.
StatusOK
,
rw
.
Code
)
"fails on argument validation and proxies request to upstream"
)
assert
.
Equal
(
t
,
data
,
rw
.
Body
.
String
())
}
}
}
}
func
TestRegisterHandlerWatcherError
(
t
*
testing
.
T
)
{
func
exceptWatcherToBeExecuted
(
t
*
testing
.
T
,
watchKeyStatus
redis
.
WatchKeyStatus
,
watchKeyError
error
,
data
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
httpStatus
int
,
msgAndArgs
...
interface
{})
{
executed
:=
false
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
executed
=
true
executed
=
true
return
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
)
return
watchKeyStatus
,
watchKeyError
}
}
h
:=
RegisterHandler
(
echoRequestFunc
,
watchKeyHandler
,
time
.
Second
)
parsableData
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
expectHandlerWithWatcher
(
t
,
watchKeyHandler
,
parsableData
,
applicationJson
,
httpStatus
,
msgAndArgs
...
)
assert
.
True
(
t
,
executed
,
msgAndArgs
...
)
}
assert
.
Equal
(
t
,
http
.
StatusInternalServerError
,
rw
.
Code
)
func
TestRegisterHandlerWatcherError
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
executed
)
exceptWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
),
http
.
StatusOK
,
"proxies data to upstream"
)
}
}
func
TestRegisterHandlerWatcherAlreadyChanged
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerWatcherAlreadyChanged
(
t
*
testing
.
T
)
{
data
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
exceptWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusAlreadyChanged
,
nil
,
http
.
StatusOK
,
"proxies data to upstream"
)
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
assert
.
Equal
(
t
,
"runner:build_queue:token"
,
key
)
assert
.
Equal
(
t
,
"last_update"
,
value
)
executed
=
true
return
redis
.
WatchKeyStatusAlreadyChanged
,
nil
}
h
:=
RegisterHandler
(
echoRequestFunc
,
watchKeyHandler
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusOK
,
rw
.
Code
)
assert
.
Equal
(
t
,
data
,
rw
.
Body
.
String
())
assert
.
True
(
t
,
executed
)
}
}
func
TestRegisterHandlerWatcherSeenChange
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerWatcherSeenChange
(
t
*
testing
.
T
)
{
data
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
exceptWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusSeenChange
,
nil
,
http
.
StatusNoContent
)
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
assert
.
Equal
(
t
,
"runner:build_queue:token"
,
key
)
assert
.
Equal
(
t
,
"last_update"
,
value
)
executed
=
true
return
redis
.
WatchKeyStatusSeenChange
,
nil
}
h
:=
RegisterHandler
(
echoRequestFunc
,
watchKeyHandler
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusNoContent
,
rw
.
Code
)
assert
.
True
(
t
,
executed
)
}
}
func
TestRegisterHandlerWatcherTimeout
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerWatcherTimeout
(
t
*
testing
.
T
)
{
data
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
exceptWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusTimeout
,
nil
,
http
.
StatusNoContent
)
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
assert
.
Equal
(
t
,
"runner:build_queue:token"
,
key
)
assert
.
Equal
(
t
,
"last_update"
,
value
)
executed
=
true
return
redis
.
WatchKeyStatusTimeout
,
nil
}
h
:=
RegisterHandler
(
echoRequestFunc
,
watchKeyHandler
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusNoContent
,
rw
.
Code
)
assert
.
True
(
t
,
executed
)
}
}
func
TestRegisterHandlerWatcherNoChange
(
t
*
testing
.
T
)
{
func
TestRegisterHandlerWatcherNoChange
(
t
*
testing
.
T
)
{
data
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
exceptWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
nil
,
http
.
StatusNoContent
)
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
assert
.
Equal
(
t
,
"runner:build_queue:token"
,
key
)
assert
.
Equal
(
t
,
"last_update"
,
value
)
executed
=
true
return
redis
.
WatchKeyStatusNoChange
,
nil
}
h
:=
RegisterHandler
(
echoRequestFunc
,
watchKeyHandler
,
time
.
Second
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
data
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
h
.
ServeHTTP
(
rw
,
req
)
assert
.
Equal
(
t
,
http
.
StatusNoContent
,
rw
.
Code
)
assert
.
True
(
t
,
executed
)
}
}
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