Refactor interfaces to use a runner manager and an environment manager.
See https://gitlab.hpi.de/codeocean/codemoon/poseidon/-/issues/44.
This commit is contained in:
24
api/api.go
24
api/api.go
@ -5,7 +5,7 @@ import (
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/auth"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/logging"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/nomad"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -17,32 +17,34 @@ const (
|
||||
RouteRunners = "/runners"
|
||||
)
|
||||
|
||||
// NewRouter returns an HTTP handler (http.Handler) which can be
|
||||
// NewRouter returns a *mux.Router which can be
|
||||
// used by the net/http package to serve the routes of our API. It
|
||||
// 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(apiClient nomad.ExecutorApi, runnerPool environment.RunnerPool) *mux.Router {
|
||||
func NewRouter(runnerManager runner.Manager, environmentManager environment.Manager) *mux.Router {
|
||||
router := mux.NewRouter()
|
||||
// this can later be restricted to a specific host with
|
||||
// `router.Host(...)` and to HTTPS with `router.Schemes("https")`
|
||||
router = newRouterV1(router, apiClient, runnerPool)
|
||||
configureV1Router(router, runnerManager, environmentManager)
|
||||
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, apiClient nomad.ExecutorApi, runnerPool environment.RunnerPool) *mux.Router {
|
||||
// configureV1Router configures a given router with the routes of version 1 of our API.
|
||||
func configureV1Router(router *mux.Router, runnerManager runner.Manager, environmentManager environment.Manager) {
|
||||
v1 := router.PathPrefix(RouteBase).Subrouter()
|
||||
v1.HandleFunc(RouteHealth, Health).Methods(http.MethodGet)
|
||||
|
||||
runnerController := &RunnerController{manager: runnerManager}
|
||||
|
||||
if auth.InitializeAuthentication() {
|
||||
// Create new authenticated subrouter.
|
||||
// All routes added to v1 after this require authentication.
|
||||
v1 = v1.PathPrefix("").Subrouter()
|
||||
v1.Use(auth.HTTPAuthenticationMiddleware)
|
||||
authenticatedV1Router := v1.PathPrefix("").Subrouter()
|
||||
authenticatedV1Router.Use(auth.HTTPAuthenticationMiddleware)
|
||||
runnerController.ConfigureRoutes(authenticatedV1Router)
|
||||
} else {
|
||||
runnerController.ConfigureRoutes(v1)
|
||||
}
|
||||
registerRunnerRoutes(v1.PathPrefix(RouteRunners).Subrouter(), apiClient, runnerPool)
|
||||
|
||||
return v1
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@ -17,7 +16,7 @@ func mockHTTPHandler(writer http.ResponseWriter, _ *http.Request) {
|
||||
func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
|
||||
config.Config.Server.Token = ""
|
||||
router := mux.NewRouter()
|
||||
v1 := newRouterV1(router, nil, environment.NewLocalRunnerPool())
|
||||
configureV1Router(router, nil, nil)
|
||||
|
||||
t.Run("health route is accessible", func(t *testing.T) {
|
||||
request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil)
|
||||
@ -30,7 +29,7 @@ func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("added route is accessible", func(t *testing.T) {
|
||||
v1.HandleFunc("/test", mockHTTPHandler)
|
||||
router.HandleFunc("/api/v1/test", mockHTTPHandler)
|
||||
request, err := http.NewRequest(http.MethodGet, "/api/v1/test", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -44,7 +43,7 @@ func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
|
||||
func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) {
|
||||
config.Config.Server.Token = "TestToken"
|
||||
router := mux.NewRouter()
|
||||
v1 := newRouterV1(router, nil, environment.NewLocalRunnerPool())
|
||||
configureV1Router(router, nil, nil)
|
||||
|
||||
t.Run("health route is accessible", func(t *testing.T) {
|
||||
request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil)
|
||||
@ -56,9 +55,8 @@ func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNoContent, recorder.Code)
|
||||
})
|
||||
|
||||
t.Run("added route is not accessible", func(t *testing.T) {
|
||||
v1.HandleFunc("/test", mockHTTPHandler)
|
||||
request, err := http.NewRequest(http.MethodGet, "/api/v1/test", nil)
|
||||
t.Run("protected route is not accessible", func(t *testing.T) {
|
||||
request, err := http.NewRequest(http.MethodPost, "/api/v1/runners", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -18,6 +18,9 @@ type RunnerResponse struct {
|
||||
Id string `json:"runnerId"`
|
||||
}
|
||||
|
||||
// FileCreation is the expected json structure of the request body for the copy files route.
|
||||
type FileCreation struct{}
|
||||
|
||||
// WebsocketResponse is the expected response when creating an execution for a runner.
|
||||
type WebsocketResponse struct {
|
||||
WebsocketUrl string `json:"websocketUrl"`
|
||||
|
20
api/environments.go
Normal file
20
api/environments.go
Normal file
@ -0,0 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type EnvironmentController struct {
|
||||
manager environment.Manager // nolint:unused,structcheck
|
||||
}
|
||||
|
||||
// create creates a new execution environment on the executor.
|
||||
func (e *EnvironmentController) create(writer http.ResponseWriter, request *http.Request) { // nolint:unused
|
||||
|
||||
}
|
||||
|
||||
// delete removes an execution environment from the executor
|
||||
func (e *EnvironmentController) delete(writer http.ResponseWriter, request *http.Request) { // nolint:unused
|
||||
|
||||
}
|
@ -4,7 +4,8 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Health tries to respond that the server is alive.
|
||||
// Health handles the health route.
|
||||
// It tries to respond that the server is alive.
|
||||
// If it is not, the response won't reach the client.
|
||||
func Health(writer http.ResponseWriter, _ *http.Request) {
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
|
169
api/runners.go
169
api/runners.go
@ -1,13 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"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/nomad"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -21,106 +18,116 @@ const (
|
||||
ExecutionIdKey = "executionId"
|
||||
)
|
||||
|
||||
// provideRunner tries to respond with the id of a runner
|
||||
type RunnerController struct {
|
||||
manager runner.Manager
|
||||
runnerRouter *mux.Router
|
||||
}
|
||||
|
||||
// ConfigureRoutes configures a given router with the runner routes of our API.
|
||||
func (r *RunnerController) ConfigureRoutes(router *mux.Router) {
|
||||
runnersRouter := router.PathPrefix(RouteRunners).Subrouter()
|
||||
runnersRouter.HandleFunc("", r.provide).Methods(http.MethodPost)
|
||||
r.runnerRouter = runnersRouter.PathPrefix(fmt.Sprintf("/{%s}", RunnerIdKey)).Subrouter()
|
||||
r.runnerRouter.Use(r.findRunnerMiddleware)
|
||||
r.runnerRouter.HandleFunc(ExecutePath, r.execute).Methods(http.MethodPost).Name(ExecutePath)
|
||||
r.runnerRouter.HandleFunc(WebsocketPath, connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
|
||||
r.runnerRouter.HandleFunc("", r.delete).Methods(http.MethodDelete).Name(DeleteRoute)
|
||||
}
|
||||
|
||||
// provide handles the provide runners API route.
|
||||
// It tries to respond with the id of a unused runner.
|
||||
// This runner is then reserved for future use
|
||||
func provideRunner(writer http.ResponseWriter, request *http.Request) {
|
||||
func (r *RunnerController) provide(writer http.ResponseWriter, request *http.Request) {
|
||||
runnerRequest := new(dto.RunnerRequest)
|
||||
if err := parseJSONRequestBody(writer, request, runnerRequest); err != nil {
|
||||
return
|
||||
}
|
||||
executionEnvironment, err := environment.GetExecutionEnvironment(runnerRequest.ExecutionEnvironmentId)
|
||||
environmentId := runner.EnvironmentId(runnerRequest.ExecutionEnvironmentId)
|
||||
nextRunner, err := r.manager.Use(environmentId)
|
||||
if err != nil {
|
||||
writeNotFound(writer, err)
|
||||
return
|
||||
}
|
||||
nextRunner, err := executionEnvironment.NextRunner()
|
||||
if err != nil {
|
||||
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
|
||||
if err == runner.ErrUnknownExecutionEnvironment {
|
||||
writeNotFound(writer, err)
|
||||
} else if err == runner.ErrNoRunnersAvailable {
|
||||
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
|
||||
} else {
|
||||
writeInternalServerError(writer, err, dto.ErrorUnknown)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
sendJson(writer, &dto.RunnerResponse{Id: nextRunner.Id()}, http.StatusOK)
|
||||
}
|
||||
|
||||
// executeCommand takes an ExecutionRequest and stores it for a runner.
|
||||
// execute handles the execute API route.
|
||||
// It 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 := parseJSONRequestBody(writer, request, executionRequest); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var scheme string
|
||||
if config.Config.Server.TLS {
|
||||
scheme = "wss"
|
||||
} else {
|
||||
scheme = "ws"
|
||||
}
|
||||
r, _ := runner.FromContext(request.Context())
|
||||
|
||||
path, err := router.Get(WebsocketPath).URL(RunnerIdKey, r.Id())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Could not create runner websocket URL.")
|
||||
writeInternalServerError(writer, err, dto.ErrorUnknown)
|
||||
return
|
||||
}
|
||||
id, err := r.AddExecution(*executionRequest)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Could not store execution.")
|
||||
writeInternalServerError(writer, err, dto.ErrorUnknown)
|
||||
return
|
||||
}
|
||||
websocketUrl := url.URL{
|
||||
Scheme: scheme,
|
||||
Host: request.Host,
|
||||
Path: path.String(),
|
||||
RawQuery: fmt.Sprintf("%s=%s", ExecutionIdKey, id),
|
||||
}
|
||||
|
||||
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl.String()}, http.StatusOK)
|
||||
func (r *RunnerController) execute(writer http.ResponseWriter, request *http.Request) {
|
||||
executionRequest := new(dto.ExecutionRequest)
|
||||
if err := parseJSONRequestBody(writer, request, executionRequest); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var scheme string
|
||||
if config.Config.Server.TLS {
|
||||
scheme = "wss"
|
||||
} else {
|
||||
scheme = "ws"
|
||||
}
|
||||
targetRunner, _ := runner.FromContext(request.Context())
|
||||
|
||||
path, err := r.runnerRouter.Get(WebsocketPath).URL(RunnerIdKey, targetRunner.Id())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Could not create runner websocket URL.")
|
||||
writeInternalServerError(writer, err, dto.ErrorUnknown)
|
||||
return
|
||||
}
|
||||
id, err := targetRunner.AddExecution(*executionRequest)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Could not store execution.")
|
||||
writeInternalServerError(writer, err, dto.ErrorUnknown)
|
||||
return
|
||||
}
|
||||
websocketUrl := url.URL{
|
||||
Scheme: scheme,
|
||||
Host: request.Host,
|
||||
Path: path.String(),
|
||||
RawQuery: fmt.Sprintf("%s=%s", ExecutionIdKey, id),
|
||||
}
|
||||
|
||||
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl.String()}, http.StatusOK)
|
||||
}
|
||||
|
||||
// The findRunnerMiddleware looks up the runnerId for routes containing it
|
||||
// and adds the runner to the context of the request.
|
||||
func findRunnerMiddleware(runnerPool environment.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)[RunnerIdKey]
|
||||
r, ok := runnerPool.Get(runnerId)
|
||||
if !ok {
|
||||
writeNotFound(writer, errors.New("no runner with this id"))
|
||||
return
|
||||
}
|
||||
ctx := runner.NewContext(request.Context(), r.(runner.Runner))
|
||||
requestWithRunner := request.WithContext(ctx)
|
||||
next.ServeHTTP(writer, requestWithRunner)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func deleteRunner(apiClient nomad.ExecutorApi, runnerPool environment.RunnerPool) func(writer http.ResponseWriter, request *http.Request) {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
targetRunner, _ := runner.FromContext(request.Context())
|
||||
|
||||
err := apiClient.DeleteRunner(targetRunner.Id())
|
||||
func (r *RunnerController) findRunnerMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
// Find runner
|
||||
runnerId := mux.Vars(request)[RunnerIdKey]
|
||||
r, err := r.manager.Get(runnerId)
|
||||
if err != nil {
|
||||
writeInternalServerError(writer, err, dto.ErrorNomadInternalServerError)
|
||||
writeNotFound(writer, err)
|
||||
return
|
||||
}
|
||||
ctx := runner.NewContext(request.Context(), r.(runner.Runner))
|
||||
requestWithRunner := request.WithContext(ctx)
|
||||
next.ServeHTTP(writer, requestWithRunner)
|
||||
})
|
||||
}
|
||||
|
||||
runnerPool.Delete(targetRunner.Id())
|
||||
// delete handles the delete runner API route.
|
||||
// It destroys the given runner on the executor and removes it from the used runners list.
|
||||
func (r *RunnerController) delete(writer http.ResponseWriter, request *http.Request) {
|
||||
targetRunner, _ := runner.FromContext(request.Context())
|
||||
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
err := r.manager.Return(targetRunner)
|
||||
if err != nil {
|
||||
if err == runner.ErrUnknownExecutionEnvironment {
|
||||
writeNotFound(writer, err)
|
||||
}
|
||||
|
||||
writeInternalServerError(writer, err, dto.ErrorNomadInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func registerRunnerRoutes(router *mux.Router, apiClient nomad.ExecutorApi, runnerPool environment.RunnerPool) {
|
||||
router.HandleFunc("", provideRunner).Methods(http.MethodPost)
|
||||
runnerRouter := router.PathPrefix(fmt.Sprintf("/{%s}", RunnerIdKey)).Subrouter()
|
||||
runnerRouter.Use(findRunnerMiddleware(runnerPool))
|
||||
runnerRouter.HandleFunc(ExecutePath, executeCommand(runnerRouter)).Methods(http.MethodPost).Name(ExecutePath)
|
||||
runnerRouter.HandleFunc(WebsocketPath, connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
|
||||
runnerRouter.HandleFunc("", deleteRunner(apiClient, runnerPool)).Methods(http.MethodDelete).Name(DeleteRoute)
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
@ -3,15 +3,12 @@ package api
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/nomad"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -20,11 +17,27 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindRunnerMiddleware(t *testing.T) {
|
||||
runnerPool := environment.NewLocalRunnerPool()
|
||||
type MiddlewareTestSuite struct {
|
||||
suite.Suite
|
||||
manager *runner.ManagerMock
|
||||
router *mux.Router
|
||||
runnerController *RunnerController
|
||||
testRunner runner.Runner
|
||||
}
|
||||
|
||||
func (suite *MiddlewareTestSuite) SetupTest() {
|
||||
suite.manager = &runner.ManagerMock{}
|
||||
suite.router = mux.NewRouter()
|
||||
suite.runnerController = &RunnerController{suite.manager, suite.router}
|
||||
suite.testRunner = runner.NewRunner("runner")
|
||||
}
|
||||
|
||||
func TestMiddlewareTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MiddlewareTestSuite))
|
||||
}
|
||||
|
||||
func (suite *MiddlewareTestSuite) TestFindRunnerMiddleware() {
|
||||
var capturedRunner runner.Runner
|
||||
testRunner := runner.NewExerciseRunner("testRunner")
|
||||
runnerPool.Add(testRunner)
|
||||
|
||||
testRunnerIdRoute := func(writer http.ResponseWriter, request *http.Request) {
|
||||
var ok bool
|
||||
@ -35,12 +48,8 @@ func TestFindRunnerMiddleware(t *testing.T) {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
router := mux.NewRouter()
|
||||
router.Use(findRunnerMiddleware(runnerPool))
|
||||
router.HandleFunc(fmt.Sprintf("/test/{%s}", RunnerIdKey), testRunnerIdRoute).Name("test-runner-id")
|
||||
|
||||
testRunnerRequest := func(t *testing.T, runnerId string) *http.Request {
|
||||
path, err := router.Get("test-runner-id").URL(RunnerIdKey, runnerId)
|
||||
path, err := suite.router.Get("test-runner-id").URL(RunnerIdKey, runnerId)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -51,36 +60,57 @@ func TestFindRunnerMiddleware(t *testing.T) {
|
||||
return request
|
||||
}
|
||||
|
||||
t.Run("sets runner in context if runner exists", func(t *testing.T) {
|
||||
suite.router.Use(suite.runnerController.findRunnerMiddleware)
|
||||
suite.router.HandleFunc(fmt.Sprintf("/test/{%s}", RunnerIdKey), testRunnerIdRoute).Name("test-runner-id")
|
||||
|
||||
suite.manager.On("Get", suite.testRunner.Id()).Return(suite.testRunner, nil)
|
||||
suite.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()))
|
||||
suite.router.ServeHTTP(recorder, testRunnerRequest(t, suite.testRunner.Id()))
|
||||
|
||||
assert.Equal(t, http.StatusOK, recorder.Code)
|
||||
assert.Equal(t, testRunner, capturedRunner)
|
||||
assert.Equal(t, suite.testRunner, capturedRunner)
|
||||
})
|
||||
|
||||
t.Run("returns 404 if runner does not exist", func(t *testing.T) {
|
||||
invalidID := "some-invalid-runner-id"
|
||||
suite.manager.On("Get", invalidID).Return(nil, runner.ErrRunnerNotFound)
|
||||
suite.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"))
|
||||
suite.router.ServeHTTP(recorder, testRunnerRequest(t, invalidID))
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, recorder.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestExecuteRoute(t *testing.T) {
|
||||
runnerPool := environment.NewLocalRunnerPool()
|
||||
router := NewRouter(nil, runnerPool)
|
||||
testRunner := runner.NewExerciseRunner("testRunner")
|
||||
runnerPool.Add(testRunner)
|
||||
func TestRunnerRouteTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(RunnerRouteTestSuite))
|
||||
}
|
||||
|
||||
path, err := router.Get(ExecutePath).URL(RunnerIdKey, testRunner.Id())
|
||||
type RunnerRouteTestSuite struct {
|
||||
suite.Suite
|
||||
runnerManager *runner.ManagerMock
|
||||
environmentManager *environment.ManagerMock
|
||||
router *mux.Router
|
||||
runner runner.Runner
|
||||
}
|
||||
|
||||
func (suite *RunnerRouteTestSuite) SetupTest() {
|
||||
suite.runnerManager = &runner.ManagerMock{}
|
||||
suite.environmentManager = &environment.ManagerMock{}
|
||||
suite.router = NewRouter(suite.runnerManager, suite.environmentManager)
|
||||
suite.runner = runner.NewRunner("test_runner")
|
||||
suite.runnerManager.On("Get", suite.runner.Id()).Return(suite.runner, nil)
|
||||
}
|
||||
|
||||
func (suite *RunnerRouteTestSuite) TestExecuteRoute() {
|
||||
path, err := suite.router.Get(ExecutePath).URL(RunnerIdKey, suite.runner.Id())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal()
|
||||
}
|
||||
|
||||
t.Run("valid request", func(t *testing.T) {
|
||||
suite.Run("valid request", func() {
|
||||
recorder := httptest.NewRecorder()
|
||||
executionRequest := dto.ExecutionRequest{
|
||||
Command: "command",
|
||||
@ -89,131 +119,70 @@ func TestExecuteRoute(t *testing.T) {
|
||||
}
|
||||
body, err := json.Marshal(executionRequest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, path.String(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
|
||||
router.ServeHTTP(recorder, request)
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
|
||||
var websocketResponse dto.WebsocketResponse
|
||||
err = json.NewDecoder(recorder.Result().Body).Decode(&websocketResponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, recorder.Code)
|
||||
suite.Equal(http.StatusOK, recorder.Code)
|
||||
|
||||
t.Run("creates an execution request for the runner", func(t *testing.T) {
|
||||
suite.Run("creates an execution request for the runner", func() {
|
||||
url, err := url.Parse(websocketResponse.WebsocketUrl)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
executionId := url.Query().Get(ExecutionIdKey)
|
||||
storedExecutionRequest, ok := testRunner.Execution(runner.ExecutionId(executionId))
|
||||
storedExecutionRequest, ok := suite.runner.Execution(runner.ExecutionId(executionId))
|
||||
|
||||
assert.True(t, ok, "No execution request with this id: ", executionId)
|
||||
assert.Equal(t, executionRequest, storedExecutionRequest)
|
||||
suite.True(ok, "No execution request with this id: ", executionId)
|
||||
suite.Equal(executionRequest, storedExecutionRequest)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
suite.Run("invalid request", func() {
|
||||
recorder := httptest.NewRecorder()
|
||||
body := ""
|
||||
request, err := http.NewRequest(http.MethodPost, path.String(), strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
router.ServeHTTP(recorder, request)
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
suite.Equal(http.StatusBadRequest, recorder.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteRunnerRouteTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(DeleteRunnerRouteTestSuite))
|
||||
}
|
||||
|
||||
type DeleteRunnerRouteTestSuite struct {
|
||||
suite.Suite
|
||||
runnerPool environment.RunnerPool
|
||||
apiClient *nomad.ExecutorApiMock
|
||||
router *mux.Router
|
||||
testRunner runner.Runner
|
||||
path string
|
||||
}
|
||||
|
||||
func (suite *DeleteRunnerRouteTestSuite) SetupTest() {
|
||||
suite.runnerPool = environment.NewLocalRunnerPool()
|
||||
suite.apiClient = &nomad.ExecutorApiMock{}
|
||||
suite.router = NewRouter(suite.apiClient, suite.runnerPool)
|
||||
|
||||
suite.testRunner = runner.NewExerciseRunner("testRunner")
|
||||
suite.runnerPool.Add(suite.testRunner)
|
||||
|
||||
var err error
|
||||
runnerUrl, err := suite.router.Get(DeleteRoute).URL(RunnerIdKey, suite.testRunner.Id())
|
||||
func (suite *RunnerRouteTestSuite) TestDeleteRoute() {
|
||||
deleteURL, err := suite.router.Get(DeleteRoute).URL(RunnerIdKey, suite.runner.Id())
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
suite.path = runnerUrl.String()
|
||||
}
|
||||
deletePath := deleteURL.String()
|
||||
suite.runnerManager.On("Return", suite.runner).Return(nil)
|
||||
|
||||
func (suite *DeleteRunnerRouteTestSuite) TestValidRequestReturnsNoContent() {
|
||||
suite.apiClient.On("DeleteRunner", mock.AnythingOfType("string")).Return(nil)
|
||||
suite.Run("valid request", func() {
|
||||
recorder := httptest.NewRecorder()
|
||||
request, err := http.NewRequest(http.MethodDelete, deletePath, nil)
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request, err := http.NewRequest(http.MethodDelete, suite.path, nil)
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
suite.Equal(http.StatusNoContent, recorder.Code)
|
||||
|
||||
suite.Equal(http.StatusNoContent, recorder.Code)
|
||||
|
||||
suite.Run("runner is deleted on nomad", func() {
|
||||
suite.apiClient.AssertCalled(suite.T(), "DeleteRunner", suite.testRunner.Id())
|
||||
})
|
||||
|
||||
suite.Run("runner is deleted from runnerPool", func() {
|
||||
returnedRunner, ok := suite.runnerPool.Get(suite.testRunner.Id())
|
||||
suite.Nil(returnedRunner)
|
||||
suite.False(ok)
|
||||
suite.Run("runner was returned to runner manager", func() {
|
||||
suite.runnerManager.AssertCalled(suite.T(), "Return", suite.runner)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *DeleteRunnerRouteTestSuite) TestReturnInternalServerErrorWhenApiCallToNomadFailed() {
|
||||
suite.apiClient.On("DeleteRunner", mock.AnythingOfType("string")).Return(errors.New("API call failed"))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request, err := http.NewRequest(http.MethodDelete, suite.path, nil)
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
|
||||
suite.Equal(http.StatusInternalServerError, recorder.Code)
|
||||
}
|
||||
|
||||
func (suite *DeleteRunnerRouteTestSuite) TestDeleteInvalidRunnerIdReturnsNotFound() {
|
||||
var err error
|
||||
runnersUrl, err := suite.router.Get(DeleteRoute).URL(RunnerIdKey, "1nv4l1dID")
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
suite.path = runnersUrl.String()
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request, err := http.NewRequest(http.MethodDelete, suite.path, nil)
|
||||
if err != nil {
|
||||
suite.T().Fatal(err)
|
||||
}
|
||||
|
||||
suite.router.ServeHTTP(recorder, request)
|
||||
|
||||
suite.Equal(http.StatusNotFound, recorder.Code)
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
|
||||
@ -15,10 +14,8 @@ import (
|
||||
)
|
||||
|
||||
type WebsocketTestSuite struct {
|
||||
suite.Suite
|
||||
runner runner.Runner
|
||||
RunnerRouteTestSuite
|
||||
server *httptest.Server
|
||||
router *mux.Router
|
||||
executionId runner.ExecutionId
|
||||
}
|
||||
|
||||
@ -26,10 +23,12 @@ func TestWebsocketTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(WebsocketTestSuite))
|
||||
}
|
||||
|
||||
func (suite *WebsocketTestSuite) SetupSuite() {
|
||||
runnerPool := environment.NewLocalRunnerPool()
|
||||
suite.runner = runner.NewExerciseRunner("testRunner")
|
||||
runnerPool.Add(suite.runner)
|
||||
func (suite *WebsocketTestSuite) SetupTest() {
|
||||
suite.runnerManager = &runner.ManagerMock{}
|
||||
suite.environmentManager = &environment.ManagerMock{}
|
||||
suite.router = NewRouter(suite.runnerManager, suite.environmentManager)
|
||||
suite.runner = runner.NewRunner("test_runner")
|
||||
suite.runnerManager.On("Get", suite.runner.Id()).Return(suite.runner, nil)
|
||||
var err error
|
||||
suite.executionId, err = suite.runner.AddExecution(dto.ExecutionRequest{
|
||||
Command: "command",
|
||||
@ -38,11 +37,12 @@ func (suite *WebsocketTestSuite) SetupSuite() {
|
||||
})
|
||||
suite.Require().NoError(err)
|
||||
|
||||
router := mux.NewRouter()
|
||||
router.Use(findRunnerMiddleware(runnerPool))
|
||||
router.HandleFunc(fmt.Sprintf("%s/{%s}%s", RouteRunners, RunnerIdKey, WebsocketPath), connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
|
||||
suite.server = httptest.NewServer(router)
|
||||
suite.router = router
|
||||
// router.HandleFunc(fmt.Sprintf("%s/{%s}%s", RouteRunners, RunnerIdKey, WebsocketPath), connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
|
||||
suite.server = httptest.NewServer(suite.router)
|
||||
}
|
||||
|
||||
func (suite *WebsocketTestSuite) TearDownSuite() {
|
||||
suite.server.Close()
|
||||
}
|
||||
|
||||
func (suite *WebsocketTestSuite) websocketUrl(scheme, runnerId string, executionId runner.ExecutionId) (*url.URL, error) {
|
||||
@ -56,10 +56,6 @@ func (suite *WebsocketTestSuite) websocketUrl(scheme, runnerId string, execution
|
||||
return websocketUrl, nil
|
||||
}
|
||||
|
||||
func (suite *WebsocketTestSuite) TearDownSuite() {
|
||||
suite.server.Close()
|
||||
}
|
||||
|
||||
func (suite *WebsocketTestSuite) TestWebsocketConnectionCanBeEstablished() {
|
||||
path, err := suite.websocketUrl("ws", suite.runner.Id(), suite.executionId)
|
||||
suite.Require().NoError(err)
|
||||
|
Reference in New Issue
Block a user