Add tests for runners execute route

Co-authored-by: Tobias Kantusch <tobias.kantusch@student.hpi.uni-potsdam.de>
This commit is contained in:
Konrad Hanff
2021-04-30 16:23:00 +02:00
parent 6a00ea3165
commit 612bc55bdd
10 changed files with 300 additions and 90 deletions

View File

@ -3,7 +3,7 @@ package api
import (
"github.com/gorilla/mux"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/auth"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"gitlab.hpi.de/codeocean/codemoon/poseidon/logging"
"net/http"
)
@ -21,18 +21,18 @@ const (
// always returns a router for the newest version of our API. We
// use gorilla/mux because it is more convenient than net/http, e.g.
// when extracting path parameters.
func NewRouter() http.Handler {
func NewRouter(runnerPool pool.RunnerPool) *mux.Router {
router := mux.NewRouter()
router.Use(logging.HTTPLoggingMiddleware)
// this can later be restricted to a specific host with
// `router.Host(...)` and to HTTPS with `router.Schemes("https")`
newRouterV1(router)
router = newRouterV1(router, runnerPool)
router.Use(logging.HTTPLoggingMiddleware)
return router
}
// newRouterV1 returns a sub-router containing the routes of version
// 1 of our API.
func newRouterV1(router *mux.Router) *mux.Router {
func newRouterV1(router *mux.Router, runnerPool pool.RunnerPool) *mux.Router {
v1 := router.PathPrefix(RouteBase).Subrouter()
v1.HandleFunc(RouteHealth, Health).Methods(http.MethodGet)
@ -42,7 +42,7 @@ func newRouterV1(router *mux.Router) *mux.Router {
v1 = v1.PathPrefix("").Subrouter()
v1.Use(auth.HTTPAuthenticationMiddleware)
}
registerRunnerRoutes(v1.PathPrefix(RouteRunners).Subrouter())
registerRunnerRoutes(v1.PathPrefix(RouteRunners).Subrouter(), runnerPool)
return v1
}

View File

@ -4,6 +4,7 @@ import (
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"net/http"
"net/http/httptest"
"testing"
@ -16,7 +17,7 @@ func mockHTTPHandler(writer http.ResponseWriter, _ *http.Request) {
func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
config.Config.Server.Token = ""
router := mux.NewRouter()
v1 := newRouterV1(router)
v1 := newRouterV1(router, pool.NewLocalRunnerPool())
t.Run("health route is accessible", func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil)
@ -43,7 +44,7 @@ func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) {
config.Config.Server.Token = "TestToken"
router := mux.NewRouter()
v1 := newRouterV1(router)
v1 := newRouterV1(router, pool.NewLocalRunnerPool())
t.Run("health route is accessible", func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil)

View File

@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"log"
"net/http"
)

View File

@ -8,13 +8,24 @@ import (
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"log"
"net/http"
"sync"
)
// ProvideRunner tries to respond with the id of a runner
var (
executions = make(map[string]map[string]dto.ExecutionRequest)
executionsLock = sync.Mutex{}
)
func allocateExecutionMap(runner runner.Runner) {
executionsLock.Lock()
executions[runner.Id()] = make(map[string]dto.ExecutionRequest)
executionsLock.Unlock()
}
// provideRunner tries to respond with the id of a runner
// This runner is then reserved for future use
func provideRunner(writer http.ResponseWriter, request *http.Request) {
runnerRequest := new(dto.RunnerRequest)
@ -31,92 +42,87 @@ func provideRunner(writer http.ResponseWriter, request *http.Request) {
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
return
}
executionsLock.Lock()
executions[runner.Id] = make(map[string]dto.ExecutionRequest)
executionsLock.Unlock()
sendJson(writer, &dto.RunnerResponse{Id: runner.Id}, http.StatusOK)
allocateExecutionMap(runner)
sendJson(writer, &dto.RunnerResponse{Id: runner.Id()}, http.StatusOK)
}
var (
executions = make(map[string]map[string]dto.ExecutionRequest)
executionsLock = sync.Mutex{}
)
// executeCommand takes an ExecutionRequest and stores it for a runner.
// It returns a url to connect to for a websocket connection to this execution in the corresponding runner.
func executeCommand(router *mux.Router) func(w http.ResponseWriter, r *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
executionRequest := new(dto.ExecutionRequest)
if err := parseRequestBodyJSON(writer, request, executionRequest); err != nil {
return
}
func executeCommand(writer http.ResponseWriter, request *http.Request) {
executionRequest := new(dto.ExecutionRequest)
if err := parseRequestBodyJSON(writer, request, executionRequest); err != nil {
return
}
var scheme string
if config.Config.Server.TLS {
scheme = "wss"
} else {
scheme = "ws"
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
}
var scheme string
if config.Config.Server.TLS {
scheme = "wss"
} else {
scheme = "ws"
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
}
id, err := uuid.NewRandom()
if err != nil {
log.Printf("Error creating new execution id: %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
id, err := uuid.NewRandom()
if err != nil {
log.Printf("Error creating new execution id: %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
executionsLock.Lock()
runnerExecutions, ok := executions[r.Id()]
if !ok {
writeNotFound(writer, errors.New("runner has not been provided"))
return
}
runnerExecutions[id.String()] = *executionRequest
executionsLock.Unlock()
executionsLock.Lock()
runnerExecutions, ok := executions[r.Id]
if !ok {
writeNotFound(writer, errors.New("runner has not been provided"))
return
path, err := router.Get("runner-websocket").URL("runnerId", r.Id())
if err != nil {
log.Printf("Error creating runner websocket URL %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
websocketUrl := fmt.Sprintf("%s://%s%s?executionId=%s", scheme, request.Host, path, id)
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl}, http.StatusOK)
}
runnerExecutions[id.String()] = *executionRequest
executionsLock.Unlock()
path, err := router.Get("runner-websocket").URL("runnerId", r.Id)
if err != nil {
log.Printf("Error creating runner websocket URL %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
websocketUrl := fmt.Sprintf("%s://%s%s?executionId=%s", scheme, request.Host, path, id)
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl}, http.StatusOK)
}
func connectToRunner(writer http.ResponseWriter, request *http.Request) {
// Upgrade the connection to websocket
// Todo: Execute the command, upgrade the connection to websocket and handle forwarding
executionId := request.URL.Query()["executionId"]
log.Printf("Websocket for execution %s requested", executionId)
writer.WriteHeader(http.StatusNotImplemented)
}
func findRunnerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Find runner
runnerId := mux.Vars(request)["runnerId"]
// TODO: Get runner from runner store using runnerId
env, err := execution_environment.GetExecutionEnvironment(1)
if err != nil {
writeNotFound(writer, err)
return
}
r, ok := env.Runners[runnerId]
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
ctx := runner.NewContext(request.Context(), r)
requestWithRunner := request.WithContext(ctx)
next.ServeHTTP(writer, requestWithRunner)
})
// The findRunnerMiddleware looks up the runnerId for routes containing it
// and adds the runner to the context of the request.
func findRunnerMiddleware(runnerPool pool.RunnerPool) func(handler http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Find runner
runnerId := mux.Vars(request)["runnerId"]
r, ok := runnerPool.GetRunner(runnerId)
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
ctx := runner.NewContext(request.Context(), r)
requestWithRunner := request.WithContext(ctx)
next.ServeHTTP(writer, requestWithRunner)
})
}
}
func registerRunnerRoutes(router *mux.Router) {
func registerRunnerRoutes(router *mux.Router, runnerPool pool.RunnerPool) {
router.HandleFunc("", provideRunner).Methods(http.MethodPost)
runnerRouter := router.PathPrefix("/{runnerId}").Subrouter()
runnerRouter.Use(findRunnerMiddleware)
runnerRouter.HandleFunc("/execute", executeCommand).Methods(http.MethodPost).Name("runner-execute")
runnerRouter.Use(findRunnerMiddleware(runnerPool))
runnerRouter.HandleFunc("/execute", executeCommand(runnerRouter)).Methods(http.MethodPost).Name("runner-execute")
runnerRouter.HandleFunc("/websocket", connectToRunner).Methods(http.MethodGet).Name("runner-websocket")
}

150
api/runners_test.go Normal file
View File

@ -0,0 +1,150 @@
package api
import (
"bytes"
"encoding/json"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestFindRunnerMiddleware(t *testing.T) {
runnerPool := pool.NewLocalRunnerPool()
var capturedRunner runner.Runner
testRunner := runner.NewExerciseRunner("testRunner")
runnerPool.AddRunner(testRunner)
testRunnerIdRoute := func(writer http.ResponseWriter, request *http.Request) {
var ok bool
capturedRunner, ok = runner.FromContext(request.Context())
if ok {
writer.WriteHeader(http.StatusOK)
} else {
writer.WriteHeader(http.StatusInternalServerError)
}
}
router := mux.NewRouter()
router.Use(findRunnerMiddleware(runnerPool))
router.HandleFunc("/test/{runnerId}", testRunnerIdRoute).Name("test-runner-id")
testRunnerRequest := func(t *testing.T, runnerId string) *http.Request {
path, err := router.Get("test-runner-id").URL("runnerId", runnerId)
if err != nil {
t.Fatal(err)
}
request, err := http.NewRequest(
http.MethodPost, path.String(), nil)
if err != nil {
t.Fatal(err)
}
return request
}
t.Run("sets runner in context if runner exists", func(t *testing.T) {
capturedRunner = nil
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, testRunnerRequest(t, testRunner.Id()))
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, testRunner, capturedRunner)
})
t.Run("returns 404 if runner does not exist", func(t *testing.T) {
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, testRunnerRequest(t, "some-invalid-runner-id"))
assert.Equal(t, http.StatusNotFound, recorder.Code)
})
}
func TestExecuteRoute(t *testing.T) {
runnerPool := pool.NewLocalRunnerPool()
router := NewRouter(runnerPool)
testRunner := runner.NewExerciseRunner("testRunner")
runnerPool.AddRunner(testRunner)
allocateExecutionMap(testRunner)
path, err := router.Get("runner-execute").URL("runnerId", testRunner.Id())
if err != nil {
t.Fatal("Could not construct execute url")
}
t.Run("valid request", func(t *testing.T) {
recorder := httptest.NewRecorder()
executionRequest := dto.ExecutionRequest{
Command: "command",
TimeLimit: 10,
Environment: nil,
}
body, err := json.Marshal(executionRequest)
if err != nil {
t.Fatal(err)
}
request, err := http.NewRequest(http.MethodPost, path.String(), bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(recorder, request)
responseBody, err := io.ReadAll(recorder.Result().Body)
if err != nil {
t.Fatal(err)
}
var websocketResponse dto.WebsocketResponse
err = json.Unmarshal(responseBody, &websocketResponse)
if err != nil {
t.Fatal(err)
}
t.Run("returns 200", func(t *testing.T) {
assert.Equal(t, http.StatusOK, recorder.Code)
})
t.Run("creates an execution request for the runner", func(t *testing.T) {
url, err := url.Parse(websocketResponse.WebsocketUrl)
if err != nil {
t.Fatal(err)
}
executionId := url.Query().Get("executionId")
assert.Equal(t, executionRequest, executions[testRunner.Id()][executionId])
})
})
t.Run("invalid request", func(t *testing.T) {
recorder := httptest.NewRecorder()
body := ""
request, err := http.NewRequest(http.MethodPost, path.String(), strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(recorder, request)
_, err = io.ReadAll(recorder.Result().Body)
if err != nil {
t.Fatal(err)
}
t.Run("returns 400", func(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, recorder.Code)
})
})
}