fleet/server/contexts/ctxerr/statistics_test.go
Victor Lyuboslavsky bab0e159a9
Adding telemetry for specific Fleet Desktop errors (#23349)
Adding telemetry for catching issue #19172

# Docs changes

In another PR: https://github.com/fleetdm/fleet/pull/23423/files

# Demo
<div>
<a href="https://www.loom.com/share/233625875eec46508c26ae315cd52d19">
<p>[Demo] Add telemetry for vital fleetd errors - Issue #23413 - Watch
Video</p>
    </a>
<a href="https://www.loom.com/share/233625875eec46508c26ae315cd52d19">
<img style="max-width:300px;"
src="https://cdn.loom.com/sessions/thumbnails/233625875eec46508c26ae315cd52d19-45ca0ec1b7b5e9e7-full-play.gif">
    </a>
  </div>

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
- For Orbit and Fleet Desktop changes:
- [x] Orbit runs on macOS, Linux and Windows. Check if the orbit
feature/bugfix should only apply to one platform (`runtime.GOOS`).
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [x] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).
2024-10-31 14:24:42 -05:00

51 lines
1.5 KiB
Go

package ctxerr
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestAggregate(t *testing.T) {
t.Run("returns an error if can't find a handler in the context", func(t *testing.T) {
_, err := Aggregate(context.Background())
require.Error(t, err)
})
t.Run("returns an error if it can't decode one of the errors stored", func(t *testing.T) {
eh := MockHandler{}
eh.RetrieveImpl = func(flush bool) ([]*StoredError, error) {
return []*StoredError{{Chain: []byte("invalid")}}, nil
}
ctx := NewContext(context.Background(), eh)
_, err := Aggregate(ctx)
require.Error(t, err)
})
t.Run("returns an aggregation of the errors stored", func(t *testing.T) {
eh := MockHandler{}
eh.RetrieveImpl = func(flush bool) ([]*StoredError, error) {
return []*StoredError{
{Count: 10, Chain: []byte(`[{"stack": ["a", "b", "c", "d"]}]`)},
{Count: 20, Chain: []byte(`[{"stack": ["x", "y"]}]`)},
{Count: 30, Chain: []byte(`[{"stack": ["a", "b", "c", "d"]}, {"stack": ["x", "y"]}]`)},
{Count: 40, Chain: []byte(`[{"stack": ["a"]}, {"stack": ["x", "y"]}]`)},
}, nil
}
ctx := NewContext(context.Background(), eh)
rawAgg, err := Aggregate(ctx)
require.NoError(t, err)
var aggs []ErrorAgg
err = json.Unmarshal(rawAgg, &aggs)
require.NoError(t, err)
require.Equal(t, []ErrorAgg{
{Count: 10, Loc: []string{"a", "b", "c"}},
{Count: 20, Loc: []string{"x", "y"}},
{Count: 30, Loc: []string{"a", "b", "c"}},
{Count: 40, Loc: []string{"a", "x", "y"}},
}, aggs)
})
}