Skip to content

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

8.7 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 main

import (
	"encoding/base64"
	"encoding/json"
	"errors"
	"strings"
	"testing"

	"github.com/alibaba/OpenSandbox/sandbox-k8s/internal/snapshot"
)

func TestGetImageDigestReturnsErrorOnInspectFailure(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })
	commandCombinedOutput = func(_ string, _ ...string) ([]byte, error) {
		return []byte("inspect failed"), errors.New("exit status 1")
	}

	digest, err := getImageDigest("registry.example.com/test/image:snap")

	if err == nil {
		t.Fatal("expected digest extraction error")
	}
	if digest != "" {
		t.Fatalf("expected empty digest on error, got %q", digest)
	}
}

func TestGetImageDigestReturnsErrorOnEmptyInspectOutput(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })
	commandCombinedOutput = func(_ string, _ ...string) ([]byte, error) {
		return []byte(" \n"), nil
	}

	digest, err := getImageDigest("registry.example.com/test/image:snap")

	if err == nil {
		t.Fatal("expected empty digest error")
	}
	if digest != "" {
		t.Fatalf("expected empty digest on error, got %q", digest)
	}
}

func TestGetImageDigestReturnsDigest(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })
	want := "sha256:" + strings.Repeat("a", 64)
	commandCombinedOutput = func(_ string, _ ...string) ([]byte, error) {
		return []byte(`[{"Image":{"Target":{"digest":"` + want + `"}}}]`), nil
	}

	digest, err := getImageDigest("registry.example.com/test/image:snap")

	if err != nil {
		t.Fatalf("expected digest extraction to succeed, got %v", err)
	}
	if digest != want {
		t.Fatalf("unexpected digest %q", digest)
	}
}

func TestGetImageDigestAcceptsDirectNativeTarget(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })
	want := "sha256:" + strings.Repeat("b", 64)
	commandCombinedOutput = func(_ string, _ ...string) ([]byte, error) {
		return []byte(`[{"Target":{"digest":"` + want + `"}}]`), nil
	}

	digest, err := getImageDigest("registry.example.com/test/image:snap")
	if err != nil {
		t.Fatalf("expected digest extraction to succeed, got %v", err)
	}
	if digest != want {
		t.Fatalf("unexpected digest %q", digest)
	}
}

func TestParseQEMUSnapshotRequest(t *testing.T) {
	request := snapshot.Request{
		Version:           snapshot.RequestVersionV1,
		PodName:           "sandbox-0",
		Namespace:         "default",
		Provider:          snapshot.ProviderQEMU,
		Containers:        []snapshot.ContainerTarget{{Name: "main", ImageURI: "registry/sandbox:snapshot"}},
		VMStateImageURI:   "registry/sandbox-vmstate:snapshot",
		LeaveSourceFrozen: true,
		QEMU:              &snapshot.QEMURequest{ContainerName: "main", QMPSocketPath: "/run/qemu/qmp.sock", LaunchManifestPath: "/run/qemu/launch.json"},
	}
	data, err := json.Marshal(request)
	if err != nil {
		t.Fatal(err)
	}
	parsed, err := parseSnapshotRequest([]string{"--request-base64", base64.StdEncoding.EncodeToString(data)})
	if err != nil {
		t.Fatal(err)
	}
	if parsed.QEMU == nil || parsed.QEMU.QMPSocketPath != "/run/qemu/qmp.sock" {
		t.Fatalf("unexpected parsed request: %#v", parsed)
	}
	if !parsed.LeaveSourceFrozen {
		t.Fatal("expected leaveSourceFrozen to survive request decoding")
	}
}

func TestPathWithinMountUsesPathBoundaries(t *testing.T) {
	for _, testCase := range []struct {
		path, mount string
		want        bool
	}{
		{"/var/lib/opensandbox/vm/disk.qcow2", "/var/lib/opensandbox", true},
		{"/ubuntu-storage2/disk.qcow2", "/ubuntu-storage", false},
		{"/ubuntu-storage/disk.qcow2", "/ubuntu-storage", true},
	} {
		if got := pathWithinMount(testCase.path, testCase.mount); got != testCase.want {
			t.Errorf("pathWithinMount(%q, %q)=%v, want %v", testCase.path, testCase.mount, got, testCase.want)
		}
	}
}

func TestValidateRootfsDiskCaptureRejectsWritableOverlayOnVolume(t *testing.T) {
	disks := []snapshot.QEMUDisk{{
		ID:          "osdisk",
		OverlayPath: "/ubuntu-storage/state.qcow2",
		Capture:     snapshot.QEMUDiskCaptureRootfs,
	}}

	identity := func(path string) (string, error) { return path, nil }
	if err := validateRootfsDiskCapture(disks, []string{"/ubuntu-storage"}, identity); err == nil {
		t.Fatal("expected mounted writable overlay to be rejected")
	}
	if err := validateRootfsDiskCapture(disks, []string{"/ubuntu-storage-base"}, identity); err != nil {
		t.Fatalf("expected path-boundary-safe mount to be accepted: %v", err)
	}
}

func TestValidateRootfsDiskCaptureRejectsSymlinkIntoVolume(t *testing.T) {
	disks := []snapshot.QEMUDisk{{
		ID:          "osdisk",
		OverlayPath: "/vm/disk-link",
		Capture:     snapshot.QEMUDiskCaptureRootfs,
	}}
	resolved := map[string]string{
		"/vm/disk-link":     "/volume-data/state.qcow2",
		"/mnt/data":         "/volume-data",
		"/unrelated-volume": "/other-volume",
	}
	resolve := func(path string) (string, error) {
		value, ok := resolved[path]
		if !ok {
			return "", errors.New("path not found")
		}
		return value, nil
	}

	if err := validateRootfsDiskCapture(disks, []string{"/mnt/data"}, resolve); err == nil {
		t.Fatal("expected symlinked writable overlay under a volume mount to be rejected")
	}
	if err := validateRootfsDiskCapture(disks, []string{"/unrelated-volume"}, resolve); err != nil {
		t.Fatalf("expected symlinked writable overlay outside volume mounts to be accepted: %v", err)
	}
}

func TestGetContainerIDByNerdctlReturnsRunningContainer(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })

	calls := 0
	commandCombinedOutput = func(name string, args ...string) ([]byte, error) {
		calls++
		if name != "nerdctl" {
			t.Fatalf("unexpected command %q", name)
		}
		if calls != 1 {
			t.Fatalf("expected a single nerdctl lookup, got %d", calls)
		}
		if !contains(args, "label=io.kubernetes.pod.uid=pod-uid-1") {
			t.Fatalf("lookup did not constrain the Pod UID: %v", args)
		}
		return []byte("container-running\n"), nil
	}

	containerID, err := getContainerIDByNerdctl("pod-1", "default", "pod-uid-1", "sandbox")
	if err != nil {
		t.Fatalf("expected running container lookup to succeed, got %v", err)
	}
	if containerID != "container-running" {
		t.Fatalf("unexpected container ID %q", containerID)
	}
}

func TestGetContainerIDByNerdctlFallsBackToStoppedContainers(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })

	var calls [][]string
	commandCombinedOutput = func(name string, args ...string) ([]byte, error) {
		if name != "nerdctl" {
			t.Fatalf("unexpected command %q", name)
		}
		calls = append(calls, append([]string(nil), args...))
		switch len(calls) {
		case 1:
			return []byte("\n"), nil
		case 2:
			return []byte("container-stopped\n"), nil
		default:
			t.Fatalf("unexpected extra nerdctl lookup #%d", len(calls))
			return nil, nil
		}
	}

	containerID, err := getContainerIDByNerdctl("pod-1", "default", "pod-uid-1", "sandbox")
	if err != nil {
		t.Fatalf("expected stopped container fallback to succeed, got %v", err)
	}
	if containerID != "container-stopped" {
		t.Fatalf("unexpected container ID %q", containerID)
	}
	if len(calls) != 2 {
		t.Fatalf("expected two nerdctl lookups, got %d", len(calls))
	}
	if contains(calls[0], "-a") {
		t.Fatalf("first lookup should only inspect running containers: %v", calls[0])
	}
	if !contains(calls[1], "-a") {
		t.Fatalf("second lookup should include stopped containers: %v", calls[1])
	}
}

func TestGetContainerIDByNerdctlReturnsHelpfulErrorWhenBothLookupsAreEmpty(t *testing.T) {
	original := commandCombinedOutput
	t.Cleanup(func() { commandCombinedOutput = original })

	commandCombinedOutput = func(_ string, _ ...string) ([]byte, error) {
		return []byte("\n"), nil
	}

	_, err := getContainerIDByNerdctl("pod-1", "default", "pod-uid-1", "sandbox")
	if err == nil {
		t.Fatal("expected lookup failure when both running and stopped container searches are empty")
	}
	if got := err.Error(); got != "container 'sandbox' not found in pod default/pod-1 (nerdctl ps and nerdctl ps -a returned empty)" {
		t.Fatalf("unexpected error %q", got)
	}
}

func contains(values []string, target string) bool {
	for _, value := range values {
		if value == target {
			return true
		}
	}
	return false
}