mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -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.
42 lines
1.1 KiB
Go
42 lines
1.1 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/grafana/grafana/pkg/components/imguploader"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
func TestUploadingService(t *testing.T) {
|
|
c := gomock.NewController(t)
|
|
defer c.Finish()
|
|
|
|
u := imguploader.NewMockImageUploader(c)
|
|
s := NewUploadingService(u, prometheus.NewRegistry())
|
|
|
|
ctx := context.Background()
|
|
|
|
u.EXPECT().Upload(ctx, "foo.png").Return("https://example.com/foo.png", nil)
|
|
image, err := s.Upload(ctx, models.Image{Path: "foo.png"})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, models.Image{
|
|
Path: "foo.png",
|
|
URL: "https://example.com/foo.png",
|
|
}, image)
|
|
|
|
// error on upload should still return screenshot on disk
|
|
u.EXPECT().Upload(ctx, "foo.png").Return("", errors.New("service is unavailable"))
|
|
image, err = s.Upload(ctx, models.Image{Path: "foo.png"})
|
|
assert.EqualError(t, err, "failed to upload screenshot: service is unavailable")
|
|
assert.Equal(t, models.Image{
|
|
Path: "foo.png",
|
|
}, image)
|
|
}
|