Skip to content

Commit

Permalink
Add tests for journal:
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Oct 14, 2024
1 parent a8e7c14 commit bef8f20
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion internal/deprecated/workflow/journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package journal

import (
"context"
"fmt"
"log/slog"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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]
}
Expand Down
68 changes: 68 additions & 0 deletions internal/deprecated/workflow/journal/journal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/deprecated/workflow/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit bef8f20

Please sign in to comment.