Chore: Fix test to retry fetching provisioned dashboard until is in place (#85408)

Retry fetching provisioned dashboard until is in place
This commit is contained in:
Sofia Papagiannaki 2024-04-08 09:47:34 +02:00 committed by GitHub
parent 2fd6bc65c7
commit 88be09420c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,6 +11,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -33,6 +34,7 @@ import (
"github.com/grafana/grafana/pkg/tests/testinfra" "github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite" "github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/retryer"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -172,6 +174,13 @@ providers:
t.Run("when provisioned directory is not empty, dashboard should be created", func(t *testing.T) { t.Run("when provisioned directory is not empty, dashboard should be created", func(t *testing.T) {
title := "Grafana Dev Overview & Home" title := "Grafana Dev Overview & Home"
dashboardList := &model.HitList{}
retry := 0
retries := 5
// retry until the provisioned dashboard is ready
err := retryer.Retry(func() (retryer.RetrySignal, error) {
retry++
u := fmt.Sprintf("http://admin:admin@%s/api/search?query=%s", grafanaListedAddr, url.QueryEscape(title)) u := fmt.Sprintf("http://admin:admin@%s/api/search?query=%s", grafanaListedAddr, url.QueryEscape(title))
// nolint:gosec // nolint:gosec
resp, err := http.Get(u) resp, err := http.Get(u)
@ -183,10 +192,19 @@ providers:
}) })
b, err := io.ReadAll(resp.Body) b, err := io.ReadAll(resp.Body)
require.NoError(t, err) require.NoError(t, err)
dashboardList := &model.HitList{}
err = json.Unmarshal(b, dashboardList) err = json.Unmarshal(b, dashboardList)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, 1, dashboardList.Len()) if dashboardList.Len() == 0 {
if retry >= retries {
return retryer.FuncError, fmt.Errorf("max retries exceeded")
}
t.Log("Dashboard is not ready", "retry", retry)
return retryer.FuncFailure, nil
}
return retryer.FuncComplete, nil
}, retries, time.Millisecond*time.Duration(10), time.Second)
require.NoError(t, err)
var dashboardUID string var dashboardUID string
var dashboardID int64 var dashboardID int64
for _, d := range *dashboardList { for _, d := range *dashboardList {