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
36 changes: 36 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,42 @@ jobs:
--distribution Ubuntu-24.04 --timeout 4m
--state-file "$env:RUNNER_TEMP\devsy-ci\docker-wsl-state.json"

# The WSLg specs and the parity probe need Compose on the Windows side.
# The fixture exports DOCKER_CONFIG, which holds the plugin.
- name: install pinned Windows Compose plugin
if: matrix.setup_docker_wsl
shell: pwsh
run: |
if ([string]::IsNullOrEmpty($env:DOCKER_CONFIG)) { throw 'WSL fixture Docker config is missing' }
$pluginDir = Join-Path $env:DOCKER_CONFIG 'cli-plugins'
New-Item -ItemType Directory -Force $pluginDir | Out-Null
$plugin = Join-Path $pluginDir 'docker-compose.exe'
Invoke-WebRequest `
-Uri 'https://github.com/docker/compose/releases/download/v5.6.0/docker-compose-windows-x86_64.exe' `
-OutFile $plugin
$expectedHash = '8aeaa0055e660e12cf07cd70680dc8333957fe6718528b91ec3da084eab62483'
$actualHash = (Get-FileHash -Path $plugin -Algorithm SHA256).Hash.ToLower()
if ($actualHash -ne $expectedHash) { throw 'Compose SHA256 mismatch' }

- name: setup Go
if: matrix.setup_docker_wsl
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
with:
go-version-file: go.mod

- name: test real noninteractive WSLg discovery and Compose parity
if: matrix.setup_docker_wsl
shell: pwsh
timeout-minutes: 8
env:
DEVSY_WSLG_TEST_DISTRO: Ubuntu-24.04
DEVSY_WSLG_TEST_USER: devsyci
run: |
docker compose version
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
go test -count=1 -v -timeout 3m ./pkg/wslg ./pkg/compose -run '^TestWSLg'
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

# Runs the suite's `host_setup` actions from hack/ci/suites.yaml
# (for example removing Docker or trusting a local registry).
- name: host setup
Expand Down
3 changes: 3 additions & 0 deletions cmd/workspace/up/secrets_preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (e *unavailableValuesError) Unwrap() []error { return e.causes }
// Preflight locally known requirements before workspace resolution can create
// remote resources. Project SOPS sources remain on the later discovery path.
func (cmd *UpCmd) preflightLocalValues(cfg *config.Config) error {
if err := cmd.preflightWSLgContext(cfg); err != nil {
return err
}
refs, err := cmd.localPreflightRefs(cfg)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions cmd/workspace/up/up_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func (cmd *UpCmd) postResolveWorkspace(
}
return err
}
if err := cmd.preflightWSLgClient(devsyConfig, client); err != nil {
return cleanupOnFailure(err)
}
if err := cmd.prepareResolvedWorkspaceSecrets(ctx, devsyConfig, client); err != nil {
return cleanupOnFailure(err)
}
Expand Down
80 changes: 80 additions & 0 deletions cmd/workspace/up/wslg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package up

import (
"os"
"runtime"

client2 "github.com/devsy-org/devsy/pkg/client"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/docker"
"github.com/devsy-org/devsy/pkg/provider"
"github.com/devsy-org/devsy/pkg/wslg"
)

const wslgGoosWindows = "windows"

func (cmd *UpCmd) preflightWSLgContext(cfg *config.Config) error {
if !cfg.ContextOptionBool(config.ContextOptionWSLgEnabled) {
return nil
}
if runtime.GOOS != wslgGoosWindows || cmd.Platform.Enabled {
return wslg.ErrUnsupported
}
return wslg.ValidateTarget(
wslg.Target{
Distro: cfg.ContextOption(config.ContextOptionWSLgDistro),
User: cfg.ContextOption(config.ContextOptionWSLgUser),
},
)
}

// Reject unsupported providers before Create/Start can provision a machine.
// The runner subsequently checks its actual driver before discovering values.
func (cmd *UpCmd) preflightWSLgClient(
cfg *config.Config,
client client2.BaseWorkspaceClient,
) error {
if !cfg.ContextOptionBool(config.ContextOptionWSLgEnabled) {
return nil
}
local, ok := client.(client2.WorkspaceClient)
if !ok || !local.AgentLocal() {
return wslg.ErrUnsupported
}
_, info, err := local.AgentInfo(cmd.CLIOptions)
if err != nil {
return err
}
if err := wslg.CheckEnvironmentSources(map[string][]string{
"process environment": os.Environ(),
"--init-env": cmd.InitEnv,
"provider Docker environment": wslg.Pairs(info.Agent.Docker.Env),
}, runtime.GOOS == wslgGoosWindows); err != nil {
return err
}
return validateWSLgAgent(info.Agent)
}

func validateWSLgAgent(agent provider.ProviderAgentConfig) error {
if agent.Driver != "" && agent.Driver != provider.DockerDriver {
return wslg.ErrUnsupported
}
var containerRuntime docker.ContainerRuntime
if agent.Docker.Runtime != "" {
var err error
containerRuntime, err = docker.RuntimeFromName(agent.Docker.Runtime)
if err != nil {
return err
}
} else {
command := agent.Docker.Path
if command == "" {
command = provider.DockerDriver
}
containerRuntime = docker.DetectRuntime(command)
}
if containerRuntime.Name() != docker.RuntimeDocker {
return wslg.ErrUnsupported
}
return nil
}
62 changes: 62 additions & 0 deletions cmd/workspace/up/wslg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package up

import (
"context"
"runtime"
"testing"

"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/provider"
"github.com/devsy-org/devsy/pkg/wslg"
"github.com/stretchr/testify/require"
)

func TestWSLgPreflightBeforeWorkspaceProvisioning(t *testing.T) {
cfg := testConfig()
cmd := &UpCmd{}
require.NoError(t, cmd.preflightWSLgContext(cfg))
cfg.Current().Options = map[string]config.OptionValue{
config.ContextOptionWSLgEnabled: {Value: config.BoolTrue},
}
if runtime.GOOS == wslgGoosWindows {
require.ErrorIs(t, cmd.preflightWSLgContext(cfg), wslg.ErrInvalidTarget)
} else {
require.ErrorIs(t, cmd.preflightWSLgContext(cfg), wslg.ErrUnsupported)
}
require.ErrorIs(t, cmd.preflightWSLgClient(cfg, &fakeWorkspaceClient{}), wslg.ErrUnsupported)
}

func TestWSLgPreflightRunsFirstInPostResolveWorkspace(t *testing.T) {
cfg := testConfig()
cfg.Current().Options = map[string]config.OptionValue{
config.ContextOptionWSLgEnabled: {Value: config.BoolTrue},
}
// The fake has no workspace config, so reaching secrets or provider update
// would fail differently; only an early preflight yields ErrUnsupported.
for _, existed := range []bool{false, true} {
client := &fakeWorkspaceClient{}
err := (&UpCmd{}).postResolveWorkspace(context.Background(), cfg, client, existed)
require.ErrorIs(t, err, wslg.ErrUnsupported)
require.Equal(t, !existed, client.deleted, "only a newly created workspace is removed")
}
}

func TestWSLgResolvedAgentRejectsUnsupportedDriversAndRuntimes(t *testing.T) {
for _, agent := range []provider.ProviderAgentConfig{
{Driver: provider.ExternalDriver},
{Driver: provider.CustomDriver},
{Driver: provider.DockerDriver, Docker: provider.ProviderDockerDriverConfig{Runtime: "podman"}},
{Driver: provider.DockerDriver, Docker: provider.ProviderDockerDriverConfig{Runtime: "nerdctl"}},
} {
require.ErrorIs(t, validateWSLgAgent(agent), wslg.ErrUnsupported)
}
require.NoError(
t,
validateWSLgAgent(
provider.ProviderAgentConfig{
Driver: provider.DockerDriver,
Docker: provider.ProviderDockerDriverConfig{Runtime: "docker"},
},
),
)
}
186 changes: 186 additions & 0 deletions e2e/tests/dockerbuildkit/wslg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package dockerbuildkit

import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"

"github.com/devsy-org/devsy/e2e/framework"
pkgconfig "github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/wslg"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

const (
wslgTestWindows = "windows"
wslgNamespace = "DEVSY_WSLG_"
)

// assertWSLgContainer checks the workspace's current container, which changes on recreate.
func assertWSLgContainer(ctx context.Context, env []string, uid, display string) {
ginkgo.GinkgoHelper()
id := dockerOutput(
ctx,
env,
"ps",
"-q",
"--filter",
"label="+pkgconfig.DevcontainerIDLabel+"="+uid,
)
gomega.Expect(id).NotTo(gomega.BeEmpty())
actual := dockerOutput(ctx, env, "inspect", "--format", "{{json .Config.Env}}", id)
var containerEnv []string
framework.ExpectNoError(json.Unmarshal([]byte(actual), &containerEnv))
gomega.Expect(containerEnv).To(gomega.ContainElement("DISPLAY=" + display))
for _, entry := range containerEnv {
gomega.Expect(strings.HasPrefix(entry, wslgNamespace)).To(
gomega.BeFalse(),
"discovery must not inject ordinary container environment: %s", entry,
)
}
gomega.Expect(dockerOutput(ctx, env, "exec", id, "test", "-S", "/tmp/.X11-unix/X0")).
To(gomega.BeEmpty())
}

var _ = ginkgo.Describe("WSLg environment transport", ginkgo.Label("wslg"), func() {
ginkgo.DescribeTable("creates and cleans up a workspace using the selected WSL user's display",
func(ctx context.Context, compose bool) {
framework.SkipUnlessOS(wslgTestWindows, "native Windows WSL fixture required")
target := wslg.Target{
Distro: requireEnv("DEVSY_WSLG_TEST_DISTRO"),
User: requireEnv("DEVSY_WSLG_TEST_USER"),
}
snapshot, err := wslg.Discover(ctx, target)
framework.ExpectNoError(err)
display := snapshot.Environment()[wslg.Display]
gomega.Expect(display).
NotTo(gomega.BeEmpty(), "the X11 transport fixture needs DISPLAY")
host := requireEnv(sshHostEnv)
env := providerEnv(host)
gomega.Expect(dockerOutput(ctx, env, "info", "--format", "{{.ID}}")).
To(gomega.Equal(requireEnv(wslDaemonEnv)))

// A private Devsy home keeps owner authorization and provider state isolated.
framework.SetEnvForSpec(pkgconfig.EnvHome, ginkgo.GinkgoT().TempDir())
framework.SetEnvForSpec(pkgconfig.EnvConfig, "")
cwd, err := os.Getwd()
framework.ExpectNoError(err)
f := framework.NewDefaultFramework(filepath.Join(cwd, "bin"))
framework.ExpectNoError(
f.DevsyContextCreate(
ctx,
"wslg-test",
"-o",
"WSLG_ENABLED=true",
"-o",
"WSLG_DISTRO="+target.Distro,
"-o",
"WSLG_USER="+target.User,
// Streaming the development agent over the runner's WSL SSH
// connection can exceed the ordinary 20-second injection budget.
"-o",
"AGENT_INJECT_TIMEOUT=120",
),
)
framework.ExpectNoError(f.DevsyContextUse(ctx, "wslg-test"))
framework.ExpectNoError(
f.DevsyProviderAdd(
ctx,
"docker",
"-o",
"DOCKER_HOST="+host,
"-o",
"DOCKER_CONTEXT=",
"-o",
"DOCKER_PATH=docker",
),
)
framework.ExpectNoError(f.DevsyProviderUse(ctx, "docker"))

dir := ginkgo.GinkgoT().TempDir()
image := "ghcr.io/devsy-org/test-images/base:alpine"
config := map[string]any{
"image": image,
"containerEnv": map[string]string{"DISPLAY": "${localEnv:DEVSY_WSLG_DISPLAY}"},
"runArgs": []string{
"--mount",
"type=bind,source=/tmp/.X11-unix,target=/tmp/.X11-unix,readonly",
},
}
if compose {
config = map[string]any{
"dockerComposeFile": "compose.yaml",
"service": "dev",
"workspaceFolder": "/workspaces/test",
}
framework.ExpectNoError(
os.WriteFile(
filepath.Join(dir, "compose.yaml"),
[]byte(
"services:\n dev:\n image: "+image+`
command: sleep infinity
environment:
DISPLAY: ${DEVSY_WSLG_DISPLAY:?required}
volumes:
- type: bind
source: /tmp/.X11-unix
target: /tmp/.X11-unix
read_only: true
bind:
create_host_path: false
`,
),
0o600,
),
)
}
data, err := json.Marshal(config)
framework.ExpectNoError(err)
framework.ExpectNoError(
os.WriteFile(filepath.Join(dir, ".devcontainer.json"), data, 0o600),
)
ginkgo.DeferCleanup(
func(ctx context.Context) { framework.ExpectNoError(f.DevsyWorkspaceDelete(ctx, dir)) },
)
framework.ExpectNoError(f.DevsyUp(ctx, dir, "--ide", "none"))
workspace, err := f.FindWorkspace(ctx, dir)
framework.ExpectNoError(err)
assertWSLgContainer(ctx, env, workspace.UID, display)

framework.ExpectNoError(f.DevsyStop(ctx, dir))
_, stderr, err := f.ExecCommandCapture(
ctx,
[]string{"workspace", "up", dir, "--ide", "none"},
)
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(stderr).To(gomega.ContainSubstring("--recreate"))
framework.ExpectNoError(f.DevsyUp(ctx, dir, "--ide", "none", "--recreate"))
// The earlier container is gone, so look it up again.
assertWSLgContainer(ctx, env, workspace.UID, display)
// Invalidate discovery while the Docker daemon remains reachable. Stop and
// delete must replay metadata rather than execute this unavailable target.
_, _, err = f.ExecCommandCapture(
ctx,
[]string{"context", "set", "-o", "WSLG_DISTRO=devsy-ci-missing-distro"},
)
framework.ExpectNoError(err)
framework.ExpectNoError(f.DevsyStop(ctx, dir))
ginkgo.AddReportEntry(
"WSLg acceptance scope",
strings.Join(
[]string{
"Native WSL printenv, image/Compose interpolation and daemon-visible X11 socket tested.",
"Stopped-container recreation and cleanup tested.",
"Visible Windows windows and native Wayland rendering require manual acceptance.",
},
" ",
),
)
},
ginkgo.Entry("image devcontainer", false, ginkgo.SpecTimeout(framework.TimeoutLong())),
ginkgo.Entry("Compose devcontainer", true, ginkgo.SpecTimeout(framework.TimeoutLong())),
)
})
Loading
Loading