@@ -209,19 +209,34 @@ func TestContextCancellation(t *testing.T) {
209209 }
210210}
211211
212- func skipIfNoBubblewrap (t * testing.T ) {
212+ func skipIfNoBubblewrap (t * testing.T ) string {
213213 t .Helper ()
214214 if runtime .GOOS != "linux" {
215215 t .Skip ("bubblewrap tests only run on Linux" )
216216 }
217- if _ , err := exec .LookPath ("bwrap" ); err != nil {
217+ bwrapPath , err := exec .LookPath ("bwrap" )
218+ if err != nil {
218219 t .Skip ("bwrap not found in PATH" )
219220 }
221+
222+ // Bubblewrap can be present but unusable in restricted environments.
223+ probeCtx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
224+ defer cancel ()
225+ probeCmd := exec .CommandContext (probeCtx , bwrapPath , "--unshare-user" , "--ro-bind" , "/" , "/" , "--" , "true" )
226+ out , err := probeCmd .CombinedOutput ()
227+ if err != nil {
228+ msg := strings .TrimSpace (string (out ))
229+ if msg == "" {
230+ msg = err .Error ()
231+ }
232+ t .Skipf ("bwrap found but unusable in this environment: %s" , msg )
233+ }
234+
235+ return bwrapPath
220236}
221237
222- func newBubblewrapParams (t * testing.T , args ... string ) runner.RunnerParams {
238+ func newBubblewrapParams (t * testing.T , bwrapPath string , args ... string ) runner.RunnerParams {
223239 t .Helper ()
224- bwrapPath , _ := exec .LookPath ("bwrap" )
225240 params := newTestParams (t , args ... )
226241 params .Sandbox = true
227242 params .BubblewrapParams = runner.BubblewrapParams {
@@ -231,10 +246,10 @@ func newBubblewrapParams(t *testing.T, args ...string) runner.RunnerParams {
231246}
232247
233248func TestBubblewrapBasicExecution (t * testing.T ) {
234- skipIfNoBubblewrap (t )
249+ bwrapPath := skipIfNoBubblewrap (t )
235250
236251 var stdout bytes.Buffer
237- params := newBubblewrapParams (t , "echo" , "hello" )
252+ params := newBubblewrapParams (t , bwrapPath , "echo" , "hello" )
238253 params .Stdout = & stdout
239254
240255 r , err := runner .GetRunner (params )
@@ -246,10 +261,10 @@ func TestBubblewrapBasicExecution(t *testing.T) {
246261}
247262
248263func TestBubblewrapArgumentPassing (t * testing.T ) {
249- skipIfNoBubblewrap (t )
264+ bwrapPath := skipIfNoBubblewrap (t )
250265
251266 var stdout bytes.Buffer
252- params := newBubblewrapParams (t , "echo" , "hello world" , "foo\t bar" , "baz\" qux" )
267+ params := newBubblewrapParams (t , bwrapPath , "echo" , "hello world" , "foo\t bar" , "baz\" qux" )
253268 params .Stdout = & stdout
254269
255270 r , err := runner .GetRunner (params )
@@ -265,10 +280,10 @@ func TestBubblewrapArgumentPassing(t *testing.T) {
265280}
266281
267282func TestBubblewrapStdoutStderr (t * testing.T ) {
268- skipIfNoBubblewrap (t )
283+ bwrapPath := skipIfNoBubblewrap (t )
269284
270285 var stdout , stderr bytes.Buffer
271- params := newBubblewrapParams (t , "output" , "stdout" , "out-msg" , "stderr" , "err-msg" )
286+ params := newBubblewrapParams (t , bwrapPath , "output" , "stdout" , "out-msg" , "stderr" , "err-msg" )
272287 params .Stdout = & stdout
273288 params .Stderr = & stderr
274289
@@ -282,12 +297,12 @@ func TestBubblewrapStdoutStderr(t *testing.T) {
282297}
283298
284299func TestBubblewrapContextCancellation (t * testing.T ) {
285- skipIfNoBubblewrap (t )
300+ bwrapPath := skipIfNoBubblewrap (t )
286301
287302 ctx , cancel := context .WithCancel (context .Background ())
288303 defer cancel ()
289304
290- params := newBubblewrapParams (t , "sleep" , "30000" )
305+ params := newBubblewrapParams (t , bwrapPath , "sleep" , "30000" )
291306 params .Ctx = ctx
292307
293308 r , err := runner .GetRunner (params )
@@ -303,8 +318,9 @@ func TestBubblewrapContextCancellation(t *testing.T) {
303318 cancel ()
304319
305320 select {
306- case <- done :
307- // Run returned promptly after cancellation
321+ case err := <- done :
322+ // Run returned promptly after cancellation and did not fail before cancellation.
323+ require .NoError (t , err )
308324 case <- time .After (5 * time .Second ):
309325 t .Fatal ("Run() did not return within 5 seconds after context cancellation" )
310326 }
0 commit comments