77 "os"
88 "os/exec"
99 "path/filepath"
10+ "strconv"
1011 "strings"
1112 "testing"
1213 "time"
@@ -37,29 +38,15 @@ func findProjectRoot(t *testing.T) string {
3738 }
3839}
3940
40- // getNamespaceName gets the single network namespace name
41- // Fails if there are 0 or multiple namespaces
42- func getNamespaceName (t * testing.T ) string {
43- cmd := exec .Command ("ip" , "netns" , "list" )
41+ func getChildProcessPID (t * testing.T ) int {
42+ cmd := exec .Command ("pgrep" , "-f" , "boundary-test" , "-n" )
4443 output , err := cmd .Output ()
45- require .NoError (t , err , "Failed to list network namespaces" )
46-
47- lines := strings .Split (string (output ), "\n " )
48- var namespaces []string
49-
50- for _ , line := range lines {
51- line = strings .TrimSpace (line )
52- if line != "" {
53- // Extract namespace name (first field)
54- parts := strings .Fields (line )
55- if len (parts ) > 0 {
56- namespaces = append (namespaces , parts [0 ])
57- }
58- }
59- }
44+ require .NoError (t , err )
6045
61- require .Len (t , namespaces , 1 , "Expected exactly one network namespace, found %d: %v" , len (namespaces ), namespaces )
62- return namespaces [0 ]
46+ pidStr := strings .TrimSpace (string (output ))
47+ pid , err := strconv .Atoi (pidStr )
48+ require .NoError (t , err )
49+ return pid
6350}
6451
6552func TestBoundaryIntegration (t * testing.T ) {
@@ -73,18 +60,18 @@ func TestBoundaryIntegration(t *testing.T) {
7360 require .NoError (t , err , "Failed to build boundary binary" )
7461
7562 // Create context for boundary process
76- ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
63+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
7764 defer cancel ()
7865
7966 // Start boundary process with sudo
8067 boundaryCmd := exec .CommandContext (ctx , "/tmp/boundary-test" ,
8168 "--allow" , "dev.coder.com" ,
8269 "--allow" , "jsonplaceholder.typicode.com" ,
8370 "--log-level" , "debug" ,
84- "--" , "bash" , "-c" , "sleep 10 && echo 'Test completed'" )
71+ "--" , "/bin/ bash" , "-c" , "/usr/bin/ sleep 10 && /usr/bin/ echo 'Test completed'" )
8572
86- // Suppress output to prevent terminal corruption
87- boundaryCmd .Stdout = os .Stdout // Let it go to /dev/null
73+ boundaryCmd . Stdin = os . Stdin
74+ boundaryCmd .Stdout = os .Stdout
8875 boundaryCmd .Stderr = os .Stderr
8976
9077 // Start the process
@@ -94,13 +81,13 @@ func TestBoundaryIntegration(t *testing.T) {
9481 // Give boundary time to start
9582 time .Sleep (2 * time .Second )
9683
97- // Get the namespace name that boundary created
98- namespaceName := getNamespaceName ( t )
84+ pidInt := getChildProcessPID ( t )
85+ pid := fmt . Sprintf ( "%v" , pidInt )
9986
10087 // Test HTTP request through boundary (from inside the jail)
10188 t .Run ("HTTPRequestThroughBoundary" , func (t * testing.T ) {
10289 // Run curl directly in the namespace using ip netns exec
103- curlCmd := exec .Command ("sudo" , "ip " , "netns " , "exec " , namespaceName ,
90+ curlCmd := exec .Command ("sudo" , "nsenter " , "-t " , pid , "-n " , "--" ,
10491 "curl" , "http://jsonplaceholder.typicode.com/todos/1" )
10592
10693 // Capture stderr separately
@@ -128,7 +115,7 @@ func TestBoundaryIntegration(t *testing.T) {
128115 certPath := fmt .Sprintf ("%v/ca-cert.pem" , configDir )
129116
130117 // Run curl directly in the namespace using ip netns exec
131- curlCmd := exec .Command ("sudo" , "ip " , "netns " , "exec " , namespaceName ,
118+ curlCmd := exec .Command ("sudo" , "sudo " , "nsenter " , "-t " , pid , "-n" , "--" ,
132119 "env" , fmt .Sprintf ("SSL_CERT_FILE=%v" , certPath ), "curl" , "-s" , "https://dev.coder.com/api/v2" )
133120
134121 // Capture stderr separately
@@ -149,7 +136,7 @@ func TestBoundaryIntegration(t *testing.T) {
149136 // Test blocked domain (from inside the jail)
150137 t .Run ("BlockedDomainTest" , func (t * testing.T ) {
151138 // Run curl directly in the namespace using ip netns exec
152- curlCmd := exec .Command ("sudo" , "ip " , "netns " , "exec " , namespaceName ,
139+ curlCmd := exec .Command ("sudo" , "sudo " , "nsenter " , "-t " , pid , "-n" , "--" ,
153140 "curl" , "-s" , "http://example.com" )
154141
155142 // Capture stderr separately
@@ -163,6 +150,11 @@ func TestBoundaryIntegration(t *testing.T) {
163150 require .Contains (t , string (output ), "Request Blocked by Boundary" )
164151 })
165152
153+ // Gracefully close process, call cleanup methods
154+ err = boundaryCmd .Process .Signal (os .Interrupt )
155+ require .NoError (t , err , "Failed to interrupt boundary process" )
156+ time .Sleep (time .Second * 1 )
157+
166158 // Clean up
167159 cancel () // This will terminate the boundary process
168160 err = boundaryCmd .Wait () // Wait for process to finish
0 commit comments