@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"io"
7
8
"log"
@@ -14,6 +15,7 @@ import (
14
15
"time"
15
16
16
17
"github.com/docker/docker/api/types/container"
18
+ "github.com/docker/docker/api/types/mount"
17
19
"github.com/docker/go-connections/nat"
18
20
"github.com/testcontainers/testcontainers-go"
19
21
"github.com/testcontainers/testcontainers-go/wait"
@@ -38,6 +40,45 @@ type fileInfo struct {
38
40
relPath string
39
41
}
40
42
43
+ func getDockerSocketPath () (string , error ) {
44
+ contextCmd := exec .Command ("docker" , "context" , "show" )
45
+ contextOutput , err := contextCmd .Output ()
46
+ if err != nil {
47
+ return "" , fmt .Errorf ("failed to get docker context: %w" , err )
48
+ }
49
+ contextName := strings .TrimSpace (string (contextOutput ))
50
+
51
+ inspectCmd := exec .Command ("docker" , "context" , "inspect" , contextName )
52
+ inspectOutput , err := inspectCmd .Output ()
53
+ if err != nil {
54
+ return "" , fmt .Errorf ("failed to inspect docker context %s: %w" , contextName , err )
55
+ }
56
+
57
+ var contexts []struct {
58
+ Endpoints struct {
59
+ Docker struct {
60
+ Host string `json:"Host"`
61
+ } `json:"docker"`
62
+ } `json:"Endpoints"`
63
+ }
64
+
65
+ if err := json .Unmarshal (inspectOutput , & contexts ); err != nil {
66
+ return "" , fmt .Errorf ("failed to parse docker context inspect output: %w" , err )
67
+ }
68
+
69
+ if len (contexts ) == 0 {
70
+ return "" , fmt .Errorf ("no docker contexts found" )
71
+ }
72
+
73
+ host := contexts [0 ].Endpoints .Docker .Host
74
+ if ! strings .HasPrefix (host , "unix://" ) {
75
+ return "" , fmt .Errorf ("docker host is not a unix socket: %s" , host )
76
+ }
77
+
78
+ socketPath := strings .TrimPrefix (host , "unix://" )
79
+ return socketPath , nil
80
+ }
81
+
41
82
func executeCommand (command string , args []string , taskDescription string ) {
42
83
fmt .Printf ("%s...\n " , taskDescription )
43
84
@@ -89,6 +130,9 @@ func prepareContainerFiles(absTestDir string) ([]testcontainers.ContainerFile, [
89
130
90
131
// Create a container request based on the test directory
91
132
func createContainerRequest (dirName string , port int , networkName string , containerFiles []testcontainers.ContainerFile ) testcontainers.ContainerRequest {
133
+ pyroscope := strings .Contains (dirName , "pyroscope" )
134
+ beyla := dirName == "beyla"
135
+
92
136
natPort , err := nat .NewPort ("tcp" , strconv .Itoa (port ))
93
137
if err != nil {
94
138
panic (fmt .Sprintf ("failed to build natPort: %v" , err ))
@@ -108,10 +152,19 @@ func createContainerRequest(dirName string, port int, networkName string, contai
108
152
},
109
153
Privileged : true ,
110
154
}
111
-
112
- // Apply special configurations for specific tests
113
- if dirName == "beyla" {
155
+ dockerSocketPath , err := getDockerSocketPath ()
156
+ if err != nil {
157
+ panic (fmt .Sprintf ("failed to get docker socket path: %v" , err ))
158
+ }
159
+ if beyla || pyroscope {
114
160
req .HostConfigModifier = func (hostConfig * container.HostConfig ) {
161
+ if pyroscope {
162
+ hostConfig .Mounts = append (hostConfig .Mounts , mount.Mount {
163
+ Type : mount .TypeBind ,
164
+ Source : dockerSocketPath ,
165
+ Target : "/host-docker.sock" ,
166
+ })
167
+ }
115
168
hostConfig .Privileged = true
116
169
hostConfig .CapAdd = []string {"SYS_ADMIN" , "SYS_PTRACE" , "SYS_RESOURCE" }
117
170
hostConfig .SecurityOpt = []string {"apparmor:unconfined" }
0 commit comments