Files
wehub-resource-sync e0e362d700
SDK Tests / SDK CI (push) Blocked by required conditions
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
SDK Tests / Python SDK Tests (sandbox) (push) Waiting to run
SDK Tests / CLI Quality (push) Waiting to run
SDK Tests / CLI Tests (push) Waiting to run
SDK Tests / Python SDK Quality (code-interpreter) (push) Waiting to run
SDK Tests / Python SDK Quality (sandbox) (push) Waiting to run
SDK Tests / Python SDK Tests (code-interpreter) (push) Waiting to run
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Go SDK Quality And Tests (push) Waiting to run
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:33 +08:00

135 lines
3.8 KiB
Go

// Copyright 2026 Alibaba Group Holding Ltd.
//
// 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 isolation
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
assert.Equal(t, "/var/lib/execd/isolation", cfg.UpperRoot)
assert.Equal(t, int64(8*1024*1024*1024), cfg.UpperMaxBytes)
assert.Equal(t, int64(4*1024*1024*1024), cfg.DiffMaxBytes)
assert.Equal(t, []string{"/workspace", "/mnt", "/media", "/data"}, cfg.AllowedWritable)
assert.Nil(t, cfg.Seccomp)
}
func TestLoadConfig_EmptyPath(t *testing.T) {
cfg, err := LoadConfig("")
require.NoError(t, err)
assert.Equal(t, DefaultConfig(), cfg)
}
func TestLoadConfig_FileNotExist(t *testing.T) {
cfg, err := LoadConfig("/nonexistent/path/isolation.toml")
require.NoError(t, err)
assert.Equal(t, DefaultConfig(), cfg)
}
func TestLoadConfig_Valid(t *testing.T) {
content := `
upper_root = "/data/isolation"
upper_max_bytes = 1073741824
diff_max_bytes = 536870912
allowed_writable = ["/tmp", "/var/run"]
[seccomp]
deny = ["mount", "ptrace"]
`
path := writeTempTOML(t, content)
cfg, err := LoadConfig(path)
require.NoError(t, err)
assert.Equal(t, "/data/isolation", cfg.UpperRoot)
assert.Equal(t, int64(1073741824), cfg.UpperMaxBytes)
assert.Equal(t, int64(536870912), cfg.DiffMaxBytes)
assert.Equal(t, []string{"/tmp", "/var/run"}, cfg.AllowedWritable)
require.NotNil(t, cfg.Seccomp)
assert.Equal(t, []string{"mount", "ptrace"}, cfg.Seccomp.Deny)
}
func TestLoadConfig_InvalidTOML(t *testing.T) {
path := writeTempTOML(t, `upper_root = [invalid`)
_, err := LoadConfig(path)
assert.Error(t, err)
assert.Contains(t, err.Error(), "parse")
}
func TestLoadConfig_PartialFields(t *testing.T) {
content := `upper_root = "/custom/path"`
path := writeTempTOML(t, content)
cfg, err := LoadConfig(path)
require.NoError(t, err)
assert.Equal(t, "/custom/path", cfg.UpperRoot)
// Missing fields keep defaults.
assert.Equal(t, int64(8*1024*1024*1024), cfg.UpperMaxBytes)
assert.Equal(t, int64(4*1024*1024*1024), cfg.DiffMaxBytes)
// Missing allowed_writable keeps the built-in default allowlist.
assert.Equal(t, []string{"/workspace", "/mnt", "/media", "/data"}, cfg.AllowedWritable)
assert.Nil(t, cfg.Seccomp)
}
func TestLoadConfig_SeccompReplace(t *testing.T) {
content := `
[seccomp]
deny = ["socket", "connect"]
`
path := writeTempTOML(t, content)
cfg, err := LoadConfig(path)
require.NoError(t, err)
require.NotNil(t, cfg.Seccomp)
assert.Equal(t, []string{"socket", "connect"}, cfg.Seccomp.Deny)
}
func TestLoadConfig_NoSeccomp(t *testing.T) {
content := `upper_root = "/data/iso"`
path := writeTempTOML(t, content)
cfg, err := LoadConfig(path)
require.NoError(t, err)
assert.Nil(t, cfg.Seccomp, "absent [seccomp] section should leave Seccomp nil")
}
func TestLoadConfig_EmptySeccompDeny(t *testing.T) {
content := `
[seccomp]
deny = []
`
path := writeTempTOML(t, content)
cfg, err := LoadConfig(path)
require.NoError(t, err)
require.NotNil(t, cfg.Seccomp, "[seccomp] present → non-nil even if deny is empty")
assert.Empty(t, cfg.Seccomp.Deny)
}
func writeTempTOML(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "isolation.toml")
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
return path
}