44package e2e
55
66import (
7+ "fmt"
78 "os"
9+ "os/exec"
810 "path/filepath"
11+ "regexp"
12+ "runtime"
913 "testing"
1014
1115 "github.com/onsi/ginkgo/v2"
@@ -38,19 +42,31 @@ func TestE2e(t *testing.T) {
3842 t .Fatalf ("failed to get the current working directory: %v" , err )
3943 }
4044
41- vmConfigFile := filepath .Join (wd , "./../_output/lima-template/fedora.yaml" )
45+ configFileName := "macos.yaml"
46+ if runtime .GOOS == "windows" {
47+ configFileName = "windows.yaml"
48+ }
49+ vmConfigFile := filepath .Join (wd , "./../_output/lima-template/" , configFileName )
4250
4351 subject := "limactl"
4452 limaOpt , err := option .New ([]string {subject })
4553 if err != nil {
4654 t .Fatalf ("failed to initialize a testing option: %v" , err )
4755 }
4856
49- vmName := "fedora"
57+ vmName := "finch"
58+
59+ // We run nerdctl through lima directly rather than the Finch CLI, so the
60+ // option layer must resolve valueless -e/--env/--env-file entries against the
61+ // host environment (the Finch CLI does this natively).
62+ nerdctlMods := []option.Modifier {option .WithResolveEnvVarPassthrough ()}
63+ if runtime .GOOS == "windows" {
64+ nerdctlMods = append (nerdctlMods , option .WithWindowsHostPathTranslation ())
65+ }
5066
5167 nerdctlOpt , err := option .New (
5268 []string {subject , "shell" , vmName , "sudo" , "-E" , "nerdctl" },
53- option . WithNoEnvironmentVariablePassthrough () ,
69+ nerdctlMods ... ,
5470 )
5571 if err != nil {
5672 t .Fatalf ("failed to initialize a testing option: %v" , err )
@@ -62,13 +78,49 @@ func TestE2e(t *testing.T) {
6278 vmType = "vz"
6379 }
6480
81+ var runOpt * tests.RunOption
82+ switch runtime .GOOS {
83+ case "windows" :
84+ runOpt = & tests.RunOption {
85+ BaseOpt : nerdctlOpt ,
86+ CGMode : tests .Hybrid ,
87+ }
88+ case "darwin" :
89+ runOpt = & tests.RunOption {
90+ BaseOpt : nerdctlOpt ,
91+ CGMode : tests .Unified ,
92+ // See https://lima-vm.io/docs/config/network/user/.
93+ DefaultHostGatewayIP : "192.168.5.2" ,
94+ }
95+ }
96+
6597 ginkgo .SynchronizedBeforeSuite (func () []byte {
6698 limactlStartOpts := []string {"start" , vmConfigFile , "--name" , vmName , "--vm-type" , vmType }
6799 if vmType == "vz" {
68100 limactlStartOpts = append (limactlStartOpts , "--set" , ".ssh.overVsock=false" )
69101 }
70102 command .New (limaOpt , limactlStartOpts ... ).WithTimeoutInSeconds (600 ).Run ()
71103 tests .SetupLocalRegistry (nerdctlOpt )
104+
105+ // Get the WSL host gateway ip using netsh. This is only available when a
106+ // WSL VM instance is running.
107+ if runtime .GOOS == "windows" {
108+ n , err := exec .Command ("netsh" , "interface" , "ipv4" , "show" , "addresses" , "vEthernet (WSL (Hyper-V firewall))" ).CombinedOutput ()
109+ gomega .Expect (err ).Should (gomega .BeNil (), "netsh output: %s" , string (n ))
110+ runOpt .DefaultHostGatewayIP = extractIPAddress (string (n ))
111+ }
112+
113+ // Finch CLI rewrites `--add-host <host>:host-gateway` to the host
114+ // IP before invoking nerdctl. Our subject runs nerdctl theough lima directly, so nerdctl
115+ // will fall back to its own default (the guest's own IP).
116+ // Configure the guest's nerdctl.toml so nerdctl resolves host-gateway to the host IP.
117+ if runOpt != nil && runOpt .DefaultHostGatewayIP != "" {
118+ script := fmt .Sprintf (
119+ "mkdir -p /etc/nerdctl && printf 'host_gateway_ip = \" %s\" \\ n' > /etc/nerdctl/nerdctl.toml" ,
120+ runOpt .DefaultHostGatewayIP ,
121+ )
122+ command .New (limaOpt , "shell" , vmName , "sudo" , "sh" , "-c" , script ).Run ()
123+ }
72124 return nil
73125 }, func (bytes []byte ) {})
74126
@@ -79,7 +131,11 @@ func TestE2e(t *testing.T) {
79131 }, func () {})
80132
81133 ginkgo .Describe (description , func () {
82- // TODO: add more e2e tests and make them work.
134+ if runOpt != nil {
135+ tests .Run (runOpt )
136+ }
137+ tests .Ps (nerdctlOpt )
138+ tests .Restart (nerdctlOpt )
83139 tests .Save (nerdctlOpt )
84140 tests .Load (nerdctlOpt )
85141 tests .Pull (nerdctlOpt )
@@ -129,6 +185,16 @@ func TestE2e(t *testing.T) {
129185 ginkgo .RunSpecs (t , description )
130186}
131187
188+ func extractIPAddress (data string ) string {
189+ re := regexp .MustCompile (`IP Address:\s+(\d+\.\d+\.\d+\.\d+)` )
190+ match := re .FindStringSubmatch (data )
191+
192+ if match != nil {
193+ return match [1 ]
194+ }
195+ return ""
196+ }
197+
132198func printLimaLogs (vmName string ) {
133199 userHomeDir := os .Getenv ("HOME" )
134200 if userHomeDir == "" {
0 commit comments