From 2863e8d4a4bf7113b1c1899ceab5758ba910d480 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Fri, 5 Apr 2024 20:21:12 -0400 Subject: [PATCH] simplify Signed-off-by: Yuri Shkuro --- plugin/storage/es/factory.go | 31 ------- .../storage/integration/elasticsearch_test.go | 80 ++++++++----------- plugin/storage/integration/integration.go | 20 +++-- 3 files changed, 48 insertions(+), 83 deletions(-) diff --git a/plugin/storage/es/factory.go b/plugin/storage/es/factory.go index cc8c71b1d8b..8a6866c74ae 100644 --- a/plugin/storage/es/factory.go +++ b/plugin/storage/es/factory.go @@ -31,7 +31,6 @@ import ( "go.uber.org/zap" "github.com/jaegertracing/jaeger/pkg/es" - estemplate "github.com/jaegertracing/jaeger/pkg/es" "github.com/jaegertracing/jaeger/pkg/es/config" "github.com/jaegertracing/jaeger/pkg/fswatcher" "github.com/jaegertracing/jaeger/pkg/metrics" @@ -86,36 +85,6 @@ func NewFactory() *Factory { } } -func NewFactoryWithConfigTest( - cfg config.Configuration, - metricsFactory metrics.Factory, - logger *zap.Logger, client estemplate.Client, -) (*Factory, error) { - cfg.Validate() - - cfg.MaxDocCount = defaultMaxDocCount - cfg.Enabled = true - - archive := make(map[string]*namespaceConfig) - archive[archiveNamespace] = &namespaceConfig{ - Configuration: cfg, - namespace: archiveNamespace, - } - - f := NewFactory() - f.InitFromOptions(Options{ - Primary: namespaceConfig{ - Configuration: cfg, - namespace: primaryNamespace, - }, - others: archive, - }) - f.Initialize(metricsFactory, logger) - - f.primaryClient.Store(&client) - return f, nil -} - func NewFactoryWithConfig( cfg config.Configuration, metricsFactory metrics.Factory, diff --git a/plugin/storage/integration/elasticsearch_test.go b/plugin/storage/integration/elasticsearch_test.go index e1276b73106..aa97184485f 100644 --- a/plugin/storage/integration/elasticsearch_test.go +++ b/plugin/storage/integration/elasticsearch_test.go @@ -18,6 +18,7 @@ package integration import ( "context" "errors" + "fmt" "net/http" "strconv" "strings" @@ -29,15 +30,17 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + "go.uber.org/zap/zaptest" + "github.com/jaegertracing/jaeger/pkg/config" estemplate "github.com/jaegertracing/jaeger/pkg/es" eswrapper "github.com/jaegertracing/jaeger/pkg/es/wrapper" "github.com/jaegertracing/jaeger/pkg/metrics" "github.com/jaegertracing/jaeger/pkg/testutils" "github.com/jaegertracing/jaeger/plugin/storage/es" - "github.com/jaegertracing/jaeger/plugin/storage/es/dependencystore" "github.com/jaegertracing/jaeger/plugin/storage/es/mappings" "github.com/jaegertracing/jaeger/plugin/storage/es/samplingstore" + "github.com/jaegertracing/jaeger/storage/dependencystore" ) const ( @@ -66,7 +69,6 @@ type ESStorageIntegration struct { logger *zap.Logger } - func (s *ESStorageIntegration) getVersion() (uint, error) { pingResult, _, err := s.client.Ping(queryURL).Do(context.Background()) if err != nil { @@ -153,60 +155,44 @@ func (s *ESStorageIntegration) getEsClient(t *testing.T) eswrapper.ClientWrapper return eswrapper.WrapESClient(s.client, bp, esVersion, s.v8Client) } -func (s *ESStorageIntegration) initSpanstore(t *testing.T, allTagsAsFields bool) error { - client := s.getEsClient(t) - mappingBuilder := mappings.MappingBuilder{ - TemplateBuilder: estemplate.TextTemplateBuilder{}, - Shards: 5, - Replicas: 1, - EsVersion: client.GetVersion(), - IndexPrefix: indexPrefix, - UseILM: false, +func (s *ESStorageIntegration) initializeESFactory(t *testing.T, allTagsAsFields bool) *es.Factory { + s.logger = zaptest.NewLogger(t) + f := es.NewFactory() + v, command := config.Viperize(f.AddFlags) + args := []string{ + fmt.Sprintf("--es.tags-as-fields.all=%v", allTagsAsFields), + fmt.Sprintf("--es.index-prefix=%v", indexPrefix), + "--es-archive.enabled=true", + fmt.Sprintf("--es-archive.tags-as-fields.all=%v", allTagsAsFields), + fmt.Sprintf("--es-archive.index-prefix=%v", indexPrefix), } + require.NoError(t, command.ParseFlags(args)) + f.InitFromViper(v, s.logger) + require.NoError(t, f.Initialize(metrics.NullFactory, s.logger)) - clientFn := func() estemplate.Client { return client } + // TODO ideally we need to close the factory once the test is finished + // but because esCleanup calls initialize() we get a panic later + // t.Cleanup(func() { + // require.NoError(t, f.Close()) + // }) + return f +} - opts := es.NewOptions(primaryNamespace, archiveNamespace) - cfg := opts.Primary.Configuration - cfg.IndexPrefix = indexPrefix - cfg.Tags.AllAsFields = allTagsAsFields - f, err := es.NewFactoryWithConfigTest(cfg, metrics.NullFactory, s.logger, client) - if err != nil { - return err - } - // Create Span Writer and Reader +func (s *ESStorageIntegration) initSpanstore(t *testing.T, allTagsAsFields bool) { + f := s.initializeESFactory(t, allTagsAsFields) + var err error s.SpanWriter, err = f.CreateSpanWriter() - if err != nil { - return err - } + require.NoError(t, err) s.SpanReader, err = f.CreateSpanReader() - if err != nil { - return err - } + require.NoError(t, err) s.ArchiveSpanReader, err = f.CreateArchiveSpanReader() - if err != nil { - return err - } + require.NoError(t, err) s.ArchiveSpanWriter, err = f.CreateArchiveSpanWriter() - if err != nil { - return err - } - - dependencyStore := dependencystore.NewDependencyStore(dependencystore.DependencyStoreParams{ - Client: clientFn, - Logger: s.logger, - IndexPrefix: indexPrefix, - IndexDateLayout: indexDateLayout, - MaxDocCount: defaultMaxDocCount, - }) - - depMapping, err := mappingBuilder.GetDependenciesMappings() require.NoError(t, err) - err = dependencyStore.CreateTemplates(depMapping) + + s.DependencyReader, err = f.CreateDependencyReader() require.NoError(t, err) - s.DependencyReader = dependencyStore - s.DependencyWriter = dependencyStore - return nil + s.DependencyWriter = s.DependencyReader.(dependencystore.Writer) } func (s *ESStorageIntegration) esRefresh(t *testing.T) { diff --git a/plugin/storage/integration/integration.go b/plugin/storage/integration/integration.go index 506900e8ee3..d6edc3a8abf 100644 --- a/plugin/storage/integration/integration.go +++ b/plugin/storage/integration/integration.go @@ -460,12 +460,22 @@ func (s *StorageIntegration) testGetDependencies(t *testing.T) { require.NoError(t, s.DependencyWriter.WriteDependencies(time.Now(), expected)) s.refresh(t) - actual, err := s.DependencyReader.GetDependencies(context.Background(), time.Now(), 5*time.Minute) - require.NoError(t, err) - sort.Slice(actual, func(i, j int) bool { - return actual[i].Parent < actual[j].Parent + + var actual []model.DependencyLink + found := s.waitForCondition(t, func(t *testing.T) bool { + var err error + actual, err = s.DependencyReader.GetDependencies(context.Background(), time.Now(), 5*time.Minute) + require.NoError(t, err) + sort.Slice(actual, func(i, j int) bool { + return actual[i].Parent < actual[j].Parent + }) + return assert.ObjectsAreEqualValues(expected, actual) }) - assert.EqualValues(t, expected, actual) + + if !assert.True(t, found) { + t.Log("\t Expected:", expected) + t.Log("\t Actual :", actual) + } } // === Sampling Store Integration Tests ===