From f974d5591ae0f269bf6890a997928f77a80c85b6 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 17 Aug 2025 10:42:10 +0200 Subject: [PATCH] orchestrator/clickhouse: run some tests without a ClickHouse database Some tests don't rely on the ClickHouse database at all. Allow them to run without it. --- orchestrator/clickhouse/http_test.go | 7 +--- orchestrator/clickhouse/migrations.go | 5 ++- orchestrator/clickhouse/networks_test.go | 8 ++-- orchestrator/clickhouse/root.go | 52 ++++++++++++------------ 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/orchestrator/clickhouse/http_test.go b/orchestrator/clickhouse/http_test.go index f46d571c..ccaa7973 100644 --- a/orchestrator/clickhouse/http_test.go +++ b/orchestrator/clickhouse/http_test.go @@ -6,7 +6,6 @@ package clickhouse import ( "testing" - "akvorado/common/clickhousedb" "akvorado/common/daemon" "akvorado/common/helpers" "akvorado/common/httpserver" @@ -17,7 +16,6 @@ import ( func TestHTTPEndpoints(t *testing.T) { r := reporter.NewMock(t) - clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false) config := DefaultConfiguration() config.SkipMigrations = true config.Networks = helpers.MustNewSubnetMap(map[string]NetworkAttributes{ @@ -43,7 +41,7 @@ func TestHTTPEndpoints(t *testing.T) { HTTP: httpserver.NewMock(t, r), Schema: sch, GeoIP: geoip.NewMock(t, r, false), - ClickHouse: clickhouseComponent, + ClickHouse: nil, }) if err != nil { t.Fatalf("New() error:\n%+v", err) @@ -95,7 +93,6 @@ func TestHTTPEndpoints(t *testing.T) { func TestAdditionalASNs(t *testing.T) { r := reporter.NewMock(t) - clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false) config := DefaultConfiguration() config.ASNs = map[uint32]string{ 1: "New network", @@ -105,7 +102,7 @@ func TestAdditionalASNs(t *testing.T) { HTTP: httpserver.NewMock(t, r), Schema: schema.NewMock(t), GeoIP: geoip.NewMock(t, r, false), - ClickHouse: clickhouseComponent, + ClickHouse: nil, }) if err != nil { t.Fatalf("New() error:\n%+v", err) diff --git a/orchestrator/clickhouse/migrations.go b/orchestrator/clickhouse/migrations.go index 92b47b49..219cd4d9 100644 --- a/orchestrator/clickhouse/migrations.go +++ b/orchestrator/clickhouse/migrations.go @@ -184,5 +184,8 @@ func (c *Component) guessHTTPBaseURL(ip string) (string, error) { // ReloadDictionary will reload the specified dictionnary. func (c *Component) ReloadDictionary(ctx context.Context, dictName string) error { - return c.d.ClickHouse.ExecOnCluster(ctx, fmt.Sprintf("SYSTEM RELOAD DICTIONARY %s.%s", c.d.ClickHouse.DatabaseName(), dictName)) + if c.d.ClickHouse != nil { + return c.d.ClickHouse.ExecOnCluster(ctx, fmt.Sprintf("SYSTEM RELOAD DICTIONARY %s.%s", c.d.ClickHouse.DatabaseName(), dictName)) + } + return nil } diff --git a/orchestrator/clickhouse/networks_test.go b/orchestrator/clickhouse/networks_test.go index 4a1be273..0f8ed537 100644 --- a/orchestrator/clickhouse/networks_test.go +++ b/orchestrator/clickhouse/networks_test.go @@ -8,7 +8,6 @@ import ( "path/filepath" "testing" - "akvorado/common/clickhousedb" "akvorado/common/daemon" "akvorado/common/helpers" "akvorado/common/httpserver" @@ -21,7 +20,6 @@ func TestNetworksCSVWithGeoip(t *testing.T) { config := DefaultConfiguration() config.SkipMigrations = true r := reporter.NewMock(t) - clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false) t.Run("only GeoIP", func(t *testing.T) { // First use only GeoIP @@ -30,7 +28,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) { HTTP: httpserver.NewMock(t, r), Schema: schema.NewMock(t), GeoIP: geoip.NewMock(t, r, true), - ClickHouse: clickhouseComponent, + ClickHouse: nil, }) if err != nil { t.Fatalf("New() error:\n%+v", err) @@ -79,7 +77,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) { HTTP: httpserver.NewMock(t, r), Schema: schema.NewMock(t), GeoIP: geoip.NewMock(t, r, true), - ClickHouse: clickhouseComponent, + ClickHouse: nil, }) if err != nil { t.Fatalf("New() error:\n%+v", err) @@ -130,7 +128,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) { HTTP: httpserver.NewMock(t, r), Schema: schema.NewMock(t), GeoIP: geoip.NewMock(t, r, true), - ClickHouse: clickhouseComponent, + ClickHouse: nil, }) if err != nil { t.Fatalf("New() error:\n%+v", err) diff --git a/orchestrator/clickhouse/root.go b/orchestrator/clickhouse/root.go index a711b513..0cd9d782 100644 --- a/orchestrator/clickhouse/root.go +++ b/orchestrator/clickhouse/root.go @@ -104,34 +104,36 @@ func (c *Component) Start() error { }) // Database migration - migrationsOnce := false - c.metrics.migrationsRunning.Set(1) - c.t.Go(func() error { - customBackoff := backoff.NewExponentialBackOff() - customBackoff.MaxElapsedTime = 0 - customBackoff.InitialInterval = time.Second - for { - if !c.config.SkipMigrations { - c.r.Info().Msg("attempting database migration") - if err := c.migrateDatabase(); err != nil { - c.r.Err(err).Msg("database migration error") - } else { + if c.d.ClickHouse != nil { + migrationsOnce := false + c.metrics.migrationsRunning.Set(1) + c.t.Go(func() error { + customBackoff := backoff.NewExponentialBackOff() + customBackoff.MaxElapsedTime = 0 + customBackoff.InitialInterval = time.Second + for { + if !c.config.SkipMigrations { + c.r.Info().Msg("attempting database migration") + if err := c.migrateDatabase(); err != nil { + c.r.Err(err).Msg("database migration error") + } else { + return nil + } + if !migrationsOnce { + close(c.migrationsOnce) + migrationsOnce = true + customBackoff.Reset() + } + } + next := customBackoff.NextBackOff() + select { + case <-c.t.Dying(): return nil - } - if !migrationsOnce { - close(c.migrationsOnce) - migrationsOnce = true - customBackoff.Reset() + case <-time.Tick(next): } } - next := customBackoff.NextBackOff() - select { - case <-c.t.Dying(): - return nil - case <-time.Tick(next): - } - } - }) + }) + } // Network sources update if err := c.networkSourcesFetcher.Start(); err != nil {