Parametrize e2e tests to also check AWS environments.

- Fix destroy runner after timeout.
- Add file deletion
This commit is contained in:
Maximilian Paß
2022-02-03 14:32:05 +01:00
parent 13eaa61f3b
commit 4ffbb712ed
13 changed files with 505 additions and 342 deletions

View File

@@ -16,39 +16,41 @@ import (
)
func (s *E2ETestSuite) TestProvideRunnerRoute() {
runnerRequestByteString, err := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.Require().NoError(err)
reader := bytes.NewReader(runnerRequestByteString)
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)
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.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)
runnerResponse := new(dto.RunnerResponse)
err = json.NewDecoder(resp.Body).Decode(runnerResponse)
s.Require().NoError(err)
s.NotEmpty(runnerResponse.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("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, err := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentID: tests.NonExistingIntegerID,
s.Run("requesting runner of unknown execution environment returns not found", func() {
runnerRequestByteString, err := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentID: tests.NonExistingIntegerID,
})
s.Require().NoError(err)
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)
})
})
s.Require().NoError(err)
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.
@@ -77,117 +79,143 @@ func ProvideRunner(request *dto.RunnerRequest) (string, error) {
}
func (s *E2ETestSuite) TestDeleteRunnerRoute() {
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.NoError(err)
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() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID), nil)
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
})
s.Run("Deleting the runner returns NoContent", func() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID), nil)
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
})
s.Run("Deleting it again returns NotFound", func() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID), nil)
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
s.Run("Deleting it again returns NotFound", func() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID), nil)
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
s.Run("Deleting non-existing runner returns NotFound", func() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingStringID), nil)
s.NoError(err)
s.Equal(http.StatusNotFound, resp.StatusCode)
})
s.Run("Deleting non-existing runner returns NotFound", func() {
resp, err := helpers.HTTPDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, tests.NonExistingStringID), nil)
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,
})
s.NoError(err)
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Copy: []dto.File{{Path: tests.DefaultFileName, Content: []byte(tests.DefaultFileContent)}},
})
s.Require().NoError(err)
sendCopyRequest := func(reader io.Reader) (*http.Response, error) {
return helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
"application/json", reader)
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)}},
})
s.Require().NoError(err)
sendCopyRequest := func(reader io.Reader) (*http.Response, error) {
return helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
"application/json", reader)
}
s.Run("File copy with valid payload succeeds", func() {
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content can be printed on runner", func() {
s.assertFileContent(runnerID, tests.DefaultFileName, tests.DefaultFileContent)
})
})
s.Run("Files are put in correct location", func() {
relativeFilePath := "relative/file/path.txt"
relativeFileContent := "Relative file content"
absoluteFilePath := "/tmp/absolute/file/path.txt"
absoluteFileContent := "Absolute file content"
testFilePathsCopyRequestString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Copy: []dto.File{
{Path: dto.FilePath(relativeFilePath), Content: []byte(relativeFileContent)},
{Path: dto.FilePath(absoluteFilePath), Content: []byte(absoluteFileContent)},
},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(testFilePathsCopyRequestString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content of file with relative path can be printed on runner", func() {
// the print command is executed in the context of the default working directory of the container
s.assertFileContent(runnerID, relativeFilePath, relativeFileContent)
})
s.Run("File content of file with absolute path can be printed on runner", func() {
s.assertFileContent(runnerID, absoluteFilePath, absoluteFileContent)
})
})
s.Run("File deletion request deletes file on runner", func() {
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Delete: []dto.FilePath{tests.DefaultFileName},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content can no longer be printed", func() {
stdout, stderr := s.PrintContentOfFileOnRunner(runnerID, tests.DefaultFileName)
s.Equal("", stdout)
s.Contains(stderr, "No such file or directory")
})
})
s.Run("File copy happens after file deletion", func() {
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Delete: []dto.FilePath{tests.DefaultFileName},
Copy: []dto.File{{Path: tests.DefaultFileName, Content: []byte(tests.DefaultFileContent)}},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
_ = resp.Body.Close()
s.Run("File content can be printed on runner", func() {
s.assertFileContent(runnerID, tests.DefaultFileName, tests.DefaultFileContent)
})
})
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(""))
s.NoError(err)
s.Equal(http.StatusBadRequest, resp.StatusCode)
})
s.Run("Copying to non-existing runner returns NotFound", func() {
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)
})
})
}
}
s.Run("File copy with valid payload succeeds", func() {
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
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)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content can be printed on runner", func() {
s.assertFileContent(runnerID, tests.DefaultFileName, tests.DefaultFileContent)
})
})
s.Run("Files are put in correct location", func() {
relativeFilePath := "relative/file/path.txt"
relativeFileContent := "Relative file content"
absoluteFilePath := "/tmp/absolute/file/path.txt"
absoluteFileContent := "Absolute file content"
testFilePathsCopyRequestString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Copy: []dto.File{
{Path: dto.FilePath(relativeFilePath), Content: []byte(relativeFileContent)},
{Path: dto.FilePath(absoluteFilePath), Content: []byte(absoluteFileContent)},
},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(testFilePathsCopyRequestString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content of file with relative path can be printed on runner", func() {
// the print command is executed in the context of the default working directory of the container
s.assertFileContent(runnerID, relativeFilePath, relativeFileContent)
})
s.Run("File content of file with absolute path can be printed on runner", func() {
s.assertFileContent(runnerID, absoluteFilePath, absoluteFileContent)
})
})
s.Run("File deletion request deletes file on runner", func() {
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Delete: []dto.FilePath{tests.DefaultFileName},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
s.Run("File content can no longer be printed", func() {
stdout, stderr := s.PrintContentOfFileOnRunner(runnerID, tests.DefaultFileName)
s.Equal("", stdout)
s.Contains(stderr, "No such file or directory")
})
})
s.Run("File copy happens after file deletion", func() {
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Delete: []dto.FilePath{tests.DefaultFileName},
Copy: []dto.File{{Path: tests.DefaultFileName, Content: []byte(tests.DefaultFileContent)}},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)
_ = resp.Body.Close()
s.Run("File content can be printed on runner", func() {
s.assertFileContent(runnerID, tests.DefaultFileName, tests.DefaultFileContent)
})
})
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{
@@ -197,7 +225,8 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
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)
@@ -211,49 +240,70 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
})
})
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(""))
s.NoError(err)
s.Equal(http.StatusBadRequest, resp.StatusCode)
})
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)
s.Run("Copying to non-existing runner returns NotFound", func() {
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)
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
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
InactivityTimeout: inactivityTimeout,
})
s.Require().NoError(err)
for _, environmentID := range environmentIDs {
s.Run(environmentID.ToString(), func() {
inactivityTimeout := 2 // seconds
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentID: int(environmentID),
InactivityTimeout: inactivityTimeout,
})
s.Require().NoError(err)
executionTerminated := make(chan bool)
var lastMessage *dto.WebSocketMessage
go func() {
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID, &dto.ExecutionRequest{Command: "sleep infinity"})
s.Require().NoError(err)
connection, err := ConnectToWebSocket(webSocketURL)
s.Require().NoError(err)
executionTerminated := make(chan bool)
var lastMessage *dto.WebSocketMessage
go func() {
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID, &dto.ExecutionRequest{Command: "sleep infinity"})
s.Require().NoError(err)
connection, err := ConnectToWebSocket(webSocketURL)
s.Require().NoError(err)
messages, err := helpers.ReceiveAllWebSocketMessages(connection)
if !s.Equal(&websocket.CloseError{Code: websocket.CloseNormalClosure}, err) {
s.Fail("websocket abnormal closure")
}
controlMessages := helpers.WebSocketControlMessages(messages)
s.Require().NotEmpty(controlMessages)
lastMessage = controlMessages[len(controlMessages)-1]
executionTerminated <- true
}()
s.Require().True(tests.ChannelReceivesSomething(executionTerminated, time.Duration(inactivityTimeout+5)*time.Second))
s.Equal(dto.WebSocketMetaTimeout, lastMessage.Type)
messages, err := helpers.ReceiveAllWebSocketMessages(connection)
if !s.Equal(&websocket.CloseError{Code: websocket.CloseNormalClosure}, err) {
s.Fail("websocket abnormal closure")
}
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)