Skip to content

Commit

Permalink
Merge pull request #1749 from BlackHole1/add-apphv-host-type
Browse files Browse the repository at this point in the history
refactor(machine): add applehv and hyperv host type
  • Loading branch information
openshift-merge-bot[bot] authored Nov 27, 2023
2 parents e2da7be + ffe7e60 commit 082c2ed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
24 changes: 10 additions & 14 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import (
"github.com/sirupsen/logrus"
)

// TODO: change name to MachineMarker since package is already called machine
//
//nolint:revive
type MachineMarker struct {
type Marker struct {
Enabled bool
Type string
}
Expand All @@ -21,11 +18,13 @@ const (
markerFile = "/etc/containers/podman-machine"
Wsl = "wsl"
Qemu = "qemu"
AppleHV = "applehv"
HyperV = "hyperv"
)

var (
markerSync sync.Once
machineMarker *MachineMarker
markerSync sync.Once
marker *Marker
)

func loadMachineMarker(file string) {
Expand All @@ -39,7 +38,7 @@ func loadMachineMarker(file string) {
kind = strings.TrimSpace(string(content))
}

machineMarker = &MachineMarker{enabled, kind}
marker = &Marker{enabled, kind}
}

func isLegacyConfigSet() bool {
Expand All @@ -57,20 +56,17 @@ func IsPodmanMachine() bool {
return GetMachineMarker().Enabled
}

// TODO: change name to HostType since package is already called machine
//
//nolint:revive
func MachineHostType() string {
func HostType() string {
return GetMachineMarker().Type
}

func IsGvProxyBased() bool {
return IsPodmanMachine() && MachineHostType() != Wsl
return IsPodmanMachine() && HostType() != Wsl
}

func GetMachineMarker() *MachineMarker {
func GetMachineMarker() *Marker {
markerSync.Do(func() {
loadMachineMarker(markerFile)
})
return machineMarker
return marker
}
32 changes: 28 additions & 4 deletions pkg/machine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,58 @@ var _ = Describe("Machine", func() {
loadMachineMarker("testdata/does-not-exist")

gomega.Expect(IsPodmanMachine()).To(gomega.BeFalse())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(HostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeFalse())
})

It("generic machine", func() {
loadMachineMarker("testdata/empty-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(HostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})

It("wsl machine", func() {
loadMachineMarker("testdata/wsl-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.Equal(Wsl))
gomega.Expect(HostType()).To(gomega.Equal(Wsl))
gomega.Expect(IsGvProxyBased()).To(gomega.BeFalse())
})

It("qemu machine", func() {
loadMachineMarker("testdata/qemu-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(HostType()).To(gomega.Equal(Qemu))
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})

It("applehv machine", func() {
loadMachineMarker("testdata/applehv-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(HostType()).To(gomega.Equal(AppleHV))
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})

It("hyperv machine", func() {
loadMachineMarker("testdata/hyperv-machine")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(HostType()).To(gomega.Equal(HyperV))
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})

It("legacy config machine", func() {
config, _ := config.Default()
//nolint:staticcheck //lint:ignore SA1019 deprecated call
config.Engine.MachineEnabled = true
loadMachineMarker("testdata/does-not-exist")

gomega.Expect(IsPodmanMachine()).To(gomega.BeTrue())
gomega.Expect(MachineHostType()).To(gomega.BeEmpty())
gomega.Expect(HostType()).To(gomega.BeEmpty())
gomega.Expect(IsGvProxyBased()).To(gomega.BeTrue())
})
})
1 change: 1 addition & 0 deletions pkg/machine/testdata/applehv-machine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
applehv
1 change: 1 addition & 0 deletions pkg/machine/testdata/hyperv-machine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hyperv
1 change: 1 addition & 0 deletions pkg/machine/testdata/qemu-machine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qemu

0 comments on commit 082c2ed

Please sign in to comment.