grafana/pkg/services/ngalert/state/historian/dashboard_test.go
Alexander Weaver 129a28919b
Alerting: Cache result of dashboard ID lookups (#56587)
* Create caching dashboard resolver

* A couple tests for dashboard resolving

* Log warning on not found

* Additional polish + review nits

* Move to singleflight instead of a plain mutex

* Store errors instead of -1 in cache and use reflection when reading

* Address linter error

* One more linter error
2022-10-14 15:48:02 -05:00

47 lines
1.5 KiB
Go

package historian
import (
"context"
"testing"
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestDashboardResolver(t *testing.T) {
t.Run("fetches dashboards from dashboard service", func(t *testing.T) {
dbs := &dashboards.FakeDashboardService{}
exp := int64(14)
dbs.On("GetDashboard", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Get(1).(*models.GetDashboardQuery).Result = &models.Dashboard{Id: exp}
}).Return(nil)
sut := createDashboardResolverSut(dbs)
id, err := sut.getID(context.Background(), 1, "dashboard-uid")
require.NoError(t, err)
require.Equal(t, exp, id)
})
t.Run("fetches dashboardNotFound if underlying dashboard does not exist", func(t *testing.T) {
dbs := &dashboards.FakeDashboardService{}
dbs.On("GetDashboard", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Get(1).(*models.GetDashboardQuery).Result = nil
}).Return(dashboards.ErrDashboardNotFound)
sut := createDashboardResolverSut(dbs)
_, err := sut.getID(context.Background(), 1, "not-exist")
require.Error(t, err)
require.ErrorIs(t, err, dashboards.ErrDashboardNotFound)
})
}
func createDashboardResolverSut(dbs *dashboards.FakeDashboardService) *dashboardResolver {
return newDashboardResolver(dbs, log.NewNopLogger(), 1*time.Nanosecond)
}