Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.0 KB
Raw Blame
package boxlite

import (
	"context"
	"errors"
	"os"
	"strings"
	"testing"
)

func TestNestedVirtualizationCreateAuthorization(t *testing.T) {
	for _, enabled := range []bool{false, true} {
		t.Run(map[bool]string{false: "ordinary", true: "nested"}[enabled], func(t *testing.T) {
			advanced, err := NewAdvancedBoxOptions()
			if err != nil {
				t.Fatal(err)
			}
			defer advanced.Close()
			if err := advanced.SetNestedVirtualization(enabled); err != nil {
				t.Fatal(err)
			}
			runtime := newNestedVirtualizationTestRuntime(t, WithHomeDir(t.TempDir()))
			defer runtime.Close()
			box, err := runtime.Create(context.Background(), "alpine:latest", WithName("authorization"), WithDetach(true), WithAutoRemove(false), WithAdvancedOptions(advanced))
			if enabled {
				if err == nil {
					box.Close()
					t.Fatal("nested create bypassed runtime authorization")
				}
				if !strings.Contains(err.Error(), "nested-virtualization") {
					t.Fatalf("unexpected authorization error: %v", err)
				}
			} else {
				if err != nil {
					t.Fatal(err)
				}
				box.Close()
			}
		})
	}
}

func TestNestedVirtualizationCreatePersistsAcrossRuntimeReopen(t *testing.T) {
	home := t.TempDir()
	ctx := context.Background()
	advanced, err := NewAdvancedBoxOptions()
	if err != nil {
		t.Fatal(err)
	}
	defer advanced.Close()
	if err := advanced.SetNestedVirtualization(true); err != nil {
		t.Fatal(err)
	}
	var id string
	for attempt := 0; attempt < 3; attempt++ {
		runtime := newNestedVirtualizationTestRuntime(t, WithHomeDir(home), WithExperimentalFeatures("nested-virtualization"))
		options := []BoxOption{WithName("saved-nesting"), WithAutoRemove(false), WithDetach(true)}
		// An ordinary replay must not overwrite the original nested configuration.
		if attempt != 1 {
			options = append(options, WithAdvancedOptions(advanced))
		}
		box, created, err := runtime.GetOrCreate(ctx, "alpine:latest", options...)
		if err != nil {
			runtime.Close()
			t.Fatalf("attempt %d: %v", attempt, err)
		}
		if created != (attempt == 0) {
			t.Fatalf("attempt %d: created=%t", attempt, created)
		}
		if attempt == 0 {
			id = box.ID()
		} else if box.ID() != id {
			t.Fatalf("identity changed: %s -> %s", id, box.ID())
		}
		box.Close()
		if err := runtime.Close(); err != nil {
			t.Fatal(err)
		}
	}
	advanced.Close()
	if err := advanced.SetNestedVirtualization(false); err == nil {
		t.Fatal("setter accepted closed handle")
	}
}

// Runtime construction needs host virtualization even for metadata-only tests.
// The dedicated x64 qualification job must execute these assertions, so missing
// host support is fatal there rather than being silently treated as coverage.
func newNestedVirtualizationTestRuntime(t *testing.T, options ...RuntimeOption) *Runtime {
	t.Helper()
	instance, err := NewRuntime(options...)
	if err != nil {
		var runtimeError *Error
		if errors.As(err, &runtimeError) && runtimeError.Code == ErrUnsupported && os.Getenv("BOXLITE_REQUIRE_NESTED_RUNTIME_TESTS") != "1" {
			t.Skipf("host virtualization unavailable: %v", err)
		}
		t.Fatalf("NewRuntime: %v", err)
	}
	return instance
}