Files
AetherForge/agent/miner/runtime_detect_test.go
AetherForge d18c5910c2
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Close automatable P2 test gaps with mocks and httptest integration.
Add container/podman exec mocks, BITS/curl HiddenRun coverage, WinRM/GPO/systemd deploy-plan httptest, and a 3-hop discover-spread Playwright stub chain.
2026-06-07 06:12:08 -07:00

92 lines
2.2 KiB
Go

package miner
import (
"os/exec"
"runtime"
"testing"
)
func mockVersionOKCmd(_ string, args ...string) *exec.Cmd {
if runtime.GOOS == "windows" {
if len(args) >= 2 && args[0] == "version" && args[1] == "--format" {
return exec.Command("cmd", "/c", "echo", "24.0.1")
}
return exec.Command("cmd", "/c", "exit", "0")
}
if len(args) >= 2 && args[0] == "version" && args[1] == "--format" {
return exec.Command("sh", "-c", "echo 24.0.1")
}
return exec.Command("true")
}
func mockVersionPodmanCmd(_ string, args ...string) *exec.Cmd {
if runtime.GOOS == "windows" {
if len(args) >= 2 && args[0] == "version" && args[1] == "--format" {
return exec.Command("cmd", "/c", "echo", "4.9.0")
}
return exec.Command("cmd", "/c", "exit", "0")
}
if len(args) >= 2 && args[0] == "version" && args[1] == "--format" {
return exec.Command("sh", "-c", "echo 4.9.0")
}
return exec.Command("true")
}
func TestDetectContainerRuntimePrefersDocker(t *testing.T) {
SetContainerRuntimeProbes(
func(name string) (string, error) {
if name == "docker" {
return "/usr/bin/docker", nil
}
if name == "podman" {
return "/usr/bin/podman", nil
}
return "", exec.ErrNotFound
},
mockVersionOKCmd,
)
defer SetContainerRuntimeProbes(nil, nil)
rt := DetectContainerRuntime()
if !rt.Available || rt.CLI != "docker" {
t.Fatalf("runtime=%+v want docker", rt)
}
if rt.Version != "24.0.1" {
t.Fatalf("version=%q", rt.Version)
}
}
func TestDetectContainerRuntimeFallsBackToPodman(t *testing.T) {
SetContainerRuntimeProbes(
func(name string) (string, error) {
if name == "podman" {
return "/usr/bin/podman", nil
}
return "", exec.ErrNotFound
},
mockVersionPodmanCmd,
)
defer SetContainerRuntimeProbes(nil, nil)
rt := DetectContainerRuntime()
if !rt.Available || rt.CLI != "podman" {
t.Fatalf("runtime=%+v want podman", rt)
}
if rt.Version != "4.9.0" {
t.Fatalf("version=%q", rt.Version)
}
}
func TestDetectContainerRuntimeUnavailable(t *testing.T) {
SetContainerRuntimeProbes(
func(string) (string, error) { return "", exec.ErrNotFound },
nil,
)
defer SetContainerRuntimeProbes(nil, nil)
rt := DetectContainerRuntime()
if rt.Available || rt.CLI != "" {
t.Fatalf("runtime=%+v want unavailable", rt)
}
}