From cc7c706e9cce1baa0b9e8f284ef3d6176e606627 Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Fri, 20 Oct 2023 14:22:27 +0200 Subject: [PATCH] task/StatusRecord: add tests --- internal/model/task.go | 8 ++++ internal/model/task_test.go | 66 ++++++++++++++++++++++++++++ internal/worker/kv_status_test.go | 3 +- internal/worker/task_handler_test.go | 2 +- internal/worker/worker_test.go | 7 ++- 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 internal/model/task_test.go diff --git a/internal/model/task.go b/internal/model/task.go index 5d47bd44..dbdb62a5 100644 --- a/internal/model/task.go +++ b/internal/model/task.go @@ -151,6 +151,10 @@ func (t *Task) State() sw.State { func NewTaskStatusRecord(s string) StatusRecord { sr := StatusRecord{} + if s == "" { + return sr + } + sr.Append(s) return sr @@ -166,6 +170,10 @@ type StatusMsg struct { } func (sr *StatusRecord) Append(s string) { + if s == "" { + return + } + for _, r := range sr.StatusMsgs { if r.Msg == s { return diff --git a/internal/model/task_test.go b/internal/model/task_test.go new file mode 100644 index 00000000..cd433f3e --- /dev/null +++ b/internal/model/task_test.go @@ -0,0 +1,66 @@ +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTaskStatusRecord(t *testing.T) { + tests := []struct { + name string + appendStatus string + appendStatuses []string + wantStatuses []string + }{ + { + "single status record appended", + "works", + nil, + []string{"works"}, + }, + { + "multiple status record appended", + "", + []string{"a", "b", "c"}, + []string{"a", "b", "c"}, + }, + { + "dup status excluded", + "", + []string{"a", "a", "b", "c"}, + []string{"a", "b", "c"}, + }, + { + "empty status excluded", + "", + []string{"a", "", "", "c"}, + []string{"a", "c"}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + sr := NewTaskStatusRecord("") + + if tc.appendStatus != "" { + sr.Append(tc.appendStatus) + + assert.Equal(t, tc.appendStatus, sr.StatusMsgs[0].Msg) + assert.False(t, sr.StatusMsgs[0].Timestamp.IsZero()) + } + + if tc.appendStatuses != nil { + for _, s := range tc.appendStatuses { + sr.Append(s) + } + + assert.Equal(t, len(tc.wantStatuses), len(sr.StatusMsgs)) + for idx, w := range tc.wantStatuses { + assert.Equal(t, w, sr.StatusMsgs[idx].Msg) + assert.False(t, sr.StatusMsgs[idx].Timestamp.IsZero()) + } + } + }) + } +} diff --git a/internal/worker/kv_status_test.go b/internal/worker/kv_status_test.go index 959eb369..ebcba4a2 100644 --- a/internal/worker/kv_status_test.go +++ b/internal/worker/kv_status_test.go @@ -101,7 +101,8 @@ func TestPublisher(t *testing.T) { require.Equal(t, types.Version, sv.MsgVersion, "version check") require.Equal(t, assetID.String(), sv.Target, "sv Target") - require.Equal(t, json.RawMessage(`{"msg":"some-status"}`), sv.Status, "sv Status") + + require.Contains(t, string(sv.Status), "some-status", "sv Status") testContext.Task.SetState(model.StateActive) require.NotPanics(t, func() { pub.Publish(testContext) }, "publish revision") diff --git a/internal/worker/task_handler_test.go b/internal/worker/task_handler_test.go index a52d3bd4..503bb5e0 100644 --- a/internal/worker/task_handler_test.go +++ b/internal/worker/task_handler_test.go @@ -135,7 +135,7 @@ func TestRemoveFirmwareAlreadyAtDesiredVersion(t *testing.T) { } got := removeFirmwareAlreadyAtDesiredVersion(ctx, fwSet) - require.Equal(t, 2, ctx.Task.Status) + require.Equal(t, 3, len(ctx.Task.Status.StatusMsgs)) require.Equal(t, 1, len(got)) require.Equal(t, expected[0], got[0]) } diff --git a/internal/worker/worker_test.go b/internal/worker/worker_test.go index a42ffd76..0bcf019e 100644 --- a/internal/worker/worker_test.go +++ b/internal/worker/worker_test.go @@ -11,7 +11,7 @@ import ( rctypes "github.com/metal-toolbox/rivets/condition" ) -func Test_newTaskFromCondition(t *testing.T) { +func TestNewTaskFromCondition(t *testing.T) { tests := []struct { name string condition *rctypes.Condition @@ -50,7 +50,10 @@ func Test_newTaskFromCondition(t *testing.T) { return } - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want.ID, got.ID) + assert.Equal(t, tt.want.State(), got.State()) + assert.Equal(t, tt.want.Parameters, got.Parameters) + assert.Contains(t, got.Status.String(), "initialized task") }) } }