Files
poseidon/tests/e2e/runners_test.go
Konrad Hanff 242d0175a2 Add tests and end-to-end tests for websocket execution
For unit tests, this mocks the runners Execute method with a
customizable function that operates on the request, streams and exit
channel to simulate a real execution.

End-to-end tests are moved to the tests/e2e_tests folder. The tests
folder allows us to have shared helper functions for all tests in a
separate package (tests) that is not included in the non-test build.

This also adds one second of delay before each end-to-end test case by
using the TestSetup method of suite. By slowing down test execution,
this gives Nomad time to create new allocations when a test requested a
runner. Another solution could be to increase the scale of the job to
have enough allocations for all end-to-end tests.

Co-authored-by: Maximilian Paß <maximilian.pass@student.hpi.uni-potsdam.de>
2021-05-31 12:32:51 +02:00

68 lines
2.3 KiB
Go

package e2e
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests/e2e/helpers"
"io"
"net/http"
"strings"
"testing"
)
func (suite *E2ETestSuite) TestProvideRunnerRoute() {
runnerRequestString, _ := json.Marshal(dto.RunnerRequest{})
reader := strings.NewReader(string(runnerRequestString))
resp, err := http.Post(helpers.BuildURL(api.RouteBase, api.RouteRunners), "application/json", reader)
suite.NoError(err)
suite.Equal(http.StatusOK, resp.StatusCode, "The response code should be ok")
runnerResponse := new(dto.RunnerResponse)
err = json.NewDecoder(resp.Body).Decode(runnerResponse)
suite.NoError(err)
suite.True(runnerResponse.Id != "", "The response contains a runner id")
}
func newRunnerId(t *testing.T) string {
runnerRequestString, _ := json.Marshal(dto.RunnerRequest{})
reader := strings.NewReader(string(runnerRequestString))
resp, err := http.Post(helpers.BuildURL(api.RouteBase, api.RouteRunners), "application/json", reader)
assert.NoError(t, err)
runnerResponse := new(dto.RunnerResponse)
_ = json.NewDecoder(resp.Body).Decode(runnerResponse)
return runnerResponse.Id
}
func (suite *E2ETestSuite) TestDeleteRunnerRoute() {
runnerId := newRunnerId(suite.T())
suite.NotEqual("", runnerId)
suite.Run("Deleting the runner returns NoContent", func() {
resp, err := httpDelete(helpers.BuildURL(api.RouteBase, api.RouteRunners, "/", runnerId), nil)
suite.NoError(err)
suite.Equal(http.StatusNoContent, resp.StatusCode)
})
suite.Run("Deleting it again returns NotFound", func() {
resp, err := httpDelete(helpers.BuildURL(api.RouteBase, api.RouteRunners, "/", runnerId), nil)
suite.NoError(err)
suite.Equal(http.StatusNotFound, resp.StatusCode)
})
suite.Run("Deleting non-existing runner returns NotFound", func() {
resp, err := httpDelete(helpers.BuildURL(api.RouteBase, api.RouteRunners, "/", "n0n-3x1st1ng-1d"), nil)
suite.NoError(err)
suite.Equal(http.StatusNotFound, resp.StatusCode)
})
}
// HttpDelete sends a Delete Http Request with body to the passed url.
func httpDelete(url string, body io.Reader) (response *http.Response, err error) {
req, _ := http.NewRequest(http.MethodDelete, url, body)
client := &http.Client{}
return client.Do(req)
}