Add unit tests for provide runner route

This commit is contained in:
Jan-Eric Hellenberg
2021-06-08 15:50:48 +02:00
committed by Jan-Eric Hellenberg
parent 7bbd7b7bae
commit 61bc7d0143
7 changed files with 206 additions and 129 deletions

View File

@@ -3,7 +3,8 @@ package tests
import "errors"
const (
NonExistingID = "n0n-3x1st1ng-1d"
NonExistingIntegerID = 9999
NonExistingStringID = "n0n-3x1st1ng-1d"
DefaultFileName = "test.txt"
DefaultFileContent = "Hello, Codemoon!"
DefaultDirectoryName = "test/"

View File

@@ -15,25 +15,45 @@ import (
)
func (s *E2ETestSuite) TestProvideRunnerRoute() {
runnerRequestString, _ := json.Marshal(dto.RunnerRequest{})
reader := strings.NewReader(string(runnerRequestString))
resp, err := http.Post(helpers.BuildURL(api.BasePath, api.RunnersPath), "application/json", reader)
s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode, "The response code should be ok")
runnerRequestByteString, _ := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
})
reader := bytes.NewReader(runnerRequestByteString)
runnerResponse := new(dto.RunnerResponse)
err = json.NewDecoder(resp.Body).Decode(runnerResponse)
s.NoError(err)
s.Run("valid request returns a runner", func() {
resp, err := http.Post(helpers.BuildURL(api.BasePath, api.RunnersPath), "application/json", reader)
s.Require().NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
s.True(runnerResponse.Id != "", "The response contains a runner id")
runnerResponse := new(dto.RunnerResponse)
err = json.NewDecoder(resp.Body).Decode(runnerResponse)
s.Require().NoError(err)
s.NotEmpty(runnerResponse.Id)
})
s.Run("invalid request returns bad request", func() {
resp, err := http.Post(helpers.BuildURL(api.BasePath, api.RunnersPath), "application/json", strings.NewReader(""))
s.Require().NoError(err)
s.Equal(http.StatusBadRequest, resp.StatusCode)
})
s.Run("requesting runner of unknown execution environment returns not found", func() {
runnerRequestByteString, _ := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentId: tests.NonExistingIntegerID,
})
reader := bytes.NewReader(runnerRequestByteString)
resp, err := http.Post(helpers.BuildURL(api.BasePath, api.RunnersPath), "application/json", reader)
s.Require().NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
}
// ProvideRunner creates a runner with the given RunnerRequest via an external request.
// It needs a running Poseidon instance to work.
func ProvideRunner(request *dto.RunnerRequest) (string, error) {
url := helpers.BuildURL(api.BasePath, api.RunnersPath)
runnerRequestString, _ := json.Marshal(request)
reader := strings.NewReader(string(runnerRequestString))
runnerRequestByteString, _ := json.Marshal(request)
reader := strings.NewReader(string(runnerRequestByteString))
resp, err := http.Post(url, "application/json", reader)
if err != nil {
return "", err
@@ -50,7 +70,9 @@ func ProvideRunner(request *dto.RunnerRequest) (string, error) {
}
func (s *E2ETestSuite) TestDeleteRunnerRoute() {
runnerId, err := ProvideRunner(&dto.RunnerRequest{})
runnerId, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
})
s.NoError(err)
s.Run("Deleting the runner returns NoContent", func() {
@@ -66,14 +88,16 @@ func (s *E2ETestSuite) TestDeleteRunnerRoute() {
})
s.Run("Deleting non-existing runner returns NotFound", func() {
resp, err := helpers.HttpDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingID), nil)
resp, err := helpers.HttpDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingStringID), nil)
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
}
func (s *E2ETestSuite) TestCopyFilesRoute() {
runnerID, err := ProvideRunner(&dto.RunnerRequest{})
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
})
s.NoError(err)
copyFilesRequestByteString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
Copy: []dto.File{{Path: tests.DefaultFileName, Content: []byte(tests.DefaultFileContent)}},
@@ -178,7 +202,7 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
})
s.Run("Copying to non-existing runner returns NotFound", func() {
resp, err := helpers.HttpPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingID, api.UpdateFileSystemPath), "application/json", bytes.NewReader(copyFilesRequestByteString))
resp, err := helpers.HttpPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingStringID, api.UpdateFileSystemPath), "application/json", bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})

View File

@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests"
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests/helpers"
"net/http"
"strings"
@@ -14,7 +15,9 @@ import (
)
func (s *E2ETestSuite) TestExecuteCommandRoute() {
runnerId, err := ProvideRunner(&dto.RunnerRequest{})
runnerId, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
})
s.Require().NoError(err)
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerId, &dto.ExecutionRequest{Command: "true"})
@@ -145,7 +148,9 @@ func (s *E2ETestSuite) TestEchoEnvironment() {
// ProvideWebSocketConnection establishes a client WebSocket connection to run the passed ExecutionRequest.
// It requires a running Poseidon instance.
func ProvideWebSocketConnection(suite *suite.Suite, request *dto.ExecutionRequest) (connection *websocket.Conn, err error) {
runnerId, err := ProvideRunner(&dto.RunnerRequest{})
runnerId, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
})
if err != nil {
return
}
@@ -161,8 +166,8 @@ func ProvideWebSocketConnection(suite *suite.Suite, request *dto.ExecutionReques
// It requires a running Poseidon instance.
func ProvideWebSocketURL(suite *suite.Suite, runnerId string, request *dto.ExecutionRequest) (string, error) {
url := helpers.BuildURL(api.BasePath, api.RunnersPath, runnerId, api.ExecutePath)
executionRequestBytes, _ := json.Marshal(request)
reader := strings.NewReader(string(executionRequestBytes))
executionRequestByteString, _ := json.Marshal(request)
reader := strings.NewReader(string(executionRequestByteString))
resp, err := http.Post(url, "application/json", reader)
suite.Require().NoError(err)
suite.Require().Equal(http.StatusOK, resp.StatusCode)

View File

@@ -3,6 +3,7 @@
package helpers
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
@@ -157,10 +158,10 @@ func HttpPut(url string, body io.Reader) (response *http.Response, err error) {
}
func HttpPutJSON(url string, body interface{}) (response *http.Response, err error) {
requestString, err := json.Marshal(body)
requestByteString, err := json.Marshal(body)
if err != nil {
return
}
reader := strings.NewReader(string(requestString))
reader := bytes.NewReader(requestByteString)
return HttpPut(url, reader)
}