Skip to content

Commit 4274bfd

Browse files
refactor: e2e-framework (#116)
* docs: improve docs * refactor: refactor: e2e-framework * refactor: remove unused code
1 parent 75013a6 commit 4274bfd

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

e2e_tests/boundary_test.go

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package e2e_tests
22

33
import (
44
"bytes"
5-
"context"
65
"fmt"
76
"io"
87
"os"
@@ -24,12 +23,9 @@ type BoundaryTest struct {
2423
binaryPath string
2524
allowedDomains []string
2625
logLevel string
27-
ctx context.Context
28-
cancel context.CancelFunc
2926
cmd *exec.Cmd
3027
pid int
3128
startupDelay time.Duration
32-
commandTimeout time.Duration
3329
}
3430

3531
// BoundaryTestOption is a function that configures BoundaryTest
@@ -47,7 +43,6 @@ func NewBoundaryTest(t *testing.T, opts ...BoundaryTestOption) *BoundaryTest {
4743
allowedDomains: []string{},
4844
logLevel: "warn",
4945
startupDelay: 2 * time.Second,
50-
commandTimeout: 30 * time.Second,
5146
}
5247

5348
// Apply options
@@ -86,13 +81,6 @@ func WithStartupDelay(delay time.Duration) BoundaryTestOption {
8681
}
8782
}
8883

89-
// WithCommandTimeout sets the timeout for the boundary command
90-
func WithCommandTimeout(timeout time.Duration) BoundaryTestOption {
91-
return func(bt *BoundaryTest) {
92-
bt.commandTimeout = timeout
93-
}
94-
}
95-
9684
// Build builds the boundary binary
9785
func (bt *BoundaryTest) Build() *BoundaryTest {
9886
buildCmd := exec.Command("go", "build", "-o", bt.binaryPath, "./cmd/...")
@@ -109,8 +97,6 @@ func (bt *BoundaryTest) Start(command ...string) *BoundaryTest {
10997
command = []string{"/bin/bash", "-c", "/usr/bin/sleep 100 && /usr/bin/echo 'Root boundary process exited'"}
11098
}
11199

112-
bt.ctx, bt.cancel = context.WithTimeout(context.Background(), bt.commandTimeout)
113-
114100
// Build command args
115101
args := []string{
116102
"--log-level", bt.logLevel,
@@ -121,7 +107,7 @@ func (bt *BoundaryTest) Start(command ...string) *BoundaryTest {
121107
args = append(args, "--")
122108
args = append(args, command...)
123109

124-
bt.cmd = exec.CommandContext(bt.ctx, bt.binaryPath, args...)
110+
bt.cmd = exec.Command(bt.binaryPath, args...)
125111
bt.cmd.Stdin = os.Stdin
126112

127113
stdout, _ := bt.cmd.StdoutPipe()
@@ -136,7 +122,7 @@ func (bt *BoundaryTest) Start(command ...string) *BoundaryTest {
136122
time.Sleep(bt.startupDelay)
137123

138124
// Get the child process PID
139-
bt.pid = getChildProcessPID(bt.t)
125+
bt.pid = getTargetProcessPID(bt.t)
140126

141127
return bt
142128
}
@@ -155,11 +141,6 @@ func (bt *BoundaryTest) Stop() {
155141

156142
time.Sleep(1 * time.Second)
157143

158-
// Cancel context
159-
if bt.cancel != nil {
160-
bt.cancel()
161-
}
162-
163144
// Wait for process to finish
164145
if bt.cmd != nil {
165146
err = bt.cmd.Wait()
@@ -178,39 +159,35 @@ func (bt *BoundaryTest) Stop() {
178159
// ExpectAllowed makes an HTTP/HTTPS request and expects it to be allowed with the given response body
179160
func (bt *BoundaryTest) ExpectAllowed(url string, expectedBody string) {
180161
bt.t.Helper()
181-
output := bt.makeRequest(url, false)
162+
output := bt.makeRequest(url)
182163
require.Equal(bt.t, expectedBody, string(output), "Expected response body does not match")
183164
}
184165

185166
// ExpectAllowedContains makes an HTTP/HTTPS request and expects it to be allowed, checking that response contains the given text
186167
func (bt *BoundaryTest) ExpectAllowedContains(url string, containsText string) {
187168
bt.t.Helper()
188-
output := bt.makeRequest(url, false)
169+
output := bt.makeRequest(url)
189170
require.Contains(bt.t, string(output), containsText, "Response does not contain expected text")
190171
}
191172

192173
// ExpectDeny makes an HTTP/HTTPS request and expects it to be denied
193174
func (bt *BoundaryTest) ExpectDeny(url string) {
194175
bt.t.Helper()
195-
output := bt.makeRequest(url, false)
176+
output := bt.makeRequest(url)
196177
require.Contains(bt.t, string(output), "Request Blocked by Boundary", "Expected request to be blocked")
197178
}
198179

199180
// makeRequest makes an HTTP/HTTPS request from inside the namespace
200181
// Always sets SSL_CERT_FILE for HTTPS support (harmless for HTTP requests)
201-
func (bt *BoundaryTest) makeRequest(url string, silent bool) []byte {
182+
func (bt *BoundaryTest) makeRequest(url string) []byte {
202183
bt.t.Helper()
203184

204185
pid := fmt.Sprintf("%v", bt.pid)
205186
_, _, _, _, configDir := util.GetUserInfo()
206187
certPath := fmt.Sprintf("%v/ca-cert.pem", configDir)
207188

208189
args := []string{"nsenter", "-t", pid, "-n", "--",
209-
"env", fmt.Sprintf("SSL_CERT_FILE=%v", certPath), "curl"}
210-
if silent {
211-
args = append(args, "-s")
212-
}
213-
args = append(args, url)
190+
"env", fmt.Sprintf("SSL_CERT_FILE=%v", certPath), "curl", "-sS", url}
214191

215192
curlCmd := exec.Command("sudo", args...)
216193

@@ -225,8 +202,12 @@ func (bt *BoundaryTest) makeRequest(url string, silent bool) []byte {
225202
return output
226203
}
227204

228-
// getChildProcessPID gets the PID of the boundary child process
229-
func getChildProcessPID(t *testing.T) int {
205+
// getTargetProcessPID gets the PID of the boundary target process.
206+
// Target process is associated with a network namespace, so you can exec into it, using this PID.
207+
// pgrep -f boundary-test -n is doing two things:
208+
// -f = match against the full command line
209+
// -n = return the newest (most recently started) matching process
210+
func getTargetProcessPID(t *testing.T) int {
230211
cmd := exec.Command("pgrep", "-f", "boundary-test", "-n")
231212
output, err := cmd.Output()
232213
require.NoError(t, err)

0 commit comments

Comments
 (0)