-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain_test.go
More file actions
351 lines (293 loc) · 10.9 KB
/
main_test.go
File metadata and controls
351 lines (293 loc) · 10.9 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
351
package main
import (
"os"
"testing"
"fmt"
"path/filepath"
"os/exec"
)
// Test Scenarios Documentation
//
// TestInitDirectories:
// - Verifies that the initDirectories function creates the required directories.
// - Setup: Ensures the directories do not exist before the test.
// - Expected Outcome: The directories should be created successfully.
func TestInitDirectories(t *testing.T) {
// Setup: Remove directories if they exist
baseDir := filepath.Join(os.TempDir(), "basic-docker")
dirs := []string{
filepath.Join(baseDir, "containers"),
filepath.Join(baseDir, "images"),
filepath.Join(baseDir, "layers"),
}
for _, dir := range dirs {
os.RemoveAll(dir)
}
// Call initDirectories
err := initDirectories()
if err != nil {
t.Fatalf("initDirectories failed: %v", err)
}
// Verify that the directories were created
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
t.Errorf("Directory %s was not created", dir)
}
}
}
// TestListContainers:
// - Verifies that the listContainers function lists running containers correctly.
// - Setup: Creates mock container directories and PID files.
// - Expected Outcome: The output includes the container IDs and their statuses.
func TestListContainers(t *testing.T) {
// Setup: Create mock container directories and PID files
baseDir := filepath.Join(os.TempDir(), "basic-docker")
containerDir := filepath.Join(baseDir, "containers")
if err := os.MkdirAll(containerDir, 0755); err != nil {
t.Fatalf("Failed to create container directory: %v", err)
}
defer os.RemoveAll(baseDir) // Cleanup
containerID := "test-container"
if err := os.MkdirAll(containerDir+"/"+containerID, 0755); err != nil {
t.Fatalf("Failed to create mock container directory: %v", err)
}
pidFile := containerDir + "/" + containerID + "/pid"
if err := os.WriteFile(pidFile, []byte("12345"), 0644); err != nil {
t.Fatalf("Failed to create mock PID file: %v", err)
}
// Capture the output of listContainers
output := captureOutput(listContainers)
// Verify the output contains the container ID
if !contains(output, containerID) {
t.Errorf("Expected output to contain container ID '%s', but got: %s", containerID, output)
}
}
// TestGetContainerStatus:
// - Verifies that the getContainerStatus function correctly identifies the status of a container.
// - Setup: Creates a mock container directory and PID file.
// - Expected Outcome: Returns "Running" if the process exists, otherwise "Stopped".
func TestGetContainerStatus(t *testing.T) {
// Setup: Create a mock container directory and PID file
baseDir := filepath.Join(os.TempDir(), "basic-docker")
containerDir := filepath.Join(baseDir, "containers", "test-container")
if err := os.MkdirAll(containerDir, 0755); err != nil {
t.Fatalf("Failed to create container directory: %v", err)
}
defer os.RemoveAll(baseDir) // Cleanup
pidFile := containerDir + "/pid"
pid := os.Getpid() // Use the current process PID for testing
if err := os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
t.Fatalf("Failed to create PID file: %v", err)
}
// Test: Check the status of the container
status := getContainerStatus("test-container")
if status != "Running" {
t.Errorf("Expected status 'Running', but got '%s'", status)
}
// Cleanup: Remove the PID file to simulate a stopped container
os.Remove(pidFile)
// Test: Check the status again
status = getContainerStatus("test-container")
if status != "Stopped" {
t.Errorf("Expected status 'Stopped', but got '%s'", status)
}
}
// TestCapsuleManager:
// - Verifies the CapsuleManager's functionality, including adding, retrieving, and attaching Resource Capsules.
// - Setup: Initializes a CapsuleManager instance.
// - Expected Outcome: Capsules are added, retrieved, and attached correctly.
func TestCapsuleManager(t *testing.T) {
cm := NewCapsuleManager()
// Add a capsule
cm.AddCapsule("libssl", "1.1.1", "/usr/lib/libssl.so")
// Retrieve the capsule
capsule, exists := cm.GetCapsule("libssl", "1.1.1")
if !exists {
t.Fatalf("Expected capsule libssl:1.1.1 to exist")
}
if capsule.Name != "libssl" || capsule.Version != "1.1.1" || capsule.Path != "/usr/lib/libssl.so" {
t.Errorf("Capsule data mismatch: got %+v", capsule)
}
// Attach the capsule to a container
err := cm.AttachCapsule("container-1234", "libssl", "1.1.1")
if err != nil {
t.Errorf("Failed to attach capsule: %v", err)
}
// Try to attach a non-existent capsule
err = cm.AttachCapsule("container-1234", "libcrypto", "1.0.0")
if err == nil {
t.Errorf("Expected error when attaching non-existent capsule, got nil")
}
}
// TestPing verifies that containers in the same network can communicate
func TestPing(t *testing.T) {
// Cleanup: Ensure no existing networks or containers interfere with the test
networks = []Network{}
saveNetworks()
// Setup: Create a network and attach two containers
networkName := "test-network"
CreateNetwork(networkName)
networkID := networks[0].ID
container1 := "container-1"
container2 := "container-2"
err := AttachContainerToNetwork(networkID, container1)
if err != nil {
t.Fatalf("Failed to attach container 1: %v", err)
}
err = AttachContainerToNetwork(networkID, container2)
if err != nil {
t.Fatalf("Failed to attach container 2: %v", err)
}
err = Ping(networkID, container1, container2)
if err != nil {
t.Errorf("Ping failed: %v", err)
}
// Create a second network and a third container
networkName2 := "test-network-2"
CreateNetwork(networkName2)
networkID2 := networks[1].ID
container3 := "container-3"
err = AttachContainerToNetwork(networkID2, container3)
if err != nil {
t.Fatalf("Failed to attach container 3 to network 2: %v", err)
}
// Test ping between container1 and container3 (should fail)
err = Ping(networkID, container1, container3)
if err == nil {
t.Errorf("Ping succeeded but was expected to fail between container1 and container3")
} else {
t.Logf("Ping failed as expected between container1 and container3: %v", err)
}
}
// Abstract Docker commands into helper functions
func dockerInspect(containerName string) (string, error) {
cmd := exec.Command("docker", "inspect", containerName)
output, err := cmd.CombinedOutput()
return string(output), err
}
func dockerPs() (string, error) {
cmd := exec.Command("docker", "ps", "-a")
output, err := cmd.CombinedOutput()
return string(output), err
}
func dockerRm(containerName string) error {
cmd := exec.Command("docker", "rm", "-f", containerName)
_, err := cmd.CombinedOutput()
return err
}
// Refactor cleanup logic into a reusable function
func cleanupDockerResources(targetPath, containerName string) {
os.Remove(targetPath)
dockerRm(containerName)
}
// Update TestAddResourceCapsuleWithDockerPsAndInspect to use helper functions
func TestAddResourceCapsuleWithDockerPsAndInspect(t *testing.T) {
dockerCapsulePath := "/tmp/docker-capsule"
os.WriteFile(dockerCapsulePath, []byte("dummy data"), 0644)
defer os.Remove(dockerCapsulePath)
err := AddResourceCapsule("docker", "test-capsule", "1.0", dockerCapsulePath)
if err != nil {
t.Errorf("Failed to add resource capsule to Docker: %v. Capsule Path: %s", err, dockerCapsulePath)
}
dockerTargetPath := filepath.Join(baseDir, "containers", "test-capsule-1.0")
if _, err := os.Lstat(dockerTargetPath); os.IsNotExist(err) {
t.Errorf("Expected symbolic link not created for Docker capsule at %s. Error: %v", dockerTargetPath, err)
}
containerName := "test-container-test-capsule"
inspectOutput, inspectErr := dockerInspect(containerName)
if inspectErr != nil {
t.Logf("'docker inspect' failed: %v\nOutput: %s\n", inspectErr, inspectOutput)
t.Errorf("Failed to fetch 'docker inspect' output for container %s", containerName)
} else {
t.Logf("'docker inspect' output:\n%s\n", inspectOutput)
}
expectedBindPath := dockerTargetPath
if !contains(inspectOutput, expectedBindPath) {
t.Errorf("Expected mounted capsule %s not found in 'docker inspect' output for container %s", expectedBindPath, containerName)
}
defer cleanupDockerResources(dockerTargetPath, containerName)
}
// BenchmarkCapsuleAccess benchmarks the access time for Resource Capsules.
func BenchmarkCapsuleAccess(b *testing.B) {
cm := NewCapsuleManager()
cm.AddCapsule("libssl", "1.1.1", "/usr/lib/libssl.so")
for i := 0; i < b.N; i++ {
_, exists := cm.GetCapsule("libssl", "1.1.1")
if !exists {
b.Fatalf("Capsule not found")
}
}
}
// BenchmarkVolumeAccess benchmarks the access time for Docker volumes.
func BenchmarkVolumeAccess(b *testing.B) {
volumePath := "/tmp/docker-volume/libssl.so"
os.WriteFile(volumePath, []byte("dummy data"), 0644)
defer os.Remove(volumePath)
for i := 0; i < b.N; i++ {
_, err := os.Stat(volumePath)
if err != nil {
b.Fatalf("Volume not found: %v", err)
}
}
}
// BenchmarkDynamicAttachment benchmarks the dynamic attachment of Resource Capsules.
func BenchmarkDynamicAttachment(b *testing.B) {
cm := NewCapsuleManager()
cm.AddCapsule("libssl", "1.1.1", "/usr/lib/libssl.so")
for i := 0; i < b.N; i++ {
err := cm.AttachCapsule("container-1234", "libssl", "1.1.1")
if err != nil {
b.Fatalf("Failed to attach capsule: %v", err)
}
}
}
// BenchmarkVolumeAttachment benchmarks the dynamic attachment of Docker volumes.
func BenchmarkVolumeAttachment(b *testing.B) {
volumePath := "/tmp/docker-volume/libssl.so"
// Ensure the directory exists before creating the file
os.MkdirAll(filepath.Dir(volumePath), 0755)
os.WriteFile(volumePath, []byte("dummy data"), 0644)
defer os.Remove(volumePath)
for i := 0; i < b.N; i++ {
// Simulate volume attachment by checking its existence
_, err := os.Stat(volumePath)
if err != nil {
b.Fatalf("Volume not found: %v", err)
}
}
}
// TestNetworkPingCLI tests the network-ping CLI command functionality
func TestNetworkPingCLI(t *testing.T) {
// Cleanup: Ensure no existing networks interfere with the test
networks = []Network{}
saveNetworks()
// Setup: Create a network and attach two containers
networkName := "test-cli-network"
CreateNetwork(networkName)
networkID := networks[0].ID
container1 := "cli-container-1"
container2 := "cli-container-2"
err := AttachContainerToNetwork(networkID, container1)
if err != nil {
t.Fatalf("Failed to attach container 1: %v", err)
}
err = AttachContainerToNetwork(networkID, container2)
if err != nil {
t.Fatalf("Failed to attach container 2: %v", err)
}
// Test successful ping - this directly tests the CLI function call
err = Ping(networkID, container1, container2)
if err != nil {
t.Errorf("CLI ping failed for containers in same network: %v", err)
}
// Test ping with non-existent container
err = Ping(networkID, container1, "non-existent-container")
if err == nil {
t.Errorf("Expected CLI ping to fail for non-existent container, but it succeeded")
}
// Test ping with non-existent network
err = Ping("non-existent-network", container1, container2)
if err == nil {
t.Errorf("Expected CLI ping to fail for non-existent network, but it succeeded")
}
}