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:
@@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
nomadApi "github.com/hashicorp/nomad/api"
|
||||
@@ -22,15 +23,15 @@ import (
|
||||
)
|
||||
|
||||
// BuildURL joins multiple route paths.
|
||||
func BuildURL(parts ...string) (url string) {
|
||||
url = config.Config.PoseidonAPIURL().String()
|
||||
func BuildURL(parts ...string) string {
|
||||
url := config.Config.PoseidonAPIURL().String()
|
||||
for _, part := range parts {
|
||||
if !strings.HasPrefix(part, "/") {
|
||||
url += "/"
|
||||
}
|
||||
url += part
|
||||
}
|
||||
return
|
||||
return url
|
||||
}
|
||||
|
||||
// WebSocketOutputMessages extracts all stdout, stderr and error messages from the passed messages.
|
||||
@@ -46,7 +47,7 @@ func WebSocketOutputMessages(messages []*dto.WebSocketMessage) (stdout, stderr s
|
||||
errors = append(errors, msg.Data)
|
||||
}
|
||||
}
|
||||
return
|
||||
return stdout, stderr, errors
|
||||
}
|
||||
|
||||
// WebSocketControlMessages extracts all meta (and exit) messages from the passed messages.
|
||||
@@ -57,11 +58,12 @@ func WebSocketControlMessages(messages []*dto.WebSocketMessage) (controls []*dto
|
||||
controls = append(controls, msg)
|
||||
}
|
||||
}
|
||||
return
|
||||
return controls
|
||||
}
|
||||
|
||||
// ReceiveAllWebSocketMessages pulls all messages from the websocket connection without sending anything.
|
||||
// This function does not return unless the server closes the connection or a readDeadline is set in the WebSocket connection.
|
||||
// This function does not return unless the server closes the connection or a readDeadline is set
|
||||
// in the WebSocket connection.
|
||||
func ReceiveAllWebSocketMessages(connection *websocket.Conn) (messages []*dto.WebSocketMessage, err error) {
|
||||
for {
|
||||
var message *dto.WebSocketMessage
|
||||
@@ -74,71 +76,99 @@ func ReceiveAllWebSocketMessages(connection *websocket.Conn) (messages []*dto.We
|
||||
}
|
||||
|
||||
// ReceiveNextWebSocketMessage pulls the next message from the websocket connection.
|
||||
// This function does not return unless the server sends a message, closes the connection or a readDeadline is set in the WebSocket connection.
|
||||
// This function does not return unless the server sends a message, closes the connection or a readDeadline
|
||||
// is set in the WebSocket connection.
|
||||
func ReceiveNextWebSocketMessage(connection *websocket.Conn) (*dto.WebSocketMessage, error) {
|
||||
_, reader, err := connection.NextReader()
|
||||
if err != nil {
|
||||
//nolint:wrapcheck // we could either wrap here and do complicated things with errors.As or just not wrap
|
||||
// the error in this test function and allow tests to use equal
|
||||
return nil, err
|
||||
}
|
||||
message := new(dto.WebSocketMessage)
|
||||
err = json.NewDecoder(reader).Decode(message)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("error decoding WebSocket message: %w", err)
|
||||
}
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// StartTLSServer runs a httptest.Server with the passed mux.Router and TLS enabled.
|
||||
func StartTLSServer(t *testing.T, router *mux.Router) (server *httptest.Server, err error) {
|
||||
func StartTLSServer(t *testing.T, router *mux.Router) (*httptest.Server, error) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
keyOut := filepath.Join(dir, "poseidon-test.key")
|
||||
certOut := filepath.Join(dir, "poseidon-test.crt")
|
||||
|
||||
err = exec.Command("openssl", "req", "-x509", "-nodes", "-newkey", "rsa:2048",
|
||||
err := exec.Command("openssl", "req", "-x509", "-nodes", "-newkey", "rsa:2048",
|
||||
"-keyout", keyOut, "-out", certOut, "-days", "1",
|
||||
"-subj", "/CN=Poseidon test", "-addext", "subjectAltName=IP:127.0.0.1,DNS:localhost").Run()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("error creating self-signed cert: %w", err)
|
||||
}
|
||||
cert, err := tls.LoadX509KeyPair(certOut, keyOut)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("error loading x509 key pair: %w", err)
|
||||
}
|
||||
|
||||
server = httptest.NewUnstartedServer(router)
|
||||
server.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
|
||||
server := httptest.NewUnstartedServer(router)
|
||||
server.TLS = &tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS13}
|
||||
server.StartTLS()
|
||||
return
|
||||
return server, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
// HTTPDelete sends a Delete Http Request with body to the passed url.
|
||||
func HTTPDelete(url string, body io.Reader) (response *http.Response, err error) {
|
||||
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests
|
||||
req, err := http.NewRequest(http.MethodDelete, url, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating request: %w", err)
|
||||
}
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
response, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error executing request: %w", err)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// HttpPatch sends a Patch Http Request with body to the passed url.
|
||||
func HttpPatch(url string, contentType string, body io.Reader) (response *http.Response, err error) {
|
||||
req, _ := http.NewRequest(http.MethodPatch, url, body)
|
||||
// HTTPPatch sends a Patch Http Request with body to the passed url.
|
||||
func HTTPPatch(url, contentType string, body io.Reader) (response *http.Response, err error) {
|
||||
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests
|
||||
req, err := http.NewRequest(http.MethodPatch, url, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error executing request: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func HttpPut(url string, body io.Reader) (response *http.Response, err error) {
|
||||
req, _ := http.NewRequest(http.MethodPut, url, body)
|
||||
func HTTPPut(url string, body io.Reader) (response *http.Response, err error) {
|
||||
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests
|
||||
req, err := http.NewRequest(http.MethodPut, url, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new request: %w", err)
|
||||
}
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error executing request: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func HttpPutJSON(url string, body interface{}) (response *http.Response, err error) {
|
||||
func HTTPPutJSON(url string, body interface{}) (response *http.Response, err error) {
|
||||
requestByteString, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
reader := bytes.NewReader(requestByteString)
|
||||
return HttpPut(url, reader)
|
||||
return HTTPPut(url, reader)
|
||||
}
|
||||
|
||||
func CreateTemplateJob() (base, job *nomadApi.Job) {
|
||||
|
Reference in New Issue
Block a user