Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

16.3 KB
Raw Blame
// Copyright 2025 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package runtime

import (
	"context"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	goruntime "runtime"

	"github.com/alibaba/opensandbox/execd/pkg/jupyter/execute"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCommandOutputTail_PreservesCRAndLF(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "stdout.log")

	initial := "line1\nprog 10%\rprog 20%\rprog 30%\nlast\n"
	require.NoError(t, os.WriteFile(logFile, []byte(initial), 0o644))

	var got []string
	var tail commandOutputTail
	tail.read(logFile, func(s string) { got = append(got, s) }, false)

	want := []string{"line1\n", "prog 10%\r", "prog 20%\r", "prog 30%\n", "last\n"}
	require.Len(t, got, len(want))
	for i := range want {
		require.Equal(t, want[i], got[i], "token[%d] mismatch", i)
	}
	require.Equal(t, initial, strings.Join(got, ""))

	appendPart := "tail1\r\ntail2\n"
	f, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0o644)
	require.NoError(t, err)
	_, err = f.WriteString(appendPart)
	require.NoError(t, err, "append write")
	_ = f.Close()

	got = got[:0]
	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	want = []string{"tail1\r", "\n", "tail2\n"}
	require.Len(t, got, len(want))
	for i := range want {
		require.Equal(t, want[i], got[i], "incremental token[%d] mismatch", i)
	}
	require.Equal(t, appendPart, strings.Join(got, ""))
}

func TestCommandOutputTail_LongLine(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "stdout.log")

	// construct a single line larger than the default 64KB, but under 5MB
	longLine := strings.Repeat("x", 256*1024) + "\n"
	require.NoError(t, os.WriteFile(logFile, []byte(longLine), 0o644))

	var got []string
	var tail commandOutputTail
	tail.read(logFile, func(s string) { got = append(got, s) }, false)

	require.Len(t, got, 1, "expected one token")
	require.Equal(t, longLine, got[0], "long line mismatch")
}

func TestCommandOutputTail_FlushesTrailingLine(t *testing.T) {
	tmpDir := t.TempDir()
	file := filepath.Join(tmpDir, "stdout.log")
	content := []byte("line1\nlastline-without-newline")
	err := os.WriteFile(file, content, 0o644)
	assert.NoError(t, err)

	var tail commandOutputTail
	var lines []string
	onExecute := func(text string) {
		lines = append(lines, text)
	}

	tail.read(file, onExecute, false)
	assert.Equal(t, int64(len(content)), tail.offset)
	assert.Equal(t, []string{"line1\n"}, lines)

	tail.read(file, onExecute, true)
	assert.Equal(t, []string{"line1\n", "lastline-without-newline"}, lines)
	assert.Equal(t, string(content), strings.Join(lines, ""))
	tail.read(file, onExecute, true)
	assert.Len(t, lines, 2, "final flush must not duplicate output")
	assert.Zero(t, tail.pending.Cap())
}

func TestCommandOutputTail_PreservesBlankLines(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "stdout.log")

	// Mix of single newlines, consecutive blank lines, leading blank, and CRLF.
	initial := "a\n\nb\n\n\nc\n\r\nd\n"
	require.NoError(t, os.WriteFile(logFile, []byte(initial), 0o644))

	var got []string
	var tail commandOutputTail
	tail.read(logFile, func(s string) { got = append(got, s) }, false)

	want := []string{"a\n", "\n", "b\n", "\n", "\n", "c\n", "\r", "\n", "d\n"}
	require.Equal(t, want, got)
	require.Equal(t, initial, strings.Join(got, ""))
}

// TestCommandOutputTail_PreservesCRLFAcrossPolls checks that a CRLF pair
// remains intact when its bytes arrive in successive polls.
func TestCommandOutputTail_PreservesCRLFAcrossPolls(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "stdout.log")

	require.NoError(t, os.WriteFile(logFile, []byte("a\r"), 0o644))

	var got []string
	var tail commandOutputTail
	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	require.Equal(t, []string{"a\r"}, got)
	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	require.Equal(t, []string{"a\r"}, got)

	f, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0o644)
	require.NoError(t, err)
	_, err = f.WriteString("\nb\n")
	require.NoError(t, err)
	_ = f.Close()

	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	require.Equal(t, []string{"a\r", "\n", "b\n"}, got)
	require.Equal(t, "a\r\nb\n", strings.Join(got, ""))
}

// TestCommandOutputTail_PreservesBlankCRLFAcrossPolls checks an empty CRLF line
// split across polls retains both bytes.
func TestCommandOutputTail_PreservesBlankCRLFAcrossPolls(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "stdout.log")

	require.NoError(t, os.WriteFile(logFile, []byte("\r"), 0o644))

	var got []string
	var tail commandOutputTail
	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	require.Equal(t, []string{"\r"}, got)

	f, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0o644)
	require.NoError(t, err)
	_, err = f.WriteString("\n")
	require.NoError(t, err)
	_ = f.Close()

	tail.read(logFile, func(s string) { got = append(got, s) }, false)
	require.Equal(t, []string{"\r", "\n"}, got)
	require.Equal(t, "\r\n", strings.Join(got, ""))
}

func TestCommandOutputTail_RetainsGrowingLine(t *testing.T) {
	path := filepath.Join(t.TempDir(), "stdout.log")
	file, err := os.Create(path)
	require.NoError(t, err)
	defer file.Close()

	var tail commandOutputTail
	var got []string
	emit := func(s string) { got = append(got, s) }
	chunk := strings.Repeat("x", 64*1024)
	for i := 1; i <= 4; i++ {
		_, err := file.WriteString(chunk)
		require.NoError(t, err)
		for poll := 0; poll < 3; poll++ {
			tail.read(path, emit, false)
			require.Equal(t, int64(i*len(chunk)), tail.offset)
			require.Equal(t, strings.Repeat(chunk, i), tail.pending.String())
			require.Empty(t, got)
		}
	}
	// A failed open must leave the unfinished output intact.
	tail.read(path+".missing", emit, false)
	require.Equal(t, int64(4*len(chunk)), tail.offset)
	require.Equal(t, strings.Repeat(chunk, 4), tail.pending.String())

	_, err = file.WriteString("\nnext\n")
	require.NoError(t, err)
	tail.read(path, emit, false)
	require.Equal(t, []string{strings.Repeat(chunk, 4) + "\n", "next\n"}, got)
	require.Zero(t, tail.pending.Cap())
	tail.read(path, emit, true)
	require.Len(t, got, 2)
}

func TestCommandOutputTail_ReleasesCompletedLineCapacity(t *testing.T) {
	path := filepath.Join(t.TempDir(), "stdout.log")
	longLine := strings.Repeat("x", 1<<20)
	require.NoError(t, os.WriteFile(path, []byte(longLine+"\nprompt"), 0o600))

	var tail commandOutputTail
	var got []string
	emit := func(s string) { got = append(got, s) }
	tail.read(path, emit, false)
	require.Equal(t, []string{longLine + "\n"}, got)
	require.Equal(t, "prompt", tail.pending.String())
	require.LessOrEqual(t, tail.pending.Cap(), 4096, "a short fragment must not retain the completed line's storage")

	tail.read(path, emit, false)
	require.Len(t, got, 1)
	file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o600)
	require.NoError(t, err)
	_, err = file.WriteString(" continued\n")
	require.NoError(t, err)
	require.NoError(t, file.Close())
	tail.read(path, emit, true)
	require.Equal(t, []string{longLine + "\n", "prompt continued\n"}, got)
	require.Equal(t, int64(len(longLine+"\nprompt continued\n")), tail.offset)
	require.Zero(t, tail.pending.Cap())
}

func TestRunCommand_Echo(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("bash not available on windows")
	}
	requireBash(t)

	c := NewController("", "")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var (
		sessionID   string
		stdoutLines []string
		stderrLines []string
		completeCh  = make(chan struct{}, 1)
	)

	req := &ExecuteCodeRequest{
		Code:    `echo "hello"; echo "errline" 1>&2`,
		Cwd:     t.TempDir(),
		Timeout: 5 * time.Second,
		Hooks: ExecuteResultHook{
			OnExecuteInit: func(s string) { sessionID = s },
			OnExecuteStdout: func(s string) {
				stdoutLines = append(stdoutLines, s)
			},
			OnExecuteStderr: func(s string) {
				stderrLines = append(stderrLines, s)
			},
			OnExecuteError: func(err *execute.ErrorOutput) {
				require.Failf(t, "unexpected error hook", "%+v", err)
			},
			OnExecuteComplete: func(_ time.Duration) {
				completeCh <- struct{}{}
			},
		},
	}

	require.NoError(t, c.runCommand(ctx, req))

	select {
	case <-completeCh:
	case <-time.After(2 * time.Second):
		require.Fail(t, "timeout waiting for completion hook")
	}

	require.NotEmpty(t, sessionID, "expected session id to be set")
	require.Equal(t, []string{"hello\n"}, stdoutLines)
	require.Equal(t, []string{"errline\n"}, stderrLines)
}

func TestRunCommand_Error(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("bash not available on windows")
	}
	requireBash(t)

	c := NewController("", "")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var (
		sessionID   string
		gotErr      *execute.ErrorOutput
		completeCh  = make(chan struct{}, 2)
		stdoutLines []string
		stderrLines []string
	)

	req := &ExecuteCodeRequest{
		Code:    `echo "before"; exit 3`,
		Cwd:     t.TempDir(),
		Timeout: 5 * time.Second,
		Hooks: ExecuteResultHook{
			OnExecuteInit:   func(s string) { sessionID = s },
			OnExecuteStdout: func(s string) { stdoutLines = append(stdoutLines, s) },
			OnExecuteStderr: func(s string) { stderrLines = append(stderrLines, s) },
			OnExecuteError: func(err *execute.ErrorOutput) {
				gotErr = err
				completeCh <- struct{}{}
			},
			OnExecuteComplete: func(_ time.Duration) {
				completeCh <- struct{}{}
			},
		},
	}

	require.NoError(t, c.runCommand(ctx, req))

	select {
	case <-completeCh:
	case <-time.After(2 * time.Second):
		require.Fail(t, "timeout waiting for completion hook")
	}

	require.NotEmpty(t, sessionID, "expected session id to be set")
	require.Equal(t, []string{"before\n"}, stdoutLines)
	require.Empty(t, stderrLines, "expected no stderr")
	require.NotNil(t, gotErr, "expected error hook to be called")
	require.Equal(t, "CommandExecError", gotErr.EName)
	require.Equal(t, "3", gotErr.EValue)
	require.NotNil(t, gotErr.ExitCode, "expected exit_code for a command that ran")
	require.Equal(t, 3, *gotErr.ExitCode)
}

func TestRunCommand_ExpandsHomeInCwd(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("bash not available on windows")
	}
	requireBash(t)

	home := t.TempDir()
	target := filepath.Join(home, "workspace")
	require.NoError(t, os.MkdirAll(target, 0o755))
	t.Setenv("HOME", home)
	t.Setenv("USERPROFILE", home)

	c := NewController("", "")
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var stdoutLines []string
	req := &ExecuteCodeRequest{
		Code:    `pwd`,
		Cwd:     "~/workspace",
		Timeout: 5 * time.Second,
		Hooks: ExecuteResultHook{
			OnExecuteInit:   func(_ string) {},
			OnExecuteStdout: func(s string) { stdoutLines = append(stdoutLines, s) },
			OnExecuteStderr: func(_ string) {},
			OnExecuteError: func(err *execute.ErrorOutput) {
				require.Failf(t, "unexpected error hook", "%+v", err)
			},
			OnExecuteComplete: func(_ time.Duration) {},
		},
	}

	require.NoError(t, c.runCommand(ctx, req))

	targetRealPath, err := filepath.EvalSymlinks(target)
	require.NoError(t, err)
	targetRealPath = filepath.Clean(targetRealPath)

	found := false
	for _, line := range stdoutLines {
		p := strings.TrimSpace(line)
		if p == "" {
			continue
		}
		pRealPath, err := filepath.EvalSymlinks(p)
		if err != nil {
			continue
		}
		if filepath.Clean(pRealPath) == targetRealPath {
			found = true
			break
		}
	}
	require.True(t, found, "pwd output does not match expected cwd; got=%v target=%s", stdoutLines, target)
}

func TestRunCommand_ExpandsCwdFromRequestEnvWithHigherPriority(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("bash not available on windows")
	}
	requireBash(t)

	processDir := t.TempDir()
	requestDir := t.TempDir()
	t.Setenv("WORKDIR", processDir)

	c := NewController("", "")
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var (
		stdoutLines []string
		gotErr      *execute.ErrorOutput
	)
	req := &ExecuteCodeRequest{
		Code:    `pwd`,
		Cwd:     `$WORKDIR`,
		Timeout: 5 * time.Second,
		Envs: map[string]string{
			"WORKDIR": requestDir,
		},
		Hooks: ExecuteResultHook{
			OnExecuteInit:   func(_ string) {},
			OnExecuteStdout: func(s string) { stdoutLines = append(stdoutLines, s) },
			OnExecuteStderr: func(_ string) {},
			OnExecuteError: func(err *execute.ErrorOutput) {
				gotErr = err
			},
			OnExecuteComplete: func(_ time.Duration) {},
		},
	}

	require.NoError(t, c.runCommand(ctx, req))
	require.Nil(t, gotErr, "expected cwd expansion to use request env")

	requestRealPath, err := filepath.EvalSymlinks(requestDir)
	require.NoError(t, err)
	requestRealPath = filepath.Clean(requestRealPath)

	found := false
	for _, line := range stdoutLines {
		p := strings.TrimSpace(line)
		if p == "" {
			continue
		}
		pRealPath, err := filepath.EvalSymlinks(p)
		if err != nil {
			continue
		}
		if filepath.Clean(pRealPath) == requestRealPath {
			found = true
			break
		}
	}
	require.True(t, found, "pwd output does not match request env cwd; got=%v requestDir=%s", stdoutLines, requestDir)
}

func TestRunCommand_StartErrorIncludesTraceback(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("bash not available on windows")
	}
	requireBash(t)

	c := NewController("", "")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var (
		sessionID      string
		gotErr         *execute.ErrorOutput
		completeCalled bool
	)

	req := &ExecuteCodeRequest{
		Code:    `echo "hello"`,
		Cwd:     filepath.Join(t.TempDir(), "missing"),
		Timeout: 5 * time.Second,
		Hooks: ExecuteResultHook{
			OnExecuteInit: func(s string) { sessionID = s },
			OnExecuteError: func(err *execute.ErrorOutput) {
				gotErr = err
			},
			OnExecuteComplete: func(_ time.Duration) {
				completeCalled = true
			},
		},
	}

	require.NoError(t, c.runCommand(ctx, req))

	require.NotEmpty(t, sessionID, "expected session id to be set")
	require.NotNil(t, gotErr, "expected error hook to be called")
	require.Equal(t, "CommandExecError", gotErr.EName)
	require.NotEmpty(t, gotErr.Traceback, "expected traceback to be populated")
	require.Equal(t, gotErr.EValue, gotErr.Traceback[0])
	require.Nil(t, gotErr.ExitCode, "start failure must not report an exit_code")
	require.False(t, completeCalled, "did not expect completion hook on start failure")
}

// TestStdLogDescriptor_AutoCreatesTempDir verifies that stdLogDescriptor
// recreates the temp directory when it has been deleted, rather than failing.
// Regression test for https://github.com/alibaba/OpenSandbox/issues/400.
func TestStdLogDescriptor_AutoCreatesTempDir(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("TMPDIR env var has no effect on Windows")
	}

	// Point os.TempDir() at a path that does not yet exist.
	missingDir := filepath.Join(t.TempDir(), "deleted_tmp")
	t.Setenv("TMPDIR", missingDir)

	c := NewController("", "")
	stdout, stderr, err := c.stdLogDescriptor("test-session")
	require.NoError(t, err)
	stdout.Close()
	stderr.Close()

	info, err := os.Stat(missingDir)
	require.NoError(t, err, "expected temp dir to be created, stat error")
	require.True(t, info.IsDir(), "expected %s to be a directory", missingDir)
}

// TestCombinedOutputDescriptor_AutoCreatesTempDir verifies that
// combinedOutputDescriptor also recreates the temp directory when missing.
// Regression test for https://github.com/alibaba/OpenSandbox/issues/400.
func TestCombinedOutputDescriptor_AutoCreatesTempDir(t *testing.T) {
	if goruntime.GOOS == "windows" {
		t.Skip("TMPDIR env var has no effect on Windows")
	}

	missingDir := filepath.Join(t.TempDir(), "deleted_tmp")
	t.Setenv("TMPDIR", missingDir)

	c := NewController("", "")
	f, err := c.combinedOutputDescriptor("test-session")
	require.NoError(t, err)
	f.Close()

	info, err := os.Stat(missingDir)
	require.NoError(t, err, "expected temp dir to be created, stat error")
	require.True(t, info.IsDir(), "expected %s to be a directory", missingDir)
}