-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
64 lines (56 loc) · 1.85 KB
/
container_test.go
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
package main
import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
)
func TestDockerComposeUpAttachAndBuild(t *testing.T) {
// 1. Build the Docker image (if not already built)
cmd := exec.Command("make", "compose-build")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to build Docker image: %s\n%s", err, output)
}
// 2. Bring up Docker Compose environment
cmd = exec.Command("make", "up")
err = cmd.Start() // Start in the background
if err != nil {
t.Fatalf("Failed to start Docker Compose: %s", err)
}
defer func() {
// 7. Stop and remove the containers
cmd := exec.Command("make", "down")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to tear down Docker Compose: %s\n%s", err, output)
}
}()
// 3. Wait for containers to stabilize
time.Sleep(5 * time.Second)
// 4. Read logs from a container
cmd = exec.Command("docker", "logs", "mosaicfs-node2")
output, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to attach to container: %s\n%s", err, output)
}
// 5. Verification steps:
nodes := []string{"node1", "node2", "node3"}
expectedLog := "starting server..."
connectedLog := "Connected with remote: "
for _, node := range nodes {
cmd := exec.Command("docker", "logs", fmt.Sprintf("mosaicfs-%s", node))
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to get logs for container mosaicfs-%s: %s\n%s", node, err, output)
}
// Check for expected log messages
if !strings.Contains(string(output), expectedLog) {
t.Errorf("Expected log message '%s' not found in mosaicfs-%s logs:\n%s", expectedLog, node, output)
}
if !strings.Contains(string(output), connectedLog) {
t.Errorf("Expected log message '%s' not found in mosaicfs-%s logs:\n%s", connectedLog, node, output)
}
}
}