test: De-duplicate driver detection

Add matchDrivers() helper for looking up --driver=name or
--vm-driver=name.
pull/21644/head
Nir Soffer 2025-09-27 17:55:24 +03:00
parent 150c63cca4
commit f16e6d958d
1 changed files with 19 additions and 7 deletions

View File

@ -134,32 +134,32 @@ func Target() string {
// NoneDriver returns whether or not this test is using the none driver // NoneDriver returns whether or not this test is using the none driver
func NoneDriver() bool { func NoneDriver() bool {
return strings.Contains(*startArgs, "--driver=none") || strings.Contains(*startArgs, "--vm-driver=none") return matchDriverFlag("none")
} }
// HyperVDriver returns whether or not this test is using the Hyper-V driver // HyperVDriver returns whether or not this test is using the Hyper-V driver
func HyperVDriver() bool { func HyperVDriver() bool {
return strings.Contains(*startArgs, "--driver=hyperv") || strings.Contains(*startArgs, "--vm-driver=hyperv") return matchDriverFlag("hyperv")
} }
// KVM returns true is is KVM driver // KVM returns true is is KVM driver
func KVMDriver() bool { func KVMDriver() bool {
return strings.Contains(*startArgs, "--driver=kvm") || strings.Contains(*startArgs, "--vm-driver=kvm") || strings.Contains(*startArgs, "--driver=kvm2") || strings.Contains(*startArgs, "--vm-driver=kvm2") return matchDriverFlag("kvm", "kvm2")
} }
// VirtualboxDriver returns whether or not this test is using the VirtualBox driver // VirtualboxDriver returns whether or not this test is using the VirtualBox driver
func VirtualboxDriver() bool { func VirtualboxDriver() bool {
return strings.Contains(*startArgs, "--driver=virtualbox") || strings.Contains(*startArgs, "--vm-driver=virtualbox") return matchDriverFlag("virtualbox")
} }
// DockerDriver returns whether or not this test is using the docker or podman driver // DockerDriver returns whether or not this test is using the docker or podman driver
func DockerDriver() bool { func DockerDriver() bool {
return strings.Contains(*startArgs, "--driver=docker") || strings.Contains(*startArgs, "--vm-driver=docker") return matchDriverFlag("docker")
} }
// PodmanDriver returns whether or not this test is using the docker or podman driver // PodmanDriver returns whether or not this test is using the docker or podman driver
func PodmanDriver() bool { func PodmanDriver() bool {
return strings.Contains(*startArgs, "--driver=podman") || strings.Contains(*startArgs, "--vm-driver=podman") return matchDriverFlag("podman")
} }
// RootlessDriver returns whether or not this test is using the rootless KIC driver // RootlessDriver returns whether or not this test is using the rootless KIC driver
@ -173,7 +173,7 @@ func KicDriver() bool {
} }
func HyperkitDriver() bool { func HyperkitDriver() bool {
return strings.Contains(*startArgs, "--driver=hyperkit") || strings.Contains(*startArgs, "--vm-driver=hyperkit") return matchDriverFlag("hyperkit")
} }
// NeedsAuxDriver Returns true if the driver needs an auxiliary driver (kvm, hyperkit,..) // NeedsAuxDriver Returns true if the driver needs an auxiliary driver (kvm, hyperkit,..)
@ -232,3 +232,15 @@ func Seconds(n int) time.Duration {
func TestingKicBaseImage() bool { func TestingKicBaseImage() bool {
return strings.Contains(*startArgs, "base-image") return strings.Contains(*startArgs, "base-image")
} }
func matchDriverFlag(names ...string) bool {
args := *startArgs
for _, name := range names {
for _, prefix := range []string{"--driver=", "--vm-driver="} {
if strings.Contains(args, prefix+name) {
return true
}
}
}
return false
}