mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 05:12:36 +08:00
fix(unified-storage): fix graceful termination for grafana target servers (#103520)
This commit is contained in:

committed by
GitHub

parent
5c53d33c8e
commit
56b4e5670d
@ -2,18 +2,15 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cuelang.org/go/pkg/regexp"
|
||||
"github.com/grafana/grafana/pkg/api"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/modules"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tests/testsuite"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -25,68 +22,47 @@ func TestIntegrationWillRunInstrumentationServerWhenTargetHasNoHttpServer(t *tes
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
dbType := os.Getenv("GRAFANA_TEST_DB")
|
||||
if dbType == "" {
|
||||
t.Skip("skipping - GRAFANA_TEST_DB not defined")
|
||||
}
|
||||
if dbType == "sqlite3" {
|
||||
t.Skip("skipping - sqlite not supported for storage server target")
|
||||
}
|
||||
// TODO - fix this test for postgres
|
||||
if dbType == "postgres" {
|
||||
t.Skip("skipping - test not working with postgres in Drone. Works locally.")
|
||||
if db.IsTestDbSQLite() {
|
||||
t.Skip("sqlite is not supported by the storage server target")
|
||||
}
|
||||
|
||||
_, cfg := db.InitTestDBWithCfg(t)
|
||||
cfg.HTTPPort = "3001"
|
||||
cfg.GRPCServer.Network = "tcp"
|
||||
cfg.GRPCServer.Address = "localhost:10000"
|
||||
addStorageServerToConfig(t, cfg, dbType)
|
||||
cfg.Target = []string{modules.StorageServer}
|
||||
|
||||
ms, err := InitializeModuleServer(cfg, Options{}, api.ServerOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
err = ms.Run()
|
||||
if err.Error() != "context canceled" {
|
||||
t.Error(err)
|
||||
}
|
||||
errChan <- ms.Run()
|
||||
}()
|
||||
time.Sleep(500 * time.Millisecond) // wait for http server to be running
|
||||
|
||||
client := http.Client{}
|
||||
res, err := client.Get("http://localhost:3001/metrics")
|
||||
require.NoError(t, err)
|
||||
err = res.Body.Close()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 200, res.StatusCode)
|
||||
require.Eventually(t, func() bool {
|
||||
client := http.Client{}
|
||||
res, err := client.Get("http://localhost:3001/metrics")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer func() {
|
||||
if err := res.Body.Close(); err != nil {
|
||||
t.Fatalf("failed to close response body: %v", err)
|
||||
}
|
||||
}()
|
||||
return res.StatusCode == http.StatusOK
|
||||
}, 10*time.Second, 1*time.Second)
|
||||
|
||||
err = ms.Shutdown(context.Background(), "test over")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func addStorageServerToConfig(t *testing.T, cfg *setting.Cfg, dbType string) {
|
||||
s, err := cfg.Raw.NewSection("resource_api")
|
||||
require.NoError(t, err)
|
||||
_, err = s.NewKey("db_type", dbType)
|
||||
require.NoError(t, err)
|
||||
|
||||
if dbType == "postgres" {
|
||||
_, _ = s.NewKey("db_host", "localhost")
|
||||
_, _ = s.NewKey("db_name", "grafanatest")
|
||||
_, _ = s.NewKey("db_user", "grafanatest")
|
||||
_, _ = s.NewKey("db_pass", "grafanatest")
|
||||
} else {
|
||||
// cant use localhost as hostname in drone tests for mysql, so need to parse it from connection string
|
||||
sec, err := cfg.Raw.GetSection("database")
|
||||
require.NoError(t, err)
|
||||
connString := sec.Key("connection_string").String()
|
||||
matches, err := regexp.FindSubmatch("(.+):(.+)@tcp\\((.+):(\\d+)\\)/(.+)\\?", connString)
|
||||
require.NoError(t, err)
|
||||
_, _ = s.NewKey("db_host", matches[3])
|
||||
_, _ = s.NewKey("db_name", matches[5])
|
||||
_, _ = s.NewKey("db_user", matches[1])
|
||||
_, _ = s.NewKey("db_pass", matches[2])
|
||||
select {
|
||||
case err := <-errChan:
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("unexpected error from module server: %v", err)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("timeout waiting for module server to shut down")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user