-
Notifications
You must be signed in to change notification settings - Fork 351
Add stacked disk integration coverage and documentation #1316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package stackedoci | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/gosuri/uitable" | ||
| "github.com/spf13/cobra" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapio" | ||
| ) | ||
|
|
||
| var ( | ||
| debug bool | ||
| baseImage string | ||
| stackedImage string | ||
| insecure bool | ||
| concurrency uint | ||
| ) | ||
|
|
||
| func NewCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "stacked-oci", | ||
| Short: "benchmark empty and prewarmed Tart homes for stacked OCI pulls", | ||
| Long: "Warm the registry once, then compare an empty Tart home with one whose " + | ||
| "immutable base has already been materialized by tart clone --stacked. " + | ||
| "Every scenario uses a disposable TART_HOME and leaves the user's Tart home untouched.", | ||
| RunE: run, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&debug, "debug", false, "enable debug logging") | ||
| cmd.Flags().StringVar(&baseImage, "base-image", "", "remote flat OCI image used as the stacked image's base") | ||
| cmd.Flags().StringVar(&stackedImage, "image", "", "remote stacked OCI image to pull and clone") | ||
| cmd.Flags().BoolVar(&insecure, "insecure", false, "connect to the OCI registry via insecure HTTP") | ||
| cmd.Flags().UintVar(&concurrency, "concurrency", 4, "network concurrency passed to tart pull and clone") | ||
| _ = cmd.MarkFlagRequired("base-image") | ||
| _ = cmd.MarkFlagRequired("image") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func run(cmd *cobra.Command, _ []string) error { | ||
| if concurrency < 1 { | ||
| return fmt.Errorf("concurrency cannot be less than 1") | ||
| } | ||
|
|
||
| config := zap.NewProductionConfig() | ||
| if debug { | ||
| config.Level = zap.NewAtomicLevelAt(zap.DebugLevel) | ||
| } | ||
| logger, err := config.Build() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = logger.Sync() }() | ||
|
|
||
| warmupHome, err := os.MkdirTemp("", "tart-stacked-oci-warmup-*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer os.RemoveAll(warmupHome) | ||
|
|
||
| emptyHome, err := os.MkdirTemp("", "tart-stacked-oci-empty-*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer os.RemoveAll(emptyHome) | ||
|
|
||
| warmHome, err := os.MkdirTemp("", "tart-stacked-oci-warm-*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer os.RemoveAll(warmHome) | ||
|
|
||
| table := uitable.New() | ||
| table.AddRow("Scenario", "Operation", "Time") | ||
|
|
||
| // Warm registry, CDN, and filesystem caches before either measured | ||
| // scenario so their difference reflects Tart's local base reuse. | ||
| if _, err := timedTart(cmd.Context(), logger, warmupHome, pullArguments(stackedImage)...); err != nil { | ||
| return fmt.Errorf("registry warmup failed: %w", err) | ||
| } | ||
| if err := os.RemoveAll(warmupHome); err != nil { | ||
| return fmt.Errorf("removing registry warmup home: %w", err) | ||
| } | ||
|
|
||
| duration, err := timedTart(cmd.Context(), logger, emptyHome, pullArguments(stackedImage)...) | ||
| if err != nil { | ||
| return fmt.Errorf("empty-home stacked pull failed: %w", err) | ||
| } | ||
| table.AddRow("empty", "pull stacked image", duration) | ||
|
|
||
| duration, err = timedTart(cmd.Context(), logger, emptyHome, "clone", stackedImage, "empty-clone") | ||
| if err != nil { | ||
| return fmt.Errorf("empty-home stacked clone failed: %w", err) | ||
| } | ||
| table.AddRow("empty", "clone stacked image", duration) | ||
| if err := os.RemoveAll(emptyHome); err != nil { | ||
| return fmt.Errorf("removing empty home: %w", err) | ||
| } | ||
|
|
||
| duration, err = timedTart(cmd.Context(), logger, warmHome, cloneBaseArguments(baseImage)...) | ||
| if err != nil { | ||
| return fmt.Errorf("base prewarm failed: %w", err) | ||
| } | ||
| table.AddRow("prewarmed", "clone --stacked base image", duration) | ||
|
|
||
| duration, err = timedTart(cmd.Context(), logger, warmHome, pullArguments(stackedImage)...) | ||
|
yzhuang-oai marked this conversation as resolved.
|
||
| if err != nil { | ||
| return fmt.Errorf("prewarmed stacked pull failed: %w", err) | ||
| } | ||
| table.AddRow("prewarmed", "pull stacked image", duration) | ||
|
|
||
| duration, err = timedTart(cmd.Context(), logger, warmHome, "clone", stackedImage, "warm-clone") | ||
| if err != nil { | ||
| return fmt.Errorf("prewarmed stacked clone failed: %w", err) | ||
| } | ||
| table.AddRow("prewarmed", "clone stacked image", duration) | ||
|
|
||
| fmt.Println(table.String()) | ||
| return nil | ||
| } | ||
|
|
||
| func pullArguments(image string) []string { | ||
| args := []string{"pull", "--concurrency", fmt.Sprint(concurrency)} | ||
| if insecure { | ||
| args = append(args, "--insecure") | ||
| } | ||
| return append(args, image) | ||
| } | ||
|
|
||
| func cloneBaseArguments(image string) []string { | ||
| args := []string{"clone", "--stacked", "--concurrency", fmt.Sprint(concurrency)} | ||
| if insecure { | ||
| args = append(args, "--insecure") | ||
| } | ||
| return append(args, image, "prewarmed-base") | ||
| } | ||
|
|
||
| func timedTart( | ||
| ctx context.Context, | ||
| logger *zap.Logger, | ||
| tartHome string, | ||
| args ...string, | ||
| ) (time.Duration, error) { | ||
| logger.Sugar().Debugf("TART_HOME=%s tart %s", tartHome, strings.Join(args, " ")) | ||
| start := time.Now() | ||
|
|
||
| command := exec.CommandContext(ctx, "tart", args...) | ||
| command.Env = append(os.Environ(), "TART_HOME="+tartHome) | ||
| loggerWriter := &zapio.Writer{Log: logger, Level: zap.DebugLevel} | ||
| command.Stdout = loggerWriter | ||
| command.Stderr = loggerWriter | ||
|
|
||
| err := command.Run() | ||
| return time.Since(start).Round(time.Millisecond), err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import os | ||
| import platform | ||
| import subprocess | ||
| import uuid | ||
| from typing import Optional | ||
|
|
||
| import pytest | ||
| from paramiko.client import AutoAddPolicy, SSHClient | ||
|
|
||
|
|
||
| def _macos_major_version() -> int: | ||
| version = platform.mac_ver()[0] | ||
| return int(version.split(".", maxsplit=1)[0]) if version else 0 | ||
|
|
||
|
|
||
| def _shutdown_vm(tart, vm_name: str, guest_command: Optional[str] = None) -> None: | ||
| tart_run_process = tart.run_async(["run", "--no-graphics", vm_name]) | ||
| client = None | ||
|
|
||
| try: | ||
| stdout, _ = tart.run(["ip", vm_name, "--wait", "180"]) | ||
| client = SSHClient() | ||
| client.set_missing_host_key_policy(AutoAddPolicy) | ||
| client.connect(stdout.strip(), username="admin", password="admin") | ||
| if guest_command: | ||
| _, command_stdout, command_stderr = client.exec_command(guest_command) | ||
| assert command_stdout.channel.recv_exit_status() == 0, command_stderr.read().decode() | ||
| client.exec_command("sudo shutdown -h now") | ||
|
|
||
| tart_run_process.wait(timeout=180) | ||
| assert tart_run_process.returncode == 0 | ||
| finally: | ||
| if client is not None: | ||
| client.close() | ||
| if tart_run_process.poll() is None: | ||
| tart_run_process.terminate() | ||
| try: | ||
| tart_run_process.wait(timeout=30) | ||
| except subprocess.TimeoutExpired: | ||
| tart_run_process.kill() | ||
| tart_run_process.wait(timeout=30) | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| _macos_major_version() < 27, | ||
| reason="stacked disk images require DiskImageKit on macOS 27 or newer", | ||
| ) | ||
| def test_stacked_oci_round_trip_and_boot(tart, docker_registry): | ||
| suffix = str(uuid.uuid4()) | ||
| source_image = os.environ.get( | ||
| "TART_STACKED_INTEGRATION_BASE_IMAGE", | ||
| "ghcr.io/cirruslabs/macos-tahoe-base:latest", | ||
| ) | ||
| source_clone_args = ["clone"] | ||
| if os.environ.get("TART_STACKED_INTEGRATION_BASE_INSECURE") == "1": | ||
| source_clone_args.append("--insecure") | ||
| standalone_vm = f"stacked-base-{suffix}" | ||
| stacked_vm = f"stacked-child-{suffix}" | ||
| restored_vm = f"stacked-restored-{suffix}" | ||
| base_remote = docker_registry.remote_name(f"stacked-base-{suffix}") | ||
| child_remote = docker_registry.remote_name(f"stacked-child-{suffix}") | ||
|
|
||
| try: | ||
| # Publish a normal Tart image, then start a stacked lineage from that | ||
| # remote image. The base remains a normal flat OCI image. | ||
| tart.run(source_clone_args + [source_image, standalone_vm]) | ||
| tart.run(["push", "--insecure", standalone_vm, base_remote]) | ||
| tart.run(["clone", "--insecure", "--stacked", base_remote, stacked_vm]) | ||
|
|
||
| stacked_path = os.path.join(tart.home(), "vms", stacked_vm) | ||
| assert os.path.isfile(os.path.join(stacked_path, "overlay.asif")) | ||
| assert os.path.isfile(os.path.join(stacked_path, "manifest.json")) | ||
| assert not os.path.exists(os.path.join(stacked_path, "disk.img")) | ||
|
|
||
| # Boot once so the top ASIF overlay contains real guest writes, then | ||
| # exercise stacked push, pull, clone, assembly, and a second boot. | ||
| _shutdown_vm(tart, stacked_vm, "touch ~/stacked-round-trip-marker") | ||
| tart.run(["push", "--insecure", stacked_vm, child_remote]) | ||
| tart.run(["delete", stacked_vm]) | ||
| tart.run(["pull", "--insecure", child_remote]) | ||
|
yzhuang-oai marked this conversation as resolved.
|
||
| tart.run(["clone", child_remote, restored_vm]) | ||
|
|
||
| restored_path = os.path.join(tart.home(), "vms", restored_vm) | ||
| assert os.path.isfile(os.path.join(restored_path, "overlay.asif")) | ||
| assert os.path.isfile(os.path.join(restored_path, "manifest.json")) | ||
| assert not os.path.exists(os.path.join(restored_path, "disk.img")) | ||
|
|
||
| _shutdown_vm(tart, restored_vm, "test -f ~/stacked-round-trip-marker") | ||
| finally: | ||
| for vm_name in (restored_vm, stacked_vm, standalone_vm, child_remote, base_remote): | ||
| try: | ||
| tart.run(["delete", vm_name]) | ||
| except Exception: | ||
| pass | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.