|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build test |
| 7 | + |
| 8 | +package agentimpl |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "testing" |
| 13 | + |
| 14 | + healthplatformpayload "github.com/DataDog/agent-payload/v5/healthplatform" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "google.golang.org/grpc/codes" |
| 18 | + "google.golang.org/grpc/status" |
| 19 | + |
| 20 | + remoteagentregistry "github.com/DataDog/datadog-agent/comp/core/remoteagentregistry/def" |
| 21 | + healthplatformstore "github.com/DataDog/datadog-agent/comp/healthplatform/store/def" |
| 22 | + healthplatformmock "github.com/DataDog/datadog-agent/comp/healthplatform/store/mock" |
| 23 | + pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core" |
| 24 | +) |
| 25 | + |
| 26 | +// stubRegistry is a minimal remoteagentregistry.Component for testing session validation. |
| 27 | +type stubRegistry struct { |
| 28 | + validSessions map[string]bool |
| 29 | +} |
| 30 | + |
| 31 | +func (r *stubRegistry) RegisterRemoteAgent(_ *remoteagentregistry.RegistrationData) (string, uint32, error) { |
| 32 | + return "", 0, nil |
| 33 | +} |
| 34 | + |
| 35 | +func (r *stubRegistry) RefreshRemoteAgent(sessionID string) bool { |
| 36 | + return r.validSessions[sessionID] |
| 37 | +} |
| 38 | + |
| 39 | +func (r *stubRegistry) ReportRemoteAgentEvent(_ string, _ []remoteagentregistry.RemoteAgentEvent) error { |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func (r *stubRegistry) GetRegisteredAgents() []remoteagentregistry.RegisteredAgent { return nil } |
| 44 | + |
| 45 | +func (r *stubRegistry) GetRegisteredAgentStatuses() []remoteagentregistry.StatusData { return nil } |
| 46 | + |
| 47 | +func serverWithStore(store healthplatformstore.Component) *serverSecure { |
| 48 | + return &serverSecure{healthPlatformStore: store} |
| 49 | +} |
| 50 | + |
| 51 | +func serverWithStoreAndRegistry(store healthplatformstore.Component, reg remoteagentregistry.Component) *serverSecure { |
| 52 | + return &serverSecure{ |
| 53 | + healthPlatformStore: store, |
| 54 | + remoteAgentRegistry: reg, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// ── ReportHealthIssue ──────────────────────────────────────────────────────── |
| 59 | + |
| 60 | +func TestReportHealthIssue_StoresIssue(t *testing.T) { |
| 61 | + storeMock := healthplatformmock.Mock(t) |
| 62 | + srv := serverWithStore(storeMock) |
| 63 | + |
| 64 | + issue := &healthplatformpayload.Issue{Id: "test-issue", IssueName: "test-issue", Title: "Test Issue", Severity: healthplatformpayload.IssueSeverity_ISSUE_SEVERITY_HIGH} |
| 65 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{Issue: issue}) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + got := storeMock.GetIssue("test-issue") |
| 69 | + require.NotNil(t, got) |
| 70 | + assert.Equal(t, "Test Issue", got.Title) |
| 71 | + assert.Equal(t, healthplatformpayload.IssueSeverity_ISSUE_SEVERITY_HIGH, got.Severity) |
| 72 | +} |
| 73 | + |
| 74 | +func TestReportHealthIssue_NilIssue(t *testing.T) { |
| 75 | + srv := serverWithStore(healthplatformmock.Mock(t)) |
| 76 | + |
| 77 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{}) |
| 78 | + require.Error(t, err) |
| 79 | + assert.Equal(t, codes.InvalidArgument, status.Code(err)) |
| 80 | +} |
| 81 | + |
| 82 | +func TestReportHealthIssue_EmptyIssueID(t *testing.T) { |
| 83 | + srv := serverWithStore(healthplatformmock.Mock(t)) |
| 84 | + |
| 85 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{Issue: &healthplatformpayload.Issue{Title: "no id"}}) |
| 86 | + require.Error(t, err) |
| 87 | + assert.Equal(t, codes.InvalidArgument, status.Code(err)) |
| 88 | +} |
| 89 | + |
| 90 | +func TestReportHealthIssue_EmptyIssueName(t *testing.T) { |
| 91 | + srv := serverWithStore(healthplatformmock.Mock(t)) |
| 92 | + |
| 93 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{Issue: &healthplatformpayload.Issue{Id: "has-id-no-name"}}) |
| 94 | + require.Error(t, err) |
| 95 | + assert.Equal(t, codes.InvalidArgument, status.Code(err)) |
| 96 | +} |
| 97 | + |
| 98 | +// TestReportHealthIssue_ValidSession verifies that a registered remote agent with a |
| 99 | +// valid session ID can report issues successfully. |
| 100 | +func TestReportHealthIssue_ValidSession(t *testing.T) { |
| 101 | + storeMock := healthplatformmock.Mock(t) |
| 102 | + reg := &stubRegistry{validSessions: map[string]bool{"sess-123": true}} |
| 103 | + srv := serverWithStoreAndRegistry(storeMock, reg) |
| 104 | + |
| 105 | + issue := &healthplatformpayload.Issue{Id: "adp-issue", IssueName: "adp-issue", Title: "ADP issue"} |
| 106 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{ |
| 107 | + RemoteAgentSessionId: "sess-123", |
| 108 | + Issue: issue, |
| 109 | + }) |
| 110 | + require.NoError(t, err) |
| 111 | + assert.NotNil(t, storeMock.GetIssue("adp-issue")) |
| 112 | +} |
| 113 | + |
| 114 | +// TestReportHealthIssue_InvalidSession verifies that a stale or unknown session ID |
| 115 | +// is rejected with UNAUTHENTICATED. |
| 116 | +func TestReportHealthIssue_InvalidSession(t *testing.T) { |
| 117 | + reg := &stubRegistry{validSessions: map[string]bool{}} |
| 118 | + srv := serverWithStoreAndRegistry(healthplatformmock.Mock(t), reg) |
| 119 | + |
| 120 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{ |
| 121 | + RemoteAgentSessionId: "stale-session", |
| 122 | + Issue: &healthplatformpayload.Issue{Id: "x", IssueName: "x"}, |
| 123 | + }) |
| 124 | + require.Error(t, err) |
| 125 | + assert.Equal(t, codes.Unauthenticated, status.Code(err)) |
| 126 | +} |
| 127 | + |
| 128 | +// TestReportHealthIssue_SessionWithoutRegistry verifies that supplying a session ID |
| 129 | +// when the registry is not wired returns Unavailable. |
| 130 | +func TestReportHealthIssue_SessionWithoutRegistry(t *testing.T) { |
| 131 | + srv := serverWithStore(healthplatformmock.Mock(t)) // no registry |
| 132 | + |
| 133 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{ |
| 134 | + RemoteAgentSessionId: "some-session", |
| 135 | + Issue: &healthplatformpayload.Issue{Id: "x"}, |
| 136 | + }) |
| 137 | + require.Error(t, err) |
| 138 | + assert.Equal(t, codes.Unavailable, status.Code(err)) |
| 139 | +} |
| 140 | + |
| 141 | +// TestReportHealthIssue_NoSessionSubAgent verifies that sub-agents (no session ID) |
| 142 | +// can report issues without a registry. |
| 143 | +func TestReportHealthIssue_NoSessionSubAgent(t *testing.T) { |
| 144 | + storeMock := healthplatformmock.Mock(t) |
| 145 | + srv := serverWithStore(storeMock) // no registry — fine for sub-agents |
| 146 | + |
| 147 | + _, err := srv.ReportHealthIssue(context.Background(), &pb.ReportHealthIssueRequest{ |
| 148 | + Issue: &healthplatformpayload.Issue{Id: "sysprobe-issue", IssueName: "sysprobe-issue"}, |
| 149 | + }) |
| 150 | + require.NoError(t, err) |
| 151 | + assert.NotNil(t, storeMock.GetIssue("sysprobe-issue")) |
| 152 | +} |
| 153 | + |
| 154 | +// ── ResolveHealthIssue ─────────────────────────────────────────────────────── |
| 155 | + |
| 156 | +func TestResolveHealthIssue_ClearsIssue(t *testing.T) { |
| 157 | + storeMock := healthplatformmock.Mock(t) |
| 158 | + srv := serverWithStore(storeMock) |
| 159 | + |
| 160 | + require.NoError(t, storeMock.ReportIssue(&healthplatformpayload.Issue{Id: "to-resolve", IssueName: "to-resolve", Title: "active"})) |
| 161 | + require.NotNil(t, storeMock.GetIssue("to-resolve")) |
| 162 | + |
| 163 | + _, err := srv.ResolveHealthIssue(context.Background(), &pb.ResolveHealthIssueRequest{IssueId: "to-resolve"}) |
| 164 | + require.NoError(t, err) |
| 165 | + assert.Nil(t, storeMock.GetIssue("to-resolve")) |
| 166 | +} |
| 167 | + |
| 168 | +func TestResolveHealthIssue_EmptyIssueID(t *testing.T) { |
| 169 | + srv := serverWithStore(healthplatformmock.Mock(t)) |
| 170 | + |
| 171 | + _, err := srv.ResolveHealthIssue(context.Background(), &pb.ResolveHealthIssueRequest{}) |
| 172 | + require.Error(t, err) |
| 173 | + assert.Equal(t, codes.InvalidArgument, status.Code(err)) |
| 174 | +} |
| 175 | + |
| 176 | +// TestResolveHealthIssue_ValidSession verifies that a registered remote agent with a |
| 177 | +// valid session can resolve its own issues. |
| 178 | +func TestResolveHealthIssue_ValidSession(t *testing.T) { |
| 179 | + storeMock := healthplatformmock.Mock(t) |
| 180 | + reg := &stubRegistry{validSessions: map[string]bool{"sess-abc": true}} |
| 181 | + srv := serverWithStoreAndRegistry(storeMock, reg) |
| 182 | + |
| 183 | + require.NoError(t, storeMock.ReportIssue(&healthplatformpayload.Issue{Id: "adp-resolved", IssueName: "adp-resolved"})) |
| 184 | + |
| 185 | + _, err := srv.ResolveHealthIssue(context.Background(), &pb.ResolveHealthIssueRequest{ |
| 186 | + RemoteAgentSessionId: "sess-abc", |
| 187 | + IssueId: "adp-resolved", |
| 188 | + }) |
| 189 | + require.NoError(t, err) |
| 190 | + assert.Nil(t, storeMock.GetIssue("adp-resolved")) |
| 191 | +} |
| 192 | + |
| 193 | +// TestResolveHealthIssue_InvalidSession verifies that a stale session is rejected. |
| 194 | +func TestResolveHealthIssue_InvalidSession(t *testing.T) { |
| 195 | + reg := &stubRegistry{validSessions: map[string]bool{}} |
| 196 | + srv := serverWithStoreAndRegistry(healthplatformmock.Mock(t), reg) |
| 197 | + |
| 198 | + _, err := srv.ResolveHealthIssue(context.Background(), &pb.ResolveHealthIssueRequest{ |
| 199 | + RemoteAgentSessionId: "stale", |
| 200 | + IssueId: "x", |
| 201 | + }) |
| 202 | + require.Error(t, err) |
| 203 | + assert.Equal(t, codes.Unauthenticated, status.Code(err)) |
| 204 | +} |
0 commit comments