Skip to content

Commit 3b34232

Browse files
Merge pull request #21666 from n1hility/reenable-wsl-tests
Reenable wsl tests
2 parents 335750d + f036476 commit 3b34232

9 files changed

Lines changed: 95 additions & 29 deletions

File tree

.cirrus.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ podman_machine_windows_task:
795795
platform: windows
796796
env: *winenv
797797
matrix:
798-
#- env:
799-
# TEST_FLAVOR: "machine-wsl"
798+
- env:
799+
TEST_FLAVOR: "machine-wsl"
800800
- env:
801801
TEST_FLAVOR: "machine-hyperv"
802802
clone_script: *winclone

cmd/podman-wslkerninst/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func installWslKernel() error {
4848
)
4949
backoff := 500 * time.Millisecond
5050
for i := 1; i < 6; i++ {
51-
err = wutil.SilentExec("wsl", "--update")
51+
err = wutil.SilentExec(wutil.FindWSL(), "--update")
5252
if err == nil {
5353
break
5454
}

cmd/podman/machine/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func inspect(cmd *cobra.Command, args []string) error {
8989
Resources: mc.Resources,
9090
SSHConfig: mc.SSH,
9191
State: state,
92-
UserModeNetworking: false,
92+
UserModeNetworking: provider.UserModeNetworkEnabled(mc),
9393
// TODO I think this should be the HostUser
9494
Rootful: mc.HostUser.Rootful,
9595
}

pkg/machine/e2e/init_windows_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/Microsoft/go-winio/vhd"
99
"github.com/containers/libhvee/pkg/hypervctl"
1010
"github.com/containers/podman/v5/pkg/machine/define"
11+
"github.com/containers/podman/v5/pkg/machine/wsl/wutil"
1112
. "github.com/onsi/ginkgo/v2"
1213
. "github.com/onsi/gomega"
1314
. "github.com/onsi/gomega/gexec"
@@ -36,6 +37,18 @@ var _ = Describe("podman machine init - windows only", func() {
3637
Expect(err).ToNot(HaveOccurred())
3738
Expect(session).To(Exit(0))
3839

40+
defer func() {
41+
_, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"}, defaultTimeout, true)
42+
if err != nil {
43+
fmt.Println("unable to terminate podman-net-usermode")
44+
}
45+
46+
_, err = runSystemCommand(wutil.FindWSL(), []string{"--unregister", "podman-net-usermode"}, defaultTimeout, true)
47+
if err != nil {
48+
fmt.Println("unable to unregister podman-net-usermode")
49+
}
50+
}()
51+
3952
inspect := new(inspectMachine)
4053
inspect = inspect.withFormat("{{.UserModeNetworking}}")
4154
inspectSession, err := mb.setName(name).setCmd(inspect).run()
@@ -81,10 +94,6 @@ var _ = Describe("podman machine init - windows only", func() {
8194
It("init should not overwrite existing WSL vms", func() {
8295
skipIfNotVmtype(define.WSLVirt, "WSL test only")
8396

84-
var (
85-
wsl string = "wsl"
86-
)
87-
8897
name := randomString()
8998
distName := fmt.Sprintf("podman-%s", name)
9099
exportedPath := filepath.Join(testDir, "bogus.tar")
@@ -102,17 +111,17 @@ var _ = Describe("podman machine init - windows only", func() {
102111
// a vm outside the context of podman-machine and also
103112
// so we dont have to download a distribution from microsoft
104113
// servers
105-
exportSession, err := runSystemCommand(wsl, []string{"--export", "podman-foobarexport", exportedPath}, defaultTimeout, true)
114+
exportSession, err := runSystemCommand(wutil.FindWSL(), []string{"--export", "podman-foobarexport", exportedPath}, defaultTimeout, true)
106115
Expect(err).ToNot(HaveOccurred())
107116
Expect(exportSession).To(Exit(0))
108117

109118
// importing the machine and creating a vm
110-
importSession, err := runSystemCommand(wsl, []string{"--import", distName, distrDir, exportedPath}, defaultTimeout, true)
119+
importSession, err := runSystemCommand(wutil.FindWSL(), []string{"--import", distName, distrDir, exportedPath}, defaultTimeout, true)
111120
Expect(err).ToNot(HaveOccurred())
112121
Expect(importSession).To(Exit(0))
113122

114123
defer func() {
115-
_, err := runSystemCommand(wsl, []string{"--unregister", distName}, defaultTimeout, true)
124+
_, err := runSystemCommand(wutil.FindWSL(), []string{"--unregister", distName}, defaultTimeout, true)
116125
if err != nil {
117126
fmt.Println("unable to remove bogus wsl instance")
118127
}

pkg/machine/e2e/machine_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func teardown(origHomeDir string, testDir string, mb *machineTestBuilder) {
200200
GinkgoWriter.Printf("error occurred rm'ing machine: %q\n", err)
201201
}
202202
}
203+
203204
if err := utils.GuardedRemoveAll(testDir); err != nil {
204205
Fail(fmt.Sprintf("failed to remove test dir: %q", err))
205206
}

pkg/machine/shim/host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func List(vmstubbers []vmconfigs.VMProvider, _ machine.ListOptions) ([]*machine.
5050
Port: mc.SSH.Port,
5151
RemoteUsername: mc.SSH.RemoteUsername,
5252
IdentityPath: mc.SSH.IdentityPath,
53-
UserModeNetworking: false, // TODO Need to plumb this for WSL
53+
UserModeNetworking: s.UserModeNetworkEnabled(mc),
5454
}
5555
lrs = append(lrs, &lr)
5656
}

pkg/machine/wsl/machine.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func provisionWSLDist(name string, imagePath string, prompt string) (string, err
9090

9191
dist := machine.ToDist(name)
9292
fmt.Println(prompt)
93-
if err = runCmdPassThrough("wsl", "--import", dist, distTarget, imagePath, "--version", "2"); err != nil {
93+
if err = runCmdPassThrough(wutil.FindWSL(), "--import", dist, distTarget, imagePath, "--version", "2"); err != nil {
9494
return "", fmt.Errorf("the WSL import of guest OS failed: %w", err)
9595
}
9696

@@ -446,7 +446,7 @@ func installWslKernel() error {
446446

447447
backoff := 500 * time.Millisecond
448448
for i := 0; i < 5; i++ {
449-
err = runCmdPassThroughTee(log, "wsl", "--update")
449+
err = runCmdPassThroughTee(log, wutil.FindWSL(), "--update")
450450
if err == nil {
451451
break
452452
}
@@ -556,17 +556,17 @@ func withUser(s string, user string) string {
556556
func wslInvoke(dist string, arg ...string) error {
557557
newArgs := []string{"-u", "root", "-d", dist}
558558
newArgs = append(newArgs, arg...)
559-
return runCmdPassThrough("wsl", newArgs...)
559+
return runCmdPassThrough(wutil.FindWSL(), newArgs...)
560560
}
561561

562562
func wslPipe(input string, dist string, arg ...string) error {
563563
newArgs := []string{"-u", "root", "-d", dist}
564564
newArgs = append(newArgs, arg...)
565-
return pipeCmdPassThrough("wsl", input, newArgs...)
565+
return pipeCmdPassThrough(wutil.FindWSL(), input, newArgs...)
566566
}
567567

568568
func wslCreateKeys(identityPath string, dist string) (string, error) {
569-
return machine.CreateSSHKeysPrefix(identityPath, true, true, "wsl", "-u", "root", "-d", dist)
569+
return machine.CreateSSHKeysPrefix(identityPath, true, true, wutil.FindWSL(), "-u", "root", "-d", dist)
570570
}
571571

572572
func runCmdPassThrough(name string, arg ...string) error {
@@ -631,7 +631,7 @@ func obtainGlobalConfigLock() (*fileLock, error) {
631631
}
632632

633633
func IsWSLFeatureEnabled() bool {
634-
return wutil.SilentExec("wsl", "--set-default-version", "2") == nil
634+
return wutil.SilentExec(wutil.FindWSL(), "--set-default-version", "2") == nil
635635
}
636636

637637
func isWSLRunning(dist string) (bool, error) {
@@ -657,7 +657,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
657657
if running {
658658
args = append(args, "--running")
659659
}
660-
cmd := exec.Command("wsl", args...)
660+
cmd := exec.Command(wutil.FindWSL(), args...)
661661
out, err := cmd.StdoutPipe()
662662
if err != nil {
663663
return nil, err
@@ -681,7 +681,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
681681
}
682682

683683
func isSystemdRunning(dist string) (bool, error) {
684-
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh")
684+
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh")
685685
cmd.Stdin = strings.NewReader(sysdpid + "\necho $SYSDPID\n")
686686
out, err := cmd.StdoutPipe()
687687
if err != nil {
@@ -706,12 +706,12 @@ func isSystemdRunning(dist string) (bool, error) {
706706
}
707707

708708
func terminateDist(dist string) error {
709-
cmd := exec.Command("wsl", "--terminate", dist)
709+
cmd := exec.Command(wutil.FindWSL(), "--terminate", dist)
710710
return cmd.Run()
711711
}
712712

713713
func unregisterDist(dist string) error {
714-
cmd := exec.Command("wsl", "--unregister", dist)
714+
cmd := exec.Command(wutil.FindWSL(), "--unregister", dist)
715715
return cmd.Run()
716716
}
717717

@@ -753,7 +753,7 @@ func getCPUs(name string) (uint64, error) {
753753
if run, _ := isWSLRunning(dist); !run {
754754
return 0, nil
755755
}
756-
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "nproc")
756+
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "nproc")
757757
out, err := cmd.StdoutPipe()
758758
if err != nil {
759759
return 0, err
@@ -777,7 +777,7 @@ func getMem(name string) (uint64, error) {
777777
if run, _ := isWSLRunning(dist); !run {
778778
return 0, nil
779779
}
780-
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "cat", "/proc/meminfo")
780+
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "cat", "/proc/meminfo")
781781
out, err := cmd.StdoutPipe()
782782
if err != nil {
783783
return 0, err

pkg/machine/wsl/stubber.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/containers/podman/v5/pkg/machine/ocipull"
1414
"github.com/containers/podman/v5/pkg/machine/stdpull"
15+
"github.com/containers/podman/v5/pkg/machine/wsl/wutil"
1516

1617
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
1718
"github.com/containers/podman/v5/pkg/machine"
@@ -105,7 +106,7 @@ func (w WSLStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() error,
105106
// below if we wanted to hard error on the wsl unregister
106107
// of the vm
107108
wslRemoveFunc := func() error {
108-
if err := runCmdPassThrough("wsl", "--unregister", machine.ToDist(mc.Name)); err != nil {
109+
if err := runCmdPassThrough(wutil.FindWSL(), "--unregister", machine.ToDist(mc.Name)); err != nil {
109110
logrus.Error(err)
110111
}
111112
return machine.ReleaseMachinePort(mc.SSH.Port)
@@ -266,13 +267,13 @@ func (w WSLStubber) StopVM(mc *vmconfigs.MachineConfig, hardStop bool) error {
266267
fmt.Fprintf(os.Stderr, "Could not stop API forwarding service (win-sshproxy.exe): %s\n", err.Error())
267268
}
268269

269-
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh")
270+
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh")
270271
cmd.Stdin = strings.NewReader(waitTerm)
271272
if err = cmd.Start(); err != nil {
272273
return fmt.Errorf("executing wait command: %w", err)
273274
}
274275

275-
exitCmd := exec.Command("wsl", "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0")
276+
exitCmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0")
276277
if err = exitCmd.Run(); err != nil {
277278
return fmt.Errorf("stopping sysd: %w", err)
278279
}

pkg/machine/wsl/wutil/wutil.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,69 @@ package wutil
55
import (
66
"bufio"
77
"io"
8+
"os"
89
"os/exec"
10+
"path/filepath"
911
"strings"
12+
"sync"
1013
"syscall"
1114

1215
"golang.org/x/text/encoding/unicode"
1316
"golang.org/x/text/transform"
1417
)
1518

19+
var (
20+
once sync.Once
21+
wslPath string
22+
)
23+
24+
func FindWSL() string {
25+
// At the time of this writing, a defect appeared in the OS preinstalled WSL executable
26+
// where it no longer reliably locates the preferred Windows App Store variant.
27+
//
28+
// Manually discover (and cache) the wsl.exe location to bypass the problem
29+
once.Do(func() {
30+
var locs []string
31+
32+
// Prefer Windows App Store version
33+
if localapp := getLocalAppData(); localapp != "" {
34+
locs = append(locs, filepath.Join(localapp, "Microsoft", "WindowsApps", "wsl.exe"))
35+
}
36+
37+
// Otherwise, the common location for the legacy system version
38+
root := os.Getenv("SystemRoot")
39+
if root == "" {
40+
root = `C:\Windows`
41+
}
42+
locs = append(locs, filepath.Join(root, "System32", "wsl.exe"))
43+
44+
for _, loc := range locs {
45+
if _, err := os.Stat(loc); err == nil {
46+
wslPath = loc
47+
return
48+
}
49+
}
50+
51+
// Hope for the best
52+
wslPath = "wsl"
53+
})
54+
55+
return wslPath
56+
}
57+
58+
func getLocalAppData() string {
59+
localapp := os.Getenv("LOCALAPPDATA")
60+
if localapp != "" {
61+
return localapp
62+
}
63+
64+
if user := os.Getenv("USERPROFILE"); user != "" {
65+
return filepath.Join(user, "AppData", "Local")
66+
}
67+
68+
return localapp
69+
}
70+
1671
func SilentExec(command string, args ...string) error {
1772
cmd := exec.Command(command, args...)
1873
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
@@ -28,7 +83,7 @@ func SilentExecCmd(command string, args ...string) *exec.Cmd {
2883
}
2984

3085
func IsWSLInstalled() bool {
31-
cmd := SilentExecCmd("wsl", "--status")
86+
cmd := SilentExecCmd(FindWSL(), "--status")
3287
out, err := cmd.StdoutPipe()
3388
cmd.Stderr = nil
3489
if err != nil {
@@ -48,7 +103,7 @@ func IsWSLInstalled() bool {
48103
}
49104

50105
func IsWSLStoreVersionInstalled() bool {
51-
cmd := SilentExecCmd("wsl", "--version")
106+
cmd := SilentExecCmd(FindWSL(), "--version")
52107
cmd.Stdout = nil
53108
cmd.Stderr = nil
54109
if err := cmd.Run(); err != nil {

0 commit comments

Comments
 (0)