Commit c0cde5db authored by Kamil Trzcinski's avatar Kamil Trzcinski

Refactor register tests

parent a5d889dc
...@@ -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)
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment