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
2ff811ed
Commit
2ff811ed
authored
Mar 02, 2017
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update after review
parent
378dcc60
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
26 deletions
+20
-26
internal/builds/register.go
internal/builds/register.go
+7
-12
internal/builds/register_test.go
internal/builds/register_test.go
+12
-14
internal/helper/helpers.go
internal/helper/helpers.go
+1
-0
No files found.
internal/builds/register.go
View file @
2ff811ed
...
...
@@ -31,15 +31,11 @@ var (
},
[]
string
{
"state"
},
)
)
var
(
registerHandlerOpenAtReading
=
registerHandlerOpen
.
WithLabelValues
(
"reading"
)
registerHandlerOpenAtProxying
=
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
registerHandlerOpenAtWatching
=
registerHandlerOpen
.
WithLabelValues
(
"watching"
)
)
var
(
registerHandlerBodyReadErrors
=
registerHandlerHits
.
WithLabelValues
(
"body-read-error"
)
registerHandlerBodyParseErrors
=
registerHandlerHits
.
WithLabelValues
(
"body-parse-error"
)
registerHandlerMissingValues
=
registerHandlerHits
.
WithLabelValues
(
"missing-values"
)
...
...
@@ -74,19 +70,18 @@ func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
return
helper
.
ReadRequestBody
(
w
,
r
,
maxRegisterBodySize
)
}
func
readRunnerRequest
(
r
*
http
.
Request
,
body
[]
byte
)
(
runnerRequest
,
error
)
{
var
runnerRequest
runnerRequest
func
readRunnerRequest
(
r
*
http
.
Request
,
body
[]
byte
)
(
*
runnerRequest
,
error
)
{
if
!
helper
.
IsApplicationJson
(
r
)
{
return
runnerRequest
,
errors
.
New
(
"invalid content-type received"
)
return
nil
,
errors
.
New
(
"invalid content-type received"
)
}
var
runnerRequest
runnerRequest
err
:=
json
.
Unmarshal
(
body
,
&
runnerRequest
)
if
err
!=
nil
{
return
runnerRequest
,
err
return
nil
,
err
}
return
runnerRequest
,
nil
return
&
runnerRequest
,
nil
}
func
proxyRegisterRequest
(
h
http
.
Handler
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -141,7 +136,7 @@ func RegisterHandler(h http.Handler, watchHandler WatchKeyHandler, pollingDurati
switch
result
{
// It means that we detected a change before starting watching on change,
// We proxy request to Rails, to see whether we
can receive the build
// We proxy request to Rails, to see whether we
have a build to receive
case
redis
.
WatchKeyStatusAlreadyChanged
:
registerHandlerAlreadyChangedHits
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
newRequest
)
...
...
@@ -150,7 +145,7 @@ func RegisterHandler(h http.Handler, watchHandler WatchKeyHandler, pollingDurati
// We could potentially proxy request to Rails, but...
// We can end-up with unreliable responses,
// as don't really know whether ResponseWriter is still in a sane state,
//
whether the connection is not
dead
//
for example the connection is
dead
case
redis
.
WatchKeyStatusSeenChange
:
registerHandlerSeenChangeHits
.
Inc
()
w
.
WriteHeader
(
http
.
StatusNoContent
)
...
...
internal/builds/register_test.go
View file @
2ff811ed
...
...
@@ -20,8 +20,6 @@ func echoRequest(rw http.ResponseWriter, req *http.Request) {
var
echoRequestFunc
=
http
.
HandlerFunc
(
echoRequest
)
const
applicationJson
=
"application/json"
func
expectHandlerWithWatcher
(
t
*
testing
.
T
,
watchHandler
WatchKeyHandler
,
data
string
,
contentType
string
,
expectedHttpStatus
int
,
msgAndArgs
...
interface
{})
{
h
:=
RegisterHandler
(
echoRequestFunc
,
watchHandler
,
time
.
Second
)
...
...
@@ -40,7 +38,7 @@ func expectHandler(t *testing.T, data string, contentType string, expectedHttpSt
func
TestRegisterHandlerLargeBody
(
t
*
testing
.
T
)
{
data
:=
strings
.
Repeat
(
"."
,
maxRegisterBodySize
+
5
)
expectHandler
(
t
,
data
,
applicationJson
,
http
.
StatusRequestEntityTooLarge
,
expectHandler
(
t
,
data
,
"application/json"
,
http
.
StatusRequestEntityTooLarge
,
"rejects body with entity too large"
)
}
...
...
@@ -50,20 +48,20 @@ func TestRegisterHandlerInvalidRunnerRequest(t *testing.T) {
}
func
TestRegisterHandlerInvalidJsonPayload
(
t
*
testing
.
T
)
{
expectHandler
(
t
,
"{["
,
applicationJson
,
http
.
StatusOK
,
expectHandler
(
t
,
`{[`
,
"application/json"
,
http
.
StatusOK
,
"fails on parsing body and proxies request to upstream"
)
}
func
TestRegisterHandlerMissingData
(
t
*
testing
.
T
)
{
dataList
:=
[]
string
{
"{
\"
token
\"
:
\"
token
\"
}"
,
"{
\"
last_update
\"
:
\"
data
\"
}"
}
dataList
:=
[]
string
{
`{"token":"token"}`
,
`{"last_update":"data"}`
}
for
_
,
data
:=
range
dataList
{
expectHandler
(
t
,
data
,
applicationJson
,
http
.
StatusOK
,
expectHandler
(
t
,
data
,
"application/json"
,
http
.
StatusOK
,
"fails on argument validation and proxies request to upstream"
)
}
}
func
ex
cep
tWatcherToBeExecuted
(
t
*
testing
.
T
,
watchKeyStatus
redis
.
WatchKeyStatus
,
watchKeyError
error
,
func
ex
pec
tWatcherToBeExecuted
(
t
*
testing
.
T
,
watchKeyStatus
redis
.
WatchKeyStatus
,
watchKeyError
error
,
httpStatus
int
,
msgAndArgs
...
interface
{})
{
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
...
...
@@ -71,33 +69,33 @@ func exceptWatcherToBeExecuted(t *testing.T, watchKeyStatus redis.WatchKeyStatus
return
watchKeyStatus
,
watchKeyError
}
parsableData
:=
"{
\"
token
\"
:
\"
token
\"
,
\"
last_update
\"
:
\"
last_update
\"
}"
parsableData
:=
`{"token":"token","last_update":"last_update"}`
expectHandlerWithWatcher
(
t
,
watchKeyHandler
,
parsableData
,
applicationJson
,
httpStatus
,
msgAndArgs
...
)
expectHandlerWithWatcher
(
t
,
watchKeyHandler
,
parsableData
,
"application/json"
,
httpStatus
,
msgAndArgs
...
)
assert
.
True
(
t
,
executed
,
msgAndArgs
...
)
}
func
TestRegisterHandlerWatcherError
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
),
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
),
http
.
StatusOK
,
"proxies data to upstream"
)
}
func
TestRegisterHandlerWatcherAlreadyChanged
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusAlreadyChanged
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusAlreadyChanged
,
nil
,
http
.
StatusOK
,
"proxies data to upstream"
)
}
func
TestRegisterHandlerWatcherSeenChange
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusSeenChange
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusSeenChange
,
nil
,
http
.
StatusNoContent
)
}
func
TestRegisterHandlerWatcherTimeout
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusTimeout
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusTimeout
,
nil
,
http
.
StatusNoContent
)
}
func
TestRegisterHandlerWatcherNoChange
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
nil
,
http
.
StatusNoContent
)
}
internal/helper/helpers.go
View file @
2ff811ed
...
...
@@ -197,6 +197,7 @@ func ReadRequestBody(w http.ResponseWriter, r *http.Request, maxBodySize int64)
func
CloneRequestWithNewBody
(
r
*
http
.
Request
,
body
[]
byte
)
*
http
.
Request
{
newReq
:=
*
r
newReq
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
newReq
.
Header
=
HeaderClone
(
r
.
Header
)
newReq
.
ContentLength
=
int64
(
len
(
body
))
return
&
newReq
}
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