grafana/pkg/services/publicdashboards/commands/generate_datasources/main_test.go
owensmallwood 7de093738d
PublicDashboards: Adds command to generate map of supported datasources (#57841)
generates map of supported datasources for pubdash
2022-11-02 13:35:57 -06:00

37 lines
1.0 KiB
Go

package generate_datasources
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCanGetCompatibleDatasources(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
w.WriteHeader(http.StatusOK)
// response for getting metadata for all datasources
if r.URL.String() == allPluginsEndpoint() {
_, err = w.Write([]byte(`{"items":[{"slug":"postgres"},{"slug":"frontendDatasource"}]}`))
}
// responses for specific datasource plugins
if r.URL.String() == pluginEndpoint("postgres") {
_, err = w.Write([]byte(`{"json":{"alerting":true,"backend":true}}`))
}
if r.URL.String() == pluginEndpoint("frontendDatasource") {
_, err = w.Write([]byte(`{"json":{}}`))
}
require.NoError(t, err)
}))
defer server.Close()
datasources, err := GetCompatibleDatasources(server.URL)
require.NoError(t, err)
assert.Len(t, datasources, 1)
assert.Equal(t, "postgres", datasources[0])
}