ResourceClient: Exercise resource client in k8s apis tests (#93473)

This commit is contained in:
Ryan McKinley
2024-09-19 17:16:48 +03:00
committed by GitHub
parent 63195664f4
commit 542105b680
9 changed files with 68 additions and 28 deletions
+4 -2
View File
@@ -5,6 +5,7 @@ import (
"strings"
"testing"
"github.com/grafana/grafana/pkg/services/apiserver/options"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/apis"
@@ -31,8 +32,9 @@ func TestIntegrationRequiresDevMode(t *testing.T) {
t.Skip("skipping integration test")
}
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
AppModeProduction: true, // should fail
DisableAnonymous: true,
AppModeProduction: true, // should fail
DisableAnonymous: true,
APIServerStorageType: options.StorageTypeUnifiedGrpc, // tests remote connection
EnableFeatureToggles: []string{
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, // Required to start the example service
},
+8
View File
@@ -41,6 +41,7 @@ import (
"github.com/grafana/grafana/pkg/services/team/teamimpl"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/storage/unified/resource"
"github.com/grafana/grafana/pkg/tests/testinfra"
)
@@ -74,6 +75,12 @@ func NewK8sTestHelper(t *testing.T, opts testinfra.GrafanaOpts) *K8sTestHelper {
c.loadAPIGroups()
// ensure unified storage is alive and running
ctx := identity.WithRequester(context.Background(), c.Org1.Admin.Identity)
rsp, err := c.env.ResourceClient.IsHealthy(ctx, &resource.HealthCheckRequest{})
require.NoError(t, err, "unable to read resource client health check")
require.Equal(t, resource.HealthCheckResponse_SERVING, rsp.Status)
return c
}
@@ -485,6 +492,7 @@ func (c *K8sTestHelper) CreateUser(name string, orgName string, basicRole org.Ro
require.Equal(c.t, orgId, u.OrgID)
require.True(c.t, u.ID > 0)
// should this always return a user with ID token?
s, err := userSvc.GetSignedInUser(context.Background(), &user.GetSignedInUserQuery{
UserID: u.ID,
Login: u.Login,
+24 -11
View File
@@ -52,6 +52,28 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
serverOpts := server.Options{Listener: listener, HomePath: grafDir}
apiServerOpts := api.ServerOptions{Listener: listener}
// Potentially allocate a real gRPC port for unified storage
runstore := false
unistore, _ := cfg.Raw.GetSection("grafana-apiserver")
if unistore != nil &&
unistore.Key("storage_type").MustString("") == string(options.StorageTypeUnifiedGrpc) &&
unistore.Key("address").String() == "" {
// Allocate a new address
listener2, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
cfg.GRPCServerNetwork = "tcp"
cfg.GRPCServerAddress = listener2.Addr().String()
cfg.GRPCServerTLSConfig = nil
_, err = unistore.NewKey("address", cfg.GRPCServerAddress)
require.NoError(t, err)
// release the one we just discovered -- it will be used by the services on startup
err = listener2.Close()
require.NoError(t, err)
runstore = true
}
env, err := server.InitializeForTest(t, cfg, serverOpts, apiServerOpts)
require.NoError(t, err)
@@ -74,16 +96,8 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
// UnifiedStorageOverGRPC
var storage sql.UnifiedStorageGrpcService
unistore, _ := env.Cfg.Raw.GetSection("grafana-apiserver")
if unistore != nil &&
unistore.Key("storage_type").MustString("") == string(options.StorageTypeUnifiedGrpc) &&
unistore.Key("address").String() == "" { // no address is configured
copy := *env.Cfg
copy.GRPCServerNetwork = "tcp"
copy.GRPCServerAddress = "localhost:0"
copy.GRPCServerTLSConfig = nil
storage, err = sql.ProvideUnifiedStorageGrpcService(&copy, env.FeatureToggles, env.SQLStore, env.Cfg.Logger)
if runstore {
storage, err = sql.ProvideUnifiedStorageGrpcService(env.Cfg, env.FeatureToggles, env.SQLStore, env.Cfg.Logger)
require.NoError(t, err)
ctx := context.Background()
err = storage.StartAsync(ctx)
@@ -91,7 +105,6 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
err = storage.AwaitRunning(ctx)
require.NoError(t, err)
_, err = unistore.NewKey("address", storage.GetAddress())
require.NoError(t, err)
t.Logf("Unified storage running on %s", storage.GetAddress())
}