-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions_test.go
More file actions
206 lines (177 loc) · 5.64 KB
/
Copy pathactions_test.go
File metadata and controls
206 lines (177 loc) · 5.64 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
package engine
import (
"context"
"fmt"
"testing"
"github.com/ibrahimkizilarslan/entropy/pkg/config"
)
func TestNewResourceChaosManager(t *testing.T) {
manager := NewResourceChaosManager()
if manager == nil {
t.Fatalf("NewResourceChaosManager returned nil")
}
if manager.timers == nil {
t.Fatalf("Manager timers map not initialized")
}
if len(manager.timers) != 0 {
t.Errorf("Manager timers should be empty, got %d", len(manager.timers))
}
}
func TestResourceChaosManagerClearAll(t *testing.T) {
manager := NewResourceChaosManager()
mock := NewMockRuntime()
// Schedule some restores and then clear all
manager.ScheduleRestore(mock, "target-1", 3600)
manager.ScheduleRestore(mock, "target-2", 3600)
manager.ClearAll()
manager.mu.Lock()
count := len(manager.timers)
manager.mu.Unlock()
if count != 0 {
t.Errorf("Expected 0 timers after ClearAll, got %d", count)
}
}
func TestActionHandlersMapExists(t *testing.T) {
expectedActions := []string{
"stop",
"restart",
"pause",
"delay",
"loss",
"limit_cpu",
"limit_memory",
}
for _, action := range expectedActions {
if _, ok := GetActionHandler(action); !ok {
t.Errorf("Action %q not found in ActionHandlers", action)
}
}
}
func TestDispatch_Stop(t *testing.T) {
mock := NewMockRuntime()
spec := config.ActionSpec{Name: "stop"}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch stop failed: %v", err)
}
if info.Status != "exited" {
t.Errorf("Expected status 'exited', got '%s'", info.Status)
}
if mock.CallCount("StopContainer") != 1 {
t.Errorf("Expected 1 StopContainer call, got %d", mock.CallCount("StopContainer"))
}
}
func TestDispatch_Restart(t *testing.T) {
mock := NewMockRuntime()
spec := config.ActionSpec{Name: "restart"}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch restart failed: %v", err)
}
if info.Status != "running" {
t.Errorf("Expected status 'running', got '%s'", info.Status)
}
if mock.CallCount("RestartContainer") != 1 {
t.Errorf("Expected 1 RestartContainer call, got %d", mock.CallCount("RestartContainer"))
}
}
func TestDispatch_Pause(t *testing.T) {
mock := NewMockRuntime()
spec := config.ActionSpec{Name: "pause"}
info, err := Dispatch(context.Background(), spec, mock, "service-b")
if err != nil {
t.Fatalf("Dispatch pause failed: %v", err)
}
if info.Status != "paused" {
t.Errorf("Expected status 'paused', got '%s'", info.Status)
}
if mock.CallCount("PauseContainer") != 1 {
t.Errorf("Expected 1 PauseContainer call, got %d", mock.CallCount("PauseContainer"))
}
}
func TestDispatch_Delay(t *testing.T) {
mock := NewMockRuntime()
dur := 10
spec := config.ActionSpec{Name: "delay", LatencyMs: 300, JitterMs: 50, Duration: &dur}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch delay failed: %v", err)
}
if info.Status != "running (delayed)" {
t.Errorf("Expected status 'running (delayed)', got '%s'", info.Status)
}
if mock.CallCount("InjectNetworkDelay") != 1 {
t.Errorf("Expected 1 InjectNetworkDelay call, got %d", mock.CallCount("InjectNetworkDelay"))
}
}
func TestDispatch_Loss(t *testing.T) {
mock := NewMockRuntime()
dur := 5
spec := config.ActionSpec{Name: "loss", LossPercent: 20, Duration: &dur}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch loss failed: %v", err)
}
if info.Status != "running (lossy)" {
t.Errorf("Expected status 'running (lossy)', got '%s'", info.Status)
}
if mock.CallCount("InjectNetworkLoss") != 1 {
t.Errorf("Expected 1 InjectNetworkLoss call, got %d", mock.CallCount("InjectNetworkLoss"))
}
}
func TestDispatch_LimitCPU(t *testing.T) {
mock := NewMockRuntime()
dur := 10
spec := config.ActionSpec{Name: "limit_cpu", CPUs: 0.5, Duration: &dur}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch limit_cpu failed: %v", err)
}
if info.Name != "service-a" {
t.Errorf("Expected name 'service-a', got '%s'", info.Name)
}
if mock.CallCount("UpdateContainerResources") != 1 {
t.Errorf("Expected 1 UpdateContainerResources call, got %d", mock.CallCount("UpdateContainerResources"))
}
}
func TestDispatch_LimitMemory(t *testing.T) {
mock := NewMockRuntime()
dur := 10
spec := config.ActionSpec{Name: "limit_memory", MemoryMB: 128, Duration: &dur}
info, err := Dispatch(context.Background(), spec, mock, "service-a")
if err != nil {
t.Fatalf("Dispatch limit_memory failed: %v", err)
}
if info.Name != "service-a" {
t.Errorf("Expected name 'service-a', got '%s'", info.Name)
}
if mock.CallCount("UpdateContainerResources") != 1 {
t.Errorf("Expected 1 UpdateContainerResources call, got %d", mock.CallCount("UpdateContainerResources"))
}
}
func TestDispatch_UnknownAction(t *testing.T) {
mock := NewMockRuntime()
spec := config.ActionSpec{Name: "explode"}
_, err := Dispatch(context.Background(), spec, mock, "service-a")
if err == nil {
t.Fatal("Expected error for unknown action, got nil")
}
}
func TestDispatch_StopError(t *testing.T) {
mock := NewMockRuntime()
mock.StopErr = fmt.Errorf("docker daemon unavailable")
spec := config.ActionSpec{Name: "stop"}
_, err := Dispatch(context.Background(), spec, mock, "service-a")
if err == nil {
t.Fatal("Expected error when runtime fails, got nil")
}
}
func TestDispatch_ContainerNotFound(t *testing.T) {
mock := NewMockRuntime()
spec := config.ActionSpec{Name: "stop"}
_, err := Dispatch(context.Background(), spec, mock, "nonexistent-service")
if err == nil {
t.Fatal("Expected error for nonexistent container, got nil")
}
}
// End of file