-
Notifications
You must be signed in to change notification settings - Fork 4
/
task_test.go
177 lines (139 loc) · 5.45 KB
/
task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package task_test
import (
"context"
"os"
"testing"
"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/distros/task"
"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/testutils"
"github.com/canonical/ubuntu-pro-for-windows/wslserviceapi"
"github.com/stretchr/testify/require"
)
// We use the following number because is not representable as a float64:
//
// 0x20000000000001 -> float64 -> uint64 -> 0x20000000000000
// Loss of precision ^
//
// so if the yaml package does some intermediate conversion these tests will catch it.
// We've seen this happen when using an intermediate map[string]interface{}.
const bigInt uint64 = 0x20000000000001
func TestRegistry(t *testing.T) {
task.BackupRegistry(t)
got := task.RegisteredTasks()
require.Empty(t, got, "Setup: registry should be empty")
task.Register[testTask]()
task.Register[emptyTask]()
want := []string{"task_test.testTask", "task_test.emptyTask"}
got = task.RegisteredTasks()
require.ElementsMatch(t, want, got, "registry should contain only the registered tasks")
}
//nolint:tparallel // Cannot make test parallel because of BackupRegistry.
func TestMarshal(t *testing.T) {
task.BackupRegistry(t)
task.Register[testTask]()
task.Register[emptyTask]()
testCases := map[string]struct {
input task.Task
}{
"Simple task": {input: testTask{Message: "Hello, world!", Number: 42}},
"Task with a line break": {input: testTask{Message: "Hello, world!\nHow are you?", Number: 846531}},
"Task with a very large integer": {input: testTask{Message: "Not representable as a float64", Number: bigInt}},
"Task with no contents": {input: emptyTask{}},
"Unregistered task should still marshal successfully": {input: unregisteredTask{Score: 9001}},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := task.MarshalYAML([]task.Task{tc.input})
require.NoError(t, err, "input task should marshal with no errors")
// Avoiding LoadWithUpdateFromGoldenYAML to decouple marshalling and unmarshalling
want := testutils.LoadWithUpdateFromGolden(t, string(got))
require.Equal(t, want, string(got), "Task was not properly marshaled")
})
}
}
//nolint:tparallel // Cannot make test parallel because of BackupRegistry.
func TestUnmarshal(t *testing.T) {
task.BackupRegistry(t)
task.Register[testTask]()
task.Register[emptyTask]()
testCases := map[string]struct {
want task.Task
wantErr bool
}{
"Simple task": {want: testTask{Message: "Hello, world!", Number: 42}},
"Task with a line break": {want: testTask{Number: 64321, Message: "Hello, world!\nHow are you?"}},
"Task with a very large integer": {want: testTask{Number: bigInt, Message: "Not representable as a float64"}},
"Empty task": {want: emptyTask{}},
// Error cases
"Error on unregistered task": {wantErr: true},
"Error on bad YAML syntax": {wantErr: true},
"Error on missing task label": {wantErr: true},
"Error on bad datatype in task": {wantErr: true},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
data, err := os.ReadFile(testutils.TestFixturePath(t))
t.Log(testutils.TestFixturePath(t))
require.NoError(t, err, "Setup: could not find fixture")
got, err := task.UnmarshalYAML(data)
if tc.wantErr {
require.Error(t, err, "Task should return an error upon unmarshalling")
return
}
require.NoError(t, err, "Registered task should not fail to unmarshal")
require.Len(t, got, 1, "One and only one task was expected")
require.Equal(t, tc.want, got[0], "Task was not properly unmarshaled")
})
}
}
//nolint:tparallel // Cannot make test parallel because of BackupRegistry.
func TestMarsallUnmarshall(t *testing.T) {
task.BackupRegistry(t)
task.Register[testTask]()
task.Register[emptyTask]()
testCases := map[string]struct {
input task.Task
}{
"Simple task": {input: testTask{Message: "Hello, world!", Number: 42}},
"Task with a line break": {input: testTask{Number: 64321, Message: "Hello, world!\nHow are you?"}},
"Task with a very large integer": {input: testTask{Number: bigInt, Message: "Not representable as a float64"}},
"Empty task": {input: emptyTask{}},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
serial, err := task.MarshalYAML([]task.Task{tc.input})
require.NoError(t, err, "input task should marshal with no errors")
got, err := task.UnmarshalYAML(serial)
require.NoError(t, err, "Registered task should not fail to unmarshal")
require.Len(t, got, 1, "One and only one task was expected")
require.Equal(t, tc.input, got[0], "Marshaling, then unmarshaling a managed task should return the same object")
})
}
}
type testTask struct {
Message string
Number uint64
// we need to make this embedded struct to be public + ignore it instead of just using a private
// embedded field so that the yaml package does not panic.
DummyImplementer `yaml:"-"`
}
type emptyTask struct {
DummyImplementer `yaml:"-"`
}
type unregisteredTask struct {
Score int
DummyImplementer `yaml:"-"`
}
// Boilerplate to implement the interface.
type DummyImplementer struct{}
func (DummyImplementer) Execute(context.Context, wslserviceapi.WSLClient) error {
return nil
}
func (DummyImplementer) ShouldRetry() bool {
return false
}