From bef8f20e0fc1611b4066b3c17360377b6f6cb3de Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Fri, 11 Oct 2024 20:05:10 -0600 Subject: [PATCH] Add tests for journal: Signed-off-by: Jacob Weinstock --- .../deprecated/workflow/journal/journal.go | 3 +- .../workflow/journal/journal_test.go | 68 +++++++++++++++++++ internal/deprecated/workflow/reconciler.go | 2 +- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 internal/deprecated/workflow/journal/journal_test.go diff --git a/internal/deprecated/workflow/journal/journal.go b/internal/deprecated/workflow/journal/journal.go index 5dabcf8be..0437ed7ae 100644 --- a/internal/deprecated/workflow/journal/journal.go +++ b/internal/deprecated/workflow/journal/journal.go @@ -2,6 +2,7 @@ package journal import ( "context" + "fmt" "log/slog" "path/filepath" "runtime" @@ -34,7 +35,7 @@ func Log(ctx context.Context, msg string, args ...any) { for i := 0; i < len(args); i += 2 { k, ok := args[i].(string) if !ok { - k = "invalid key" + k = fmt.Sprintf("%v", args[i]) } m[k] = args[i+1] } diff --git a/internal/deprecated/workflow/journal/journal_test.go b/internal/deprecated/workflow/journal/journal_test.go new file mode 100644 index 000000000..175fcb65a --- /dev/null +++ b/internal/deprecated/workflow/journal/journal_test.go @@ -0,0 +1,68 @@ +package journal + +import ( + "context" + "log/slog" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" +) + +func TestJournal(t *testing.T) { + type input struct { + msg string + args []any + } + tests := map[string]struct { + want []Entry + inputs []input + }{ + "empty": { + want: []Entry{}, + }, + "single": { + want: []Entry{ + { + Msg: "one", + Args: map[string]any{"key": "value"}, + Source: slog.Source{ + File: "journal_test.go", + Function: "func1()", + }, + }, + }, + inputs: []input{ + {msg: "one", args: []any{"key", "value"}}, + }, + }, + "non normal key": { + want: []Entry{ + { + Msg: "msg", + Args: map[string]any{"1.1": "value"}, + Source: slog.Source{ + File: "journal_test.go", + Function: "func1()", + }, + }, + }, + inputs: []input{ + {msg: "msg", args: []any{1.1, "value"}}, + }, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + ctx := New(context.Background()) + for _, input := range tc.inputs { + Log(ctx, input.msg, input.args...) + } + got := Journal(ctx) + if diff := cmp.Diff(tc.want, got, cmpopts.IgnoreFields(Entry{}, "Time"), cmpopts.IgnoreFields(slog.Source{}, "Line")); diff != "" { + t.Errorf("unexpected journal (-want +got):\n%s", diff) + } + }) + } +} diff --git a/internal/deprecated/workflow/reconciler.go b/internal/deprecated/workflow/reconciler.go index 0f2de8c2a..5922fa669 100644 --- a/internal/deprecated/workflow/reconciler.go +++ b/internal/deprecated/workflow/reconciler.go @@ -115,7 +115,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco return rc, serrors.Join(err, mergePatchStatus(ctx, r.client, stored, wflow)) case v1alpha1.WorkflowStatePending, v1alpha1.WorkflowStateTimeout, v1alpha1.WorkflowStateFailed, v1alpha1.WorkflowStateSuccess: - journal.Log(ctx, "workflow state will not trigger another reconcile", "state", wflow.Status.State) + journal.Log(ctx, "controller will not trigger another reconcile", "state", wflow.Status.State) return reconcile.Result{}, nil }