2019-04-25 02:06:44 -05:00
|
|
|
package provisioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2019-04-30 03:35:54 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-30 02:21:27 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
dashboardstore "github.com/grafana/grafana/pkg/services/dashboards"
|
2024-01-10 09:48:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2022-10-13 07:40:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2019-04-25 02:06:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/provisioning/dashboards"
|
2022-02-23 04:12:37 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/provisioning/utils"
|
2019-04-25 02:06:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProvisioningServiceImpl(t *testing.T) {
|
|
|
|
t.Run("Restart dashboard provisioning and stop service", func(t *testing.T) {
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest := setup()
|
2021-10-05 06:26:24 -05:00
|
|
|
err := serviceTest.service.ProvisionDashboards(context.Background())
|
2019-04-25 02:06:44 -05:00
|
|
|
assert.Nil(t, err)
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.startService()
|
|
|
|
serviceTest.waitForPollChanges()
|
2019-04-25 02:06:44 -05:00
|
|
|
|
2019-04-30 03:35:54 -05:00
|
|
|
assert.Equal(t, 1, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called")
|
|
|
|
|
2021-10-05 06:26:24 -05:00
|
|
|
err = serviceTest.service.ProvisionDashboards(context.Background())
|
2019-04-25 02:06:44 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.waitForPollChanges()
|
|
|
|
assert.Equal(t, 2, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called 2 times")
|
|
|
|
|
|
|
|
pollingCtx := serviceTest.mock.Calls.PollChanges[0].(context.Context)
|
2019-04-25 02:06:44 -05:00
|
|
|
assert.Equal(t, context.Canceled, pollingCtx.Err(), "Polling context from first call should have been cancelled")
|
2019-04-30 03:35:54 -05:00
|
|
|
assert.True(t, serviceTest.serviceRunning, "Service should be still running")
|
2019-04-25 02:06:44 -05:00
|
|
|
|
|
|
|
// Cancelling the root context and stopping the service
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.cancel()
|
|
|
|
serviceTest.waitForStop()
|
2019-04-25 02:06:44 -05:00
|
|
|
|
2019-04-30 03:35:54 -05:00
|
|
|
assert.False(t, serviceTest.serviceRunning, "Service should not be running")
|
2023-08-03 08:19:01 -05:00
|
|
|
assert.Equal(t, context.Canceled, serviceTest.serviceError, "Service should have returned canceled error")
|
2019-04-25 02:06:44 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Failed reloading does not stop polling with old provisioned", func(t *testing.T) {
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest := setup()
|
2021-10-05 06:26:24 -05:00
|
|
|
err := serviceTest.service.ProvisionDashboards(context.Background())
|
2019-04-25 02:06:44 -05:00
|
|
|
assert.Nil(t, err)
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.startService()
|
|
|
|
serviceTest.waitForPollChanges()
|
|
|
|
assert.Equal(t, 1, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called")
|
2019-04-25 02:06:44 -05:00
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
serviceTest.mock.ProvisionFunc = func(ctx context.Context) error {
|
2019-04-25 02:06:44 -05:00
|
|
|
return errors.New("Test error")
|
|
|
|
}
|
2021-10-05 06:26:24 -05:00
|
|
|
err = serviceTest.service.ProvisionDashboards(context.Background())
|
2019-04-25 02:06:44 -05:00
|
|
|
assert.NotNil(t, err)
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.waitForPollChanges()
|
|
|
|
|
2019-04-25 02:06:44 -05:00
|
|
|
// This should have been called with the old provisioner, after the last one failed.
|
2019-04-30 03:35:54 -05:00
|
|
|
assert.Equal(t, 2, len(serviceTest.mock.Calls.PollChanges), "PollChanges should have been called 2 times")
|
|
|
|
assert.True(t, serviceTest.serviceRunning, "Service should be still running")
|
2019-04-25 02:06:44 -05:00
|
|
|
|
|
|
|
// Cancelling the root context and stopping the service
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.cancel()
|
2019-04-25 02:06:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-30 03:35:54 -05:00
|
|
|
type serviceTestStruct struct {
|
|
|
|
waitForPollChanges func()
|
|
|
|
waitForStop func()
|
|
|
|
waitTimeout time.Duration
|
|
|
|
|
|
|
|
serviceRunning bool
|
|
|
|
serviceError error
|
|
|
|
|
|
|
|
startService func()
|
|
|
|
cancel func()
|
|
|
|
|
2020-04-15 01:12:52 -05:00
|
|
|
mock *dashboards.ProvisionerMock
|
2021-08-25 08:11:22 -05:00
|
|
|
service *ProvisioningServiceImpl
|
2019-04-30 03:35:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func setup() *serviceTestStruct {
|
|
|
|
serviceTest := &serviceTestStruct{}
|
|
|
|
serviceTest.waitTimeout = time.Second
|
|
|
|
|
|
|
|
pollChangesChannel := make(chan context.Context)
|
|
|
|
serviceStopped := make(chan interface{})
|
|
|
|
|
|
|
|
serviceTest.mock = dashboards.NewDashboardProvisionerMock()
|
|
|
|
serviceTest.mock.PollChangesFunc = func(ctx context.Context) {
|
|
|
|
pollChangesChannel <- ctx
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
serviceTest.service = newProvisioningServiceImpl(
|
2024-01-10 09:48:28 -06:00
|
|
|
func(context.Context, string, dashboardstore.DashboardProvisioningService, org.Service, utils.DashboardStore, folder.Service) (dashboards.DashboardProvisioner, error) {
|
2019-04-30 03:35:54 -05:00
|
|
|
return serviceTest.mock, nil
|
2019-04-25 02:06:44 -05:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2019-04-30 03:35:54 -05:00
|
|
|
serviceTest.service.Cfg = setting.NewCfg()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
serviceTest.cancel = cancel
|
|
|
|
|
|
|
|
serviceTest.startService = func() {
|
|
|
|
go func() {
|
|
|
|
serviceTest.serviceRunning = true
|
|
|
|
serviceTest.serviceError = serviceTest.service.Run(ctx)
|
|
|
|
serviceTest.serviceRunning = false
|
|
|
|
serviceStopped <- true
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceTest.waitForPollChanges = func() {
|
|
|
|
timeoutChan := time.After(serviceTest.waitTimeout)
|
|
|
|
select {
|
|
|
|
case <-pollChangesChannel:
|
|
|
|
case <-timeoutChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceTest.waitForStop = func() {
|
|
|
|
timeoutChan := time.After(serviceTest.waitTimeout)
|
|
|
|
select {
|
|
|
|
case <-serviceStopped:
|
|
|
|
case <-timeoutChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceTest
|
2019-04-25 02:06:44 -05:00
|
|
|
}
|