-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from Code-Hex/add/available-version-tests
added available version tests
- Loading branch information
Showing
8 changed files
with
312 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package vz | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
func macosMajorVersionLessThan(major int) bool { | ||
return macOSMajorVersion() < major | ||
} | ||
|
||
var ( | ||
majorVersion int | ||
majorVersionOnce interface{ Do(func()) } = &sync.Once{} | ||
) | ||
|
||
// This can be replaced in the test code to enable mock. | ||
// It will not be changed in production. | ||
var fetchMajorVersion = func() { | ||
osver, err := syscall.Sysctl("kern.osproductversion") | ||
if err != nil { | ||
panic(err) | ||
} | ||
osverArray := strings.Split(osver, ".") | ||
major, err := strconv.Atoi(osverArray[0]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
majorVersion = major | ||
} | ||
|
||
func macOSMajorVersion() int { | ||
majorVersionOnce.Do(fetchMajorVersion) | ||
return majorVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package vz | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestAvailableVersionArm64(t *testing.T) { | ||
majorVersionOnce = &nopDoer{} | ||
defer func() { | ||
majorVersion = 0 | ||
majorVersionOnce = &sync.Once{} | ||
}() | ||
t.Run("macOS 12", func(t *testing.T) { | ||
majorVersion = 11 | ||
cases := map[string]func() error{ | ||
"NewMacOSBootLoader": func() error { | ||
_, err := NewMacOSBootLoader() | ||
return err | ||
}, | ||
"NewMacGraphicsDeviceConfiguration": func() error { | ||
_, err := NewMacGraphicsDeviceConfiguration() | ||
return err | ||
}, | ||
"NewMacGraphicsDisplayConfiguration": func() error { | ||
_, err := NewMacGraphicsDisplayConfiguration(0, 0, 0) | ||
return err | ||
}, | ||
"NewMacPlatformConfiguration": func() error { | ||
_, err := NewMacPlatformConfiguration() | ||
return err | ||
}, | ||
"NewMacHardwareModelWithData": func() error { | ||
_, err := NewMacHardwareModelWithData(nil) | ||
return err | ||
}, | ||
"NewMacMachineIdentifierWithData": func() error { | ||
_, err := NewMacMachineIdentifierWithData(nil) | ||
return err | ||
}, | ||
"NewMacMachineIdentifier": func() error { | ||
_, err := NewMacMachineIdentifier() | ||
return err | ||
}, | ||
"NewMacAuxiliaryStorage": func() error { | ||
_, err := NewMacAuxiliaryStorage("") | ||
return err | ||
}, | ||
"FetchLatestSupportedMacOSRestoreImage": func() error { | ||
_, err := FetchLatestSupportedMacOSRestoreImage(context.Background(), "") | ||
return err | ||
}, | ||
"LoadMacOSRestoreImageFromPath": func() error { | ||
_, err := LoadMacOSRestoreImageFromPath("") | ||
return err | ||
}, | ||
"NewMacOSInstaller": func() error { | ||
_, err := NewMacOSInstaller(nil, "") | ||
return err | ||
}, | ||
} | ||
for name, fn := range cases { | ||
err := fn() | ||
if !errors.Is(err, ErrUnsupportedOSVersion) { | ||
t.Fatalf("unexpected error %v in %s", err, name) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package vz | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
type nopDoer struct{} | ||
|
||
func (*nopDoer) Do(func()) {} | ||
|
||
func TestAvailableVersion(t *testing.T) { | ||
majorVersionOnce = &nopDoer{} | ||
defer func() { | ||
majorVersion = 0 | ||
majorVersionOnce = &sync.Once{} | ||
}() | ||
|
||
t.Run("macOS 11", func(t *testing.T) { | ||
majorVersion = 10 | ||
cases := map[string]func() error{ | ||
"NewLinuxBootLoader": func() error { | ||
_, err := NewLinuxBootLoader("") | ||
return err | ||
}, | ||
"NewVirtualMachineConfiguration": func() error { | ||
_, err := NewVirtualMachineConfiguration(nil, 0, 0) | ||
return err | ||
}, | ||
"NewFileHandleSerialPortAttachment": func() error { | ||
_, err := NewFileHandleSerialPortAttachment(nil, nil) | ||
return err | ||
}, | ||
"NewFileSerialPortAttachment": func() error { | ||
_, err := NewFileSerialPortAttachment("", false) | ||
return err | ||
}, | ||
"NewVirtioConsoleDeviceSerialPortConfiguration": func() error { | ||
_, err := NewVirtioConsoleDeviceSerialPortConfiguration(nil) | ||
return err | ||
}, | ||
"NewVirtioEntropyDeviceConfiguration": func() error { | ||
_, err := NewVirtioEntropyDeviceConfiguration() | ||
return err | ||
}, | ||
"NewVirtioTraditionalMemoryBalloonDeviceConfiguration": func() error { | ||
_, err := NewVirtioTraditionalMemoryBalloonDeviceConfiguration() | ||
return err | ||
}, | ||
"NewNATNetworkDeviceAttachment": func() error { | ||
_, err := NewNATNetworkDeviceAttachment() | ||
return err | ||
}, | ||
"NewBridgedNetworkDeviceAttachment": func() error { | ||
_, err := NewBridgedNetworkDeviceAttachment(nil) | ||
return err | ||
}, | ||
"NewFileHandleNetworkDeviceAttachment": func() error { | ||
_, err := NewFileHandleNetworkDeviceAttachment(nil) | ||
return err | ||
}, | ||
"NewVirtioNetworkDeviceConfiguration": func() error { | ||
_, err := NewVirtioNetworkDeviceConfiguration(nil) | ||
return err | ||
}, | ||
"NewMACAddress": func() error { | ||
_, err := NewMACAddress(nil) | ||
return err | ||
}, | ||
"NewRandomLocallyAdministeredMACAddress": func() error { | ||
_, err := NewRandomLocallyAdministeredMACAddress() | ||
return err | ||
}, | ||
"NewVirtioSocketDeviceConfiguration": func() error { | ||
_, err := NewVirtioSocketDeviceConfiguration() | ||
return err | ||
}, | ||
"NewVirtioSocketListener": func() error { | ||
_, err := NewVirtioSocketListener(nil) | ||
return err | ||
}, | ||
"NewDiskImageStorageDeviceAttachment": func() error { | ||
_, err := NewDiskImageStorageDeviceAttachment("", false) | ||
return err | ||
}, | ||
"NewVirtioBlockDeviceConfiguration": func() error { | ||
_, err := NewVirtioBlockDeviceConfiguration(nil) | ||
return err | ||
}, | ||
"NewVirtualMachine": func() error { | ||
_, err := NewVirtualMachine(nil) | ||
return err | ||
}, | ||
} | ||
for name, fn := range cases { | ||
err := fn() | ||
if !errors.Is(err, ErrUnsupportedOSVersion) { | ||
t.Fatalf("unexpected error %v in %s", err, name) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("macOS 12", func(t *testing.T) { | ||
majorVersion = 11 | ||
cases := map[string]func() error{ | ||
"NewVirtioSoundDeviceConfiguration": func() error { | ||
_, err := NewVirtioSoundDeviceConfiguration() | ||
return err | ||
}, | ||
"NewVirtioSoundDeviceHostInputStreamConfiguration": func() error { | ||
_, err := NewVirtioSoundDeviceHostInputStreamConfiguration() | ||
return err | ||
}, | ||
"NewVirtioSoundDeviceHostOutputStreamConfiguration": func() error { | ||
_, err := NewVirtioSoundDeviceHostOutputStreamConfiguration() | ||
return err | ||
}, | ||
"NewUSBKeyboardConfiguration": func() error { | ||
_, err := NewUSBKeyboardConfiguration() | ||
return err | ||
}, | ||
"NewGenericPlatformConfiguration": func() error { | ||
_, err := NewGenericPlatformConfiguration() | ||
return err | ||
}, | ||
"NewUSBScreenCoordinatePointingDeviceConfiguration": func() error { | ||
_, err := NewUSBScreenCoordinatePointingDeviceConfiguration() | ||
return err | ||
}, | ||
"NewVirtioFileSystemDeviceConfiguration": func() error { | ||
_, err := NewVirtioFileSystemDeviceConfiguration("") | ||
return err | ||
}, | ||
"NewSharedDirectory": func() error { | ||
_, err := NewSharedDirectory("", false) | ||
return err | ||
}, | ||
"NewSingleDirectoryShare": func() error { | ||
_, err := NewSingleDirectoryShare(nil) | ||
return err | ||
}, | ||
"NewMultipleDirectoryShare": func() error { | ||
_, err := NewMultipleDirectoryShare(nil) | ||
return err | ||
}, | ||
"(*VirtualMachine).Stop": func() error { | ||
var err error | ||
(*VirtualMachine)(nil).Stop(func(e error) { | ||
err = e | ||
}) | ||
return err | ||
}, | ||
"(*VirtualMachine).StartGraphicApplication": func() error { | ||
return (*VirtualMachine)(nil).StartGraphicApplication(0, 0) | ||
}, | ||
} | ||
for name, fn := range cases { | ||
err := fn() | ||
if !errors.Is(err, ErrUnsupportedOSVersion) { | ||
t.Fatalf("unexpected error %v in %s", err, name) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.