Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/devcontainer/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ func TestSecretsEnvironmentTmpfsMount(t *testing.T) {
if err != nil {
t.Fatal("create secret environment mount")
}
if mount == nil || mount.Type != "tmpfs" || mount.Target != config.SecretsEnvDir {
t.Error("secret environment mount is not configured for the protected directory")
}
require.NotNil(t, mount)
require.Equal(t, "tmpfs", mount.Type)
require.Equal(t, config.SecretsEnvDir, mount.Target)
// Docker syntax is the shared contract; the Apple driver translates it.
require.Equal(t, []string{"tmpfs-mode=0700"}, mount.Other)
if _, err := secretsEnvironmentTmpfsMount(true, false); err == nil {
t.Error("unsupported tmpfs provider was allowed to store workspace secrets")
}
Expand All @@ -223,6 +225,7 @@ func TestWithSecretsMountUsesCurrentUpRequest(t *testing.T) {
require.Len(t, mounts, 1)
require.Equal(t, config.SecretsMountDir, mounts[0].Target)
require.Equal(t, "tmpfs", mounts[0].Type)
require.Equal(t, []string{"tmpfs-mode=0755"}, mounts[0].Other)

mounts, err = r.withSecretsMount(nil, false)
require.NoError(t, err)
Expand Down
27 changes: 25 additions & 2 deletions pkg/driver/apple/runargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (d *appleDriver) addPorts(args []string, parsedConfig *config.DevContainerC

func (d *appleDriver) addWorkspaceMount(args []string, options *driver.RunOptions) []string {
if options.WorkspaceMount != nil {
args = append(args, "--mount", cleanMount(options.WorkspaceMount.String()))
args = appendMount(args, options.WorkspaceMount)
}
return args
}
Expand Down Expand Up @@ -92,11 +92,34 @@ func addMounts(args []string, options *driver.RunOptions) []string {
continue
}
}
args = append(args, "--mount", cleanMount(mount.String()))
args = appendMount(args, mount)
}
return args
}

// appendMount formats a mount for `container run --mount`. Docker spells the
// tmpfs permission option "tmpfs-mode"; Apple's CLI rejects it and expects
// "mode". "tmpfs-size" is deliberately not translated: container 1.5.0 rounds
// sub-MiB sizes to zero (an uncapped mount), so the Docker spelling is left
// as is and the CLI refuses it instead of silently dropping the cap.
func appendMount(args []string, mount *config.Mount) []string {
translated := *mount
if mount.Type == driver.MountTypeTmpfs {
translated.Other = make([]string, len(mount.Other))
for i, option := range mount.Other {
translated.Other[i] = nativeTmpfsOption(option)
}
}
return append(args, "--mount", cleanMount(translated.String()))
}

func nativeTmpfsOption(option string) string {
if value, ok := strings.CutPrefix(option, "tmpfs-mode="); ok {
return "mode=" + value
}
return option
}

func (d *appleDriver) addLabels(
args []string,
workspaceID string,
Expand Down
143 changes: 143 additions & 0 deletions pkg/driver/apple/runargs_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package apple

import (
"bytes"
"context"
"fmt"
"os"
"testing"
"time"

"github.com/devsy-org/devsy/pkg/apple"
"github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/driver"
"github.com/stretchr/testify/require"
)

const tmpfsIntegrationImage = "ghcr.io/devsy-org/test-images/base:ubuntu"

// TestTmpfsMountsIntegration starts a container with the tmpfs mounts Devsy
// generates for secrets and checks the native CLI accepts and honors them.
// It is gated behind DEVSY_APPLE_E2E=1 (Apple silicon, macOS 26+, `container`
// installed) and never runs in normal CI.
func TestTmpfsMountsIntegration(t *testing.T) {
if os.Getenv("DEVSY_APPLE_E2E") != "1" {
t.Skip("set DEVSY_APPLE_E2E=1 to run against a real container CLI")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
helper := &apple.AppleHelper{Command: "container"}
require.NoError(t, helper.EnsureSystemRunning(ctx))

var out bytes.Buffer
err := helper.Pull(ctx, apple.PullOptions{
Image: tmpfsIntegrationImage,
Stdout: &out,
Stderr: &out,
})
require.NoError(t, err, out.String())

name := fmt.Sprintf("devsy-tmpfs-%d", time.Now().UnixNano())
label := "devsy.e2e.tmpfs=" + name
// Registered before run so a partially created container is still removed.
t.Cleanup(func() { cleanupTmpfsProbe(t, helper, name, label) })

args := (&appleDriver{}).buildRunArgs(tmpfsIntegrationParams(name, label))
out.Reset()
err = helper.Run(ctx, args, apple.Streams{Stdout: &out, Stderr: &out})
require.NoError(t, err, out.String())
require.NoError(t, helper.WaitContainerRunning(ctx, name))

out.Reset()
err = helper.Run(
ctx,
[]string{"exec", name, "sh", "-c", tmpfsIntegrationOracle},
apple.Streams{Stdout: &out, Stderr: &out},
)
require.NoError(t, err, out.String())
t.Log(out.String())
}

// cleanupTmpfsProbe removes the probe container if it exists. Failures are
// reported with t.Errorf so they never replace the first failing assertion,
// and a container that was never created is not an error.
func cleanupTmpfsProbe(t *testing.T, helper *apple.AppleHelper, name, label string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
found, err := helper.FindDevContainer(ctx, []string{label})
if err != nil {
t.Errorf("list probe container %s: %v", name, err)
return
}
if found == nil {
return
}
if err := helper.Stop(ctx, name); err != nil {
t.Logf("stop probe container %s: %v", name, err)
}
if err := helper.Remove(ctx, name); err != nil {
t.Errorf("remove probe container %s: %v", name, err)
}
}

func tmpfsIntegrationParams(name, label string) *driver.RunImageDevContainerParams {
return &driver.RunImageDevContainerParams{
WorkspaceID: name,
ParsedConfig: &config.DevContainerConfig{
NonComposeBase: config.NonComposeBase{
RunArgs: []string{"--name", name, "--cpus", "1", "--memory", "256M"},
},
},
Options: &driver.RunOptions{
Image: tmpfsIntegrationImage,
Entrypoint: "/bin/sh",
Cmd: []string{"-c", "sleep 120"},
Labels: []string{label},
Mounts: []*config.Mount{
// Docker-style options exactly as the shared secret mounts emit them.
{
Type: driver.MountTypeTmpfs,
Target: config.SecretsMountDir,
Other: []string{"tmpfs-mode=0755"},
},
{
Type: driver.MountTypeTmpfs,
Target: config.SecretsEnvDir,
Other: []string{"tmpfs-mode=0700"},
},
// Native options pass through unchanged. Whole-MiB sizes only:
// container 1.5.0 turns smaller sizes into zero (uncapped).
{
Type: driver.MountTypeTmpfs,
Target: "/probe-native",
Other: []string{"mode=0700", "size=1M"},
},
{
Type: driver.MountTypeTmpfs,
Target: "/probe-size",
Other: []string{"size=2M"},
},
{Type: driver.MountTypeTmpfs, Target: "/probe-ro", Other: []string{"readonly"}},
},
},
}
}

// Capacity is block size times block count, so the check does not depend on
// the guest page geometry.
const tmpfsIntegrationOracle = `set -eu
bytes() { echo $(( $(stat -f -c %S "$1") * $(stat -f -c %b "$1") )); }
stat -c '%n %a' /run/secrets /run/devsy/secrets-env /probe-native
test "$(stat -c %a /run/secrets)" = 755
test "$(stat -c %a /run/devsy/secrets-env)" = 700
test "$(stat -c %a /probe-native)" = 700
for p in /run/secrets /run/devsy/secrets-env /probe-native /probe-size /probe-ro; do
test "$(stat -f -c %T "$p")" = tmpfs
done
test "$(bytes /probe-native)" = 1048576
test "$(bytes /probe-size)" = 2097152
touch /probe-native/writable
if touch /probe-ro/forbidden 2>/dev/null; then exit 1; fi
echo 'native CLI tmpfs mount behavior verified'
`
163 changes: 163 additions & 0 deletions pkg/driver/apple/runargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/driver"
"github.com/devsy-org/devsy/pkg/types"
"github.com/stretchr/testify/require"
)

const testMountFlag = "--mount"

func TestBuildRunArgs(t *testing.T) {
d := &appleDriver{IDLabels: []string{"dev.containers.id"}}
init := true
Expand Down Expand Up @@ -105,3 +108,163 @@ func TestRedactArgs(t *testing.T) {
}
}
}

// mountArgs returns every value that follows a --mount flag, so tests assert
// mount contents independent of unrelated flag ordering.
func mountArgs(args []string) []string {
var mounts []string
for i := range len(args) - 1 {
if args[i] == testMountFlag {
mounts = append(mounts, args[i+1])
}
}
return mounts
}

func runArgsWithMounts(
workspace *config.Mount,
mounts ...*config.Mount,
) []string {
return (&appleDriver{}).buildRunArgs(&driver.RunImageDevContainerParams{
ParsedConfig: &config.DevContainerConfig{},
Options: &driver.RunOptions{
Image: testImageRef,
WorkspaceMount: workspace,
Mounts: mounts,
},
})
}

func TestBuildRunArgsTmpfs(t *testing.T) {
for _, tt := range []struct{ mount, want string }{
{"type=tmpfs,target=/run/secrets,tmpfs-mode=0755", "type=tmpfs,dst=/run/secrets,mode=0755"},
{"type=tmpfs,target=/run/devsy/secrets-env,tmpfs-mode=0700", "type=tmpfs,dst=/run/devsy/secrets-env,mode=0700"},
{
"type=tmpfs,target=/scratch,tmpfs-mode=0700,future-option=enabled",
"type=tmpfs,dst=/scratch,mode=0700,future-option=enabled",
},
{"type=tmpfs,target=/scratch,tmpfs-mode=invalid", "type=tmpfs,dst=/scratch,mode=invalid"},
{
"type=tmpfs,target=/scratch,tmpfs-mode=0777,tmpfs-mode=0700,readonly=false",
"type=tmpfs,dst=/scratch,mode=0777,mode=0700,readonly=false",
},
{"type=tmpfs,target=/scratch,tmpfs-mode=0000", "type=tmpfs,dst=/scratch,mode=0000"},
} {
t.Run(tt.mount, func(t *testing.T) {
mount := config.ParseMount(tt.mount)
original := mount.String()
originalOther := append([]string(nil), mount.Other...)
args := runArgsWithMounts(nil, &mount)
require.Equal(t, []string{tt.want}, mountArgs(args))
require.Equal(t, original, mount.String())
require.Equal(t, originalOther, mount.Other)
})
}
}

// tmpfs-size is not translated: Apple container 1.5.0 turns sub-MiB sizes into
// zero (uncapped), so a Docker-style size must reach the CLI unchanged and be
// rejected there. A native size= is the user's own CLI syntax.
func TestBuildRunArgsLeavesTmpfsSizeUntranslated(t *testing.T) {
for _, tt := range []struct{ mount, want string }{
{"type=tmpfs,target=/scratch,tmpfs-size=4096", "type=tmpfs,dst=/scratch,tmpfs-size=4096"},
{
"type=tmpfs,target=/scratch,tmpfs-size=1M,tmpfs-mode=0700",
"type=tmpfs,dst=/scratch,tmpfs-size=1M,mode=0700",
},
{"type=tmpfs,target=/scratch,size=2M", "type=tmpfs,dst=/scratch,size=2M"},
} {
t.Run(tt.mount, func(t *testing.T) {
mount := config.ParseMount(tt.mount)
require.Equal(t, []string{tt.want}, mountArgs(runArgsWithMounts(nil, &mount)))
})
}
}

func TestBuildRunArgsTmpfsWorkspaceMount(t *testing.T) {
workspace := config.ParseMount("type=tmpfs,target=/workspaces/app,tmpfs-mode=0700")
original := workspace.String()
originalOther := append([]string(nil), workspace.Other...)

args := runArgsWithMounts(&workspace)

require.Equal(t, []string{"type=tmpfs,dst=/workspaces/app,mode=0700"}, mountArgs(args))
require.Equal(t, original, workspace.String())
require.Equal(t, originalOther, workspace.Other)
}

func TestBuildRunArgsWorkspaceAndMountsBothTranslated(t *testing.T) {
workspace := config.ParseMount("type=tmpfs,target=/workspaces/app,tmpfs-mode=0700")
secrets := config.ParseMount("type=tmpfs,target=/run/secrets,tmpfs-mode=0755")
require.Equal(
t,
[]string{
"type=tmpfs,dst=/workspaces/app,mode=0700",
"type=tmpfs,dst=/run/secrets,mode=0755",
},
mountArgs(runArgsWithMounts(&workspace, &secrets)),
)
}

func TestBuildRunArgsBindWorkspaceMountIsNotTranslated(t *testing.T) {
workspace := config.Mount{
Type: "bind",
Source: t.TempDir(),
Target: "/workspaces/app",
Other: []string{"tmpfs-mode=0700", "consistency=cached"},
}
args := runArgsWithMounts(&workspace)
// tmpfs-mode is only translated for tmpfs; consistency= is still cleaned.
require.Equal(
t,
[]string{"type=bind,src=" + workspace.Source + ",dst=/workspaces/app,tmpfs-mode=0700"},
mountArgs(args),
)
}

func TestBuildRunArgsPreservesOtherMountDirectives(t *testing.T) {
for _, spec := range []string{
"type=tmpfs,source=bad,target=/scratch", "type=tmpfs,target=relative", "type=tmpfs,target=/scratch:other",
"type=tmpfs,target=/scratch,tmpfs-mode", "type=tmpfs,target=/scratch,mode=0700,size=1M",
"type=tmpfs,target=/scratch,future-option=enabled", "type=tmpfs,target=/scratch,consistency",
"type=tmpfs,target=/scratch,Consistency=cached", "type=tmpfs,target=/scratch,bind-create-src",
"type=volume,source=volume,target=/scratch,tmpfs-mode=0700",
} {
t.Run(spec, func(t *testing.T) {
mount := config.ParseMount(spec)
require.Equal(t, []string{testMountFlag, mount.String()}, appendMount(nil, &mount))
})
}
}

func TestBuildRunArgsPreservesRunArgs(t *testing.T) {
raw := []string{
"--tmpfs",
"/native:mode=0700",
testMountFlag,
"type=tmpfs,target=/native-mount,mode=0755",
}
params := &driver.RunImageDevContainerParams{
ParsedConfig: &config.DevContainerConfig{
NonComposeBase: config.NonComposeBase{RunArgs: raw},
},
Options: &driver.RunOptions{Image: testImageRef},
}
args := (&appleDriver{}).buildRunArgs(params)
require.Equal(
t,
append(append([]string{"run", "-d", "-l", "dev.containers.id="}, raw...), testImageRef),
args,
)
}

func TestAppendMountKeepsLegacyCleaningOnFallback(t *testing.T) {
mount := config.ParseMount(
"type=tmpfs,target=/scratch,mode=0700,consistency=cached,bind-create-src=true",
)
require.Equal(
t,
[]string{testMountFlag, "type=tmpfs,dst=/scratch,mode=0700"},
appendMount(nil, &mount),
)
}
Loading