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.
This commit is contained in:
Vincent Bernat
2025-08-17 10:42:10 +02:00
parent 2c3834c6fc
commit f974d5591a
4 changed files with 36 additions and 36 deletions

View File

@@ -6,7 +6,6 @@ package clickhouse
import ( import (
"testing" "testing"
"akvorado/common/clickhousedb"
"akvorado/common/daemon" "akvorado/common/daemon"
"akvorado/common/helpers" "akvorado/common/helpers"
"akvorado/common/httpserver" "akvorado/common/httpserver"
@@ -17,7 +16,6 @@ import (
func TestHTTPEndpoints(t *testing.T) { func TestHTTPEndpoints(t *testing.T) {
r := reporter.NewMock(t) r := reporter.NewMock(t)
clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false)
config := DefaultConfiguration() config := DefaultConfiguration()
config.SkipMigrations = true config.SkipMigrations = true
config.Networks = helpers.MustNewSubnetMap(map[string]NetworkAttributes{ config.Networks = helpers.MustNewSubnetMap(map[string]NetworkAttributes{
@@ -43,7 +41,7 @@ func TestHTTPEndpoints(t *testing.T) {
HTTP: httpserver.NewMock(t, r), HTTP: httpserver.NewMock(t, r),
Schema: sch, Schema: sch,
GeoIP: geoip.NewMock(t, r, false), GeoIP: geoip.NewMock(t, r, false),
ClickHouse: clickhouseComponent, ClickHouse: nil,
}) })
if err != nil { if err != nil {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)
@@ -95,7 +93,6 @@ func TestHTTPEndpoints(t *testing.T) {
func TestAdditionalASNs(t *testing.T) { func TestAdditionalASNs(t *testing.T) {
r := reporter.NewMock(t) r := reporter.NewMock(t)
clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false)
config := DefaultConfiguration() config := DefaultConfiguration()
config.ASNs = map[uint32]string{ config.ASNs = map[uint32]string{
1: "New network", 1: "New network",
@@ -105,7 +102,7 @@ func TestAdditionalASNs(t *testing.T) {
HTTP: httpserver.NewMock(t, r), HTTP: httpserver.NewMock(t, r),
Schema: schema.NewMock(t), Schema: schema.NewMock(t),
GeoIP: geoip.NewMock(t, r, false), GeoIP: geoip.NewMock(t, r, false),
ClickHouse: clickhouseComponent, ClickHouse: nil,
}) })
if err != nil { if err != nil {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)

View File

@@ -184,5 +184,8 @@ func (c *Component) guessHTTPBaseURL(ip string) (string, error) {
// ReloadDictionary will reload the specified dictionnary. // ReloadDictionary will reload the specified dictionnary.
func (c *Component) ReloadDictionary(ctx context.Context, dictName string) error { 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
} }

View File

@@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"akvorado/common/clickhousedb"
"akvorado/common/daemon" "akvorado/common/daemon"
"akvorado/common/helpers" "akvorado/common/helpers"
"akvorado/common/httpserver" "akvorado/common/httpserver"
@@ -21,7 +20,6 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
config := DefaultConfiguration() config := DefaultConfiguration()
config.SkipMigrations = true config.SkipMigrations = true
r := reporter.NewMock(t) r := reporter.NewMock(t)
clickhouseComponent := clickhousedb.SetupClickHouse(t, r, false)
t.Run("only GeoIP", func(t *testing.T) { t.Run("only GeoIP", func(t *testing.T) {
// First use only GeoIP // First use only GeoIP
@@ -30,7 +28,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
HTTP: httpserver.NewMock(t, r), HTTP: httpserver.NewMock(t, r),
Schema: schema.NewMock(t), Schema: schema.NewMock(t),
GeoIP: geoip.NewMock(t, r, true), GeoIP: geoip.NewMock(t, r, true),
ClickHouse: clickhouseComponent, ClickHouse: nil,
}) })
if err != nil { if err != nil {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)
@@ -79,7 +77,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
HTTP: httpserver.NewMock(t, r), HTTP: httpserver.NewMock(t, r),
Schema: schema.NewMock(t), Schema: schema.NewMock(t),
GeoIP: geoip.NewMock(t, r, true), GeoIP: geoip.NewMock(t, r, true),
ClickHouse: clickhouseComponent, ClickHouse: nil,
}) })
if err != nil { if err != nil {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)
@@ -130,7 +128,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
HTTP: httpserver.NewMock(t, r), HTTP: httpserver.NewMock(t, r),
Schema: schema.NewMock(t), Schema: schema.NewMock(t),
GeoIP: geoip.NewMock(t, r, true), GeoIP: geoip.NewMock(t, r, true),
ClickHouse: clickhouseComponent, ClickHouse: nil,
}) })
if err != nil { if err != nil {
t.Fatalf("New() error:\n%+v", err) t.Fatalf("New() error:\n%+v", err)

View File

@@ -104,34 +104,36 @@ func (c *Component) Start() error {
}) })
// Database migration // Database migration
migrationsOnce := false if c.d.ClickHouse != nil {
c.metrics.migrationsRunning.Set(1) migrationsOnce := false
c.t.Go(func() error { c.metrics.migrationsRunning.Set(1)
customBackoff := backoff.NewExponentialBackOff() c.t.Go(func() error {
customBackoff.MaxElapsedTime = 0 customBackoff := backoff.NewExponentialBackOff()
customBackoff.InitialInterval = time.Second customBackoff.MaxElapsedTime = 0
for { customBackoff.InitialInterval = time.Second
if !c.config.SkipMigrations { for {
c.r.Info().Msg("attempting database migration") if !c.config.SkipMigrations {
if err := c.migrateDatabase(); err != nil { c.r.Info().Msg("attempting database migration")
c.r.Err(err).Msg("database migration error") if err := c.migrateDatabase(); err != nil {
} else { 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 return nil
} case <-time.Tick(next):
if !migrationsOnce {
close(c.migrationsOnce)
migrationsOnce = true
customBackoff.Reset()
} }
} }
next := customBackoff.NextBackOff() })
select { }
case <-c.t.Dying():
return nil
case <-time.Tick(next):
}
}
})
// Network sources update // Network sources update
if err := c.networkSourcesFetcher.Start(); err != nil { if err := c.networkSourcesFetcher.Start(); err != nil {