| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "runtime" |
| 9 | "slices" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/lima-vm/sshocker/pkg/ssh" |
| 13 | ) |
| 14 | |
| 15 | func TestResourceProbeCommand(t *testing.T) { |
| 16 | agent := HostAgent{ |
| 17 | sshLocalPort: 60022, instSSHAddress: "127.0.0.1", |
| 18 | sshConfig: &ssh.SSHConfig{AdditionalArgs: []string{ |
| 19 | "-o", "IdentityFile=private-instance-key", "-o", "ControlMaster=auto", |
| 20 | "-o", "ControlPath=instance/ssh.sock", "-o", "ControlPersist=5m", |
| 21 | }}, |
| 22 | } |
| 23 | configured := slices.Clone(agent.sshConfig.Args()) |
| 24 | const probe = "cat /proc/meminfo" |
| 25 | cmd := agent.resourceProbeCommand(context.Background(), probe) |
| 26 | want := slices.Clone(configured) |
| 27 | if runtime.GOOS == "windows" { |
| 28 | want = []string{ |
| 29 | "-o", "ControlMaster=no", "-o", "ControlPath=none", "-o", "ControlPersist=no", |
| 30 | "-o", "IdentityFile=private-instance-key", |
| 31 | } |
| 32 | } |
| 33 | want = append(want, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", "60022", "127.0.0.1", "--", probe) |
| 34 | if !slices.Equal(cmd.Args[1:], want) { |
| 35 | t.Fatalf("probe arguments: got %q, want %q", cmd.Args[1:], want) |
| 36 | } |
| 37 | if !slices.Equal(configured, agent.sshConfig.Args()) { |
| 38 | t.Fatal("resource probe changed the SSH configuration used by other host-agent tasks") |
| 39 | } |
| 40 | } |