Skip to content

Commit 0cdcefd

Browse files
authored
Merge pull request #915 from afbjorklund/windows-unittests
Fix test failures on windows, mostly paths
2 parents e0852c9 + d46f694 commit 0cdcefd

File tree

7 files changed

+65
-33
lines changed

7 files changed

+65
-33
lines changed

pkg/cidata/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io/fs"
9-
"path/filepath"
9+
"path"
1010

1111
"github.com/lima-vm/lima/pkg/iso9660util"
1212

@@ -82,7 +82,7 @@ func ValidateTemplateArgs(args TemplateArgs) error {
8282
}
8383
for i, m := range args.Mounts {
8484
f := m.MountPoint
85-
if !filepath.IsAbs(f) {
85+
if !path.IsAbs(f) {
8686
return fmt.Errorf("field mounts[%d] must be absolute, got %q", i, f)
8787
}
8888
}

pkg/hostagent/dns/dns_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package dns
22

33
import (
4+
"fmt"
45
"io"
56
"log"
67
"net"
78
"regexp"
9+
"runtime"
810
"testing"
911

1012
"github.com/foxcpp/go-mockdns"
1113
"github.com/miekg/dns"
1214
"gotest.tools/v3/assert"
15+
"gotest.tools/v3/assert/cmp"
1316
)
1417

1518
var (
@@ -43,6 +46,10 @@ func TestTXTRecords(t *testing.T) {
4346
}, log.New(io.Discard, "mockdns server: ", log.LstdFlags), false)
4447
defer srv.Close()
4548

49+
if runtime.GOOS == "windows" {
50+
// "On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery."
51+
t.Skip()
52+
}
4653
srv.PatchNet(net.DefaultResolver)
4754
defer mockdns.UnpatchNet(net.DefaultResolver)
4855

@@ -56,8 +63,17 @@ func TestTXTRecords(t *testing.T) {
5663
req := new(dns.Msg)
5764
req.SetQuestion(dns.Fqdn(testDomains[i]), dns.TypeTXT)
5865
h.ServeDNS(w, req)
59-
regex_check := regexp.MustCompile(expectedResults[i]).MatchString(dnsResult.String())
60-
assert.Assert(t, regex_check)
66+
regex_match := func(value string, pattern string) cmp.Comparison {
67+
return func() cmp.Result {
68+
re := regexp.MustCompile(pattern)
69+
if re.MatchString(value) {
70+
return cmp.ResultSuccess
71+
}
72+
return cmp.ResultFailure(
73+
fmt.Sprintf("%q did not match pattern %q", value, pattern))
74+
}
75+
}
76+
assert.Assert(t, regex_match(dnsResult.String(), expectedResults[i]))
6177
}
6278
}
6379
})

pkg/limayaml/defaults.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net"
88
"os"
9-
osuser "os/user"
109
"path/filepath"
1110
"runtime"
1211
"strconv"
@@ -569,7 +568,7 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
569568
if rule.HostSocket != "" {
570569
tmpl, err := template.New("").Parse(rule.HostSocket)
571570
if err == nil {
572-
user, _ := osuser.Current()
571+
user, _ := osutil.LimaUser(false)
573572
home, _ := os.UserHomeDir()
574573
limaHome, _ := dirnames.LimaDir()
575574
data := map[string]string{

pkg/limayaml/validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net"
66
"os"
7+
"path"
78
"path/filepath"
89
"runtime"
910
"strings"
@@ -217,7 +218,7 @@ func Validate(y LimaYAML, warn bool) error {
217218
return fmt.Errorf("field `%s.hostPortRange` must specify the same number of ports as field `%s.guestPortRange`", field, field)
218219
}
219220
if rule.GuestSocket != "" {
220-
if !filepath.IsAbs(rule.GuestSocket) {
221+
if !path.IsAbs(rule.GuestSocket) {
221222
return fmt.Errorf("field `%s.guestSocket` must be an absolute path", field)
222223
}
223224
if rule.HostSocket == "" && rule.HostPortRange[1]-rule.HostPortRange[0] > 0 {

pkg/networks/commands.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package networks
22

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/lima-vm/lima/pkg/osutil"
78
"github.com/lima-vm/lima/pkg/store/dirnames"
@@ -23,16 +24,16 @@ func (config *NetworksConfig) Check(name string) error {
2324
}
2425

2526
func (config *NetworksConfig) VDESock(name string) string {
26-
return fmt.Sprintf("%s/%s.ctl", config.Paths.VarRun, name)
27+
return filepath.Join(config.Paths.VarRun, fmt.Sprintf("%s.ctl", name))
2728
}
2829

2930
func (config *NetworksConfig) PIDFile(name, daemon string) string {
30-
return fmt.Sprintf("%s/%s_%s.pid", config.Paths.VarRun, name, daemon)
31+
return filepath.Join(config.Paths.VarRun, fmt.Sprintf("%s_%s.pid", name, daemon))
3132
}
3233

3334
func (config *NetworksConfig) LogFile(name, daemon, stream string) string {
3435
networksDir, _ := dirnames.LimaNetworksDir()
35-
return fmt.Sprintf("%s/%s_%s.%s.log", networksDir, name, daemon, stream)
36+
return filepath.Join(networksDir, fmt.Sprintf("%s_%s.%s.log", name, daemon, stream))
3637
}
3738

3839
func (config *NetworksConfig) User(daemon string) (osutil.User, error) {

pkg/networks/commands_darwin_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package networks
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestVDESock(t *testing.T) {
10+
config, err := DefaultConfig()
11+
assert.NilError(t, err)
12+
13+
vdeSock := config.VDESock("foo")
14+
assert.Equal(t, vdeSock, "/private/var/run/lima/foo.ctl")
15+
}
16+
17+
func TestPIDFile(t *testing.T) {
18+
config, err := DefaultConfig()
19+
assert.NilError(t, err)
20+
21+
pidFile := config.PIDFile("name", "daemon")
22+
assert.Equal(t, pidFile, "/private/var/run/lima/name_daemon.pid")
23+
}

pkg/networks/commands_test.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ func TestCheck(t *testing.T) {
2121
assert.ErrorContains(t, err, "not defined")
2222
}
2323

24-
func TestVDESock(t *testing.T) {
25-
config, err := DefaultConfig()
26-
assert.NilError(t, err)
27-
28-
vdeSock := config.VDESock("foo")
29-
assert.Equal(t, vdeSock, "/private/var/run/lima/foo.ctl")
30-
}
31-
32-
func TestPIDFile(t *testing.T) {
33-
config, err := DefaultConfig()
34-
assert.NilError(t, err)
35-
36-
pidFile := config.PIDFile("name", "daemon")
37-
assert.Equal(t, pidFile, "/private/var/run/lima/name_daemon.pid")
38-
}
39-
4024
func TestLogFile(t *testing.T) {
4125
config, err := DefaultConfig()
4226
assert.NilError(t, err)
@@ -54,6 +38,10 @@ func TestUser(t *testing.T) {
5438
// The "everyone" group is a specific macOS feature to include non-local accounts.
5539
config.Group = "staff"
5640
}
41+
if runtime.GOOS == "windows" {
42+
// unimplemented
43+
t.Skip()
44+
}
5745

5846
user, err := config.User(Switch)
5947
assert.NilError(t, err)
@@ -87,23 +75,27 @@ func TestStartCmd(t *testing.T) {
8775
config, err := DefaultConfig()
8876
assert.NilError(t, err)
8977

78+
varRunDir := filepath.Join("/", "private", "var", "run", "lima")
79+
9080
cmd := config.StartCmd("shared", Switch)
91-
assert.Equal(t, cmd, "/opt/vde/bin/vde_switch --pidfile=/private/var/run/lima/shared_switch.pid "+
92-
"--sock=/private/var/run/lima/shared.ctl --group=everyone --dirmode=0770 --nostdin")
81+
assert.Equal(t, cmd, "/opt/vde/bin/vde_switch --pidfile="+filepath.Join(varRunDir, "shared_switch.pid")+" "+
82+
"--sock="+filepath.Join(varRunDir, "shared.ctl")+" --group=everyone --dirmode=0770 --nostdin")
9383

9484
cmd = config.StartCmd("shared", VMNet)
95-
assert.Equal(t, cmd, "/opt/vde/bin/vde_vmnet --pidfile=/private/var/run/lima/shared_vmnet.pid --vde-group=everyone --vmnet-mode=shared "+
96-
"--vmnet-gateway=192.168.105.1 --vmnet-dhcp-end=192.168.105.254 --vmnet-mask=255.255.255.0 /private/var/run/lima/shared.ctl")
85+
assert.Equal(t, cmd, "/opt/vde/bin/vde_vmnet --pidfile="+filepath.Join(varRunDir, "shared_vmnet.pid")+" --vde-group=everyone --vmnet-mode=shared "+
86+
"--vmnet-gateway=192.168.105.1 --vmnet-dhcp-end=192.168.105.254 --vmnet-mask=255.255.255.0 "+filepath.Join(varRunDir, "shared.ctl"))
9787

9888
cmd = config.StartCmd("bridged", VMNet)
99-
assert.Equal(t, cmd, "/opt/vde/bin/vde_vmnet --pidfile=/private/var/run/lima/bridged_vmnet.pid --vde-group=everyone --vmnet-mode=bridged "+
100-
"--vmnet-interface=en0 /private/var/run/lima/bridged.ctl")
89+
assert.Equal(t, cmd, "/opt/vde/bin/vde_vmnet --pidfile="+filepath.Join(varRunDir, "bridged_vmnet.pid")+" --vde-group=everyone --vmnet-mode=bridged "+
90+
"--vmnet-interface=en0 "+filepath.Join(varRunDir, "bridged.ctl"))
10191
}
10292

10393
func TestStopCmd(t *testing.T) {
10494
config, err := DefaultConfig()
10595
assert.NilError(t, err)
10696

97+
varRunDir := filepath.Join("/", "private", "var", "run", "lima")
98+
10799
cmd := config.StopCmd("name", "daemon")
108-
assert.Equal(t, cmd, "/usr/bin/pkill -F /private/var/run/lima/name_daemon.pid")
100+
assert.Equal(t, cmd, "/usr/bin/pkill -F "+filepath.Join(varRunDir, "name_daemon.pid"))
109101
}

0 commit comments

Comments
 (0)