-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask_test.go
More file actions
256 lines (228 loc) · 6.83 KB
/
Copy pathtask_test.go
File metadata and controls
256 lines (228 loc) · 6.83 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package iapetus_test
import (
"errors"
"fmt"
"testing"
"github.com/yindia/iapetus"
)
func TestIntegrationTest_Run(t *testing.T) {
test := &iapetus.Task{
Command: "echo",
Args: []string{"Hello, World!"},
Asserts: []func(*iapetus.Task) error{
iapetus.AssertOutputEquals("Hello, World!\n"),
},
}
test.SetBackend("bash")
iapetus.RegisterBackend("bash", &iapetus.BashBackend{})
err := test.Run()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if test.Actual.Output != "Hello, World!\n" {
t.Errorf("expected output 'Hello, World!', got %v", test.Actual.Output)
}
}
func TestIntegrationTest_AddAssertion(t *testing.T) {
test := &iapetus.Task{}
assertion := func(i *iapetus.Task) error {
return nil
}
test.AddAssertion(assertion)
if len(test.Asserts) != 1 {
t.Errorf("expected 1 assertion, got %d", len(test.Asserts))
}
}
func TestIntegrationTest_AddMultipleAssertions(t *testing.T) {
test := &iapetus.Task{}
assertion1 := func(i *iapetus.Task) error {
return nil
}
assertion2 := func(i *iapetus.Task) error {
return errors.New("failed assertion")
}
test.AddAssertion(assertion1)
test.AddAssertion(assertion2)
if len(test.Asserts) != 2 {
t.Errorf("expected 2 assertions, got %d", len(test.Asserts))
}
}
func TestIntegrationTest_RunCommandError(t *testing.T) {
test := &iapetus.Task{
Command: "invalid_command",
Asserts: []func(*iapetus.Task) error{
iapetus.AssertExitCode(1),
},
}
test.SetBackend("bash")
iapetus.RegisterBackend("bash", &iapetus.BashBackend{})
err := test.Run()
if err == nil {
t.Fatalf("expected error for invalid command, got nil")
}
if test.Actual.ExitCode == 0 {
t.Errorf("expected non-zero exit code for invalid command, got 0")
}
}
func TestTask_AssertExitCode(t *testing.T) {
task := iapetus.NewTask("test", 0, nil).AssertExitCode(0)
task.Actual.ExitCode = 0
if err := iapetus.RunAssertions(task); err != nil {
t.Errorf("expected no error, got %v", err)
}
task.Actual.ExitCode = 1
if err := iapetus.RunAssertions(task); err == nil {
t.Errorf("expected error for wrong exit code, got nil")
}
}
func TestTask_AssertOutputContains(t *testing.T) {
task := iapetus.NewTask("test", 0, nil).AssertOutputContains("foo")
task.Actual.Output = "hello foo bar"
if err := iapetus.RunAssertions(task); err != nil {
t.Errorf("expected no error, got %v", err)
}
task.Actual.Output = "hello bar"
if err := iapetus.RunAssertions(task); err == nil {
t.Errorf("expected error for missing substring, got nil")
}
}
func TestTask_AssertOutputEquals(t *testing.T) {
task := iapetus.NewTask("test", 0, nil).AssertOutputEquals("foo")
task.Actual.Output = "foo"
if err := iapetus.RunAssertions(task); err != nil {
t.Errorf("expected no error, got %v", err)
}
task.Actual.Output = "bar"
if err := iapetus.RunAssertions(task); err == nil {
t.Errorf("expected error for output mismatch, got nil")
}
}
func TestTask_AssertOutputMatchesRegexp(t *testing.T) {
task := iapetus.NewTask("test", 0, nil).AssertOutputMatchesRegexp(`foo\d+`)
task.Actual.Output = "foo123"
if err := iapetus.RunAssertions(task); err != nil {
t.Errorf("expected no error, got %v", err)
}
task.Actual.Output = "bar"
if err := iapetus.RunAssertions(task); err == nil {
t.Errorf("expected error for regex mismatch, got nil")
}
}
func TestTask_ExpectDSL(t *testing.T) {
task := iapetus.NewTask("test", 0, nil).
Expect().
ExitCode(0).
OutputContains("foo").
Done()
task.Actual.ExitCode = 0
task.Actual.Output = "foo bar"
if err := iapetus.RunAssertions(task); err != nil {
t.Errorf("expected no error, got %v", err)
}
task.Actual.ExitCode = 1
if err := iapetus.RunAssertions(task); err == nil {
t.Errorf("expected error for wrong exit code, got nil")
}
}
func registerTestBackend(t *testing.T, name string, backend iapetus.Backend) {
iapetus.RegisterBackend(name, backend)
t.Cleanup(func() {
// No-op: add backend registry reset here if needed for isolation
})
}
func TestTask_EnvVars(t *testing.T) {
cases := []struct {
name string
env map[string]string
expect string
}{
{"only EnvMap", map[string]string{"FOO": "bar"}, "bar\n"},
{"EnvMap wins", map[string]string{"FOO": "baz"}, "baz\n"},
{"no envs, default", nil, "\n"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
b := &iapetus.BashBackend{}
task := &iapetus.Task{
Command: "sh",
Args: []string{"-c", "echo $FOO"},
EnvMap: tc.env,
Asserts: []func(*iapetus.Task) error{iapetus.AssertOutputEquals(tc.expect)},
}
task.SetBackend("bash")
registerTestBackend(t, "bash", b)
if err := task.Run(); err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
}
}
// --- Additional tests for Task backend/logger/env/validation ---
type testBackend struct {
called *bool
fail bool
validateErr error
}
func (b *testBackend) RunTask(task *iapetus.Task) error {
if b.called != nil {
*b.called = true
}
if b.fail {
return fmt.Errorf("backend run error")
}
return nil
}
func (b *testBackend) ValidateTask(task *iapetus.Task) error {
return b.validateErr
}
func (b *testBackend) GetName() string {
return "test"
}
func (b *testBackend) GetStatus() string {
return "test"
}
func TestTask_DefaultsAndValidation(t *testing.T) {
t.Run("defaults backend/logger/env", func(t *testing.T) {
called := false
iapetus.RegisterBackend("test", &testBackend{called: &called})
task := &iapetus.Task{Command: "echo", Backend: "test"}
if err := task.Run(); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !called {
t.Errorf("expected backend RunTask to be called")
}
if task.Logger() == nil {
t.Errorf("expected logger to be set")
}
if task.EnvMap == nil {
t.Errorf("expected EnvMap to be initialized")
}
})
t.Run("missing command", func(t *testing.T) {
task := &iapetus.Task{Backend: "test"}
err := task.Run()
if err == nil || err.Error() != "task task-: command is required" && !contains(err.Error(), "command is required") {
t.Errorf("expected error for missing command, got %v", err)
}
})
t.Run("validate error", func(t *testing.T) {
iapetus.RegisterBackend("test-validate", &testBackend{validateErr: fmt.Errorf("bad task")})
task := &iapetus.Task{Command: "echo", Backend: "test-validate"}
err := task.Run()
if err == nil || err.Error() != "bad task" {
t.Errorf("expected validation error, got %v", err)
}
})
t.Run("backend run error and retry", func(t *testing.T) {
iapetus.RegisterBackend("test-fail", &testBackend{fail: true})
task := &iapetus.Task{Command: "echo", Backend: "test-fail", Retries: 2}
err := task.Run()
if err == nil || !contains(err.Error(), "failed after 2 attempts") {
t.Errorf("expected retry error, got %v", err)
}
})
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || (len(s) > len(substr) && (contains(s[1:], substr) || contains(s[:len(s)-1], substr))))
}