Files
kandev/apps/backend/internal/agent/lifecycle/mock_agent_resolver.go
T
José Almeida 2779fb24aa feat(docker): make mock-agent runnable inside Docker for E2E
Lay the backend groundwork so the existing mock-agent can run inside
local_docker containers — needed for real-Docker E2E tests that don't
require Anthropic/OpenAI API keys.

Container builder bind-mounts a host mock-agent binary at
/usr/local/bin/mock-agent when KANDEV_MOCK_AGENT_LINUX_BINARY is set
(or build/mock-agent-linux-amd64 sits next to the kandev binary), the
same pattern already used for agentctl. MockAgent.BuildCommand always
resolves via PATH so the same command works on host (E2E PATH includes
bin/) and in containers (mounted at /usr/local/bin/mock-agent).

Mock now declares non-nil empty RemoteAuth so it counts as a
remote-capable agent. The remoteauth catalog includes specs even with
zero methods, and the frontend gating helper treats an empty-method
spec as "no credentials needed" — letting mock-agent pass the
configure-on-executor check that hides uncatalogued agents on remote
executors.

repositoryCloneURL falls back to the local origin remote URL when the
repo has no provider info, so local-only repos with a real remote
(including file:// remotes used in E2E) work on remote executors that
clone inside the container.

Makefile: build-mock-agent-linux target mirrors build-agentctl-linux.
2026-05-04 21:09:39 +01:00

62 lines
1.9 KiB
Go

package lifecycle
import (
"fmt"
"os"
"path/filepath"
"go.uber.org/zap"
"github.com/kandev/kandev/internal/common/logger"
)
// MockAgentResolver finds the path to a linux/amd64 mock-agent binary so it
// can be bind-mounted into Docker containers for E2E tests. Resolution order:
// 1. KANDEV_MOCK_AGENT_LINUX_BINARY env var
// 2. build/mock-agent-linux-amd64 relative to the running binary (dev mode)
//
// Unlike AgentctlResolver, "not found" is not an error — only the Docker E2E
// suite needs this binary, so production paths get a nil mount silently.
type MockAgentResolver struct {
logger *logger.Logger
}
// NewMockAgentResolver creates a new resolver.
func NewMockAgentResolver(log *logger.Logger) *MockAgentResolver {
return &MockAgentResolver{
logger: log.WithFields(zap.String("component", "mock_agent_resolver")),
}
}
// ResolveLinuxBinary returns the path to a linux/amd64 mock-agent binary, or
// ("", nil) when none is configured. Returns an error only when an explicit
// env var path is invalid.
func (r *MockAgentResolver) ResolveLinuxBinary() (string, error) {
if envPath := os.Getenv("KANDEV_MOCK_AGENT_LINUX_BINARY"); envPath != "" {
if _, err := os.Stat(envPath); err == nil {
r.logger.Debug("using mock-agent from env var", zap.String("path", envPath))
return envPath, nil
}
return "", fmt.Errorf("KANDEV_MOCK_AGENT_LINUX_BINARY=%q does not exist", envPath)
}
exePath, err := os.Executable()
if err != nil {
return "", nil
}
exeDir := filepath.Dir(exePath)
candidates := []string{
filepath.Join(exeDir, "mock-agent-linux-amd64"),
filepath.Join(exeDir, "..", "build", "mock-agent-linux-amd64"),
filepath.Join(exeDir, "..", "bin", "mock-agent-linux-amd64"),
}
for _, candidate := range candidates {
if _, statErr := os.Stat(candidate); statErr == nil {
abs, _ := filepath.Abs(candidate)
r.logger.Debug("found mock-agent binary", zap.String("path", abs))
return abs, nil
}
}
return "", nil
}