-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackend_test.go
More file actions
257 lines (237 loc) · 7.31 KB
/
Copy pathbackend_test.go
File metadata and controls
257 lines (237 loc) · 7.31 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
257
package iapetus
import (
"errors"
"os/exec"
"strings"
"testing"
"time"
"go.uber.org/zap"
)
type mockBackend struct {
name string
status string
called *bool
}
func (m *mockBackend) RunTask(task *Task) error {
if m.called != nil {
*m.called = true
}
return nil
}
func (m *mockBackend) ValidateTask(task *Task) error { return nil }
func (m *mockBackend) GetName() string { return m.name }
func (m *mockBackend) GetStatus() string { return m.status }
func TestRegisterAndGetBackend(t *testing.T) {
mb := &mockBackend{name: "mock", status: "available"}
RegisterBackend("mock", mb)
b := GetBackend("mock")
if b == nil {
t.Fatal("expected backend to be registered and retrievable")
}
if b.GetName() != "mock" {
t.Errorf("expected GetName to return 'mock', got %q", b.GetName())
}
if b.GetStatus() != "available" {
t.Errorf("expected GetStatus to return 'available', got %q", b.GetStatus())
}
}
func TestGetBackend_NotFound(t *testing.T) {
b := GetBackend("doesnotexist")
if b != nil {
t.Errorf("expected nil for unregistered backend, got %v", b)
}
}
func dockerAvailable() bool {
_, err := exec.LookPath("docker")
if err != nil {
return false
}
cmd := exec.Command("docker", "version")
if err := cmd.Run(); err != nil {
return false
}
return true
}
func TestDockerBackend_ValidateTask(t *testing.T) {
b := &DockerBackend{}
task := NewTask("test", 0, zap.NewNop())
task.Image = "alpine"
task.Command = "echo"
if err := b.ValidateTask(task); err != nil {
t.Errorf("expected nil, got %v", err)
}
task.Image = ""
if err := b.ValidateTask(task); err == nil || !strings.Contains(err.Error(), "Image") {
t.Errorf("expected image error, got %v", err)
}
task.Image = "alpine"
task.Command = ""
if err := b.ValidateTask(task); err == nil || !strings.Contains(err.Error(), "Command") {
t.Errorf("expected command error, got %v", err)
}
}
func TestDockerBackend_RunTask_ValidateFail(t *testing.T) {
b := &DockerBackend{}
task := NewTask("test", 0, zap.NewNop())
task.Image = ""
task.Command = "echo"
if err := b.RunTask(task); err == nil || !strings.Contains(err.Error(), "Image") {
t.Errorf("expected image error, got %v", err)
}
}
func TestDockerBackend_RunTask_Success(t *testing.T) {
if !dockerAvailable() {
t.Skip("docker not available")
}
b := &DockerBackend{}
task := NewTask("test", 5*time.Second, zap.NewNop())
task.Image = "alpine"
task.Command = "echo"
task.Args = []string{"hello"}
task.Asserts = []func(*Task) error{AssertOutputContains("hello")}
if err := b.RunTask(task); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !strings.Contains(task.Actual.Output, "hello") {
t.Errorf("expected output to contain 'hello', got %q", task.Actual.Output)
}
if task.Actual.ExitCode != 0 {
t.Errorf("expected exit code 0, got %d", task.Actual.ExitCode)
}
}
func TestDockerBackend_RunTask_EnvMap(t *testing.T) {
if !dockerAvailable() {
t.Skip("docker not available")
}
b := &DockerBackend{}
task := NewTask("test", 5*time.Second, zap.NewNop())
task.Image = "alpine"
task.Command = "sh"
task.Args = []string{"-c", "echo $FOO"}
task.EnvMap = map[string]string{"FOO": "bar"}
task.Asserts = []func(*Task) error{AssertOutputEquals("bar\n")}
if err := b.RunTask(task); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if task.Actual.Output != "bar\n" {
t.Errorf("expected output 'bar\n', got %q", task.Actual.Output)
}
}
func TestDockerBackend_RunTask_AssertionFail(t *testing.T) {
if !dockerAvailable() {
t.Skip("docker not available")
}
b := &DockerBackend{}
task := NewTask("test", 5*time.Second, zap.NewNop())
task.Image = "alpine"
task.Command = "echo"
task.Args = []string{"foo"}
task.Asserts = []func(*Task) error{func(t *Task) error { return errors.New("fail") }}
if err := b.RunTask(task); err == nil || !strings.Contains(err.Error(), "fail") {
t.Errorf("expected assertion error, got %v", err)
}
}
func TestBashBackend_ValidateTask(t *testing.T) {
b := &BashBackend{}
task := NewTask("test", 0, zap.NewNop())
task.Command = "echo"
if err := b.ValidateTask(task); err != nil {
t.Errorf("expected nil, got %v", err)
}
}
func TestBashBackend_RunTask_Success(t *testing.T) {
b := &BashBackend{}
task := NewTask("test", 2*time.Second, zap.NewNop())
task.Command = "echo"
task.Args = []string{"hello"}
task.Asserts = []func(*Task) error{AssertOutputContains("hello")}
if err := b.RunTask(task); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !strings.Contains(task.Actual.Output, "hello") {
t.Errorf("expected output to contain 'hello', got %q", task.Actual.Output)
}
if task.Actual.ExitCode != 0 {
t.Errorf("expected exit code 0, got %d", task.Actual.ExitCode)
}
}
func TestBashBackend_RunTask_EnvMap(t *testing.T) {
b := &BashBackend{}
task := NewTask("test", 2*time.Second, zap.NewNop())
task.Command = "sh"
task.Args = []string{"-c", "echo $FOO"}
task.EnvMap = map[string]string{"FOO": "bar"}
task.Asserts = []func(*Task) error{AssertOutputEquals("bar\n")}
if err := b.RunTask(task); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if task.Actual.Output != "bar\n" {
t.Errorf("expected output 'bar\n', got %q", task.Actual.Output)
}
}
func TestBashBackend_RunTask_Timeout(t *testing.T) {
b := &BashBackend{}
task := NewTask("test", 500*time.Millisecond, zap.NewNop())
task.Command = "sleep"
task.Args = []string{"2"}
if err := b.RunTask(task); err == nil || !strings.Contains(err.Error(), "timed out") {
t.Errorf("expected timeout error, got %v", err)
}
}
func TestBashBackend_RunTask_AssertionFail(t *testing.T) {
b := &BashBackend{}
task := NewTask("test", 2*time.Second, zap.NewNop())
task.Command = "echo"
task.Args = []string{"foo"}
task.Asserts = []func(*Task) error{func(t *Task) error { return errors.New("fail") }}
if err := b.RunTask(task); err == nil || !strings.Contains(err.Error(), "fail") {
t.Errorf("expected assertion error, got %v", err)
}
}
func kubectlAvailable() bool {
_, err := exec.LookPath("kubectl")
if err != nil {
return false
}
// Check if we can reach a cluster
cmd := exec.Command("kubectl", "cluster-info")
if err := cmd.Run(); err != nil {
return false
}
return true
}
func TestKubernetesBackend_ValidateTask(t *testing.T) {
b := &KubernetesBackend{}
task := NewTask("test", 0, zap.NewNop())
task.Image = "alpine"
task.Command = "echo"
if err := b.ValidateTask(task); err != nil {
t.Errorf("expected nil, got %v", err)
}
task.Image = ""
if err := b.ValidateTask(task); err == nil || !strings.Contains(err.Error(), "Image") {
t.Errorf("expected image error, got %v", err)
}
task.Image = "alpine"
task.Command = ""
if err := b.ValidateTask(task); err == nil || !strings.Contains(err.Error(), "Command") {
t.Errorf("expected command error, got %v", err)
}
}
func TestKubernetesBackend_RunTask(t *testing.T) {
if !kubectlAvailable() {
t.Skip("kubectl not available")
}
b := &KubernetesBackend{}
task := NewTask("k8s-echo", 5*time.Second, zap.NewNop())
task.Image = "alpine"
task.Command = "echo"
task.Args = []string{"hello-from-k8s"}
task.Asserts = []func(*Task) error{AssertOutputContains("hello-from-k8s")}
if err := b.RunTask(task); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !strings.Contains(task.Actual.Output, "hello-from-k8s") {
t.Errorf("expected output to contain 'hello-from-k8s', got %q", task.Actual.Output)
}
}