|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 10 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/storage" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "go.uber.org/zap" |
| 15 | +) |
| 16 | + |
| 17 | +// mockDockerStatusController lets a test drive the genuine daemon probe and the |
| 18 | +// configured docker_isolation.enabled flag independently, while keeping |
| 19 | +// GetDockerRecoveryStatus returning the synthetic DockerAvailable:true that |
| 20 | +// production returns when Docker recovery is disabled (isolation off). This |
| 21 | +// proves /api/v1/docker/status reports the GENUINE probe value, not the |
| 22 | +// synthetic recovery-state value (MCP-2478). |
| 23 | +type mockDockerStatusController struct { |
| 24 | + baseController |
| 25 | + apiKey string |
| 26 | + dockerAvailable bool |
| 27 | + isolationEnabled bool |
| 28 | +} |
| 29 | + |
| 30 | +func (m *mockDockerStatusController) GetCurrentConfig() any { |
| 31 | + return &config.Config{APIKey: m.apiKey} |
| 32 | +} |
| 33 | + |
| 34 | +func (m *mockDockerStatusController) IsDockerAvailable() bool { return m.dockerAvailable } |
| 35 | + |
| 36 | +func (m *mockDockerStatusController) GetDockerRecoveryStatus() *storage.DockerRecoveryState { |
| 37 | + // Mirror the production short-circuit: when recovery is disabled the manager |
| 38 | + // returns a synthetic DockerAvailable:true. The endpoint must NOT forward |
| 39 | + // this as genuine availability. |
| 40 | + return &storage.DockerRecoveryState{DockerAvailable: true} |
| 41 | +} |
| 42 | + |
| 43 | +func (m *mockDockerStatusController) GetConfig() (*config.Config, error) { |
| 44 | + return &config.Config{ |
| 45 | + APIKey: m.apiKey, |
| 46 | + DockerIsolation: &config.DockerIsolationConfig{Enabled: m.isolationEnabled}, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func TestHandleGetDockerStatus_GenuineAvailability(t *testing.T) { |
| 51 | + logger := zap.NewNop().Sugar() |
| 52 | + const apiKey = "test-docker-status-key" |
| 53 | + |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + dockerAvailable bool |
| 57 | + isolationEnabled bool |
| 58 | + wantAvailable bool |
| 59 | + wantIsolationOn bool |
| 60 | + }{ |
| 61 | + { |
| 62 | + // The reported bug: no Docker daemon + isolation disabled must NOT |
| 63 | + // report docker_available:true (which the synthetic recovery state |
| 64 | + // would). |
| 65 | + name: "no docker daemon and isolation disabled", |
| 66 | + dockerAvailable: false, |
| 67 | + isolationEnabled: false, |
| 68 | + wantAvailable: false, |
| 69 | + wantIsolationOn: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "docker present and isolation enabled", |
| 73 | + dockerAvailable: true, |
| 74 | + isolationEnabled: true, |
| 75 | + wantAvailable: true, |
| 76 | + wantIsolationOn: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "docker present but isolation disabled", |
| 80 | + dockerAvailable: true, |
| 81 | + isolationEnabled: false, |
| 82 | + wantAvailable: true, |
| 83 | + wantIsolationOn: false, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tc := range tests { |
| 88 | + t.Run(tc.name, func(t *testing.T) { |
| 89 | + ctrl := &mockDockerStatusController{ |
| 90 | + apiKey: apiKey, |
| 91 | + dockerAvailable: tc.dockerAvailable, |
| 92 | + isolationEnabled: tc.isolationEnabled, |
| 93 | + } |
| 94 | + srv := NewServer(ctrl, logger, nil) |
| 95 | + |
| 96 | + req := httptest.NewRequest("GET", "/api/v1/docker/status?apikey="+apiKey, nil) |
| 97 | + w := httptest.NewRecorder() |
| 98 | + srv.ServeHTTP(w, req) |
| 99 | + |
| 100 | + require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String()) |
| 101 | + |
| 102 | + var resp struct { |
| 103 | + Success bool `json:"success"` |
| 104 | + Data struct { |
| 105 | + DockerAvailable bool `json:"docker_available"` |
| 106 | + IsolationEnabled bool `json:"isolation_enabled"` |
| 107 | + } `json:"data"` |
| 108 | + } |
| 109 | + require.NoError(t, json.NewDecoder(w.Body).Decode(&resp)) |
| 110 | + require.True(t, resp.Success) |
| 111 | + |
| 112 | + // docker_available must reflect the GENUINE probe, not the synthetic |
| 113 | + // DockerAvailable:true returned by GetDockerRecoveryStatus. |
| 114 | + assert.Equal(t, tc.wantAvailable, resp.Data.DockerAvailable, |
| 115 | + "docker_available should report genuine daemon availability") |
| 116 | + assert.Equal(t, tc.wantIsolationOn, resp.Data.IsolationEnabled, |
| 117 | + "isolation_enabled should report docker_isolation.enabled") |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments