-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonitor_test.go
More file actions
350 lines (288 loc) · 9.13 KB
/
monitor_test.go
File metadata and controls
350 lines (288 loc) · 9.13 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestProcessMonitor(t *testing.T) {
// Test monitoring the current process
pid := os.Getpid()
pm := NewProcessMonitor(pid)
if pm.GetLevel() != ProcessLevel {
t.Errorf("Expected process level, got %s", pm.GetLevel())
}
metrics, err := pm.GetMetrics()
if err != nil {
t.Fatalf("Failed to get process metrics: %v", err)
}
processMetrics, ok := metrics.(ProcessMetrics)
if !ok {
t.Fatalf("Expected ProcessMetrics, got %T", metrics)
}
if processMetrics.PID != pid {
t.Errorf("Expected PID %d, got %d", pid, processMetrics.PID)
}
if processMetrics.Name == "" {
t.Error("Process name should not be empty")
}
t.Logf("Process metrics: PID=%d, Name=%s, Status=%s, Memory=%d, Threads=%d",
processMetrics.PID, processMetrics.Name, processMetrics.Status,
processMetrics.MemoryVmRSS, processMetrics.Threads)
}
func TestHostMonitor(t *testing.T) {
hm := NewHostMonitor()
if hm.GetLevel() != HostLevel {
t.Errorf("Expected host level, got %s", hm.GetLevel())
}
metrics, err := hm.GetMetrics()
if err != nil {
t.Fatalf("Failed to get host metrics: %v", err)
}
hostMetrics, ok := metrics.(HostMetrics)
if !ok {
t.Fatalf("Expected HostMetrics, got %T", metrics)
}
if hostMetrics.Hostname == "" {
t.Error("Hostname should not be empty")
}
if hostMetrics.CPUCount <= 0 {
t.Error("CPU count should be positive")
}
if hostMetrics.MemoryTotal <= 0 {
t.Error("Memory total should be positive")
}
if hostMetrics.RuntimeContext != "data center" {
t.Errorf("Expected runtime context 'data center', got '%s'", hostMetrics.RuntimeContext)
}
t.Logf("Host metrics: Hostname=%s, CPUs=%d, Memory=%dMB, Load=%v",
hostMetrics.Hostname, hostMetrics.CPUCount,
hostMetrics.MemoryTotal/(1024*1024), hostMetrics.LoadAverage)
}
func TestContainerMonitor(t *testing.T) {
// Create a test container directory
testContainerID := "test-monitor-container"
containerDir := filepath.Join(baseDir, "containers", testContainerID)
err := os.MkdirAll(containerDir, 0755)
if err != nil {
t.Fatalf("Failed to create test container directory: %v", err)
}
defer os.RemoveAll(containerDir)
// Create a PID file
pidFile := filepath.Join(containerDir, "pid")
err = os.WriteFile(pidFile, []byte("1"), 0644)
if err != nil {
t.Fatalf("Failed to create PID file: %v", err)
}
cm := NewContainerMonitor(testContainerID)
if cm.GetLevel() != ContainerLevel {
t.Errorf("Expected container level, got %s", cm.GetLevel())
}
metrics, err := cm.GetMetrics()
if err != nil {
t.Fatalf("Failed to get container metrics: %v", err)
}
containerMetrics, ok := metrics.(ContainerMetrics)
if !ok {
t.Fatalf("Expected ContainerMetrics, got %T", metrics)
}
if containerMetrics.ContainerID != testContainerID {
t.Errorf("Expected container ID %s, got %s", testContainerID, containerMetrics.ContainerID)
}
if containerMetrics.DockerPath != containerDir {
t.Errorf("Expected Docker path %s, got %s", containerDir, containerMetrics.DockerPath)
}
if len(containerMetrics.VethInterfaces) == 0 {
t.Error("Should have at least one veth interface")
}
t.Logf("Container metrics: ID=%s, Status=%s, Memory=%d, VethInterfaces=%v",
containerMetrics.ContainerID, containerMetrics.Status,
containerMetrics.MemoryUsage, containerMetrics.VethInterfaces)
}
func TestMonitoringAggregator(t *testing.T) {
aggregator := NewMonitoringAggregator()
// Add monitors
aggregator.AddMonitor(NewProcessMonitor(os.Getpid()))
aggregator.AddMonitor(NewHostMonitor())
metrics, err := aggregator.GetAllMetrics()
if err != nil {
t.Fatalf("Failed to get aggregated metrics: %v", err)
}
if len(metrics) != 2 {
t.Errorf("Expected 2 monitoring levels, got %d", len(metrics))
}
if _, exists := metrics[ProcessLevel]; !exists {
t.Error("Process level metrics should exist")
}
if _, exists := metrics[HostLevel]; !exists {
t.Error("Host level metrics should exist")
}
// Test formatted metrics
formatted, err := aggregator.GetFormattedMetrics()
if err != nil {
t.Fatalf("Failed to get formatted metrics: %v", err)
}
if len(formatted) == 0 {
t.Error("Formatted metrics should not be empty")
}
t.Logf("Aggregated metrics length: %d characters", len(formatted))
}
func TestMonitoringGapAnalysis(t *testing.T) {
// Create sample metrics for gap analysis
metrics := map[MonitoringLevel]interface{}{
ProcessLevel: ProcessMetrics{PID: 1, Name: "test"},
ContainerLevel: ContainerMetrics{ContainerID: "test"},
HostLevel: HostMetrics{Hostname: "test-host"},
}
gap := AnalyzeMonitoringGap(metrics)
if len(gap.ProcessToContainer) == 0 {
t.Error("Process to container gaps should be identified")
}
if len(gap.ContainerToHost) == 0 {
t.Error("Container to host gaps should be identified")
}
if len(gap.CrossLevel) == 0 {
t.Error("Cross-level gaps should be identified")
}
t.Logf("Gap analysis: ProcessToContainer=%d, ContainerToHost=%d, CrossLevel=%d",
len(gap.ProcessToContainer), len(gap.ContainerToHost), len(gap.CrossLevel))
}
func TestMonitoringLevelsTable(t *testing.T) {
// Test that our monitoring implementation addresses the table from the problem statement
testCases := []struct {
level MonitoringLevel
spec string
onDisk string
inMemory string
inNetwork string
runtime string
isolation string
}{
{
level: ProcessLevel,
spec: "Source",
onDisk: ".TEXT",
inMemory: "PID",
inNetwork: "Socket",
runtime: "server core",
isolation: "moderate: memory space, etc.",
},
{
level: ContainerLevel,
spec: "Dockerfile",
onDisk: "/var/lib/docker",
inMemory: "Container ID",
inNetwork: "veth*",
runtime: "host",
isolation: "private OS view: own PID space, file system, network interfaces",
},
{
level: HostLevel,
spec: "Kickstart",
onDisk: "/",
inMemory: "Hostname",
inNetwork: "eth*",
runtime: "data center",
isolation: "full: including own page caches and kernel",
},
}
for _, tc := range testCases {
t.Logf("Testing monitoring level: %s", tc.level)
switch tc.level {
case ProcessLevel:
pm := NewProcessMonitor(os.Getpid())
metrics, err := pm.GetMetrics()
if err != nil {
t.Errorf("Failed to get process metrics: %v", err)
continue
}
processMetrics := metrics.(ProcessMetrics)
// Verify PID is captured (in memory)
if processMetrics.PID == 0 {
t.Error("Process PID should be captured")
}
// Verify socket information (in network)
if processMetrics.Socket == "" {
t.Error("Process socket information should be captured")
}
case ContainerLevel:
// Create test container for this test
testContainerID := "test-levels-container"
containerDir := filepath.Join(baseDir, "containers", testContainerID)
os.MkdirAll(containerDir, 0755)
defer os.RemoveAll(containerDir)
cm := NewContainerMonitor(testContainerID)
metrics, err := cm.GetMetrics()
if err != nil {
t.Errorf("Failed to get container metrics: %v", err)
continue
}
containerMetrics := metrics.(ContainerMetrics)
// Verify container ID is captured (in memory)
if containerMetrics.ContainerID != testContainerID {
t.Error("Container ID should be captured")
}
// Verify veth interfaces (in network)
if len(containerMetrics.VethInterfaces) == 0 {
t.Error("Container veth interfaces should be captured")
}
// Verify docker path (on disk)
if !strings.Contains(containerMetrics.DockerPath, "docker") {
t.Error("Container docker path should be captured")
}
case HostLevel:
hm := NewHostMonitor()
metrics, err := hm.GetMetrics()
if err != nil {
t.Errorf("Failed to get host metrics: %v", err)
continue
}
hostMetrics := metrics.(HostMetrics)
// Verify hostname is captured (in memory)
if hostMetrics.Hostname == "" {
t.Error("Host hostname should be captured")
}
// Verify network interfaces (in network) - should have some interfaces
if len(hostMetrics.NetworkInterfaces) == 0 {
// If no real interfaces found, this is expected in test environments
t.Logf("No network interfaces found (expected in test environments)")
}
// Verify runtime context (runtime)
if hostMetrics.RuntimeContext != tc.runtime {
t.Errorf("Expected runtime context '%s', got '%s'", tc.runtime, hostMetrics.RuntimeContext)
}
}
}
}
func BenchmarkProcessMonitoring(b *testing.B) {
pm := NewProcessMonitor(os.Getpid())
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := pm.GetMetrics()
if err != nil {
b.Fatalf("Error getting process metrics: %v", err)
}
}
}
func BenchmarkHostMonitoring(b *testing.B) {
hm := NewHostMonitor()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := hm.GetMetrics()
if err != nil {
b.Fatalf("Error getting host metrics: %v", err)
}
}
}
func BenchmarkMonitoringAggregator(b *testing.B) {
aggregator := NewMonitoringAggregator()
aggregator.AddMonitor(NewProcessMonitor(os.Getpid()))
aggregator.AddMonitor(NewHostMonitor())
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := aggregator.GetAllMetrics()
if err != nil {
b.Fatalf("Error getting aggregated metrics: %v", err)
}
}
}