mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
bad4f7fec5
* Alerting: Change screenshots to use components This commit changes screenshots to use a number of components instead of a set of functional wrappers. It moves the uploading of screenshots from the screenshot package to the image package so we can re-use the same code for both uploading screenshots and server-side images; SingleFlight from the screenshot package to the image package so we can use it for both taking and uploading the screenshot, where as before it was used just for taking the screenshot; and it also removes the use of a cache because we know that screenshots can be taken at most once per tick of the scheduler.
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
package image
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/xorcare/pointer"
|
|
|
|
"github.com/grafana/grafana/pkg/components/imguploader"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
|
"github.com/grafana/grafana/pkg/services/screenshot"
|
|
)
|
|
|
|
func TestScreenshotImageService(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
var (
|
|
images = store.NewFakeImageStore(t)
|
|
limiter = screenshot.NoOpRateLimiter{}
|
|
screenshots = screenshot.NewMockScreenshotService(ctrl)
|
|
uploads = imguploader.NewMockImageUploader(ctrl)
|
|
)
|
|
|
|
s := NewScreenshotImageService(&limiter, log.NewNopLogger(), screenshots, images,
|
|
NewUploadingService(uploads, prometheus.NewRegistry()))
|
|
|
|
ctx := context.Background()
|
|
|
|
// assert that a screenshot is taken
|
|
screenshots.EXPECT().Take(gomock.Any(), screenshot.ScreenshotOptions{
|
|
DashboardUID: "foo",
|
|
PanelID: 1,
|
|
Timeout: screenshotTimeout,
|
|
}).Return(&screenshot.Screenshot{
|
|
Path: "foo.png",
|
|
}, nil)
|
|
|
|
// the screenshot is made into an image and uploaded
|
|
uploads.EXPECT().Upload(gomock.Any(), "foo.png").
|
|
Return("https://example.com/foo.png", nil)
|
|
|
|
// and then saved into the database
|
|
expected := models.Image{
|
|
ID: 1,
|
|
Token: "foo",
|
|
Path: "foo.png",
|
|
URL: "https://example.com/foo.png",
|
|
}
|
|
|
|
image, err := s.NewImage(ctx, &models.AlertRule{
|
|
DashboardUID: pointer.String("foo"),
|
|
PanelID: pointer.Int64(1)})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, *image)
|
|
|
|
// assert that a screenshot is taken
|
|
screenshots.EXPECT().Take(gomock.Any(), screenshot.ScreenshotOptions{
|
|
DashboardUID: "bar",
|
|
PanelID: 1,
|
|
Timeout: screenshotTimeout,
|
|
}).Return(&screenshot.Screenshot{
|
|
Path: "bar.png",
|
|
}, nil)
|
|
|
|
// the screenshot is made into an image and uploaded, but the upload returns an error
|
|
uploads.EXPECT().Upload(gomock.Any(), "bar.png").
|
|
Return("", errors.New("failed to upload bar.png"))
|
|
|
|
// and then saved into the database, but without a URL
|
|
expected = models.Image{
|
|
ID: 2,
|
|
Token: "bar",
|
|
Path: "bar.png",
|
|
}
|
|
|
|
image, err = s.NewImage(ctx, &models.AlertRule{
|
|
DashboardUID: pointer.String("bar"),
|
|
PanelID: pointer.Int64(1)})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, *image)
|
|
}
|