Skip to content
Open
20 changes: 10 additions & 10 deletions go/core/internal/httpserver/handlers/substrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ func (h *SubstrateHandler) HandleGetSubstrateStatus(w ErrorResponseWriter, r *ht
Workers: []api.SubstrateWorkerEntry{},
}

for _, ns := range namespaces {
wpEntries, tmplEntries, err := h.listSubstrateCRs(r.Context(), ns)
if err != nil {
log.Error(err, "list substrate CRs", "namespace", ns)
w.RespondWithError(errors.NewInternalServerError("Failed to list substrate resources from Kubernetes", err))
return
if h.AteClient != nil {
for _, ns := range namespaces {
wpEntries, tmplEntries, err := h.listSubstrateCRs(r.Context(), ns)
if err != nil {
log.Error(err, "list substrate CRs", "namespace", ns)
w.RespondWithError(errors.NewInternalServerError("Failed to list substrate resources from Kubernetes", err))
return
}
resp.WorkerPools = append(resp.WorkerPools, wpEntries...)
resp.ActorTemplates = append(resp.ActorTemplates, tmplEntries...)
}
resp.WorkerPools = append(resp.WorkerPools, wpEntries...)
resp.ActorTemplates = append(resp.ActorTemplates, tmplEntries...)
}

if h.AteClient != nil {
actors, workers, ateErr := h.listAteAPIState(r.Context(), namespaces)
resp.Actors = actors
resp.Workers = workers
Expand Down
37 changes: 37 additions & 0 deletions go/core/internal/httpserver/handlers/substrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,50 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

// noMatchKubeClient is a minimal client.Client stub whose List always returns
// a *meta.NoKindMatchError, simulating a cluster where the ate.dev CRDs are absent.
type noMatchKubeClient struct {
client.Client
}

func (noMatchKubeClient) List(_ context.Context, _ client.ObjectList, _ ...client.ListOption) error {
return &apimeta.NoKindMatchError{}
}

// TestHandleGetSubstrateStatus_SubstrateNotConfigured verifies that when AteClient is nil
// (substrate not configured), the endpoint returns 200 with Enabled:false and empty slices
// without making any CRD List calls.
func TestHandleGetSubstrateStatus_SubstrateNotConfigured(t *testing.T) {
t.Parallel()

base := &handlers.Base{KubeClient: noMatchKubeClient{}, Authorizer: &auth.NoopAuthorizer{}}
h := handlers.NewSubstrateHandler(base, nil)
Comment on lines +28 to +45

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


req := httptest.NewRequest(http.MethodGet, "/api/substrate/status?namespace=kagent", nil)
req = setUser(req, "test-user")
rec := httptest.NewRecorder()
h.HandleGetSubstrateStatus(&testErrorResponseWriter{ResponseWriter: rec}, req)

require.Equal(t, http.StatusOK, rec.Code)

var wrapped api.StandardResponse[api.SubstrateStatusResponse]
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &wrapped))
require.False(t, wrapped.Data.Enabled)
require.Empty(t, wrapped.Data.WorkerPools)
require.Empty(t, wrapped.Data.ActorTemplates)
require.Empty(t, wrapped.Data.Actors)
require.Empty(t, wrapped.Data.Workers)
}

type stubAteControl struct {
Comment on lines +61 to 63

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

ateapipb.ControlClient
actors []*ateapipb.Actor
Expand Down
Loading