2022-02-16 07:15:44 -06:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package service
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
import (
|
2021-11-03 08:10:39 -05:00
|
|
|
"context"
|
2022-02-16 07:15:44 -06:00
|
|
|
"errors"
|
2020-06-29 07:08:32 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-03-10 11:19:50 -06:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2018-02-19 04:12:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-02-16 07:15:44 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/services/dashboards"
|
2019-04-10 06:29:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-02-19 04:12:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardService(t *testing.T) {
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Dashboard service tests", func(t *testing.T) {
|
2018-03-27 08:12:47 -05:00
|
|
|
bus.ClearBusHandlers()
|
|
|
|
|
2022-03-10 11:19:50 -06:00
|
|
|
fakeStore := m.FakeDashboardStore{}
|
2022-02-16 07:15:44 -06:00
|
|
|
defer fakeStore.AssertExpectations(t)
|
|
|
|
service := &DashboardServiceImpl{
|
2022-02-28 02:54:56 -06:00
|
|
|
log: log.New("test.logger"),
|
|
|
|
dashboardStore: &fakeStore,
|
|
|
|
dashAlertExtractor: &dummyDashAlertExtractor{},
|
2020-02-28 07:32:01 -06:00
|
|
|
}
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
origNewDashboardGuardian := guardian.New
|
2021-10-27 08:17:50 -05:00
|
|
|
defer func() { guardian.New = origNewDashboardGuardian }()
|
2018-02-20 11:08:19 -06:00
|
|
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Save dashboard validation", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
dto := &m.SaveDashboardDTO{}
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("When saving a dashboard with empty title it should return error", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
titles := []string{"", " ", " \t "}
|
|
|
|
|
|
|
|
for _, title := range titles {
|
|
|
|
dto.Dashboard = models.NewDashboard(title)
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardTitleEmpty)
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if it's a folder and have a folder id", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
dto.Dashboard = models.NewDashboardFolder("Folder")
|
|
|
|
dto.Dashboard.FolderId = 1
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardFolderCannotHaveParent)
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if folder is named General", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
dto.Dashboard = models.NewDashboardFolder("General")
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardFolderNameExists)
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("When saving a dashboard should validate uid", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
testCases := []struct {
|
|
|
|
Uid string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{Uid: "", Error: nil},
|
|
|
|
{Uid: " ", Error: nil},
|
|
|
|
{Uid: " \t ", Error: nil},
|
|
|
|
{Uid: "asdf90_-", Error: nil},
|
|
|
|
{Uid: "asdf/90", Error: models.ErrDashboardInvalidUid},
|
|
|
|
{Uid: " asdfghjklqwertyuiopzxcvbnmasdfghjklqwer ", Error: nil},
|
2020-07-21 04:12:01 -05:00
|
|
|
{Uid: "asdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnm", Error: models.ErrDashboardUidTooLong},
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
dto.Dashboard = models.NewDashboard("title")
|
|
|
|
dto.Dashboard.SetUid(tc.Uid)
|
|
|
|
dto.User = &models.SignedInUser{}
|
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
if tc.Error == nil {
|
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
}
|
|
|
|
_, err := service.BuildSaveDashboardCommand(context.Background(), dto, true, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, tc.Error)
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(&models.DashboardProvisioning{}, nil).Once()
|
2018-03-27 08:12:47 -05:00
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetId(3)
|
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardCannotSaveProvisionedDashboard)
|
2018-03-27 08:12:47 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should not return validation error if dashboard is provisioned but UI updates allowed", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
|
|
|
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
2019-10-31 08:27:31 -05:00
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetId(3)
|
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, true)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2019-10-31 08:27:31 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if alert data is invalid", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(nil, nil).Once()
|
|
|
|
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
|
|
|
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("alert validation error")).Once()
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
2022-02-16 07:15:44 -06:00
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err.Error(), "alert validation error")
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Save provisioned dashboard validation", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
dto := &m.SaveDashboardDTO{}
|
2018-04-13 10:45:32 -05:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should not return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
|
|
|
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
2018-04-13 10:45:32 -05:00
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetId(3)
|
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveProvisionedDashboard(context.Background(), dto, nil)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2018-04-13 10:45:32 -05:00
|
|
|
})
|
2020-02-28 07:32:01 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should override invalid refresh interval if dashboard is provisioned", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
|
|
|
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
|
|
|
|
2020-02-28 07:32:01 -06:00
|
|
|
oldRefreshInterval := setting.MinRefreshInterval
|
|
|
|
setting.MinRefreshInterval = "5m"
|
|
|
|
defer func() { setting.MinRefreshInterval = oldRefreshInterval }()
|
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetId(3)
|
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
|
|
|
dto.Dashboard.Data.Set("refresh", "1s")
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveProvisionedDashboard(context.Background(), dto, nil)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, dto.Dashboard.Data.Get("refresh").MustString(), "5m")
|
2020-02-28 07:32:01 -06:00
|
|
|
})
|
2018-04-13 10:45:32 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Import dashboard validation", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
dto := &m.SaveDashboardDTO{}
|
2018-04-13 10:54:41 -05:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(&models.DashboardProvisioning{}, nil).Once()
|
2018-04-13 10:54:41 -05:00
|
|
|
|
|
|
|
dto.Dashboard = models.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetId(3)
|
|
|
|
dto.User = &models.SignedInUser{UserId: 1}
|
2021-11-12 07:35:38 -06:00
|
|
|
_, err := service.ImportDashboard(context.Background(), dto)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardCannotSaveProvisionedDashboard)
|
2018-04-13 10:54:41 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Given provisioned dashboard", func(t *testing.T) {
|
|
|
|
t.Run("DeleteProvisionedDashboard should delete it", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
result := setupDeleteHandlers(t)
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteProvisionedDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, result.deleteWasCalled)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("DeleteDashboard should fail to delete it", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(&models.DashboardProvisioning{}, nil).Once()
|
|
|
|
result := setupDeleteHandlers(t)
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, models.ErrDashboardCannotDeleteProvisionedDashboard)
|
|
|
|
require.False(t, result.deleteWasCalled)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Given non provisioned dashboard", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
result := setupDeleteHandlers(t)
|
2019-04-10 06:29:10 -05:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("DeleteProvisionedDashboard should delete it", func(t *testing.T) {
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteProvisionedDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, result.deleteWasCalled)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("DeleteDashboard should delete it", func(t *testing.T) {
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(nil, nil).Once()
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, result.deleteWasCalled)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
})
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
}
|
2019-04-10 06:29:10 -05:00
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
deleteWasCalled bool
|
|
|
|
}
|
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
func setupDeleteHandlers(t *testing.T) *Result {
|
2021-03-17 10:06:10 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2019-04-10 06:29:10 -05:00
|
|
|
result := &Result{}
|
2021-12-28 09:08:07 -06:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, cmd *models.DeleteDashboardCommand) error {
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, cmd.Id, int64(1))
|
|
|
|
require.Equal(t, cmd.OrgId, int64(1))
|
2019-04-10 06:29:10 -05:00
|
|
|
result.deleteWasCalled = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|