From f309d0f70ee6721c47e748e525a71d43ca5f008d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Pa=C3=9F?= <22845248+mpass99@users.noreply.github.com> Date: Sun, 26 Feb 2023 21:28:30 +0000 Subject: [PATCH] Ensure sending of the Sentry End debug message. --- internal/nomad/api_querier.go | 4 +++- internal/nomad/sentry_debug_writer.go | 5 ++--- internal/nomad/sentry_debug_writer_test.go | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/internal/nomad/api_querier.go b/internal/nomad/api_querier.go index 70f082b..6e3b07b 100644 --- a/internal/nomad/api_querier.go +++ b/internal/nomad/api_querier.go @@ -9,6 +9,7 @@ import ( "github.com/openHPI/poseidon/internal/config" "github.com/openHPI/poseidon/pkg/logging" "io" + "strings" ) var ( @@ -89,7 +90,8 @@ func (nc *nomadAPIClient) Execute(runnerID string, ctx context.Context, command []string, tty bool, stdin io.Reader, stdout, stderr io.Writer, ) (int, error) { - log.WithField("command", command).Trace("Requesting Nomad Exec") + log.WithField("command", strings.ReplaceAll(strings.Join(command, ", "), "\n", "")). + Trace("Requesting Nomad Exec") var allocations []*nomadApi.AllocationListStub var err error logging.StartSpan("nomad.execute.list", "List Allocations for id", ctx, func(_ context.Context) { diff --git a/internal/nomad/sentry_debug_writer.go b/internal/nomad/sentry_debug_writer.go index 834d51c..c07f3d4 100644 --- a/internal/nomad/sentry_debug_writer.go +++ b/internal/nomad/sentry_debug_writer.go @@ -12,11 +12,10 @@ import ( const ( // timeDebugMessageFormat is the format of messages that will be converted to debug messages. - timeDebugMessageFormat = "echo -ne \"\\x1EPoseidon %s $(date +%%s%%3N)\\x1E\"" // Format Parameters: 1. Debug Comment, 2. command. - timeDebugMessageFormatStart = timeDebugMessageFormat + "; %s" + timeDebugMessageFormatStart = `echo -ne "\x1EPoseidon %s $(date +%%s%%3N)\x1E"; %s` // Format Parameters: 1. command, 2. Debug Comment. - timeDebugMessageFormatEnd = "%s && " + timeDebugMessageFormat + timeDebugMessageFormatEnd = `%s; bash -c "ec=$?; echo -ne \"\\x1EPoseidon %s \$(date +%%s%%3N)\\x1E\" && exit \$ec"` ) var ( diff --git a/internal/nomad/sentry_debug_writer_test.go b/internal/nomad/sentry_debug_writer_test.go index 090777a..45f762c 100644 --- a/internal/nomad/sentry_debug_writer_test.go +++ b/internal/nomad/sentry_debug_writer_test.go @@ -20,3 +20,25 @@ func TestSentryDebugWriter_Write(t *testing.T) { assert.Equal(t, len(data), count) assert.NotContains(t, buf.String(), description) } + +func TestSentryDebugWriter_Close(t *testing.T) { + buf := &bytes.Buffer{} + s := NewSentryDebugWriter(buf, context.Background()) + require.Empty(t, s.lastSpan.Tags) + + s.Close(42) + require.Contains(t, s.lastSpan.Tags, "exit_code") + assert.Equal(t, "42", s.lastSpan.Tags["exit_code"]) +} + +func TestSentryDebugWriter_handleTimeDebugMessage(t *testing.T) { + buf := &bytes.Buffer{} + s := NewSentryDebugWriter(buf, context.Background()) + require.Equal(t, "nomad.execute.connect", s.lastSpan.Op) + + description := "TestDebugMessageDescription" + match := map[string][]byte{"time": []byte("1676646791482"), "text": []byte(description)} + s.handleTimeDebugMessage(match) + assert.Equal(t, "nomad.execute.bash", s.lastSpan.Op) + assert.Equal(t, description, s.lastSpan.Description) +}