Files
grafana/pkg/tests/apis/openapi_test.go
Moustafa Baiou ca8324e62a Alerting: Add support for alpha rules apis in legacy storage
Rules created in the new api makes the rule have no group in the database, but the rule is returned in the old group api with a sentinel group name formatted with the rule uid for compatiblity with the old api.
This makes the UI continue to work with the rules without a group, and the ruler will continue to work with the rules without a group.

Rules are not allowed to be created in the provisioning api with a NoGroup sentinel mask, but NoGroup rules can be manipulated through both the new and old apis.

Co-authored-by: William Wernert <william.wernert@grafana.com>
2025-09-10 09:30:56 -04:00

106 lines
2.9 KiB
Go

package apis
import (
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/client-go/kubernetes"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/grafana/grafana/pkg/util/testutil"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestIntegrationOpenAPIs(t *testing.T) {
testutil.SkipIntegrationTestInShortMode(t)
h := NewK8sTestHelper(t, testinfra.GrafanaOpts{
AppModeProduction: true,
EnableFeatureToggles: []string{
featuremgmt.FlagQueryService, // Query Library
featuremgmt.FlagProvisioning,
featuremgmt.FlagInvestigationsBackend,
featuremgmt.FlagGrafanaAdvisor,
featuremgmt.FlagKubernetesAlertingRules,
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, // all datasources
},
})
t.Run("check valid version response", func(t *testing.T) {
client, err := kubernetes.NewForConfig(h.NewAdminRestConfig())
require.NoError(t, err)
info, err := client.ServerVersion()
require.NoError(t, err)
require.Equal(t, runtime.Version(), info.GoVersion)
require.Equal(t, "1", info.Major)
require.Equal(t, "33", info.Minor)
// Make sure the gitVersion is parsable
v, err := version.Parse(info.GitVersion)
require.NoError(t, err)
require.Equal(t, info.Major, fmt.Sprintf("%d", v.Major()))
require.Equal(t, info.Minor, fmt.Sprintf("%d", v.Minor()))
// Check the v3 path resolves properly
// NOTE: fetching the v2 schema sometimes returns a 503 in our test infrastructure
// Removing the explicit `OneOf` properties from InlineSecureValue in:
// https://github.com/grafana/grafana/blob/main/pkg/apimachinery/apis/common/v0alpha1/secure_values.go#L78
// will consistently support V2, however kubectl and everything else continues to work
disco := h.NewDiscoveryClient()
paths, err := disco.OpenAPIV3().Paths()
require.NoError(t, err, "requesting OpenAPI v3")
require.NotEmpty(t, paths, "has registered paths")
})
dir := "openapi_snapshots"
var groups = []schema.GroupVersion{{
Group: "dashboard.grafana.app",
Version: "v0alpha1",
}, {
Group: "dashboard.grafana.app",
Version: "v1beta1",
}, {
Group: "dashboard.grafana.app",
Version: "v2alpha1",
}, {
Group: "folder.grafana.app",
Version: "v1beta1",
}, {
Group: "provisioning.grafana.app",
Version: "v0alpha1",
}, {
Group: "iam.grafana.app",
Version: "v0alpha1",
}, {
Group: "investigations.grafana.app",
Version: "v0alpha1",
}, {
Group: "advisor.grafana.app",
Version: "v0alpha1",
}, {
Group: "playlist.grafana.app",
Version: "v0alpha1",
}, {
Group: "notifications.alerting.grafana.app",
Version: "v0alpha1",
}, {
Group: "rules.alerting.grafana.app",
Version: "v0alpha1",
}}
for _, gv := range groups {
VerifyOpenAPISnapshots(t, dir, gv, h)
}
}