Fix a lot of linting issues

After we introduced the linter we haven't really touched the old code.
This commit now fixes all linting issue that exist right now.
This commit is contained in:
sirkrypt0
2021-07-08 22:41:21 +02:00
parent bd7fb53385
commit c7606f3d5f
37 changed files with 902 additions and 689 deletions

View File

@@ -16,9 +16,10 @@ import (
)
func (s *E2ETestSuite) TestProvideRunnerRoute() {
runnerRequestByteString, _ := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
runnerRequestByteString, err := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.Require().NoError(err)
reader := bytes.NewReader(runnerRequestByteString)
s.Run("valid request returns a runner", func() {
@@ -29,7 +30,7 @@ func (s *E2ETestSuite) TestProvideRunnerRoute() {
runnerResponse := new(dto.RunnerResponse)
err = json.NewDecoder(resp.Body).Decode(runnerResponse)
s.Require().NoError(err)
s.NotEmpty(runnerResponse.Id)
s.NotEmpty(runnerResponse.ID)
})
s.Run("invalid request returns bad request", func() {
@@ -39,9 +40,10 @@ func (s *E2ETestSuite) TestProvideRunnerRoute() {
})
s.Run("requesting runner of unknown execution environment returns not found", func() {
runnerRequestByteString, _ := json.Marshal(dto.RunnerRequest{
ExecutionEnvironmentId: tests.NonExistingIntegerID,
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)
@@ -53,13 +55,17 @@ func (s *E2ETestSuite) TestProvideRunnerRoute() {
// It needs a running Poseidon instance to work.
func ProvideRunner(request *dto.RunnerRequest) (string, error) {
url := helpers.BuildURL(api.BasePath, api.RunnersPath)
runnerRequestByteString, _ := json.Marshal(request)
runnerRequestByteString, err := json.Marshal(request)
if err != nil {
return "", err
}
reader := strings.NewReader(string(runnerRequestByteString))
resp, err := http.Post(url, "application/json", reader)
resp, err := http.Post(url, "application/json", reader) //nolint:gosec // url is not influenced by a user
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
//nolint:goerr113 // dynamic error is ok in here, as it is a test
return "", fmt.Errorf("expected response code 200 when getting runner, got %v", resp.StatusCode)
}
runnerResponse := new(dto.RunnerResponse)
@@ -67,44 +73,47 @@ func ProvideRunner(request *dto.RunnerRequest) (string, error) {
if err != nil {
return "", err
}
return runnerResponse.Id, nil
return runnerResponse.ID, nil
}
func (s *E2ETestSuite) TestDeleteRunnerRoute() {
runnerId, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.NoError(err)
s.Run("Deleting the runner returns NoContent", func() {
resp, err := helpers.HttpDelete(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerId), nil)
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)
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)
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,
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.NoError(err)
copyFilesRequestByteString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
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)
return helpers.HTTPPatch(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath),
"application/json", reader)
}
s.Run("File copy with valid payload succeeds", func() {
@@ -122,12 +131,13 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
relativeFileContent := "Relative file content"
absoluteFilePath := "/tmp/absolute/file/path.txt"
absoluteFileContent := "Absolute file content"
testFilePathsCopyRequestString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
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)
@@ -144,9 +154,10 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
})
s.Run("File deletion request deletes file on runner", func() {
copyFilesRequestByteString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
copyFilesRequestByteString, err := json.Marshal(&dto.UpdateFileSystemRequest{
Delete: []dto.FilePath{tests.DefaultFileName},
})
s.Require().NoError(err)
resp, err := sendCopyRequest(bytes.NewReader(copyFilesRequestByteString))
s.NoError(err)
@@ -160,10 +171,11 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
})
s.Run("File copy happens after file deletion", func() {
copyFilesRequestByteString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
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)
@@ -177,12 +189,13 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
s.Run("If one file produces permission denied error, others are still copied", func() {
newFileContent := []byte("New content")
copyFilesRequestByteString, _ := json.Marshal(&dto.UpdateFileSystemRequest{
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)
@@ -199,13 +212,16 @@ 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(""))
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))
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)
})
@@ -214,7 +230,7 @@ func (s *E2ETestSuite) TestCopyFilesRoute() {
func (s *E2ETestSuite) TestRunnerGetsDestroyedAfterInactivityTimeout() {
inactivityTimeout := 5 // seconds
runnerID, err := ProvideRunner(&dto.RunnerRequest{
ExecutionEnvironmentId: tests.DefaultEnvironmentIDAsInteger,
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
InactivityTimeout: inactivityTimeout,
})
s.Require().NoError(err)
@@ -240,20 +256,23 @@ func (s *E2ETestSuite) TestRunnerGetsDestroyedAfterInactivityTimeout() {
s.Equal(dto.WebSocketMetaTimeout, lastMessage.Type)
}
func (s *E2ETestSuite) assertFileContent(runnerID, fileName string, expectedContent string) {
func (s *E2ETestSuite) assertFileContent(runnerID, fileName, expectedContent string) {
stdout, stderr := s.PrintContentOfFileOnRunner(runnerID, fileName)
s.Equal(expectedContent, stdout)
s.Equal("", stderr)
}
func (s *E2ETestSuite) PrintContentOfFileOnRunner(runnerId string, filename string) (string, string) {
webSocketURL, _ := ProvideWebSocketURL(&s.Suite, runnerId, &dto.ExecutionRequest{Command: fmt.Sprintf("cat %s", filename)})
connection, _ := ConnectToWebSocket(webSocketURL)
func (s *E2ETestSuite) PrintContentOfFileOnRunner(runnerID, filename string) (stdout, stderr string) {
webSocketURL, err := ProvideWebSocketURL(&s.Suite, runnerID,
&dto.ExecutionRequest{Command: fmt.Sprintf("cat %s", filename)})
s.Require().NoError(err)
connection, err := ConnectToWebSocket(webSocketURL)
s.Require().NoError(err)
messages, err := helpers.ReceiveAllWebSocketMessages(connection)
s.Require().Error(err)
s.Equal(&websocket.CloseError{Code: websocket.CloseNormalClosure}, err)
stdout, stderr, _ := helpers.WebSocketOutputMessages(messages)
stdout, stderr, _ = helpers.WebSocketOutputMessages(messages)
return stdout, stderr
}