provisioning_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package provisioning
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/grafana/grafana/pkg/services/provisioning/dashboards"
  6. "github.com/grafana/grafana/pkg/setting"
  7. "github.com/stretchr/testify/assert"
  8. "testing"
  9. "time"
  10. )
  11. func TestProvisioningServiceImpl(t *testing.T) {
  12. t.Run("Restart dashboard provisioning and stop service", func(t *testing.T) {
  13. service, mock := setup()
  14. ctx, cancel := context.WithCancel(context.Background())
  15. var serviceRunning bool
  16. var serviceError error
  17. err := service.ProvisionDashboards()
  18. assert.Nil(t, err)
  19. go func() {
  20. serviceRunning = true
  21. serviceError = service.Run(ctx)
  22. serviceRunning = false
  23. }()
  24. time.Sleep(time.Millisecond)
  25. assert.Equal(t, 1, len(mock.Calls.PollChanges), "PollChanges should have been called")
  26. err = service.ProvisionDashboards()
  27. assert.Nil(t, err)
  28. time.Sleep(time.Millisecond)
  29. assert.Equal(t, 2, len(mock.Calls.PollChanges), "PollChanges should have been called 2 times")
  30. pollingCtx := mock.Calls.PollChanges[0].(context.Context)
  31. assert.Equal(t, context.Canceled, pollingCtx.Err(), "Polling context from first call should have been cancelled")
  32. assert.True(t, serviceRunning, "Service should be still running")
  33. // Cancelling the root context and stopping the service
  34. cancel()
  35. time.Sleep(time.Millisecond)
  36. assert.False(t, serviceRunning, "Service should not be running")
  37. assert.Equal(t, context.Canceled, serviceError, "Service should have returned canceled error")
  38. })
  39. t.Run("Failed reloading does not stop polling with old provisioned", func(t *testing.T) {
  40. service, mock := setup()
  41. ctx, cancel := context.WithCancel(context.Background())
  42. var serviceRunning bool
  43. err := service.ProvisionDashboards()
  44. assert.Nil(t, err)
  45. go func() {
  46. serviceRunning = true
  47. _ = service.Run(ctx)
  48. serviceRunning = false
  49. }()
  50. time.Sleep(time.Millisecond)
  51. assert.Equal(t, 1, len(mock.Calls.PollChanges), "PollChanges should have been called")
  52. mock.ProvisionFunc = func() error {
  53. return errors.New("Test error")
  54. }
  55. err = service.ProvisionDashboards()
  56. assert.NotNil(t, err)
  57. time.Sleep(time.Millisecond)
  58. // This should have been called with the old provisioner, after the last one failed.
  59. assert.Equal(t, 2, len(mock.Calls.PollChanges), "PollChanges should have been called 2 times")
  60. assert.True(t, serviceRunning, "Service should be still running")
  61. // Cancelling the root context and stopping the service
  62. cancel()
  63. })
  64. }
  65. func setup() (*provisioningServiceImpl, *dashboards.DashboardProvisionerMock) {
  66. dashMock := dashboards.NewDashboardProvisionerMock()
  67. service := NewProvisioningServiceImpl(
  68. func(path string) (dashboards.DashboardProvisioner, error) {
  69. return dashMock, nil
  70. },
  71. nil,
  72. nil,
  73. )
  74. service.Cfg = setting.NewCfg()
  75. return service, dashMock
  76. }