From 74115f1f08e3a6aca65384a43ff28b9540c647f9 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 29 Feb 2024 14:58:49 -0800 Subject: [PATCH] Chore: fix apiserver integration tests (#83724) Co-authored-by: Todd Treece --- pkg/tests/apis/dashboard/dashboards_test.go | 9 ++++-- .../apis/dashboardsnapshot/snapshots_test.go | 11 ++++++-- pkg/tests/apis/datasource/testdata_test.go | 9 ++++-- pkg/tests/apis/example/example_test.go | 16 +++++++++-- pkg/tests/apis/folder/folders_test.go | 20 +++++++++++-- pkg/tests/apis/helper.go | 28 ++++++++++++++----- pkg/tests/apis/playlist/playlist_test.go | 7 ++++- pkg/tests/apis/query/query_test.go | 7 ++++- 8 files changed, 85 insertions(+), 22 deletions(-) diff --git a/pkg/tests/apis/dashboard/dashboards_test.go b/pkg/tests/apis/dashboard/dashboards_test.go index 92010b98cdd..a3859c084dd 100644 --- a/pkg/tests/apis/dashboard/dashboards_test.go +++ b/pkg/tests/apis/dashboard/dashboards_test.go @@ -8,9 +8,14 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestRequiresDevMode(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationRequiresDevMode(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } @@ -26,7 +31,7 @@ func TestRequiresDevMode(t *testing.T) { require.Error(t, err) } -func TestDashboardsApp(t *testing.T) { +func TestIntegrationDashboardsApp(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } diff --git a/pkg/tests/apis/dashboardsnapshot/snapshots_test.go b/pkg/tests/apis/dashboardsnapshot/snapshots_test.go index c25c4029d0a..bf787a79160 100644 --- a/pkg/tests/apis/dashboardsnapshot/snapshots_test.go +++ b/pkg/tests/apis/dashboardsnapshot/snapshots_test.go @@ -8,9 +8,14 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestDashboardSnapshots(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationDashboardSnapshots(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } @@ -31,14 +36,14 @@ func TestDashboardSnapshots(t *testing.T) { "freshness": "Current", "resources": [ { - "resource": "dashboardsnapshot", + "resource": "dashboardsnapshots", "responseKind": { "group": "", "kind": "DashboardSnapshot", "version": "" }, "scope": "Namespaced", - "singularResource": "dashsnap", + "singularResource": "dashboardsnapshot", "subresources": [ { "responseKind": { diff --git a/pkg/tests/apis/datasource/testdata_test.go b/pkg/tests/apis/datasource/testdata_test.go index b0449903fe3..c9f1e64b95a 100644 --- a/pkg/tests/apis/datasource/testdata_test.go +++ b/pkg/tests/apis/datasource/testdata_test.go @@ -12,9 +12,14 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestTestDatasource(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationTestDatasource(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } @@ -70,7 +75,7 @@ func TestTestDatasource(t *testing.T) { { "responseKind": { "group": "", - "kind": "Status", + "kind": "QueryDataResponse", "version": "" }, "subresource": "query", diff --git a/pkg/tests/apis/example/example_test.go b/pkg/tests/apis/example/example_test.go index b84db254f2f..c702402837f 100644 --- a/pkg/tests/apis/example/example_test.go +++ b/pkg/tests/apis/example/example_test.go @@ -8,14 +8,20 @@ import ( "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestExampleApp(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationExampleApp(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } @@ -49,7 +55,7 @@ func TestExampleApp(t *testing.T) { v1Disco, err := json.MarshalIndent(resources, "", " ") require.NoError(t, err) - // fmt.Printf("%s", string(v1Disco)) + //fmt.Printf("%s", string(v1Disco)) require.JSONEq(t, `{ "kind": "APIResourceList", @@ -147,7 +153,11 @@ func TestExampleApp(t *testing.T) { rsp, err := client.Get(context.Background(), "test2", metav1.GetOptions{}) require.NoError(t, err) - require.Equal(t, "dummy: test2", rsp.Object["spec"]) + v, ok, err := unstructured.NestedString(rsp.Object, "spec", "Dummy") + require.NoError(t, err) + require.True(t, ok) + + require.Equal(t, "test2", v) require.Equal(t, "DummyResource", rsp.GetObjectKind().GroupVersionKind().Kind) // Now a sub-resource diff --git a/pkg/tests/apis/folder/folders_test.go b/pkg/tests/apis/folder/folders_test.go index 25d8b1504e5..1dbbddf3ff6 100644 --- a/pkg/tests/apis/folder/folders_test.go +++ b/pkg/tests/apis/folder/folders_test.go @@ -9,9 +9,14 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestFoldersApp(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationFoldersApp(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } @@ -51,10 +56,19 @@ func TestFoldersApp(t *testing.T) { ] }, { - "name": "folders/children", + "name": "folders/access", "singularName": "", "namespaced": true, - "kind": "FolderInfoList", + "kind": "FolderAccessInfo", + "verbs": [ + "get" + ] + }, + { + "name": "folders/count", + "singularName": "", + "namespaced": true, + "kind": "DescendantCounts", "verbs": [ "get" ] diff --git a/pkg/tests/apis/helper.go b/pkg/tests/apis/helper.go index d0fca227bc4..f1712e47010 100644 --- a/pkg/tests/apis/helper.go +++ b/pkg/tests/apis/helper.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "testing" + "time" "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/api/errors" @@ -54,6 +55,7 @@ func NewK8sTestHelper(t *testing.T, opts testinfra.GrafanaOpts) *K8sTestHelper { t.Helper() dir, path := testinfra.CreateGrafDir(t, opts) _, env := testinfra.StartGrafanaEnv(t, dir, path) + c := &K8sTestHelper{ env: *env, t: t, @@ -63,16 +65,28 @@ func NewK8sTestHelper(t *testing.T, opts testinfra.GrafanaOpts) *K8sTestHelper { c.Org1 = c.createTestUsers("Org1") c.OrgB = c.createTestUsers("OrgB") - // Read the API groups - rsp := DoRequest(c, RequestParams{ - User: c.Org1.Viewer, - Path: "/apis", - // Accept: "application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList,application/json", - }, &metav1.APIGroupList{}) - c.groups = rsp.Result.Groups + c.loadAPIGroups() + return c } +func (c *K8sTestHelper) loadAPIGroups() { + for { + rsp := DoRequest(c, RequestParams{ + User: c.Org1.Viewer, + Path: "/apis", + // Accept: "application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList,application/json", + }, &metav1.APIGroupList{}) + + if rsp.Response.StatusCode == http.StatusOK { + c.groups = rsp.Result.Groups + return + } + + time.Sleep(100 * time.Millisecond) + } +} + func (c *K8sTestHelper) Shutdown() { err := c.env.Server.Shutdown(context.Background(), "done") require.NoError(c.t, err) diff --git a/pkg/tests/apis/playlist/playlist_test.go b/pkg/tests/apis/playlist/playlist_test.go index 25919c4964f..91ec2f1d23c 100644 --- a/pkg/tests/apis/playlist/playlist_test.go +++ b/pkg/tests/apis/playlist/playlist_test.go @@ -18,15 +18,20 @@ import ( "github.com/grafana/grafana/pkg/services/playlist" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) +func TestMain(m *testing.M) { + testsuite.Run(m) +} + var gvr = schema.GroupVersionResource{ Group: "playlist.grafana.app", Version: "v0alpha1", Resource: "playlists", } -func TestPlaylist(t *testing.T) { +func TestIntegrationPlaylist(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } diff --git a/pkg/tests/apis/query/query_test.go b/pkg/tests/apis/query/query_test.go index 8a70de87986..772f21a934c 100644 --- a/pkg/tests/apis/query/query_test.go +++ b/pkg/tests/apis/query/query_test.go @@ -16,9 +16,14 @@ import ( "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" + "github.com/grafana/grafana/pkg/tests/testsuite" ) -func TestSimpleQuery(t *testing.T) { +func TestMain(m *testing.M) { + testsuite.Run(m) +} + +func TestIntegrationSimpleQuery(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") }