Parametrize e2e tests to also check AWS environments.
- Fix destroy runner after timeout. - Add file deletion
This commit is contained in:
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -106,6 +106,10 @@ jobs:
|
||||
e2e-test:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [ compile, dep-scan, test ]
|
||||
env:
|
||||
POSEIDON_AWS_ENABLED: true
|
||||
POSEIDON_AWS_ENDPOINT: ${{ secrets.POSEIDON_AWS_ENDPOINT }}
|
||||
POSEIDON_AWS_FUNCTIONS: ${{ secrets.POSEIDON_AWS_FUNCTIONS }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
|
@ -10,10 +10,11 @@ import (
|
||||
type AWSEnvironment struct {
|
||||
id dto.EnvironmentID
|
||||
awsEndpoint string
|
||||
onDestroyRunner runner.DestroyRunnerHandler
|
||||
}
|
||||
|
||||
func NewAWSEnvironment() *AWSEnvironment {
|
||||
return &AWSEnvironment{}
|
||||
func NewAWSEnvironment(onDestroyRunner runner.DestroyRunnerHandler) *AWSEnvironment {
|
||||
return &AWSEnvironment{onDestroyRunner: onDestroyRunner}
|
||||
}
|
||||
|
||||
func (a *AWSEnvironment) MarshalJSON() ([]byte, error) {
|
||||
@ -86,11 +87,11 @@ func (a *AWSEnvironment) Register() error {
|
||||
}
|
||||
|
||||
func (a *AWSEnvironment) Delete() error {
|
||||
panic("implement me")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *AWSEnvironment) Sample() (r runner.Runner, ok bool) {
|
||||
workload, err := runner.NewAWSFunctionWorkload(a, nil)
|
||||
workload, err := runner.NewAWSFunctionWorkload(a, a.onDestroyRunner)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func (a *AWSEnvironmentManager) CreateOrUpdate(
|
||||
}
|
||||
|
||||
_, ok := a.runnerManager.GetEnvironment(id)
|
||||
e := NewAWSEnvironment()
|
||||
e := NewAWSEnvironment(a.runnerManager.Return)
|
||||
e.SetID(id)
|
||||
e.SetImage(request.Image)
|
||||
a.runnerManager.StoreEnvironment(e)
|
||||
|
@ -66,7 +66,7 @@ func TestAWSEnvironmentManager_Get(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Returns environment when it was added before", func(t *testing.T) {
|
||||
expectedEnvironment := NewAWSEnvironment()
|
||||
expectedEnvironment := NewAWSEnvironment(nil)
|
||||
expectedEnvironment.SetID(tests.DefaultEnvironmentIDAsInteger)
|
||||
runnerManager.StoreEnvironment(expectedEnvironment)
|
||||
|
||||
@ -82,7 +82,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
|
||||
|
||||
t.Run("returs also environments of the rest of the manager chain", func(t *testing.T) {
|
||||
nextHandler := &ManagerHandlerMock{}
|
||||
existingEnvironment := NewAWSEnvironment()
|
||||
existingEnvironment := NewAWSEnvironment(nil)
|
||||
nextHandler.On("List", mock.AnythingOfType("bool")).
|
||||
Return([]runner.ExecutionEnvironment{existingEnvironment}, nil)
|
||||
m.SetNextHandler(nextHandler)
|
||||
@ -95,7 +95,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) {
|
||||
m.SetNextHandler(nil)
|
||||
|
||||
t.Run("Returns added environment", func(t *testing.T) {
|
||||
localEnvironment := NewAWSEnvironment()
|
||||
localEnvironment := NewAWSEnvironment(nil)
|
||||
localEnvironment.SetID(tests.DefaultEnvironmentIDAsInteger)
|
||||
runnerManager.StoreEnvironment(localEnvironment)
|
||||
|
||||
|
@ -27,13 +27,14 @@ type AWSFunctionWorkload struct {
|
||||
id string
|
||||
fs map[dto.FilePath][]byte
|
||||
executions execution.Storer
|
||||
onDestroy destroyRunnerHandler
|
||||
runningExecutions map[execution.ID]context.CancelFunc
|
||||
onDestroy DestroyRunnerHandler
|
||||
environment ExecutionEnvironment
|
||||
}
|
||||
|
||||
// NewAWSFunctionWorkload creates a new AWSFunctionWorkload with the provided id.
|
||||
func NewAWSFunctionWorkload(
|
||||
environment ExecutionEnvironment, onDestroy destroyRunnerHandler) (*AWSFunctionWorkload, error) {
|
||||
environment ExecutionEnvironment, onDestroy DestroyRunnerHandler) (*AWSFunctionWorkload, error) {
|
||||
newUUID, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed generating runner id: %w", err)
|
||||
@ -43,10 +44,13 @@ func NewAWSFunctionWorkload(
|
||||
id: newUUID.String(),
|
||||
fs: make(map[dto.FilePath][]byte),
|
||||
executions: execution.NewLocalStorage(),
|
||||
runningExecutions: make(map[execution.ID]context.CancelFunc),
|
||||
onDestroy: onDestroy,
|
||||
environment: environment,
|
||||
}
|
||||
workload.InactivityTimer = NewInactivityTimer(workload, onDestroy)
|
||||
workload.InactivityTimer = NewInactivityTimer(workload, func(_ Runner) error {
|
||||
return workload.Destroy()
|
||||
})
|
||||
return workload, nil
|
||||
}
|
||||
|
||||
@ -73,16 +77,21 @@ func (w *AWSFunctionWorkload) ExecuteInteractively(id string, _ io.ReadWriter, s
|
||||
if !ok {
|
||||
return nil, nil, ErrorUnknownExecution
|
||||
}
|
||||
|
||||
command, ctx, cancel := prepareExecution(request)
|
||||
exitInternal := make(chan ExitInfo)
|
||||
exit := make(chan ExitInfo, 1)
|
||||
go w.executeCommand(ctx, command, stdout, stderr, exit)
|
||||
|
||||
go w.executeCommand(ctx, command, stdout, stderr, exitInternal)
|
||||
go w.handleRunnerTimeout(ctx, exitInternal, exit, execution.ID(id))
|
||||
|
||||
return exit, cancel, nil
|
||||
}
|
||||
|
||||
// UpdateFileSystem copies Files into the executor.
|
||||
// ToDo: Currently, file deletion is not supported (but it could be).
|
||||
func (w *AWSFunctionWorkload) UpdateFileSystem(request *dto.UpdateFileSystemRequest) error {
|
||||
for _, path := range request.Delete {
|
||||
delete(w.fs, path)
|
||||
}
|
||||
for _, file := range request.Copy {
|
||||
w.fs[file.Path] = file.Content
|
||||
}
|
||||
@ -90,6 +99,9 @@ func (w *AWSFunctionWorkload) UpdateFileSystem(request *dto.UpdateFileSystemRequ
|
||||
}
|
||||
|
||||
func (w *AWSFunctionWorkload) Destroy() error {
|
||||
for _, cancel := range w.runningExecutions {
|
||||
cancel()
|
||||
}
|
||||
if err := w.onDestroy(w); err != nil {
|
||||
return fmt.Errorf("error while destroying aws runner: %w", err)
|
||||
}
|
||||
@ -99,6 +111,7 @@ func (w *AWSFunctionWorkload) Destroy() error {
|
||||
func (w *AWSFunctionWorkload) executeCommand(ctx context.Context, command []string,
|
||||
stdout, stderr io.Writer, exit chan<- ExitInfo,
|
||||
) {
|
||||
defer close(exit)
|
||||
data := &awsFunctionRequest{
|
||||
Action: w.environment.Image(),
|
||||
Cmd: command,
|
||||
@ -128,7 +141,6 @@ func (w *AWSFunctionWorkload) executeCommand(ctx context.Context, command []stri
|
||||
err = ErrorRunnerInactivityTimeout
|
||||
}
|
||||
exit <- ExitInfo{exitCode, err}
|
||||
close(exit)
|
||||
}
|
||||
|
||||
func (w *AWSFunctionWorkload) receiveOutput(
|
||||
@ -157,7 +169,7 @@ func (w *AWSFunctionWorkload) receiveOutput(
|
||||
case dto.WebSocketOutputStdout:
|
||||
// We do not check the written bytes as the rawToCodeOceanWriter receives everything or nothing.
|
||||
_, err = stdout.Write([]byte(wsMessage.Data))
|
||||
case dto.WebSocketOutputStderr:
|
||||
case dto.WebSocketOutputStderr, dto.WebSocketOutputError:
|
||||
_, err = stderr.Write([]byte(wsMessage.Data))
|
||||
}
|
||||
if err != nil {
|
||||
@ -166,3 +178,20 @@ func (w *AWSFunctionWorkload) receiveOutput(
|
||||
}
|
||||
return 1, fmt.Errorf("receiveOutput stpped by context: %w", ctx.Err())
|
||||
}
|
||||
|
||||
// handleRunnerTimeout listens for a runner timeout and aborts the execution in that case.
|
||||
// It listens via a context in runningExecutions that is canceled on the timeout event.
|
||||
func (w *AWSFunctionWorkload) handleRunnerTimeout(ctx context.Context,
|
||||
exitInternal <-chan ExitInfo, exit chan<- ExitInfo, id execution.ID) {
|
||||
executionCtx, cancelExecution := context.WithCancel(ctx)
|
||||
w.runningExecutions[id] = cancelExecution
|
||||
defer delete(w.runningExecutions, id)
|
||||
defer close(exit)
|
||||
|
||||
select {
|
||||
case exitInfo := <-exitInternal:
|
||||
exit <- exitInfo
|
||||
case <-executionCtx.Done():
|
||||
exit <- ExitInfo{255, ErrorRunnerInactivityTimeout}
|
||||
}
|
||||
}
|
||||
|
@ -37,11 +37,11 @@ type InactivityTimerImplementation struct {
|
||||
duration time.Duration
|
||||
state TimerState
|
||||
runner Runner
|
||||
onDestroy destroyRunnerHandler
|
||||
onDestroy DestroyRunnerHandler
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewInactivityTimer(runner Runner, onDestroy destroyRunnerHandler) InactivityTimer {
|
||||
func NewInactivityTimer(runner Runner, onDestroy DestroyRunnerHandler) InactivityTimer {
|
||||
return &InactivityTimerImplementation{
|
||||
state: TimerInactive,
|
||||
runner: runner,
|
||||
|
@ -41,12 +41,12 @@ type NomadJob struct {
|
||||
id string
|
||||
portMappings []nomadApi.PortMapping
|
||||
api nomad.ExecutorAPI
|
||||
onDestroy destroyRunnerHandler
|
||||
onDestroy DestroyRunnerHandler
|
||||
}
|
||||
|
||||
// NewNomadJob creates a new NomadJob with the provided id.
|
||||
func NewNomadJob(id string, portMappings []nomadApi.PortMapping,
|
||||
apiClient nomad.ExecutorAPI, onDestroy destroyRunnerHandler,
|
||||
apiClient nomad.ExecutorAPI, onDestroy DestroyRunnerHandler,
|
||||
) *NomadJob {
|
||||
job := &NomadJob{
|
||||
id: id,
|
||||
|
@ -391,7 +391,7 @@ func (s *UpdateFileSystemTestSuite) readFilesFromTarArchive(tarArchive io.Reader
|
||||
|
||||
// NewRunner creates a new runner with the provided id and manager.
|
||||
func NewRunner(id string, manager Accessor) Runner {
|
||||
var handler destroyRunnerHandler
|
||||
var handler DestroyRunnerHandler
|
||||
if manager != nil {
|
||||
handler = manager.Return
|
||||
} else {
|
||||
|
@ -11,7 +11,7 @@ type ExitInfo struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
type destroyRunnerHandler = func(r Runner) error
|
||||
type DestroyRunnerHandler = func(r Runner) error
|
||||
|
||||
type Runner interface {
|
||||
InactivityTimer
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -27,6 +28,7 @@ var (
|
||||
testDockerImage = flag.String("dockerImage", "", "Docker image to use in E2E tests")
|
||||
nomadClient *nomadApi.Client
|
||||
nomadNamespace string
|
||||
environmentIDs []dto.EnvironmentID
|
||||
)
|
||||
|
||||
type E2ETestSuite struct {
|
||||
@ -45,11 +47,42 @@ func TestE2ETestSuite(t *testing.T) {
|
||||
// Overwrite TestMain for custom setup.
|
||||
func TestMain(m *testing.M) {
|
||||
log.Info("Test Setup")
|
||||
err := config.InitConfig()
|
||||
if err != nil {
|
||||
if err := config.InitConfig(); err != nil {
|
||||
log.WithError(err).Fatal("Could not initialize configuration")
|
||||
}
|
||||
initNomad()
|
||||
initAWS()
|
||||
|
||||
// wait for environment to become ready
|
||||
<-time.After(10 * time.Second)
|
||||
log.Info("Test Run")
|
||||
code := m.Run()
|
||||
|
||||
deleteE2EEnvironments()
|
||||
cleanupJobsForEnvironment(&testing.T{}, tests.DefaultEnvironmentIDAsString)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func initAWS() {
|
||||
for i, function := range strings.Fields(config.Config.AWS.Functions) {
|
||||
id := dto.EnvironmentID(tests.DefaultEnvironmentIDAsInteger + i + 1)
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, id.ToString())
|
||||
request := dto.ExecutionEnvironmentRequest{Image: function}
|
||||
resp, err := helpers.HTTPPutJSON(path, request)
|
||||
if err != nil || resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
|
||||
log.WithField("function", function).WithError(err).Fatal("Couldn't create default environment for e2e tests")
|
||||
}
|
||||
environmentIDs = append(environmentIDs, id)
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal("Failed closing body")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initNomad() {
|
||||
nomadNamespace = config.Config.Nomad.Namespace
|
||||
var err error
|
||||
nomadClient, err = nomadApi.NewClient(&nomadApi.Config{
|
||||
Address: config.Config.Nomad.URL().String(),
|
||||
TLSConfig: &nomadApi.TLSConfig{},
|
||||
@ -57,16 +90,9 @@ func TestMain(m *testing.M) {
|
||||
})
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Could not create Nomad client")
|
||||
return
|
||||
}
|
||||
log.Info("Test Run")
|
||||
createDefaultEnvironment()
|
||||
|
||||
// wait for environment to become ready
|
||||
<-time.After(10 * time.Second)
|
||||
|
||||
code := m.Run()
|
||||
cleanupJobsForEnvironment(&testing.T{}, tests.DefaultEnvironmentIDAsString)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func createDefaultEnvironment() {
|
||||
@ -89,8 +115,15 @@ func createDefaultEnvironment() {
|
||||
if err != nil || resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
|
||||
log.WithError(err).Fatal("Couldn't create default environment for e2e tests")
|
||||
}
|
||||
environmentIDs = append(environmentIDs, tests.DefaultEnvironmentIDAsInteger)
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal("Failed closing body")
|
||||
}
|
||||
}
|
||||
|
||||
func deleteE2EEnvironments() {
|
||||
for _, id := range environmentIDs {
|
||||
deleteEnvironment(&testing.T{}, id.ToString())
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,10 @@ package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
nomadApi "github.com/hashicorp/nomad/api"
|
||||
"github.com/openHPI/poseidon/internal/api"
|
||||
"github.com/openHPI/poseidon/internal/config"
|
||||
"github.com/openHPI/poseidon/internal/nomad"
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
@ -17,6 +19,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var isAWSEnvironment = []bool{false, true}
|
||||
|
||||
func TestCreateOrUpdateEnvironment(t *testing.T) {
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, tests.AnotherEnvironmentIDAsString)
|
||||
|
||||
@ -73,13 +77,13 @@ func TestCreateOrUpdateEnvironment(t *testing.T) {
|
||||
func TestListEnvironments(t *testing.T) {
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath)
|
||||
|
||||
t.Run("returns list with one element", func(t *testing.T) {
|
||||
t.Run("returns list with all static and the e2e environment", func(t *testing.T) {
|
||||
response, err := http.Get(path) //nolint:gosec // because we build this path right above
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusOK, response.StatusCode)
|
||||
environmentsArray := assertEnvironmentArrayInResponse(t, response)
|
||||
assert.Equal(t, 1, len(environmentsArray))
|
||||
assert.Equal(t, len(environmentIDs), len(environmentsArray))
|
||||
})
|
||||
|
||||
t.Run("returns list including the default environment", func(t *testing.T) {
|
||||
@ -88,24 +92,28 @@ func TestListEnvironments(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, response.StatusCode)
|
||||
|
||||
environmentsArray := assertEnvironmentArrayInResponse(t, response)
|
||||
require.Equal(t, 1, len(environmentsArray))
|
||||
|
||||
assertEnvironment(t, environmentsArray[0], tests.DefaultEnvironmentIDAsInteger)
|
||||
require.Equal(t, len(environmentIDs), len(environmentsArray))
|
||||
foundIDs := parseIDsFromEnvironments(t, environmentsArray)
|
||||
assert.Contains(t, foundIDs, dto.EnvironmentID(tests.DefaultEnvironmentIDAsInteger))
|
||||
})
|
||||
|
||||
for _, useAWS := range isAWSEnvironment {
|
||||
t.Run(fmt.Sprintf("AWS-%t", useAWS), func(t *testing.T) {
|
||||
t.Run("Added environments can be retrieved without fetch", func(t *testing.T) {
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString, useAWS)
|
||||
|
||||
response, err := http.Get(path) //nolint:gosec // because we build this path right above
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, response.StatusCode)
|
||||
|
||||
environmentsArray := assertEnvironmentArrayInResponse(t, response)
|
||||
require.Equal(t, 2, len(environmentsArray))
|
||||
require.Equal(t, len(environmentIDs)+1, len(environmentsArray))
|
||||
foundIDs := parseIDsFromEnvironments(t, environmentsArray)
|
||||
assert.Contains(t, foundIDs, dto.EnvironmentID(tests.AnotherEnvironmentIDAsInteger))
|
||||
})
|
||||
deleteEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("Added environments can be retrieved with fetch", func(t *testing.T) {
|
||||
// Add environment without Poseidon
|
||||
@ -122,16 +130,17 @@ func TestListEnvironments(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, response.StatusCode)
|
||||
environmentsArray := assertEnvironmentArrayInResponse(t, response)
|
||||
require.Equal(t, 1, len(environmentsArray))
|
||||
assertEnvironment(t, environmentsArray[0], tests.DefaultEnvironmentIDAsInteger)
|
||||
require.Equal(t, len(environmentIDs), len(environmentsArray))
|
||||
foundIDs := parseIDsFromEnvironments(t, environmentsArray)
|
||||
assert.Contains(t, foundIDs, dto.EnvironmentID(tests.DefaultEnvironmentIDAsInteger))
|
||||
|
||||
// List with fetch should include the added environment
|
||||
response, err = http.Get(path + "?fetch=true") //nolint:gosec // because we build this path right above
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, response.StatusCode)
|
||||
environmentsArray = assertEnvironmentArrayInResponse(t, response)
|
||||
require.Equal(t, 2, len(environmentsArray))
|
||||
foundIDs := parseIDsFromEnvironments(t, environmentsArray)
|
||||
require.Equal(t, len(environmentIDs)+1, len(environmentsArray))
|
||||
foundIDs = parseIDsFromEnvironments(t, environmentsArray)
|
||||
assert.Contains(t, foundIDs, dto.EnvironmentID(tests.AnotherEnvironmentIDAsInteger))
|
||||
})
|
||||
deleteEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
@ -148,8 +157,10 @@ func TestGetEnvironment(t *testing.T) {
|
||||
assertEnvironment(t, environment, tests.DefaultEnvironmentIDAsInteger)
|
||||
})
|
||||
|
||||
for _, useAWS := range isAWSEnvironment {
|
||||
t.Run(fmt.Sprintf("AWS-%t", useAWS), func(t *testing.T) {
|
||||
t.Run("Added environments can be retrieved without fetch", func(t *testing.T) {
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString, useAWS)
|
||||
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, tests.AnotherEnvironmentIDAsString)
|
||||
response, err := http.Get(path) //nolint:gosec // because we build this path right above
|
||||
@ -160,6 +171,8 @@ func TestGetEnvironment(t *testing.T) {
|
||||
assertEnvironment(t, environment, tests.AnotherEnvironmentIDAsInteger)
|
||||
})
|
||||
deleteEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("Added environments can be retrieved with fetch", func(t *testing.T) {
|
||||
// Add environment without Poseidon
|
||||
@ -188,17 +201,21 @@ func TestGetEnvironment(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteEnvironment(t *testing.T) {
|
||||
for _, useAWS := range isAWSEnvironment {
|
||||
t.Run(fmt.Sprintf("AWS-%t", useAWS), func(t *testing.T) {
|
||||
t.Run("Removes added environment", func(t *testing.T) {
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString, useAWS)
|
||||
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, tests.AnotherEnvironmentIDAsString)
|
||||
response, err := helpers.HTTPDelete(path, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusNoContent, response.StatusCode)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("Removes Nomad Job", func(t *testing.T) {
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString)
|
||||
createEnvironment(t, tests.AnotherEnvironmentIDAsString, false)
|
||||
|
||||
// Expect created Nomad job
|
||||
jobID := nomad.TemplateJobID(tests.AnotherEnvironmentIDAsInteger)
|
||||
@ -295,17 +312,23 @@ func cleanupJobsForEnvironment(t *testing.T, environmentID string) {
|
||||
}
|
||||
|
||||
//nolint:unparam // Because its more clear if the environment id is written in the real test
|
||||
func createEnvironment(t *testing.T, environmentID string) {
|
||||
func createEnvironment(t *testing.T, environmentID string, aws bool) {
|
||||
t.Helper()
|
||||
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, environmentID)
|
||||
request := dto.ExecutionEnvironmentRequest{
|
||||
PrewarmingPoolSize: 1,
|
||||
CPULimit: 100,
|
||||
MemoryLimit: 100,
|
||||
Image: *testDockerImage,
|
||||
NetworkAccess: false,
|
||||
ExposedPorts: nil,
|
||||
}
|
||||
if aws {
|
||||
functions := strings.Fields(config.Config.AWS.Functions)
|
||||
require.NotZero(t, len(functions))
|
||||
request.Image = functions[0]
|
||||
} else {
|
||||
request.Image = *testDockerImage
|
||||
}
|
||||
assertPutReturnsStatusAndZeroContent(t, path, request, http.StatusCreated)
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,9 @@ import (
|
||||
)
|
||||
|
||||
func (s *E2ETestSuite) TestProvideRunnerRoute() {
|
||||
runnerRequestByteString, err := json.Marshal(dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
runnerRequestByteString, err := json.Marshal(dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
s.Require().NoError(err)
|
||||
reader := bytes.NewReader(runnerRequestByteString)
|
||||
|
||||
@ -49,6 +49,8 @@ func (s *E2ETestSuite) TestProvideRunnerRoute() {
|
||||
s.Require().NoError(err)
|
||||
s.Equal(http.StatusNotFound, resp.StatusCode)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ProvideRunner creates a runner with the given RunnerRequest via an external request.
|
||||
@ -77,9 +79,9 @@ func ProvideRunner(request *dto.RunnerRequest) (string, error) {
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestDeleteRunnerRoute() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
s.NoError(err)
|
||||
|
||||
s.Run("Deleting the runner returns NoContent", func() {
|
||||
@ -99,13 +101,15 @@ func (s *E2ETestSuite) TestDeleteRunnerRoute() {
|
||||
s.NoError(err)
|
||||
s.Equal(http.StatusNotFound, resp.StatusCode)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:funlen // there are a lot of tests for the files route, this function can be a little longer than 100 lines ;)
|
||||
func (s *E2ETestSuite) TestCopyFilesRoute() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
s.NoError(err)
|
||||
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
|
||||
Copy: []dto.File{{Path: tests.DefaultFileName, Content: []byte(tests.DefaultFileContent)}},
|
||||
@ -187,30 +191,6 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
|
||||
})
|
||||
})
|
||||
|
||||
s.Run("If one file produces permission denied error, others are still copied", func() {
|
||||
newFileContent := []byte("New content")
|
||||
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
|
||||
Copy: []dto.File{
|
||||
{Path: "/dev/sda", Content: []byte(tests.DefaultFileContent)},
|
||||
{Path: tests.DefaultFileName, Content: newFileContent},
|
||||
},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
|
||||
s.NoError(err)
|
||||
s.Equal(http.StatusInternalServerError, resp.StatusCode)
|
||||
internalServerError := new(dto.InternalServerError)
|
||||
err = json.NewDecoder(resp.Body).Decode(internalServerError)
|
||||
s.NoError(err)
|
||||
s.Contains(internalServerError.Message, "Cannot open: Permission denied")
|
||||
_ = resp.Body.Close()
|
||||
|
||||
s.Run("File content can be printed on runner", func() {
|
||||
s.assertFileContent(runnerID, tests.DefaultFileName, string(newFileContent))
|
||||
})
|
||||
})
|
||||
|
||||
s.Run("File copy with invalid payload returns bad request", func() {
|
||||
resp, err := helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
|
||||
"text/html", strings.NewReader(""))
|
||||
@ -225,12 +205,79 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
|
||||
s.NoError(err)
|
||||
s.Equal(http.StatusNotFound, resp.StatusCode)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestCopyFilesRoute_PermissionDenied() {
|
||||
s.Run("Nomad/If one file produces permission denied error, others are still copied", func() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
s.NoError(err)
|
||||
|
||||
newFileContent := []byte("New content")
|
||||
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
|
||||
Copy: []dto.File{
|
||||
{Path: "/dev/sda", Content: []byte(tests.DefaultFileContent)},
|
||||
{Path: tests.DefaultFileName, Content: newFileContent},
|
||||
},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
resp, err := helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
|
||||
"application/json", bytes.NewReader(copyFilesRequestByteString))
|
||||
s.NoError(err)
|
||||
s.Equal(http.StatusInternalServerError, resp.StatusCode)
|
||||
internalServerError := new(dto.InternalServerError)
|
||||
err = json.NewDecoder(resp.Body).Decode(internalServerError)
|
||||
s.NoError(err)
|
||||
s.Contains(internalServerError.Message, "Cannot open: Permission denied")
|
||||
_ = resp.Body.Close()
|
||||
|
||||
s.Run("File content can be printed on runner", func() {
|
||||
s.assertFileContent(runnerID, tests.DefaultFileName, string(newFileContent))
|
||||
})
|
||||
})
|
||||
|
||||
s.Run("AWS/If one file produces permission denied error, others are still copied", func() {
|
||||
for _, environmentID := range environmentIDs {
|
||||
if environmentID == tests.DefaultEnvironmentIDAsInteger {
|
||||
continue
|
||||
}
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
s.NoError(err)
|
||||
|
||||
newFileContent := []byte("New content")
|
||||
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
|
||||
Copy: []dto.File{
|
||||
{Path: "/dev/sda", Content: []byte(tests.DefaultFileContent)},
|
||||
{Path: tests.DefaultFileName, Content: newFileContent},
|
||||
},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
resp, err := helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
|
||||
"application/json", bytes.NewReader(copyFilesRequestByteString))
|
||||
s.NoError(err)
|
||||
s.Equal(http.StatusNoContent, resp.StatusCode)
|
||||
_ = resp.Body.Close()
|
||||
|
||||
stdout, stderr := s.PrintContentOfFileOnRunner(runnerID, tests.DefaultFileName)
|
||||
s.Equal(string(newFileContent), stdout)
|
||||
s.Contains(stderr, "Permission denied")
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestRunnerGetsDestroyedAfterInactivityTimeout() {
|
||||
inactivityTimeout := 5 // seconds
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
inactivityTimeout := 2 // seconds
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
ExecutionEnvironmentID: int(environmentID),
|
||||
InactivityTimeout: inactivityTimeout,
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
@ -250,10 +297,13 @@ func (s *E2ETestSuite) TestRunnerGetsDestroyedAfterInactivityTimeout() {
|
||||
controlMessages := helpers.WebSocketControlMessages(messages)
|
||||
s.Require().NotEmpty(controlMessages)
|
||||
lastMessage = controlMessages[len(controlMessages)-1]
|
||||
log.Warn("")
|
||||
executionTerminated <- true
|
||||
}()
|
||||
s.Require().True(tests.ChannelReceivesSomething(executionTerminated, time.Duration(inactivityTimeout+5)*time.Second))
|
||||
s.Equal(dto.WebSocketMetaTimeout, lastMessage.Type)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) assertFileContent(runnerID, fileName, expectedContent string) {
|
||||
@ -263,8 +313,10 @@ func (s *E2ETestSuite) assertFileContent(runnerID, fileName, expectedContent str
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) PrintContentOfFileOnRunner(runnerID, filename string) (stdout, stderr string) {
|
||||
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID,
|
||||
&dto.ExecutionRequest{Command: fmt.Sprintf("cat %s", filename)})
|
||||
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID, &dto.ExecutionRequest{
|
||||
Command: fmt.Sprintf("cat %s", filename),
|
||||
TimeLimit: int(tests.DefaultTestTimeout.Seconds()),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
connection, err := ConnectToWebSocket(webSocketURL)
|
||||
s.Require().NoError(err)
|
||||
|
@ -18,9 +18,9 @@ import (
|
||||
)
|
||||
|
||||
func (s *E2ETestSuite) TestExecuteCommandRoute() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
s.Require().NoError(err)
|
||||
|
||||
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID, &dto.ExecutionRequest{Command: "true"})
|
||||
@ -53,10 +53,15 @@ func (s *E2ETestSuite) TestExecuteCommandRoute() {
|
||||
_, _, err = connection.ReadMessage()
|
||||
s.True(websocket.IsCloseError(err, websocket.CloseNormalClosure))
|
||||
s.True(connectionClosed, "connection should be closed")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestOutputToStdout() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, &dto.ExecutionRequest{Command: "echo Hello World"})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, environmentID,
|
||||
&dto.ExecutionRequest{Command: "echo -n Hello World"})
|
||||
s.Require().NoError(err)
|
||||
|
||||
messages, err := helpers.ReceiveAllWebSocketMessages(connection)
|
||||
@ -68,11 +73,16 @@ func (s *E2ETestSuite) TestOutputToStdout() {
|
||||
s.Require().Equal(&dto.WebSocketMessage{Type: dto.WebSocketExit}, controlMessages[1])
|
||||
|
||||
stdout, _, _ := helpers.WebSocketOutputMessages(messages)
|
||||
s.Require().Equal("Hello World\r\n", stdout)
|
||||
s.Require().Equal("Hello World", stdout)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestOutputToStderr() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, &dto.ExecutionRequest{Command: "cat -invalid"})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, environmentID,
|
||||
&dto.ExecutionRequest{Command: "cat -invalid"})
|
||||
s.Require().NoError(err)
|
||||
|
||||
messages, err := helpers.ReceiveAllWebSocketMessages(connection)
|
||||
@ -88,11 +98,15 @@ func (s *E2ETestSuite) TestOutputToStderr() {
|
||||
s.NotContains(stdout, "cat: invalid option", "Stdout should not contain the error")
|
||||
s.Contains(stderr, "cat: invalid option", "Stderr should contain the error")
|
||||
s.Empty(errors)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// AWS environments do not support stdin at this moment therefore they cannot take this test.
|
||||
func (s *E2ETestSuite) TestCommandHead() {
|
||||
hello := "Hello World!"
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, &dto.ExecutionRequest{Command: "head -n 1"})
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, tests.DefaultEnvironmentIDAsInteger,
|
||||
&dto.ExecutionRequest{Command: "head -n 1"})
|
||||
s.Require().NoError(err)
|
||||
|
||||
startMessage, err := helpers.ReceiveNextWebSocketMessage(connection)
|
||||
@ -110,7 +124,10 @@ func (s *E2ETestSuite) TestCommandHead() {
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestCommandReturnsAfterTimeout() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, &dto.ExecutionRequest{Command: "sleep 4", TimeLimit: 1})
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, environmentID,
|
||||
&dto.ExecutionRequest{Command: "sleep 4", TimeLimit: 1})
|
||||
s.Require().NoError(err)
|
||||
|
||||
c := make(chan bool)
|
||||
@ -132,11 +149,15 @@ func (s *E2ETestSuite) TestCommandReturnsAfterTimeout() {
|
||||
}
|
||||
}
|
||||
s.T().Fail()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestEchoEnvironment() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, &dto.ExecutionRequest{
|
||||
Command: "echo $hello",
|
||||
for _, environmentID := range environmentIDs {
|
||||
s.Run(environmentID.ToString(), func() {
|
||||
connection, err := ProvideWebSocketConnection(&s.Suite, environmentID, &dto.ExecutionRequest{
|
||||
Command: "echo -n $hello",
|
||||
Environment: map[string]string{"hello": "world"},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
@ -149,10 +170,12 @@ func (s *E2ETestSuite) TestEchoEnvironment() {
|
||||
s.Require().Error(err)
|
||||
s.Equal(err, &websocket.CloseError{Code: websocket.CloseNormalClosure})
|
||||
stdout, _, _ := helpers.WebSocketOutputMessages(messages)
|
||||
s.Equal("world\r\n", stdout)
|
||||
s.Equal("world", stdout)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestStderrFifoIsRemoved() {
|
||||
func (s *E2ETestSuite) TestNomadStderrFifoIsRemoved() {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
@ -191,11 +214,9 @@ func (s *E2ETestSuite) ListTempDirectory(runnerID string) string {
|
||||
}
|
||||
|
||||
// ProvideWebSocketConnection establishes a client WebSocket connection to run the passed ExecutionRequest.
|
||||
// It requires a running Poseidon instance.
|
||||
func ProvideWebSocketConnection(s *suite.Suite, request *dto.ExecutionRequest) (*websocket.Conn, error) {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{
|
||||
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
|
||||
})
|
||||
func ProvideWebSocketConnection(
|
||||
s *suite.Suite, environmentID dto.EnvironmentID, request *dto.ExecutionRequest) (*websocket.Conn, error) {
|
||||
runnerID, err := ProvideRunner(&dto.RunnerRequest{ExecutionEnvironmentID: int(environmentID)})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error providing runner: %w", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user