Skip to content

Commit

Permalink
task/StatusRecord: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Oct 23, 2023
1 parent 61f522b commit cc7c706
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
8 changes: 8 additions & 0 deletions internal/model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
66 changes: 66 additions & 0 deletions internal/model/task_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
})
}
}
3 changes: 2 additions & 1 deletion internal/worker/kv_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/worker/task_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
7 changes: 5 additions & 2 deletions internal/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
})
}
}

0 comments on commit cc7c706

Please sign in to comment.